暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TextStyle.cs 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using System;
  2. using UnityEngine;
  3. #if dUI_TextMeshPro
  4. using TMPro;
  5. #endif
  6. namespace XCharts.Runtime
  7. {
  8. /// <summary>
  9. /// Settings related to text.
  10. /// |文本的相关设置。
  11. /// </summary>
  12. [Serializable]
  13. public class TextStyle : ChildComponent
  14. {
  15. [SerializeField] private bool m_Show = true;
  16. [SerializeField] private Font m_Font;
  17. [SerializeField] private bool m_AutoWrap = false;
  18. [SerializeField] private bool m_AutoAlign = true;
  19. [SerializeField] private float m_Rotate = 0;
  20. [SerializeField] private bool m_AutoColor = false;
  21. [SerializeField] private Color m_Color = Color.clear;
  22. [SerializeField] private int m_FontSize = 0;
  23. [SerializeField] private FontStyle m_FontStyle = FontStyle.Normal;
  24. [SerializeField] private float m_LineSpacing = 1f;
  25. [SerializeField] private TextAnchor m_Alignment = TextAnchor.MiddleCenter;
  26. #if dUI_TextMeshPro
  27. [SerializeField] private TMP_FontAsset m_TMPFont;
  28. [SerializeField] private FontStyles m_TMPFontStyle = FontStyles.Normal;
  29. [SerializeField] private TextAlignmentOptions m_TMPAlignment = TextAlignmentOptions.Left;
  30. [SerializeField][Since("v3.1.0")] private TMP_SpriteAsset m_TMPSpriteAsset;
  31. #endif
  32. public bool show
  33. {
  34. get { return m_Show; }
  35. set { if (PropertyUtil.SetStruct(ref m_Show, value)) SetComponentDirty(); }
  36. }
  37. /// <summary>
  38. /// Rotation of text.
  39. /// |文本的旋转。
  40. /// [default: `0f`]
  41. /// </summary>
  42. public float rotate
  43. {
  44. get { return m_Rotate; }
  45. set { if (PropertyUtil.SetStruct(ref m_Rotate, value)) SetComponentDirty(); }
  46. }
  47. /// <summary>
  48. /// 是否开启自动颜色。当开启时,会自动设置颜色。
  49. /// </summary>
  50. public bool autoColor
  51. {
  52. get { return m_AutoColor; }
  53. set { if (PropertyUtil.SetStruct(ref m_AutoColor, value)) SetAllDirty(); }
  54. }
  55. /// <summary>
  56. /// the color of text.
  57. /// |文本的颜色。
  58. /// [default: `Color.clear`]
  59. /// </summary>
  60. public Color color
  61. {
  62. get { return m_Color; }
  63. set { if (PropertyUtil.SetColor(ref m_Color, value)) SetComponentDirty(); }
  64. }
  65. /// <summary>
  66. /// the font of text. When `null`, the theme's font is used by default.
  67. /// |文本字体。
  68. /// [default: null]
  69. /// </summary>
  70. public Font font
  71. {
  72. get { return m_Font; }
  73. set { if (PropertyUtil.SetClass(ref m_Font, value)) SetComponentDirty(); }
  74. }
  75. /// <summary>
  76. /// font size.
  77. /// |文本字体大小。
  78. /// [default: 18]
  79. /// </summary>
  80. public int fontSize
  81. {
  82. get { return m_FontSize; }
  83. set { if (PropertyUtil.SetStruct(ref m_FontSize, value)) SetComponentDirty(); }
  84. }
  85. /// <summary>
  86. /// font style.
  87. /// |文本字体的风格。
  88. /// [default: FontStyle.Normal]
  89. /// </summary>
  90. public FontStyle fontStyle
  91. {
  92. get { return m_FontStyle; }
  93. set { if (PropertyUtil.SetStruct(ref m_FontStyle, value)) SetComponentDirty(); }
  94. }
  95. /// <summary>
  96. /// text line spacing.
  97. /// |行间距。
  98. /// [default: 1f]
  99. /// </summary>
  100. public float lineSpacing
  101. {
  102. get { return m_LineSpacing; }
  103. set { if (PropertyUtil.SetStruct(ref m_LineSpacing, value)) SetComponentDirty(); }
  104. }
  105. /// <summary>
  106. /// 是否自动换行。
  107. /// </summary>
  108. public bool autoWrap
  109. {
  110. get { return m_AutoWrap; }
  111. set { if (PropertyUtil.SetStruct(ref m_AutoWrap, value)) SetComponentDirty(); }
  112. }
  113. /// <summary>
  114. /// 文本是否让系统自动选对齐方式。为false时才会用alignment。
  115. /// </summary>
  116. public bool autoAlign
  117. {
  118. get { return m_AutoAlign; }
  119. set { if (PropertyUtil.SetStruct(ref m_AutoAlign, value)) SetComponentDirty(); }
  120. }
  121. /// <summary>
  122. /// 对齐方式。
  123. /// </summary>
  124. public TextAnchor alignment
  125. {
  126. get { return m_Alignment; }
  127. set { if (PropertyUtil.SetStruct(ref m_Alignment, value)) SetComponentDirty(); }
  128. }
  129. #if dUI_TextMeshPro
  130. /// <summary>
  131. /// the font of textmeshpro.
  132. /// |TextMeshPro字体。
  133. /// </summary>
  134. public TMP_FontAsset tmpFont
  135. {
  136. get { return m_TMPFont; }
  137. set { if (PropertyUtil.SetClass(ref m_TMPFont, value)) SetComponentDirty(); }
  138. }
  139. /// <summary>
  140. /// the font style of TextMeshPro.
  141. /// |TextMeshPro字体类型。
  142. /// </summary>
  143. public FontStyles tmpFontStyle
  144. {
  145. get { return m_TMPFontStyle; }
  146. set { if (PropertyUtil.SetStruct(ref m_TMPFontStyle, value)) SetComponentDirty(); }
  147. }
  148. /// <summary>
  149. /// the text alignment of TextMeshPro.
  150. /// |TextMeshPro字体对齐方式。
  151. /// </summary>
  152. public TextAlignmentOptions tmpAlignment
  153. {
  154. get { return m_TMPAlignment; }
  155. set { if (PropertyUtil.SetStruct(ref m_TMPAlignment, value)) SetComponentDirty(); }
  156. }
  157. /// <summary>
  158. /// the sprite asset of TextMeshPro.
  159. /// |TextMeshPro的Sprite Asset。
  160. /// </summary>
  161. public TMP_SpriteAsset tmpSpriteAsset
  162. {
  163. get { return m_TMPSpriteAsset; }
  164. set { if (PropertyUtil.SetClass(ref m_TMPSpriteAsset, value)) SetComponentDirty(); }
  165. }
  166. #endif
  167. public TextStyle() { }
  168. public TextStyle(int fontSize)
  169. {
  170. this.fontSize = fontSize;
  171. }
  172. public TextStyle(int fontSize, FontStyle fontStyle)
  173. {
  174. this.fontSize = fontSize;
  175. this.fontStyle = fontStyle;
  176. }
  177. public TextStyle(int fontSize, FontStyle fontStyle, Color color)
  178. {
  179. this.fontSize = fontSize;
  180. this.fontStyle = fontStyle;
  181. this.color = color;
  182. }
  183. public TextStyle(int fontSize, FontStyle fontStyle, Color color, int rorate)
  184. {
  185. this.fontSize = fontSize;
  186. this.fontStyle = fontStyle;
  187. this.color = color;
  188. this.rotate = rotate;
  189. }
  190. public void Copy(TextStyle textStyle)
  191. {
  192. font = textStyle.font;
  193. rotate = textStyle.rotate;
  194. color = textStyle.color;
  195. fontSize = textStyle.fontSize;
  196. fontStyle = textStyle.fontStyle;
  197. lineSpacing = textStyle.lineSpacing;
  198. alignment = textStyle.alignment;
  199. autoWrap = textStyle.autoWrap;
  200. autoAlign = textStyle.autoAlign;
  201. #if dUI_TextMeshPro
  202. m_TMPFont = textStyle.tmpFont;
  203. m_TMPFontStyle = textStyle.tmpFontStyle;
  204. m_TMPSpriteAsset = textStyle.tmpSpriteAsset;
  205. #endif
  206. }
  207. public void UpdateAlignmentByLocation(Location location)
  208. {
  209. #if dUI_TextMeshPro
  210. m_TMPAlignment = location.runtimeTMPTextAlignment;
  211. #else
  212. m_Alignment = location.runtimeTextAlignment;
  213. #endif
  214. }
  215. public Color GetColor(Color defaultColor)
  216. {
  217. if (ChartHelper.IsClearColor(color))
  218. return defaultColor;
  219. else
  220. return color;
  221. }
  222. public int GetFontSize(ComponentTheme defaultTheme)
  223. {
  224. if (fontSize == 0)
  225. return defaultTheme.fontSize;
  226. else
  227. return fontSize;
  228. }
  229. public TextAnchor GetAlignment(TextAnchor defaultAlignment)
  230. {
  231. return m_AutoAlign ? defaultAlignment : alignment;
  232. }
  233. }
  234. }