Nessuna descrizione
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThemeStyle.cs 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. /// <value></value>
  110. public bool enableCustomTheme
  111. {
  112. get { return m_EnableCustomTheme; }
  113. set { m_EnableCustomTheme = value; _colorDic.Clear(); SetAllDirty(); }
  114. }
  115. /// <summary>
  116. /// the custom background color of chart.
  117. /// |自定义的背景颜色。
  118. /// </summary>
  119. public Color32 customBackgroundColor
  120. {
  121. get { return m_CustomBackgroundColor; }
  122. set { m_CustomBackgroundColor = value; SetAllDirty(); }
  123. }
  124. /// <summary>
  125. /// 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.
  126. /// |调色盘颜色列表。如果系列没有设置颜色,则会依次循环从该列表中取颜色作为系列颜色。
  127. /// </summary>
  128. public List<Color32> colorPalette
  129. {
  130. get { return m_EnableCustomTheme ? m_CustomColorPalette : sharedTheme.colorPalette; }
  131. }
  132. public List<Color32> customColorPalette { get { return m_CustomColorPalette; } set { m_CustomColorPalette = value; SetVerticesDirty(); } }
  133. public ComponentTheme common { get { return sharedTheme.common; } }
  134. public TitleTheme title { get { return sharedTheme.title; } }
  135. public SubTitleTheme subTitle { get { return sharedTheme.subTitle; } }
  136. public LegendTheme legend { get { return sharedTheme.legend; } }
  137. public AxisTheme axis { get { return sharedTheme.axis; } }
  138. public TooltipTheme tooltip { get { return sharedTheme.tooltip; } }
  139. public DataZoomTheme dataZoom { get { return sharedTheme.dataZoom; } }
  140. public VisualMapTheme visualMap { get { return sharedTheme.visualMap; } }
  141. public SerieTheme serie { get { return sharedTheme.serie; } }
  142. /// <summary>
  143. /// Gets the color of the specified index from the palette.
  144. /// |获得调色盘对应系列索引的颜色值。
  145. /// </summary>
  146. /// <param name="index">编号索引</param>
  147. /// <returns>the color,or Color.clear when failed.颜色值,失败时返回Color.clear</returns>
  148. public Color32 GetColor(int index)
  149. {
  150. if (colorPalette.Count <= 0) return Color.clear;
  151. if (index < 0) index = 0;
  152. var newIndex = index < colorPalette.Count ? index : index % colorPalette.Count;
  153. if (newIndex < colorPalette.Count)
  154. return colorPalette[newIndex];
  155. else return Color.clear;
  156. }
  157. public Color32 GetBackgroundColor(Background background)
  158. {
  159. if (background != null && background.show && !background.autoColor)
  160. return background.imageColor;
  161. else
  162. return backgroundColor;
  163. }
  164. public void SyncSharedThemeColorToCustom()
  165. {
  166. m_CustomBackgroundColor = sharedTheme.backgroundColor;
  167. m_CustomColorPalette.Clear();
  168. foreach (var color in sharedTheme.colorPalette)
  169. {
  170. m_CustomColorPalette.Add(color);
  171. }
  172. SetAllDirty();
  173. }
  174. public void CheckWarning(StringBuilder sb)
  175. {
  176. #if dUI_TextMeshPro
  177. if (sharedTheme.tmpFont == null)
  178. {
  179. sb.AppendFormat("warning:theme->tmpFont is null\n");
  180. }
  181. #else
  182. if (sharedTheme.font == null)
  183. {
  184. sb.AppendFormat("warning:theme->font is null\n");
  185. }
  186. #endif
  187. if (sharedTheme.colorPalette.Count == 0)
  188. {
  189. sb.AppendFormat("warning:theme->colorPalette is empty\n");
  190. }
  191. for (int i = 0; i < sharedTheme.colorPalette.Count; i++)
  192. {
  193. if (!ChartHelper.IsClearColor(sharedTheme.colorPalette[i]) && sharedTheme.colorPalette[i].a == 0)
  194. sb.AppendFormat("warning:theme->colorPalette[{0}] alpha = 0\n", i);
  195. }
  196. }
  197. Dictionary<int, string> _colorDic = new Dictionary<int, string>();
  198. /// <summary>
  199. /// Gets the hexadecimal color string of the specified index from the palette.
  200. /// |获得指定索引的十六进制颜色值字符串。
  201. /// </summary>
  202. /// <param name="index"></param>
  203. /// <returns></returns>
  204. public string GetColorStr(int index)
  205. {
  206. if (index < 0)
  207. {
  208. index = 0;
  209. }
  210. index = index % colorPalette.Count;
  211. if (_colorDic.ContainsKey(index)) return _colorDic[index];
  212. else
  213. {
  214. _colorDic[index] = ColorUtility.ToHtmlStringRGBA(GetColor(index));
  215. return _colorDic[index];
  216. }
  217. }
  218. /// <summary>
  219. /// Convert the html string to color.
  220. /// |将字符串颜色值转成Color。
  221. /// </summary>
  222. /// <param name="hexColorStr"></param>
  223. /// <returns></returns>
  224. public static Color32 GetColor(string hexColorStr)
  225. {
  226. Color color;
  227. ColorUtility.TryParseHtmlString(hexColorStr, out color);
  228. return (Color32) color;
  229. }
  230. }
  231. }