No Description
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.

Theme.cs 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. /// Theme.
  12. /// |主题相关配置。
  13. /// </summary>
  14. [Serializable]
  15. public class Theme : ScriptableObject
  16. {
  17. [SerializeField] private ThemeType m_ThemeType = ThemeType.Default;
  18. [SerializeField] private string m_ThemeName = ThemeType.Default.ToString();
  19. [SerializeField] private Font m_Font;
  20. #if dUI_TextMeshPro
  21. [SerializeField] private TMP_FontAsset m_TMPFont;
  22. #endif
  23. [SerializeField] private Color32 m_ContrastColor;
  24. [SerializeField] private Color32 m_BackgroundColor;
  25. #if UNITY_2020_2
  26. [NonReorderable]
  27. #endif
  28. [SerializeField] private List<Color32> m_ColorPalette = new List<Color32>(13);
  29. [SerializeField] private ComponentTheme m_Common;
  30. [SerializeField] private TitleTheme m_Title;
  31. [SerializeField] private SubTitleTheme m_SubTitle;
  32. [SerializeField] private LegendTheme m_Legend;
  33. [SerializeField] private AxisTheme m_Axis;
  34. [SerializeField] private TooltipTheme m_Tooltip;
  35. [SerializeField] private DataZoomTheme m_DataZoom;
  36. [SerializeField] private VisualMapTheme m_VisualMap;
  37. [SerializeField] private SerieTheme m_Serie;
  38. /// <summary>
  39. /// the theme of chart.
  40. /// |主题类型。
  41. /// </summary>
  42. public ThemeType themeType
  43. {
  44. get { return m_ThemeType; }
  45. set { PropertyUtil.SetStruct(ref m_ThemeType, value); }
  46. }
  47. /// <summary>
  48. /// the name of theme.
  49. /// |主题名称。
  50. /// </summary>
  51. public string themeName
  52. {
  53. get { return m_ThemeName; }
  54. set { PropertyUtil.SetClass(ref m_ThemeName, value); }
  55. }
  56. /// <summary>
  57. /// the contrast color of chart.
  58. /// |对比色。
  59. /// </summary>
  60. public Color32 contrastColor
  61. {
  62. get { return m_ContrastColor; }
  63. set { PropertyUtil.SetColor(ref m_ContrastColor, value); }
  64. }
  65. /// <summary>
  66. /// the background color of chart.
  67. /// |背景颜色。
  68. /// </summary>
  69. public Color32 backgroundColor
  70. {
  71. get { return m_BackgroundColor; }
  72. set { PropertyUtil.SetColor(ref m_BackgroundColor, value); }
  73. }
  74. /// <summary>
  75. /// 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.
  76. /// |调色盘颜色列表。如果系列没有设置颜色,则会依次循环从该列表中取颜色作为系列颜色。
  77. /// </summary>
  78. public List<Color32> colorPalette { get { return m_ColorPalette; } set { m_ColorPalette = value; } }
  79. public ComponentTheme common { get { return m_Common; } set { m_Common = value; } }
  80. public TitleTheme title { get { return m_Title; } set { m_Title = value; } }
  81. public SubTitleTheme subTitle { get { return m_SubTitle; } set { m_SubTitle = value; } }
  82. public LegendTheme legend { get { return m_Legend; } set { m_Legend = value; } }
  83. public AxisTheme axis { get { return m_Axis; } set { m_Axis = value; } }
  84. public TooltipTheme tooltip { get { return m_Tooltip; } set { m_Tooltip = value; } }
  85. public DataZoomTheme dataZoom { get { return m_DataZoom; } set { m_DataZoom = value; } }
  86. public VisualMapTheme visualMap { get { return m_VisualMap; } set { m_VisualMap = value; } }
  87. public SerieTheme serie { get { return m_Serie; } set { m_Serie = value; } }
  88. #if dUI_TextMeshPro
  89. /// <summary>
  90. /// the font of chart text。
  91. /// |主题字体。
  92. /// </summary>
  93. public TMP_FontAsset tmpFont
  94. {
  95. get { return m_TMPFont; }
  96. set
  97. {
  98. m_TMPFont = value;
  99. SyncTMPFontToSubComponent();
  100. }
  101. }
  102. #endif
  103. /// <summary>
  104. /// the font of chart text。
  105. /// |主题字体。
  106. /// </summary>
  107. public Font font
  108. {
  109. get { return m_Font; }
  110. set
  111. {
  112. m_Font = value;
  113. SyncFontToSubComponent();
  114. }
  115. }
  116. // void OnEnable()
  117. // {
  118. // }
  119. // void OnDisable()
  120. // {
  121. // }
  122. public void SetDefaultFont()
  123. {
  124. #if dUI_TextMeshPro
  125. tmpFont = XCSettings.tmpFont;
  126. SyncTMPFontToSubComponent();
  127. #else
  128. font = XCSettings.font;
  129. SyncFontToSubComponent();
  130. #endif
  131. }
  132. /// <summary>
  133. /// Gets the color of the specified index from the palette.
  134. /// |获得调色盘对应系列索引的颜色值。
  135. /// </summary>
  136. /// <param name="index">编号索引</param>
  137. /// <returns>the color,or Color.clear when failed.颜色值,失败时返回Color.clear</returns>
  138. public Color32 GetColor(int index)
  139. {
  140. if (index < 0) index = 0;
  141. var newIndex = index < m_ColorPalette.Count ? index : index % m_ColorPalette.Count;
  142. if (newIndex < m_ColorPalette.Count)
  143. return m_ColorPalette[newIndex];
  144. else return Color.clear;
  145. }
  146. public void CheckWarning(StringBuilder sb)
  147. {
  148. #if dUI_TextMeshPro
  149. if (m_TMPFont == null)
  150. {
  151. sb.AppendFormat("warning:theme->tmpFont is null\n");
  152. }
  153. #else
  154. if (m_Font == null)
  155. {
  156. sb.AppendFormat("warning:theme->font is null\n");
  157. }
  158. #endif
  159. if (m_ColorPalette.Count == 0)
  160. {
  161. sb.AppendFormat("warning:theme->colorPalette is empty\n");
  162. }
  163. for (int i = 0; i < m_ColorPalette.Count; i++)
  164. {
  165. if (!ChartHelper.IsClearColor(m_ColorPalette[i]) && m_ColorPalette[i].a == 0)
  166. sb.AppendFormat("warning:theme->colorPalette[{0}] alpha = 0\n", i);
  167. }
  168. }
  169. Dictionary<int, string> _colorDic = new Dictionary<int, string>();
  170. /// <summary>
  171. /// Gets the hexadecimal color string of the specified index from the palette.
  172. /// |获得指定索引的十六进制颜色值字符串。
  173. /// </summary>
  174. /// <param name="index"></param>
  175. /// <returns></returns>
  176. public string GetColorStr(int index)
  177. {
  178. if (index < 0)
  179. {
  180. index = 0;
  181. }
  182. index = index % m_ColorPalette.Count;
  183. if (_colorDic.ContainsKey(index)) return _colorDic[index];
  184. else
  185. {
  186. _colorDic[index] = ColorUtility.ToHtmlStringRGBA(GetColor(index));
  187. return _colorDic[index];
  188. }
  189. }
  190. public bool CopyTheme(ThemeType theme)
  191. {
  192. switch (theme)
  193. {
  194. case ThemeType.Dark:
  195. ResetToDarkTheme(this);
  196. return true;
  197. case ThemeType.Default:
  198. ResetToDefaultTheme(this);
  199. return true;
  200. }
  201. return false;
  202. }
  203. /// <summary>
  204. /// copy all configurations from theme.
  205. /// |复制主题的所有配置。
  206. /// </summary>
  207. /// <param name="theme"></param>
  208. public void CopyTheme(Theme theme)
  209. {
  210. m_ThemeType = theme.themeType;
  211. m_ThemeName = theme.themeName;
  212. #if dUI_TextMeshPro
  213. tmpFont = theme.tmpFont;
  214. #endif
  215. font = theme.font;
  216. m_BackgroundColor = theme.backgroundColor;
  217. m_Common.Copy(theme.common);
  218. m_Legend.Copy(theme.legend);
  219. m_Title.Copy(theme.title);
  220. m_SubTitle.Copy(theme.subTitle);
  221. m_Axis.Copy(theme.axis);
  222. m_Tooltip.Copy(theme.tooltip);
  223. m_DataZoom.Copy(theme.dataZoom);
  224. m_VisualMap.Copy(theme.visualMap);
  225. m_Serie.Copy(theme.serie);
  226. ChartHelper.CopyList(m_ColorPalette, theme.colorPalette);
  227. }
  228. /// <summary>
  229. /// Clear all custom configurations.
  230. /// |重置,清除所有自定义配置。
  231. /// </summary>
  232. public bool ResetTheme()
  233. {
  234. switch (m_ThemeType)
  235. {
  236. case ThemeType.Default:
  237. ResetToDefaultTheme(this);
  238. return true;
  239. case ThemeType.Dark:
  240. ResetToDarkTheme(this);
  241. return true;
  242. case ThemeType.Custom:
  243. return false;
  244. }
  245. return false;
  246. }
  247. /// <summary>
  248. /// 克隆主题。
  249. /// </summary>
  250. /// <returns></returns>
  251. public Theme CloneTheme()
  252. {
  253. var theme = ScriptableObject.CreateInstance<Theme>();
  254. InitChartComponentTheme(theme);
  255. theme.CopyTheme(this);
  256. return theme;
  257. }
  258. /// <summary>
  259. /// default theme.
  260. /// |默认主题。
  261. /// </summary>
  262. /// <value></value>
  263. public static void ResetToDefaultTheme(Theme theme)
  264. {
  265. theme.themeType = ThemeType.Default;
  266. theme.themeName = ThemeType.Default.ToString();
  267. theme.backgroundColor = new Color32(255, 255, 255, 255);
  268. theme.colorPalette = new List<Color32>
  269. {
  270. ColorUtil.GetColor("#5470c6"),
  271. ColorUtil.GetColor("#91cc75"),
  272. ColorUtil.GetColor("#fac858"),
  273. ColorUtil.GetColor("#ee6666"),
  274. ColorUtil.GetColor("#73c0de"),
  275. ColorUtil.GetColor("#3ba272"),
  276. ColorUtil.GetColor("#fc8452"),
  277. ColorUtil.GetColor("#9a60b4"),
  278. ColorUtil.GetColor("#ea7ccc"),
  279. };
  280. InitChartComponentTheme(theme);
  281. }
  282. /// <summary>
  283. /// dark theme.
  284. /// |暗主题。
  285. /// </summary>
  286. /// <value></value>
  287. public static void ResetToDarkTheme(Theme theme)
  288. {
  289. theme.themeType = ThemeType.Dark;
  290. theme.themeName = ThemeType.Dark.ToString();
  291. theme.backgroundColor = ColorUtil.GetColor("#100C2A");
  292. theme.colorPalette = new List<Color32>
  293. {
  294. ColorUtil.GetColor("#4992ff"),
  295. ColorUtil.GetColor("#7cffb2"),
  296. ColorUtil.GetColor("#fddd60"),
  297. ColorUtil.GetColor("#ff6e76"),
  298. ColorUtil.GetColor("#58d9f9"),
  299. ColorUtil.GetColor("#05c091"),
  300. ColorUtil.GetColor("#ff8a45"),
  301. ColorUtil.GetColor("#8d48e3"),
  302. ColorUtil.GetColor("#dd79ff"),
  303. };
  304. InitChartComponentTheme(theme);
  305. }
  306. public static Theme EmptyTheme
  307. {
  308. get
  309. {
  310. var theme = ScriptableObject.CreateInstance<Theme>();
  311. theme.themeType = ThemeType.Custom;
  312. theme.themeName = ThemeType.Custom.ToString();
  313. theme.backgroundColor = Color.clear;
  314. theme.colorPalette = new List<Color32>();
  315. InitChartComponentTheme(theme);
  316. return theme;
  317. }
  318. }
  319. public void SyncFontToSubComponent()
  320. {
  321. common.font = font;
  322. title.font = font;
  323. subTitle.font = font;
  324. legend.font = font;
  325. axis.font = font;
  326. tooltip.font = font;
  327. dataZoom.font = font;
  328. visualMap.font = font;
  329. }
  330. #if dUI_TextMeshPro
  331. public void SyncTMPFontToSubComponent()
  332. {
  333. common.tmpFont = tmpFont;
  334. title.tmpFont = tmpFont;
  335. subTitle.tmpFont = tmpFont;
  336. legend.tmpFont = tmpFont;
  337. axis.tmpFont = tmpFont;
  338. tooltip.tmpFont = tmpFont;
  339. dataZoom.tmpFont = tmpFont;
  340. visualMap.tmpFont = tmpFont;
  341. }
  342. #endif
  343. private static void InitChartComponentTheme(Theme theme)
  344. {
  345. theme.common = new ComponentTheme(theme.themeType);
  346. theme.title = new TitleTheme(theme.themeType);
  347. theme.subTitle = new SubTitleTheme(theme.themeType);
  348. theme.legend = new LegendTheme(theme.themeType);
  349. theme.axis = new AxisTheme(theme.themeType);
  350. theme.tooltip = new TooltipTheme(theme.themeType);
  351. theme.dataZoom = new DataZoomTheme(theme.themeType);
  352. theme.visualMap = new VisualMapTheme(theme.themeType);
  353. theme.serie = new SerieTheme(theme.themeType);
  354. theme.SetDefaultFont();
  355. }
  356. /// <summary>
  357. /// Convert the html string to color.
  358. /// |将字符串颜色值转成Color。
  359. /// </summary>
  360. /// <param name="hexColorStr"></param>
  361. /// <returns></returns>
  362. public static Color32 GetColor(string hexColorStr)
  363. {
  364. Color color;
  365. ColorUtility.TryParseHtmlString(hexColorStr, out color);
  366. return (Color32) color;
  367. }
  368. public void SetColorPalette(List<string> hexColorStringList)
  369. {
  370. m_ColorPalette.Clear();
  371. foreach (var hexColor in hexColorStringList)
  372. m_ColorPalette.Add(ColorUtil.GetColor(hexColor));
  373. }
  374. public override int GetHashCode()
  375. {
  376. return base.GetHashCode();
  377. }
  378. }
  379. }