ThemeStyle.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using UnityEngine;
  5. #if dUI_TextMeshPro
  6. using TMPro;
  7. #endif
  8. namespace XCharts.Runtime
  9. {
  10. /// <summary>
  11. /// 主题
  12. /// </summary>
  13. public enum ThemeType
  14. {
  15. /// <summary>
  16. /// 默认主题。
  17. /// </summary>
  18. Default,
  19. /// <summary>
  20. /// 亮主题。
  21. /// </summary>
  22. Light,
  23. /// <summary>
  24. /// 暗主题。
  25. /// </summary>
  26. Dark,
  27. /// <summary>
  28. /// 自定义主题。
  29. /// </summary>
  30. Custom,
  31. }
  32. [Serializable]
  33. /// <summary>
  34. /// Theme.
  35. /// ||主题相关配置。
  36. /// </summary>
  37. public class ThemeStyle : ChildComponent
  38. {
  39. [SerializeField] private bool m_Show = true;
  40. [SerializeField] private Theme m_SharedTheme;
  41. [SerializeField] private bool m_TransparentBackground = false;
  42. [SerializeField] private bool m_EnableCustomTheme = false;
  43. [SerializeField] private Font m_CustomFont;
  44. [SerializeField] private Color32 m_CustomBackgroundColor;
  45. #if UNITY_2020_2
  46. [NonReorderable]
  47. #endif
  48. [SerializeField] private List<Color32> m_CustomColorPalette = new List<Color32>(13);
  49. public bool show { get { return m_Show; } }
  50. /// <summary>
  51. /// the theme of chart.
  52. /// ||主题类型。
  53. /// </summary>
  54. public ThemeType themeType
  55. {
  56. get { return sharedTheme.themeType; }
  57. }
  58. /// <summary>
  59. /// theme name.
  60. /// ||主题名字。
  61. /// </summary>
  62. public string themeName
  63. {
  64. get { return sharedTheme.themeName; }
  65. }
  66. /// <summary>
  67. /// the asset of theme.
  68. /// ||主题配置。
  69. /// </summary>
  70. public Theme sharedTheme
  71. {
  72. get { return m_SharedTheme; }
  73. set { m_SharedTheme = value; SetAllDirty(); }
  74. }
  75. /// <summary>
  76. /// the contrast color of chart.
  77. /// ||对比色。
  78. /// </summary>
  79. public Color32 contrastColor
  80. {
  81. get { return sharedTheme.contrastColor; }
  82. }
  83. /// <summary>
  84. /// the background color of chart.
  85. /// ||背景颜色。
  86. /// </summary>
  87. public Color32 backgroundColor
  88. {
  89. get
  90. {
  91. if (m_TransparentBackground) return ColorUtil.clearColor32;
  92. else return m_EnableCustomTheme ? m_CustomBackgroundColor : sharedTheme.backgroundColor;
  93. }
  94. }
  95. /// <summary>
  96. /// Whether the background color is transparent. When true, the background color is not drawn.
  97. /// ||是否透明背景颜色。当设置为true时,不绘制背景颜色。
  98. /// </summary>
  99. public bool transparentBackground
  100. {
  101. get { return m_TransparentBackground; }
  102. set { m_TransparentBackground = value; SetAllDirty(); }
  103. }
  104. /// <summary>
  105. /// Whether to customize theme colors. When set to true,
  106. /// you can use 'sync color to custom' to synchronize the theme color to the custom color. It can also be set manually.
  107. /// ||是否自定义主题颜色。当设置为true时,可以用‘sync color to custom’同步主题的颜色到自定义颜色。也可以手动设置。
  108. /// </summary>
  109. public bool enableCustomTheme
  110. {
  111. get { return m_EnableCustomTheme; }
  112. set { m_EnableCustomTheme = value; _colorDic.Clear(); SetAllDirty(); }
  113. }
  114. /// <summary>
  115. /// the custom background color of chart.
  116. /// ||自定义的背景颜色。
  117. /// </summary>
  118. public Color32 customBackgroundColor
  119. {
  120. get { return m_CustomBackgroundColor; }
  121. set { m_CustomBackgroundColor = value; SetAllDirty(); }
  122. }
  123. /// <summary>
  124. /// The color list of palette. If no color is set in series, the colors would be adopted sequentially and circularly from this list as the colors of series.
  125. /// ||调色盘颜色列表。如果系列没有设置颜色,则会依次循环从该列表中取颜色作为系列颜色。
  126. /// </summary>
  127. public List<Color32> colorPalette
  128. {
  129. get { return m_EnableCustomTheme ? m_CustomColorPalette : sharedTheme.colorPalette; }
  130. }
  131. public List<Color32> customColorPalette { get { return m_CustomColorPalette; } set { m_CustomColorPalette = value; SetVerticesDirty(); } }
  132. public ComponentTheme common { get { return sharedTheme.common; } }
  133. public TitleTheme title { get { return sharedTheme.title; } }
  134. public SubTitleTheme subTitle { get { return sharedTheme.subTitle; } }
  135. public LegendTheme legend { get { return sharedTheme.legend; } }
  136. public AxisTheme axis { get { return sharedTheme.axis; } }
  137. public TooltipTheme tooltip { get { return sharedTheme.tooltip; } }
  138. public DataZoomTheme dataZoom { get { return sharedTheme.dataZoom; } }
  139. public VisualMapTheme visualMap { get { return sharedTheme.visualMap; } }
  140. public SerieTheme serie { get { return sharedTheme.serie; } }
  141. /// <summary>
  142. /// Gets the color of the specified index from the palette.
  143. /// ||获得调色盘对应系列索引的颜色值。
  144. /// </summary>
  145. /// <param name="index">编号索引</param>
  146. /// <returns>the color,or Color.clear when failed.颜色值,失败时返回Color.clear</returns>
  147. public Color32 GetColor(int index)
  148. {
  149. if (colorPalette.Count <= 0) return Color.clear;
  150. if (index < 0) index = 0;
  151. var newIndex = index < colorPalette.Count ? index : index % colorPalette.Count;
  152. if (newIndex < colorPalette.Count)
  153. return colorPalette[newIndex];
  154. else return Color.clear;
  155. }
  156. public Color32 GetBackgroundColor(Background background)
  157. {
  158. if (background != null && background.show && !background.autoColor)
  159. return background.imageColor;
  160. else
  161. return backgroundColor;
  162. }
  163. public void SyncSharedThemeColorToCustom()
  164. {
  165. m_CustomBackgroundColor = sharedTheme.backgroundColor;
  166. m_CustomColorPalette.Clear();
  167. foreach (var color in sharedTheme.colorPalette)
  168. {
  169. m_CustomColorPalette.Add(color);
  170. }
  171. SetAllDirty();
  172. }
  173. public void CheckWarning(StringBuilder sb)
  174. {
  175. #if dUI_TextMeshPro
  176. if (sharedTheme.tmpFont == null)
  177. {
  178. sb.AppendFormat("warning:theme->tmpFont is null\n");
  179. }
  180. #else
  181. if (sharedTheme.font == null)
  182. {
  183. sb.AppendFormat("warning:theme->font is null\n");
  184. }
  185. #endif
  186. if (sharedTheme.colorPalette.Count == 0)
  187. {
  188. sb.AppendFormat("warning:theme->colorPalette is empty\n");
  189. }
  190. for (int i = 0; i < sharedTheme.colorPalette.Count; i++)
  191. {
  192. if (!ChartHelper.IsClearColor(sharedTheme.colorPalette[i]) && sharedTheme.colorPalette[i].a == 0)
  193. sb.AppendFormat("warning:theme->colorPalette[{0}] alpha = 0\n", i);
  194. }
  195. }
  196. Dictionary<int, string> _colorDic = new Dictionary<int, string>();
  197. /// <summary>
  198. /// Gets the hexadecimal color string of the specified index from the palette.
  199. /// ||获得指定索引的十六进制颜色值字符串。
  200. /// </summary>
  201. /// <param name="index"></param>
  202. /// <returns></returns>
  203. public string GetColorStr(int index)
  204. {
  205. if (index < 0)
  206. {
  207. index = 0;
  208. }
  209. index = index % colorPalette.Count;
  210. if (_colorDic.ContainsKey(index)) return _colorDic[index];
  211. else
  212. {
  213. _colorDic[index] = ColorUtility.ToHtmlStringRGBA(GetColor(index));
  214. return _colorDic[index];
  215. }
  216. }
  217. /// <summary>
  218. /// Convert the html string to color.
  219. /// ||将字符串颜色值转成Color。
  220. /// </summary>
  221. /// <param name="hexColorStr"></param>
  222. /// <returns></returns>
  223. public static Color32 GetColor(string hexColorStr)
  224. {
  225. Color color;
  226. ColorUtility.TryParseHtmlString(hexColorStr, out color);
  227. return (Color32) color;
  228. }
  229. }
  230. }