Files
XCharts/Runtime/Component/Child/TextStyle.cs

245 lines
7.9 KiB
C#
Raw Normal View History

using System;
using UnityEngine;
2021-01-11 08:54:28 +08:00
#if dUI_TextMeshPro
using TMPro;
#endif
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
{
/// <summary>
/// Settings related to text.
2023-11-11 23:32:24 +08:00
/// ||文本的相关设置。
/// </summary>
[Serializable]
2021-11-23 13:20:07 +08:00
public class TextStyle : ChildComponent
{
2022-04-26 08:24:45 +08:00
[SerializeField] private bool m_Show = true;
[SerializeField] private Font m_Font;
2021-05-29 22:07:09 +08:00
[SerializeField] private bool m_AutoWrap = false;
[SerializeField] private bool m_AutoAlign = true;
[SerializeField] private float m_Rotate = 0;
2022-03-29 22:06:10 +08:00
[SerializeField] private bool m_AutoColor = false;
[SerializeField] private Color m_Color = Color.clear;
2021-01-11 08:54:28 +08:00
[SerializeField] private int m_FontSize = 0;
[SerializeField] private FontStyle m_FontStyle = FontStyle.Normal;
[SerializeField] private float m_LineSpacing = 1f;
2021-01-11 08:54:28 +08:00
[SerializeField] private TextAnchor m_Alignment = TextAnchor.MiddleCenter;
#if dUI_TextMeshPro
[SerializeField] private TMP_FontAsset m_TMPFont;
[SerializeField] private FontStyles m_TMPFontStyle = FontStyles.Normal;
[SerializeField] private TextAlignmentOptions m_TMPAlignment = TextAlignmentOptions.Left;
2022-06-25 11:45:22 +08:00
[SerializeField][Since("v3.1.0")] private TMP_SpriteAsset m_TMPSpriteAsset;
2021-01-11 08:54:28 +08:00
#endif
2022-04-26 08:24:45 +08:00
public bool show
{
get { return m_Show; }
set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); }
}
/// <summary>
/// Rotation of text.
2023-11-11 23:32:24 +08:00
/// ||文本的旋转。
2020-07-10 09:13:26 +08:00
/// [default: `0f`]
/// </summary>
public float rotate
{
get { return m_Rotate; }
2021-01-11 08:54:28 +08:00
set { if (PropertyUtil.SetStruct(ref m_Rotate, value)) SetComponentDirty(); }
}
2021-12-19 20:53:55 +08:00
/// <summary>
2022-03-29 22:06:10 +08:00
/// 是否开启自动颜色。当开启时,会自动设置颜色。
/// </summary>
public bool autoColor
{
get { return m_AutoColor; }
set { if (PropertyUtil.SetStruct(ref m_AutoColor, value)) SetAllDirty(); }
}
/// <summary>
2022-03-24 08:37:06 +08:00
/// the color of text.
2023-11-11 23:32:24 +08:00
/// ||文本的颜色。
2020-07-10 09:13:26 +08:00
/// [default: `Color.clear`]
/// </summary>
public Color color
{
get { return m_Color; }
2021-01-11 08:54:28 +08:00
set { if (PropertyUtil.SetColor(ref m_Color, value)) SetComponentDirty(); }
}
/// <summary>
2020-07-10 09:13:26 +08:00
/// the font of text. When `null`, the theme's font is used by default.
2023-11-11 23:32:24 +08:00
/// ||文本字体。
2020-07-10 09:13:26 +08:00
/// [default: null]
/// </summary>
public Font font
{
get { return m_Font; }
2021-01-11 08:54:28 +08:00
set { if (PropertyUtil.SetClass(ref m_Font, value)) SetComponentDirty(); }
}
/// <summary>
/// font size.
2023-11-11 23:32:24 +08:00
/// ||文本字体大小。
2020-07-10 09:13:26 +08:00
/// [default: 18]
/// </summary>
public int fontSize
{
get { return m_FontSize; }
2021-01-11 08:54:28 +08:00
set { if (PropertyUtil.SetStruct(ref m_FontSize, value)) SetComponentDirty(); }
}
/// <summary>
/// font style.
2023-11-11 23:32:24 +08:00
/// ||文本字体的风格。
2020-07-10 09:13:26 +08:00
/// [default: FontStyle.Normal]
/// </summary>
public FontStyle fontStyle
{
get { return m_FontStyle; }
2021-01-11 08:54:28 +08:00
set { if (PropertyUtil.SetStruct(ref m_FontStyle, value)) SetComponentDirty(); }
}
/// <summary>
/// text line spacing.
2023-11-11 23:32:24 +08:00
/// ||行间距。
2020-07-10 09:13:26 +08:00
/// [default: 1f]
/// </summary>
public float lineSpacing
{
get { return m_LineSpacing; }
2021-01-11 08:54:28 +08:00
set { if (PropertyUtil.SetStruct(ref m_LineSpacing, value)) SetComponentDirty(); }
}
/// <summary>
/// 是否自动换行。
/// </summary>
public bool autoWrap
{
get { return m_AutoWrap; }
set { if (PropertyUtil.SetStruct(ref m_AutoWrap, value)) SetComponentDirty(); }
}
/// <summary>
/// 文本是否让系统自动选对齐方式。为false时才会用alignment。
/// </summary>
public bool autoAlign
{
get { return m_AutoAlign; }
set { if (PropertyUtil.SetStruct(ref m_AutoAlign, value)) SetComponentDirty(); }
}
/// <summary>
2021-01-11 08:54:28 +08:00
/// 对齐方式。
/// </summary>
public TextAnchor alignment
{
get { return m_Alignment; }
set { if (PropertyUtil.SetStruct(ref m_Alignment, value)) SetComponentDirty(); }
}
#if dUI_TextMeshPro
2022-06-15 07:36:05 +08:00
/// <summary>
/// the font of textmeshpro.
2023-11-11 23:32:24 +08:00
/// ||TextMeshPro字体。
2022-06-15 07:36:05 +08:00
/// </summary>
2021-01-11 08:54:28 +08:00
public TMP_FontAsset tmpFont
{
get { return m_TMPFont; }
set { if (PropertyUtil.SetClass(ref m_TMPFont, value)) SetComponentDirty(); }
}
2022-06-15 07:36:05 +08:00
/// <summary>
/// the font style of TextMeshPro.
2023-11-11 23:32:24 +08:00
/// ||TextMeshPro字体类型。
2022-06-15 07:36:05 +08:00
/// </summary>
2021-01-11 08:54:28 +08:00
public FontStyles tmpFontStyle
{
get { return m_TMPFontStyle; }
set { if (PropertyUtil.SetStruct(ref m_TMPFontStyle, value)) SetComponentDirty(); }
}
2022-06-15 07:36:05 +08:00
/// <summary>
/// the text alignment of TextMeshPro.
2023-11-11 23:32:24 +08:00
/// ||TextMeshPro字体对齐方式。
2022-06-15 07:36:05 +08:00
/// </summary>
2022-06-20 13:05:44 +08:00
public TextAlignmentOptions tmpAlignment
2022-06-15 07:36:05 +08:00
{
get { return m_TMPAlignment; }
set { if (PropertyUtil.SetStruct(ref m_TMPAlignment, value)) SetComponentDirty(); }
}
/// <summary>
/// the sprite asset of TextMeshPro.
2023-11-11 23:32:24 +08:00
/// ||TextMeshPro的Sprite Asset。
/// </summary>
public TMP_SpriteAsset tmpSpriteAsset
{
get { return m_TMPSpriteAsset; }
set { if (PropertyUtil.SetClass(ref m_TMPSpriteAsset, value)) SetComponentDirty(); }
}
2021-01-11 08:54:28 +08:00
#endif
2022-05-22 22:17:38 +08:00
public TextStyle() { }
public TextStyle(int fontSize)
{
this.fontSize = fontSize;
}
public TextStyle(int fontSize, FontStyle fontStyle)
{
this.fontSize = fontSize;
this.fontStyle = fontStyle;
}
public TextStyle(int fontSize, FontStyle fontStyle, Color color)
{
this.fontSize = fontSize;
this.fontStyle = fontStyle;
this.color = color;
}
public TextStyle(int fontSize, FontStyle fontStyle, Color color, int rorate)
{
this.fontSize = fontSize;
this.fontStyle = fontStyle;
this.color = color;
this.rotate = rotate;
}
2021-01-11 08:54:28 +08:00
public void Copy(TextStyle textStyle)
{
font = textStyle.font;
rotate = textStyle.rotate;
color = textStyle.color;
fontSize = textStyle.fontSize;
fontStyle = textStyle.fontStyle;
lineSpacing = textStyle.lineSpacing;
alignment = textStyle.alignment;
autoWrap = textStyle.autoWrap;
autoAlign = textStyle.autoAlign;
2021-01-11 08:54:28 +08:00
#if dUI_TextMeshPro
m_TMPFont = textStyle.tmpFont;
m_TMPFontStyle = textStyle.tmpFontStyle;
m_TMPSpriteAsset = textStyle.tmpSpriteAsset;
2021-01-11 08:54:28 +08:00
#endif
}
public void UpdateAlignmentByLocation(Location location)
{
#if dUI_TextMeshPro
m_TMPAlignment = location.runtimeTMPTextAlignment;
#else
m_Alignment = location.runtimeTextAlignment;
#endif
}
public Color GetColor(Color defaultColor)
{
2021-11-23 13:20:07 +08:00
if (ChartHelper.IsClearColor(color))
return defaultColor;
else
return color;
2021-01-11 08:54:28 +08:00
}
public int GetFontSize(ComponentTheme defaultTheme)
{
2021-11-23 13:20:07 +08:00
if (fontSize == 0)
return defaultTheme.fontSize;
else
return fontSize;
2021-01-11 08:54:28 +08:00
}
2022-04-26 08:24:45 +08:00
public TextAnchor GetAlignment(TextAnchor defaultAlignment)
2022-03-29 22:06:10 +08:00
{
2022-04-26 08:24:45 +08:00
return m_AutoAlign ? defaultAlignment : alignment;
2022-03-29 22:06:10 +08:00
}
}
}