ChartText.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. #if dUI_TextMeshPro
  4. using TMPro;
  5. #endif
  6. namespace XCharts.Runtime
  7. {
  8. [System.Serializable]
  9. public class ChartText
  10. {
  11. private Text m_Text;
  12. private TextAnchor m_TextAlignment;
  13. public Text text
  14. {
  15. get { return m_Text; }
  16. set { m_Text = value; }
  17. }
  18. #if dUI_TextMeshPro
  19. private TextMeshProUGUI m_TMPText;
  20. public TextMeshProUGUI tmpText { get { return m_TMPText; } set { m_TMPText = value; } }
  21. #endif
  22. public GameObject gameObject
  23. {
  24. get
  25. {
  26. #if dUI_TextMeshPro
  27. if (m_TMPText != null) return m_TMPText.gameObject;
  28. #else
  29. if (m_Text != null) return m_Text.gameObject;
  30. #endif
  31. return null;
  32. }
  33. }
  34. public TextAnchor alignment
  35. {
  36. get
  37. {
  38. return m_TextAlignment;
  39. }
  40. set
  41. {
  42. SetAlignment(alignment);
  43. }
  44. }
  45. public ChartText()
  46. { }
  47. public ChartText(GameObject textParent)
  48. {
  49. #if dUI_TextMeshPro
  50. m_TMPText = textParent.GetComponentInChildren<TextMeshProUGUI>();
  51. if (m_TMPText == null)
  52. {
  53. Debug.LogError("can't find TextMeshProUGUI component:" + textParent);
  54. }
  55. #else
  56. m_Text = textParent.GetComponentInChildren<Text>();
  57. if (m_Text == null)
  58. {
  59. Debug.LogError("can't find Text component:" + textParent);
  60. }
  61. #endif
  62. }
  63. public void SetFontSize(float fontSize)
  64. {
  65. #if dUI_TextMeshPro
  66. if (m_TMPText != null) m_TMPText.fontSize = fontSize;
  67. #else
  68. if (m_Text != null) m_Text.fontSize = (int)fontSize;
  69. #endif
  70. }
  71. public void SetText(string text)
  72. {
  73. if (text == null) text = string.Empty;
  74. else text = text.Replace("\\n", "\n");
  75. #if dUI_TextMeshPro
  76. if (m_TMPText != null) m_TMPText.text = text;
  77. #else
  78. if (m_Text != null) m_Text.text = text;
  79. #endif
  80. }
  81. public string GetText()
  82. {
  83. #if dUI_TextMeshPro
  84. if (m_TMPText != null) return m_TMPText.text;
  85. #else
  86. if (m_Text != null) return m_Text.text;
  87. #endif
  88. return string.Empty;
  89. }
  90. public void SetColor(Color color)
  91. {
  92. #if dUI_TextMeshPro
  93. if (m_TMPText != null) m_TMPText.color = color;
  94. #else
  95. if (m_Text != null) m_Text.color = color;
  96. #endif
  97. }
  98. public Color GetColor()
  99. {
  100. #if dUI_TextMeshPro
  101. if (m_TMPText != null) return m_TMPText.color;
  102. #else
  103. if (m_Text != null) return m_Text.color;
  104. #endif
  105. return Color.clear;
  106. }
  107. public void SetLineSpacing(float lineSpacing)
  108. {
  109. #if dUI_TextMeshPro
  110. if (m_TMPText != null) m_TMPText.lineSpacing = lineSpacing;
  111. #else
  112. if (m_Text != null) m_Text.lineSpacing = lineSpacing;
  113. #endif
  114. }
  115. public void SetActive(bool flag)
  116. {
  117. #if dUI_TextMeshPro
  118. //m_TMPText.gameObject.SetActive(flag);
  119. if (m_TMPText != null) ChartHelper.SetActive(m_TMPText.gameObject, flag);
  120. #else
  121. //m_Text.gameObject.SetActive(flag);
  122. if (m_Text != null) ChartHelper.SetActive(m_Text.gameObject, flag);
  123. #endif
  124. }
  125. public void SetLocalPosition(Vector3 position)
  126. {
  127. #if dUI_TextMeshPro
  128. if (m_TMPText != null) m_TMPText.transform.localPosition = position;
  129. #else
  130. if (m_Text != null) m_Text.transform.localPosition = position;
  131. #endif
  132. }
  133. public void SetRectPosition(Vector3 position)
  134. {
  135. #if dUI_TextMeshPro
  136. if (m_TMPText != null) m_TMPText.GetComponent<RectTransform>().anchoredPosition3D = position;
  137. #else
  138. if (m_Text != null) m_Text.GetComponent<RectTransform>().anchoredPosition3D = position;
  139. #endif
  140. }
  141. public void SetSizeDelta(Vector2 sizeDelta)
  142. {
  143. #if dUI_TextMeshPro
  144. if (m_TMPText != null) m_TMPText.GetComponent<RectTransform>().sizeDelta = sizeDelta;
  145. #else
  146. if (m_Text != null) m_Text.GetComponent<RectTransform>().sizeDelta = sizeDelta;
  147. #endif
  148. }
  149. public void SetLocalEulerAngles(Vector3 position)
  150. {
  151. #if dUI_TextMeshPro
  152. if (m_TMPText != null) m_TMPText.transform.localEulerAngles = position;
  153. #else
  154. if (m_Text != null) m_Text.transform.localEulerAngles = position;
  155. #endif
  156. }
  157. public void SetAlignment(TextAnchor alignment)
  158. {
  159. m_TextAlignment = alignment;
  160. #if dUI_TextMeshPro
  161. if (m_TMPText == null) return;
  162. switch (alignment)
  163. {
  164. case TextAnchor.LowerCenter:
  165. m_TMPText.alignment = TextAlignmentOptions.Bottom;
  166. break;
  167. case TextAnchor.LowerLeft:
  168. m_TMPText.alignment = TextAlignmentOptions.BottomLeft;
  169. break;
  170. case TextAnchor.LowerRight:
  171. m_TMPText.alignment = TextAlignmentOptions.BottomRight;
  172. break;
  173. case TextAnchor.MiddleCenter:
  174. m_TMPText.alignment = TextAlignmentOptions.Center;
  175. break;
  176. case TextAnchor.MiddleLeft:
  177. m_TMPText.alignment = TextAlignmentOptions.Left;
  178. break;
  179. case TextAnchor.MiddleRight:
  180. m_TMPText.alignment = TextAlignmentOptions.Right;
  181. break;
  182. case TextAnchor.UpperCenter:
  183. m_TMPText.alignment = TextAlignmentOptions.Top;
  184. break;
  185. case TextAnchor.UpperLeft:
  186. m_TMPText.alignment = TextAlignmentOptions.TopLeft;
  187. break;
  188. case TextAnchor.UpperRight:
  189. m_TMPText.alignment = TextAlignmentOptions.TopRight;
  190. break;
  191. default:
  192. m_TMPText.alignment = TextAlignmentOptions.Center;
  193. m_TextAlignment = TextAnchor.MiddleCenter;
  194. break;
  195. }
  196. #else
  197. if (m_Text != null) m_Text.alignment = alignment;
  198. #endif
  199. }
  200. public void SetFont(Font font)
  201. {
  202. if (m_Text) m_Text.font = font;
  203. }
  204. public void SetFontStyle(FontStyle fontStyle)
  205. {
  206. #if dUI_TextMeshPro
  207. if (m_TMPText == null) return;
  208. switch (fontStyle)
  209. {
  210. case FontStyle.Normal:
  211. m_TMPText.fontStyle = FontStyles.Normal;
  212. break;
  213. case FontStyle.Bold:
  214. m_TMPText.fontStyle = FontStyles.Bold;
  215. break;
  216. case FontStyle.BoldAndItalic:
  217. m_TMPText.fontStyle = FontStyles.Bold | FontStyles.Italic;
  218. break;
  219. case FontStyle.Italic:
  220. m_TMPText.fontStyle = FontStyles.Italic;
  221. break;
  222. }
  223. #else
  224. if (m_Text != null) m_Text.fontStyle = fontStyle;
  225. #endif
  226. }
  227. public void SetFontAndSizeAndStyle(TextStyle textStyle, ComponentTheme theme)
  228. {
  229. #if dUI_TextMeshPro
  230. if (m_TMPText == null) return;
  231. m_TMPText.font = textStyle.tmpFont == null ? theme.tmpFont : textStyle.tmpFont;
  232. m_TMPText.fontSize = textStyle.fontSize == 0 ? theme.fontSize : textStyle.fontSize;
  233. m_TMPText.fontStyle = textStyle.tmpFontStyle;
  234. #else
  235. if (m_Text != null)
  236. {
  237. m_Text.font = textStyle.font == null ? theme.font : textStyle.font;
  238. m_Text.fontSize = textStyle.fontSize == 0 ? theme.fontSize : textStyle.fontSize;
  239. m_Text.fontStyle = textStyle.fontStyle;
  240. }
  241. #endif
  242. }
  243. public float GetPreferredWidth(string content)
  244. {
  245. #if dUI_TextMeshPro
  246. if (m_TMPText != null && !string.IsNullOrEmpty(content))
  247. {
  248. return m_TMPText.GetPreferredValues(content).x;
  249. }
  250. #else
  251. if (m_Text != null && !string.IsNullOrEmpty(content))
  252. {
  253. var tg = m_Text.cachedTextGeneratorForLayout;
  254. var setting = m_Text.GetGenerationSettings(Vector2.zero);
  255. return tg.GetPreferredWidth(content, setting) / m_Text.pixelsPerUnit;
  256. }
  257. #endif
  258. return 0;
  259. }
  260. public float GetPreferredWidth()
  261. {
  262. #if dUI_TextMeshPro
  263. if (m_TMPText != null) return m_TMPText.preferredWidth;
  264. #else
  265. if (m_Text != null) return m_Text.preferredWidth;
  266. #endif
  267. return 0;
  268. }
  269. public float GetPreferredHeight()
  270. {
  271. #if dUI_TextMeshPro
  272. if (m_TMPText != null) return m_TMPText.preferredHeight;
  273. #else
  274. if (m_Text != null) return m_Text.preferredHeight;
  275. #endif
  276. return 0;
  277. }
  278. public string GetPreferredText(string content, string suffix, float maxWidth)
  279. {
  280. var sourWid = GetPreferredWidth(content);
  281. if (sourWid < maxWidth) return content;
  282. var suffixWid = GetPreferredWidth(suffix);
  283. var textWid = maxWidth - 1.3f * suffixWid;
  284. for (int i = content.Length; i > 0; i--)
  285. {
  286. var temp = content.Substring(0, i);
  287. if (GetPreferredWidth(temp) < textWid)
  288. {
  289. return temp + suffix;
  290. }
  291. }
  292. return string.Empty;
  293. }
  294. #if dUI_TextMeshPro
  295. public void SetFont(TMP_FontAsset font)
  296. {
  297. if (m_TMPText != null) m_TMPText.font = font;
  298. }
  299. #endif
  300. }
  301. }