ChartLabel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. namespace XCharts.Runtime
  4. {
  5. public class ChartLabel : Image
  6. {
  7. [SerializeField] private ChartText m_LabelText;
  8. private bool m_HideIconIfTextEmpty = false;
  9. private bool m_AutoSize = true;
  10. private float m_PaddingLeft = 0;
  11. private float m_PaddingRight = 0;
  12. private float m_PaddingTop = 0;
  13. private float m_PaddingBottom = 0;
  14. private float m_Width = 0;
  15. private float m_Height = 0;
  16. private RectTransform m_TextRect;
  17. private RectTransform m_IconRect;
  18. private RectTransform m_ObjectRect;
  19. private Vector3 m_IconOffest;
  20. private Align m_Align = Align.Left;
  21. private Image m_IconImage;
  22. private bool m_Active = true;
  23. public Image icon
  24. {
  25. get { return m_IconImage; }
  26. set { SetIcon(value); }
  27. }
  28. public ChartText text
  29. {
  30. get { return m_LabelText; }
  31. set
  32. {
  33. m_LabelText = value;
  34. if (value != null) m_TextRect = m_LabelText.gameObject.GetComponent<RectTransform>();
  35. }
  36. }
  37. public bool hideIconIfTextEmpty { set { m_HideIconIfTextEmpty = value; } }
  38. public bool isIconActive { get; private set; }
  39. public bool isAnimationEnd { get; internal set; }
  40. public Rect rect { get; set; }
  41. internal RectTransform objectRect
  42. {
  43. get
  44. {
  45. if (m_ObjectRect == null)
  46. m_ObjectRect = gameObject.GetComponent<RectTransform>();
  47. return m_ObjectRect;
  48. }
  49. }
  50. protected override void Awake()
  51. {
  52. raycastTarget = false;
  53. SetActive(true);
  54. }
  55. public void SetTextPadding(TextPadding padding)
  56. {
  57. m_PaddingLeft = padding.left;
  58. m_PaddingRight = padding.right;
  59. m_PaddingTop = padding.top;
  60. m_PaddingBottom = padding.bottom;
  61. UpdatePadding();
  62. }
  63. public void SetPadding(float[] padding)
  64. {
  65. if (padding.Length >= 4)
  66. {
  67. m_PaddingLeft = padding[3];
  68. m_PaddingRight = padding[1];
  69. m_PaddingTop = padding[0];
  70. m_PaddingBottom = padding[2];
  71. }
  72. else if (padding.Length >= 2)
  73. {
  74. m_PaddingLeft = padding[1];
  75. m_PaddingRight = padding[1];
  76. m_PaddingTop = padding[0];
  77. m_PaddingBottom = padding[0];
  78. }
  79. else if (padding.Length == 1)
  80. {
  81. m_PaddingLeft = padding[0];
  82. m_PaddingRight = padding[0];
  83. m_PaddingTop = padding[0];
  84. m_PaddingBottom = padding[0];
  85. }
  86. UpdatePadding();
  87. }
  88. public void SetIcon(Image image)
  89. {
  90. m_IconImage = image;
  91. if (image != null)
  92. {
  93. m_IconRect = m_IconImage.GetComponent<RectTransform>();
  94. }
  95. }
  96. public float GetWidth()
  97. {
  98. return m_Width;
  99. }
  100. public float GetHeight()
  101. {
  102. return m_Height;
  103. }
  104. public void SetSize(float width, float height)
  105. {
  106. this.m_Width = width;
  107. this.m_Height = height;
  108. m_AutoSize = width == 0 && height == 0;
  109. objectRect.sizeDelta = new Vector2(width, height);
  110. }
  111. public void SetIconSprite(Sprite sprite)
  112. {
  113. if (m_IconImage != null) m_IconImage.sprite = sprite;
  114. }
  115. public void SetIconSize(float width, float height)
  116. {
  117. if (m_IconRect != null) m_IconRect.sizeDelta = new Vector3(width, height);
  118. }
  119. public void UpdateIcon(IconStyle iconStyle, Sprite sprite = null, Color color = default(Color))
  120. {
  121. if (m_IconImage == null || iconStyle == null)
  122. return;
  123. SetIconActive(iconStyle.show);
  124. if (iconStyle.show)
  125. {
  126. m_IconImage.sprite = sprite == null ? iconStyle.sprite : sprite;
  127. m_IconImage.color = ChartHelper.IsClearColor(iconStyle.color) ? color : iconStyle.color;
  128. m_IconImage.type = iconStyle.type;
  129. m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height);
  130. m_IconOffest = iconStyle.offset;
  131. m_Align = iconStyle.align;
  132. m_HideIconIfTextEmpty = iconStyle.autoHideWhenLabelEmpty;
  133. AdjustIconPos();
  134. if (iconStyle.layer == IconStyle.Layer.UnderText)
  135. m_IconRect.SetSiblingIndex(0);
  136. else
  137. m_IconRect.SetSiblingIndex(transform.childCount - 1);
  138. }
  139. }
  140. public float GetTextWidth()
  141. {
  142. if (m_TextRect) return m_TextRect.sizeDelta.x;
  143. else return 0;
  144. }
  145. public float GetTextHeight()
  146. {
  147. if (m_TextRect) return m_TextRect.sizeDelta.y;
  148. return 0;
  149. }
  150. public void SetTextColor(Color color)
  151. {
  152. if (m_LabelText != null) m_LabelText.SetColor(color);
  153. }
  154. public void SetRotate(float rotate)
  155. {
  156. transform.localEulerAngles = new Vector3(0, 0, rotate);
  157. }
  158. public void SetTextRotate(float rotate)
  159. {
  160. if (m_LabelText != null) m_LabelText.SetLocalEulerAngles(new Vector3(0, 0, rotate));
  161. }
  162. public void SetPosition(Vector3 position)
  163. {
  164. transform.localPosition = position;
  165. }
  166. public void SetRectPosition(Vector3 position)
  167. {
  168. objectRect.anchoredPosition3D = position;
  169. }
  170. public Vector3 GetPosition()
  171. {
  172. return transform.localPosition;
  173. }
  174. public bool IsActiveByScale()
  175. {
  176. return m_Active;
  177. }
  178. public void SetActive(bool flag)
  179. {
  180. m_Active = flag;
  181. ChartHelper.SetActive(gameObject, flag);
  182. }
  183. public void SetTextActive(bool flag)
  184. {
  185. if (m_LabelText != null) m_LabelText.SetActive(flag);
  186. }
  187. public void SetIconActive(bool flag)
  188. {
  189. isIconActive = flag;
  190. if (m_IconImage) ChartHelper.SetActive(m_IconImage, flag);
  191. }
  192. public bool SetText(string text)
  193. {
  194. if (m_TextRect == null || m_LabelText == null)
  195. return false;
  196. if (text == null)
  197. text = "";
  198. if (!m_LabelText.GetText().Equals(text))
  199. {
  200. m_LabelText.SetText(text);
  201. if (m_AutoSize)
  202. {
  203. var newSize = string.IsNullOrEmpty(text) ? Vector2.zero :
  204. new Vector2(m_LabelText.GetPreferredWidth(),
  205. m_LabelText.GetPreferredHeight());
  206. var sizeChange = newSize.x != m_TextRect.sizeDelta.x || newSize.y != m_TextRect.sizeDelta.y;
  207. this.m_Width = newSize.x;
  208. this.m_Height = newSize.y;
  209. if (sizeChange)
  210. {
  211. m_TextRect.sizeDelta = newSize;
  212. UpdateSize();
  213. UpdatePadding();
  214. AdjustIconPos();
  215. }
  216. return sizeChange;
  217. }
  218. AdjustIconPos();
  219. if (m_HideIconIfTextEmpty && isIconActive)
  220. {
  221. ChartHelper.SetActive(m_IconImage.gameObject, !string.IsNullOrEmpty(text));
  222. }
  223. }
  224. return false;
  225. }
  226. private void UpdateSize()
  227. {
  228. if (m_AutoSize)
  229. {
  230. var sizeDelta = m_TextRect.sizeDelta;
  231. m_Width = sizeDelta.x + m_PaddingLeft + m_PaddingRight;
  232. m_Height = sizeDelta.y + m_PaddingTop + m_PaddingBottom;
  233. objectRect.sizeDelta = new Vector2(m_Width, m_Height);
  234. }
  235. }
  236. private void UpdatePadding()
  237. {
  238. if (m_TextRect == null) return;
  239. switch (text.alignment)
  240. {
  241. case TextAnchor.LowerLeft:
  242. m_TextRect.anchoredPosition = new Vector2(m_PaddingLeft, m_PaddingBottom);
  243. break;
  244. case TextAnchor.UpperLeft:
  245. m_TextRect.anchoredPosition = new Vector2(m_PaddingLeft, -m_PaddingTop);
  246. break;
  247. case TextAnchor.MiddleLeft:
  248. m_TextRect.anchoredPosition = new Vector2(m_PaddingLeft, m_Height / 2 - m_PaddingTop - m_TextRect.sizeDelta.y / 2);
  249. break;
  250. case TextAnchor.LowerRight:
  251. m_TextRect.anchoredPosition = new Vector2(-m_PaddingRight, m_PaddingBottom);
  252. break;
  253. case TextAnchor.UpperRight:
  254. m_TextRect.anchoredPosition = new Vector2(-m_PaddingRight, -m_PaddingTop);
  255. break;
  256. case TextAnchor.MiddleRight:
  257. m_TextRect.anchoredPosition = new Vector2(-m_PaddingRight, m_Height / 2 - m_PaddingTop - m_TextRect.sizeDelta.y / 2);
  258. break;
  259. case TextAnchor.LowerCenter:
  260. m_TextRect.anchoredPosition = new Vector2(-(m_Width / 2 - m_PaddingLeft - m_TextRect.sizeDelta.x / 2), m_PaddingBottom);
  261. break;
  262. case TextAnchor.UpperCenter:
  263. m_TextRect.anchoredPosition = new Vector2(-(m_Width / 2 - m_PaddingLeft - m_TextRect.sizeDelta.x / 2), -m_PaddingTop);
  264. break;
  265. case TextAnchor.MiddleCenter:
  266. m_TextRect.anchoredPosition = new Vector2(-(m_Width / 2 - m_PaddingLeft - m_TextRect.sizeDelta.x / 2), m_Height / 2 - m_PaddingTop - m_TextRect.sizeDelta.y / 2);
  267. break;
  268. default:
  269. break;
  270. }
  271. }
  272. private void AdjustIconPos()
  273. {
  274. if (m_IconImage && m_IconRect && m_LabelText != null && m_TextRect != null)
  275. {
  276. var iconX = 0f;
  277. switch (m_Align)
  278. {
  279. case Align.Left:
  280. switch (m_LabelText.alignment)
  281. {
  282. case TextAnchor.LowerLeft:
  283. case TextAnchor.UpperLeft:
  284. case TextAnchor.MiddleLeft:
  285. iconX = -m_TextRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2;
  286. break;
  287. case TextAnchor.LowerRight:
  288. case TextAnchor.UpperRight:
  289. case TextAnchor.MiddleRight:
  290. iconX = m_TextRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2;
  291. break;
  292. case TextAnchor.LowerCenter:
  293. case TextAnchor.UpperCenter:
  294. case TextAnchor.MiddleCenter:
  295. iconX = -m_LabelText.GetPreferredWidth() / 2 - m_IconRect.sizeDelta.x / 2;
  296. break;
  297. }
  298. break;
  299. case Align.Right:
  300. switch (m_LabelText.alignment)
  301. {
  302. case TextAnchor.LowerLeft:
  303. case TextAnchor.UpperLeft:
  304. case TextAnchor.MiddleLeft:
  305. iconX = m_TextRect.sizeDelta.x / 2 + m_IconRect.sizeDelta.x / 2;
  306. break;
  307. case TextAnchor.LowerRight:
  308. case TextAnchor.UpperRight:
  309. case TextAnchor.MiddleRight:
  310. iconX = m_IconRect.sizeDelta.x / 2;
  311. break;
  312. case TextAnchor.LowerCenter:
  313. case TextAnchor.UpperCenter:
  314. case TextAnchor.MiddleCenter:
  315. iconX = m_LabelText.GetPreferredWidth() / 2 + m_IconRect.sizeDelta.x / 2;
  316. break;
  317. }
  318. break;
  319. }
  320. m_IconRect.anchoredPosition = m_IconOffest + new Vector3(iconX, 0);
  321. }
  322. }
  323. }
  324. }