Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChartText.cs 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 void SetLineSpacing(float lineSpacing)
  99. {
  100. #if dUI_TextMeshPro
  101. if (m_TMPText != null) m_TMPText.lineSpacing = lineSpacing;
  102. #else
  103. if (m_Text != null) m_Text.lineSpacing = lineSpacing;
  104. #endif
  105. }
  106. public void SetActive(bool flag)
  107. {
  108. #if dUI_TextMeshPro
  109. //m_TMPText.gameObject.SetActive(flag);
  110. if (m_TMPText != null) ChartHelper.SetActive(m_TMPText.gameObject, flag);
  111. #else
  112. //m_Text.gameObject.SetActive(flag);
  113. if (m_Text != null) ChartHelper.SetActive(m_Text.gameObject, flag);
  114. #endif
  115. }
  116. public void SetLocalPosition(Vector3 position)
  117. {
  118. #if dUI_TextMeshPro
  119. if (m_TMPText != null) m_TMPText.transform.localPosition = position;
  120. #else
  121. if (m_Text != null) m_Text.transform.localPosition = position;
  122. #endif
  123. }
  124. public void SetRectPosition(Vector3 position)
  125. {
  126. #if dUI_TextMeshPro
  127. if (m_TMPText != null) m_TMPText.GetComponent<RectTransform>().anchoredPosition3D = position;
  128. #else
  129. if (m_Text != null) m_Text.GetComponent<RectTransform>().anchoredPosition3D = position;
  130. #endif
  131. }
  132. public void SetSizeDelta(Vector2 sizeDelta)
  133. {
  134. #if dUI_TextMeshPro
  135. if (m_TMPText != null) m_TMPText.GetComponent<RectTransform>().sizeDelta = sizeDelta;
  136. #else
  137. if (m_Text != null) m_Text.GetComponent<RectTransform>().sizeDelta = sizeDelta;
  138. #endif
  139. }
  140. public void SetLocalEulerAngles(Vector3 position)
  141. {
  142. #if dUI_TextMeshPro
  143. if (m_TMPText != null) m_TMPText.transform.localEulerAngles = position;
  144. #else
  145. if (m_Text != null) m_Text.transform.localEulerAngles = position;
  146. #endif
  147. }
  148. public void SetAlignment(TextAnchor alignment)
  149. {
  150. m_TextAlignment = alignment;
  151. #if dUI_TextMeshPro
  152. if (m_TMPText == null) return;
  153. switch (alignment)
  154. {
  155. case TextAnchor.LowerCenter:
  156. m_TMPText.alignment = TextAlignmentOptions.Bottom;
  157. break;
  158. case TextAnchor.LowerLeft:
  159. m_TMPText.alignment = TextAlignmentOptions.BottomLeft;
  160. break;
  161. case TextAnchor.LowerRight:
  162. m_TMPText.alignment = TextAlignmentOptions.BottomRight;
  163. break;
  164. case TextAnchor.MiddleCenter:
  165. m_TMPText.alignment = TextAlignmentOptions.Center;
  166. break;
  167. case TextAnchor.MiddleLeft:
  168. m_TMPText.alignment = TextAlignmentOptions.Left;
  169. break;
  170. case TextAnchor.MiddleRight:
  171. m_TMPText.alignment = TextAlignmentOptions.Right;
  172. break;
  173. case TextAnchor.UpperCenter:
  174. m_TMPText.alignment = TextAlignmentOptions.Top;
  175. break;
  176. case TextAnchor.UpperLeft:
  177. m_TMPText.alignment = TextAlignmentOptions.TopLeft;
  178. break;
  179. case TextAnchor.UpperRight:
  180. m_TMPText.alignment = TextAlignmentOptions.TopRight;
  181. break;
  182. default:
  183. m_TMPText.alignment = TextAlignmentOptions.Center;
  184. m_TextAlignment = TextAnchor.MiddleCenter;
  185. 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:
  202. m_TMPText.fontStyle = FontStyles.Normal;
  203. break;
  204. case FontStyle.Bold:
  205. m_TMPText.fontStyle = FontStyles.Bold;
  206. break;
  207. case FontStyle.BoldAndItalic:
  208. m_TMPText.fontStyle = FontStyles.Bold | FontStyles.Italic;
  209. break;
  210. case FontStyle.Italic:
  211. m_TMPText.fontStyle = FontStyles.Italic;
  212. break;
  213. }
  214. #else
  215. if (m_Text != null) m_Text.fontStyle = fontStyle;
  216. #endif
  217. }
  218. public void SetFontAndSizeAndStyle(TextStyle textStyle, ComponentTheme theme)
  219. {
  220. #if dUI_TextMeshPro
  221. if (m_TMPText == null) return;
  222. m_TMPText.font = textStyle.tmpFont == null ? theme.tmpFont : textStyle.tmpFont;
  223. m_TMPText.fontSize = textStyle.fontSize == 0 ? theme.fontSize : textStyle.fontSize;
  224. m_TMPText.fontStyle = textStyle.tmpFontStyle;
  225. #else
  226. if (m_Text != null)
  227. {
  228. m_Text.font = textStyle.font == null ? theme.font : textStyle.font;
  229. m_Text.fontSize = textStyle.fontSize == 0 ? theme.fontSize : textStyle.fontSize;
  230. m_Text.fontStyle = textStyle.fontStyle;
  231. }
  232. #endif
  233. }
  234. public float GetPreferredWidth(string content)
  235. {
  236. #if dUI_TextMeshPro
  237. if (m_TMPText != null) return 0; // TODO:
  238. #else
  239. if (m_Text != null)
  240. {
  241. var tg = m_Text.cachedTextGeneratorForLayout;
  242. var setting = m_Text.GetGenerationSettings(Vector2.zero);
  243. return tg.GetPreferredWidth(content, setting) / m_Text.pixelsPerUnit;
  244. }
  245. #endif
  246. return 0;
  247. }
  248. public float GetPreferredWidth()
  249. {
  250. #if dUI_TextMeshPro
  251. if (m_TMPText != null) return m_TMPText.preferredWidth;
  252. #else
  253. if (m_Text != null) return m_Text.preferredWidth;
  254. #endif
  255. return 0;
  256. }
  257. public float GetPreferredHeight()
  258. {
  259. #if dUI_TextMeshPro
  260. if (m_TMPText != null) return m_TMPText.preferredHeight;
  261. #else
  262. if (m_Text != null) return m_Text.preferredHeight;
  263. #endif
  264. return 0;
  265. }
  266. public string GetPreferredText(string content, string suffix, float maxWidth)
  267. {
  268. #if dUI_TextMeshPro
  269. if (m_TMPText != null) return content; // TODO:
  270. #else
  271. if (m_Text != null)
  272. {
  273. var sourWid = GetPreferredWidth(content);
  274. if (sourWid < maxWidth) return content;
  275. var suffixWid = GetPreferredWidth(suffix);
  276. var textWid = maxWidth - 1.3f * suffixWid;
  277. for (int i = content.Length; i > 0; i--)
  278. {
  279. var temp = content.Substring(0, i);
  280. if (GetPreferredWidth(temp) < textWid)
  281. {
  282. return temp + suffix;
  283. }
  284. }
  285. }
  286. #endif
  287. return string.Empty;
  288. }
  289. #if dUI_TextMeshPro
  290. public void SetFont(TMP_FontAsset font)
  291. {
  292. if (m_TMPText != null) m_TMPText.font = font;
  293. }
  294. #endif
  295. }
  296. }