using System; using System.Collections.Generic; using System.Text; using UnityEngine; #if dUI_TextMeshPro using TMPro; #endif namespace XCharts.Runtime { /// /// 主题 /// public enum ThemeType { /// /// 默认主题。 /// Default, /// /// 亮主题。 /// Light, /// /// 暗主题。 /// Dark, /// /// 自定义主题。 /// Custom, } [Serializable] /// /// Theme. /// |主题相关配置。 /// public class ThemeStyle : ChildComponent { [SerializeField] private bool m_Show = true; [SerializeField] private Theme m_SharedTheme; [SerializeField] private bool m_TransparentBackground = false; [SerializeField] private bool m_EnableCustomTheme = false; [SerializeField] private Font m_CustomFont; [SerializeField] private Color32 m_CustomBackgroundColor; #if UNITY_2020_2 [NonReorderable] #endif [SerializeField] private List m_CustomColorPalette = new List(13); public bool show { get { return m_Show; } } /// /// the theme of chart. /// |主题类型。 /// public ThemeType themeType { get { return sharedTheme.themeType; } } public string themeName { get { return sharedTheme.themeName; } } public Theme sharedTheme { get { return m_SharedTheme; } set { m_SharedTheme = value; SetAllDirty(); } } /// /// the contrast color of chart. /// |对比色。 /// public Color32 contrastColor { get { return sharedTheme.contrastColor; } } /// /// the background color of chart. /// |背景颜色。 /// public Color32 backgroundColor { get { if (m_TransparentBackground) return ColorUtil.clearColor32; else return m_EnableCustomTheme ? m_CustomBackgroundColor : sharedTheme.backgroundColor; } } /// /// Whether the background color is transparent. When true, the background color is not drawn. /// |是否透明背景颜色。当设置为true时,不绘制背景颜色。 /// public bool transparentBackground { get { return m_TransparentBackground; } set { m_TransparentBackground = value; SetAllDirty(); } } /// /// Whether to customize theme colors. When set to true, /// you can use 'sync color to custom' to synchronize the theme color to the custom color. It can also be set manually. /// |是否自定义主题颜色。当设置为true时,可以用‘sync color to custom’同步主题的颜色到自定义颜色。也可以手动设置。 /// /// public bool enableCustomTheme { get { return m_EnableCustomTheme; } set { m_EnableCustomTheme = value; _colorDic.Clear(); SetAllDirty(); } } /// /// the custom background color of chart. /// |自定义的背景颜色。 /// public Color32 customBackgroundColor { get { return m_CustomBackgroundColor; } set { m_CustomBackgroundColor = value; SetAllDirty(); } } /// /// 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. /// |调色盘颜色列表。如果系列没有设置颜色,则会依次循环从该列表中取颜色作为系列颜色。 /// public List colorPalette { get { return m_EnableCustomTheme ? m_CustomColorPalette : sharedTheme.colorPalette; } } public List customColorPalette { get { return m_CustomColorPalette; } set { m_CustomColorPalette = value; SetVerticesDirty(); } } public ComponentTheme common { get { return sharedTheme.common; } } public TitleTheme title { get { return sharedTheme.title; } } public SubTitleTheme subTitle { get { return sharedTheme.subTitle; } } public LegendTheme legend { get { return sharedTheme.legend; } } public AxisTheme axis { get { return sharedTheme.axis; } } public TooltipTheme tooltip { get { return sharedTheme.tooltip; } } public DataZoomTheme dataZoom { get { return sharedTheme.dataZoom; } } public VisualMapTheme visualMap { get { return sharedTheme.visualMap; } } public SerieTheme serie { get { return sharedTheme.serie; } } /// /// Gets the color of the specified index from the palette. /// |获得调色盘对应系列索引的颜色值。 /// /// 编号索引 /// the color,or Color.clear when failed.颜色值,失败时返回Color.clear public Color32 GetColor(int index) { if (colorPalette.Count <= 0) return Color.clear; if (index < 0) index = 0; var newIndex = index < colorPalette.Count ? index : index % colorPalette.Count; if (newIndex < colorPalette.Count) return colorPalette[newIndex]; else return Color.clear; } public Color32 GetBackgroundColor(Background background) { if (background != null && background.show && !background.autoColor) return background.imageColor; else return backgroundColor; } public void SyncSharedThemeColorToCustom() { m_CustomBackgroundColor = sharedTheme.backgroundColor; m_CustomColorPalette.Clear(); foreach (var color in sharedTheme.colorPalette) { m_CustomColorPalette.Add(color); } SetAllDirty(); } public void CheckWarning(StringBuilder sb) { #if dUI_TextMeshPro if (sharedTheme.tmpFont == null) { sb.AppendFormat("warning:theme->tmpFont is null\n"); } #else if (sharedTheme.font == null) { sb.AppendFormat("warning:theme->font is null\n"); } #endif if (sharedTheme.colorPalette.Count == 0) { sb.AppendFormat("warning:theme->colorPalette is empty\n"); } for (int i = 0; i < sharedTheme.colorPalette.Count; i++) { if (!ChartHelper.IsClearColor(sharedTheme.colorPalette[i]) && sharedTheme.colorPalette[i].a == 0) sb.AppendFormat("warning:theme->colorPalette[{0}] alpha = 0\n", i); } } Dictionary _colorDic = new Dictionary(); /// /// Gets the hexadecimal color string of the specified index from the palette. /// |获得指定索引的十六进制颜色值字符串。 /// /// /// public string GetColorStr(int index) { if (index < 0) { index = 0; } index = index % colorPalette.Count; if (_colorDic.ContainsKey(index)) return _colorDic[index]; else { _colorDic[index] = ColorUtility.ToHtmlStringRGBA(GetColor(index)); return _colorDic[index]; } } /// /// Convert the html string to color. /// |将字符串颜色值转成Color。 /// /// /// public static Color32 GetColor(string hexColorStr) { Color color; ColorUtility.TryParseHtmlString(hexColorStr, out color); return (Color32) color; } } }