ChartText.cs 10 KB

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