2021-11-23 13:20:07 +08:00
using System.Collections.Generic ;
using UnityEngine ;
using System ;
using System.Text ;
#if dUI_TextMeshPro
using TMPro ;
#endif
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
2021-11-23 13:20:07 +08:00
{
/// <summary>
/// 主题
/// </summary>
public enum ThemeType
{
/// <summary>
/// 默认主题。
/// </summary>
Default ,
/// <summary>
/// 亮主题。
/// </summary>
Light ,
/// <summary>
/// 暗主题。
/// </summary>
Dark ,
/// <summary>
/// 自定义主题。
/// </summary>
Custom ,
}
[Serializable]
/// <summary>
/// Theme.
2022-03-24 08:37:06 +08:00
/// |主题相关配置。
2021-11-23 13:20:07 +08:00
/// </summary>
public class ThemeStyle : ChildComponent
{
2022-02-25 08:10:09 +08:00
[SerializeField] private bool m_Show = true ;
2021-11-23 13:20:07 +08:00
[SerializeField] private Theme m_SharedTheme ;
2022-03-30 08:24:47 +08:00
[SerializeField] private bool m_TransparentBackground = false ;
[SerializeField] private bool m_EnableCustomTheme = false ;
2022-02-12 20:10:29 +08:00
[SerializeField] private Font m_CustomFont ;
2021-11-23 13:20:07 +08:00
[SerializeField] private Color32 m_CustomBackgroundColor ;
#if UNITY_2020_2
[NonReorderable]
#endif
[SerializeField] private List < Color32 > m_CustomColorPalette = new List < Color32 > ( 13 ) ;
2022-02-25 08:10:09 +08:00
public bool show { get { return m_Show ; } }
2021-11-23 13:20:07 +08:00
/// <summary>
/// the theme of chart.
2022-03-24 08:37:06 +08:00
/// |主题类型。
2021-11-23 13:20:07 +08:00
/// </summary>
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 ( ) ; }
}
/// <summary>
/// the contrast color of chart.
2022-03-24 08:37:06 +08:00
/// |对比色。
2021-11-23 13:20:07 +08:00
/// </summary>
public Color32 contrastColor
{
get { return sharedTheme . contrastColor ; }
}
/// <summary>
/// the background color of chart.
2022-03-24 08:37:06 +08:00
/// |背景颜色。
2021-11-23 13:20:07 +08:00
/// </summary>
public Color32 backgroundColor
{
2022-03-30 08:24:47 +08:00
get
{
if ( m_TransparentBackground ) return ColorUtil . clearColor32 ;
else return m_EnableCustomTheme ? m_CustomBackgroundColor : sharedTheme . backgroundColor ;
}
2021-11-23 13:20:07 +08:00
}
2022-03-30 08:24:47 +08:00
/// <summary>
/// Whether the background color is transparent. When true, the background color is not drawn.
/// | 是否透明背景颜色。当设置为true时, 不绘制背景颜色。
/// </summary>
public bool transparentBackground
{
get { return m_TransparentBackground ; }
set { m_TransparentBackground = value ; SetAllDirty ( ) ; }
}
/// <summary>
/// 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’ 同步主题的颜色到自定义颜色。也可以手动设置。
/// </summary>
/// <value></value>
2021-11-23 13:20:07 +08:00
public bool enableCustomTheme
{
get { return m_EnableCustomTheme ; }
set { m_EnableCustomTheme = value ; _colorDic . Clear ( ) ; SetAllDirty ( ) ; }
}
/// <summary>
/// the custom background color of chart.
2022-03-24 08:37:06 +08:00
/// |自定义的背景颜色。
2021-11-23 13:20:07 +08:00
/// </summary>
public Color32 customBackgroundColor
{
get { return m_CustomBackgroundColor ; }
set { m_CustomBackgroundColor = value ; SetAllDirty ( ) ; }
}
/// <summary>
/// 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.
2022-03-24 08:37:06 +08:00
/// |调色盘颜色列表。如果系列没有设置颜色,则会依次循环从该列表中取颜色作为系列颜色。
2021-11-23 13:20:07 +08:00
/// </summary>
public List < Color32 > colorPalette
{
get { return m_EnableCustomTheme ? m_CustomColorPalette : sharedTheme . colorPalette ; }
}
public List < Color32 > 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 ; } }
/// <summary>
2022-03-24 08:37:06 +08:00
/// Gets the color of the specified index from the palette.
/// |获得调色盘对应系列索引的颜色值。
2021-11-23 13:20:07 +08:00
/// </summary>
/// <param name="index">编号索引</param>
/// <returns>the color,or Color.clear when failed.颜色值, 失败时返回Color.clear</returns>
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 . hideThemeBackgroundColor )
2022-03-29 22:06:10 +08:00
return background . imageColor ;
2021-11-23 13:20:07 +08:00
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
2022-01-13 21:45:59 +08:00
if ( sharedTheme . tmpFont = = null )
2021-11-23 13:20:07 +08:00
{
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 < int , string > _colorDic = new Dictionary < int , string > ( ) ;
/// <summary>
2022-03-24 08:37:06 +08:00
/// Gets the hexadecimal color string of the specified index from the palette.
/// |获得指定索引的十六进制颜色值字符串。
2021-11-23 13:20:07 +08:00
/// </summary>
/// <param name="index"></param>
/// <returns></returns>
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 ] ;
}
}
/// <summary>
2022-03-24 08:37:06 +08:00
/// Convert the html string to color.
/// |将字符串颜色值转成Color。
2021-11-23 13:20:07 +08:00
/// </summary>
/// <param name="hexColorStr"></param>
/// <returns></returns>
public static Color32 GetColor ( string hexColorStr )
{
Color color ;
ColorUtility . TryParseHtmlString ( hexColorStr , out color ) ;
return ( Color32 ) color ;
}
}
}