ChartLabel.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /************************************************/
  2. /* */
  3. /* Copyright (c) 2018 - 2021 monitor1394 */
  4. /* https://github.com/monitor1394 */
  5. /* */
  6. /************************************************/
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. namespace XCharts
  10. {
  11. public class ChartLabel : ChartObject
  12. {
  13. private bool m_AutoHideIconWhenLabelEmpty = false;
  14. private bool m_LabelAutoSize = true;
  15. private float m_LabelPaddingLeftRight = 3f;
  16. private float m_LabelPaddingTopBottom = 3f;
  17. private ChartText m_LabelText;
  18. private RectTransform m_LabelRect;
  19. private RectTransform m_IconRect;
  20. private RectTransform m_ObjectRect;
  21. private Vector3 m_IconOffest;
  22. private Align m_Align = Align.Left;
  23. private Image m_IconImage;
  24. public GameObject gameObject
  25. {
  26. get { return m_GameObject; }
  27. set
  28. {
  29. m_GameObject = value;
  30. m_ObjectRect = value.GetComponent<RectTransform>();
  31. }
  32. }
  33. public Image icon
  34. {
  35. get { return m_IconImage; }
  36. set { SetIcon(value); }
  37. }
  38. public ChartText label
  39. {
  40. get { return m_LabelText; }
  41. set
  42. {
  43. m_LabelText = value;
  44. if (value != null) m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
  45. }
  46. }
  47. public bool autoHideIconWhenLabelEmpty { set { m_AutoHideIconWhenLabelEmpty = value; } }
  48. public bool isIconActive { get; private set; }
  49. public ChartLabel()
  50. {
  51. }
  52. public void SetLabel(GameObject labelObj, bool autoSize, float paddingLeftRight, float paddingTopBottom)
  53. {
  54. m_GameObject = labelObj;
  55. m_LabelAutoSize = autoSize;
  56. m_LabelPaddingLeftRight = paddingLeftRight;
  57. m_LabelPaddingTopBottom = paddingTopBottom;
  58. m_LabelText = new ChartText(labelObj);
  59. m_LabelRect = m_LabelText.gameObject.GetComponent<RectTransform>();
  60. m_ObjectRect = labelObj.GetComponent<RectTransform>();
  61. m_Align = Align.Left;
  62. }
  63. public void SetAutoSize(bool flag)
  64. {
  65. m_LabelAutoSize = flag;
  66. }
  67. public void SetIcon(Image image)
  68. {
  69. m_IconImage = image;
  70. if (image != null)
  71. {
  72. m_IconRect = m_IconImage.GetComponent<RectTransform>();
  73. }
  74. }
  75. public void SetIconSprite(Sprite sprite)
  76. {
  77. if (m_IconImage != null) m_IconImage.sprite = sprite;
  78. }
  79. public void SetIconSize(float width, float height)
  80. {
  81. if (m_IconRect != null) m_IconRect.sizeDelta = new Vector3(width, height);
  82. }
  83. public void UpdateIcon(IconStyle iconStyle, Sprite sprite = null)
  84. {
  85. if (m_IconImage == null) return;
  86. SetIconActive(iconStyle.show);
  87. if (iconStyle.show)
  88. {
  89. m_IconImage.sprite = sprite == null ? iconStyle.sprite : sprite;
  90. m_IconImage.color = iconStyle.color;
  91. m_IconRect.sizeDelta = new Vector2(iconStyle.width, iconStyle.height);
  92. m_IconOffest = iconStyle.offset;
  93. m_Align = iconStyle.align;
  94. m_AutoHideIconWhenLabelEmpty = iconStyle.autoHideWhenLabelEmpty;
  95. AdjustIconPos();
  96. if (iconStyle.layer == IconStyle.Layer.UnderLabel)
  97. m_IconRect.SetSiblingIndex(0);
  98. else
  99. m_IconRect.SetSiblingIndex(m_GameObject.transform.childCount - 1);
  100. }
  101. }
  102. public float GetLabelWidth()
  103. {
  104. if (m_LabelRect) return m_LabelRect.sizeDelta.x;
  105. else return 0;
  106. }
  107. public float GetLabelHeight()
  108. {
  109. if (m_LabelRect) return m_LabelRect.sizeDelta.y;
  110. return 0;
  111. }
  112. public void SetLabelColor(Color color)
  113. {
  114. if (m_LabelText != null) m_LabelText.SetColor(color);
  115. }
  116. public void SetLabelRotate(float rotate)
  117. {
  118. if (m_LabelText != null) m_LabelText.SetLocalEulerAngles(new Vector3(0, 0, rotate));
  119. }
  120. public void SetPosition(Vector3 position)
  121. {
  122. if (m_GameObject != null)
  123. {
  124. m_GameObject.transform.localPosition = position;
  125. }
  126. }
  127. public void SetLabelPosition(Vector3 position)
  128. {
  129. if (m_LabelRect) m_LabelRect.localPosition = position;
  130. }
  131. public void SetActive(bool flag)
  132. {
  133. if (m_GameObject) ChartHelper.SetActive(m_GameObject, flag);
  134. }
  135. public void SetLabelActive(bool flag)
  136. {
  137. if (m_LabelText != null) m_LabelText.SetActive(flag);
  138. }
  139. public void SetIconActive(bool flag)
  140. {
  141. isIconActive = flag;
  142. if (m_IconImage) ChartHelper.SetActive(m_IconImage, flag);
  143. }
  144. public bool SetText(string text)
  145. {
  146. if (m_LabelRect == null) return false;
  147. if (m_LabelText != null && !m_LabelText.GetText().Equals(text))
  148. {
  149. m_LabelText.SetText(text);
  150. if (m_LabelAutoSize)
  151. {
  152. var newSize = string.IsNullOrEmpty(text) ? Vector2.zero :
  153. new Vector2(m_LabelText.GetPreferredWidth() + m_LabelPaddingLeftRight * 2,
  154. m_LabelText.GetPreferredHeight() + m_LabelPaddingTopBottom * 2);
  155. var sizeChange = newSize.x != m_LabelRect.sizeDelta.x || newSize.y != m_LabelRect.sizeDelta.y;
  156. if (sizeChange)
  157. {
  158. m_LabelRect.sizeDelta = newSize;
  159. AdjustIconPos();
  160. }
  161. return sizeChange;
  162. }
  163. AdjustIconPos();
  164. if (m_AutoHideIconWhenLabelEmpty && isIconActive)
  165. {
  166. ChartHelper.SetActive(m_IconImage.gameObject, !string.IsNullOrEmpty(text));
  167. }
  168. }
  169. return false;
  170. }
  171. private void AdjustIconPos()
  172. {
  173. if (m_IconImage && m_IconRect)
  174. {
  175. var iconX = 0f;
  176. switch (m_Align)
  177. {
  178. case Align.Left:
  179. switch (m_LabelText.alignment)
  180. {
  181. case TextAnchor.LowerLeft:
  182. case TextAnchor.UpperLeft:
  183. case TextAnchor.MiddleLeft:
  184. iconX = -m_ObjectRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2;
  185. break;
  186. case TextAnchor.LowerRight:
  187. case TextAnchor.UpperRight:
  188. case TextAnchor.MiddleRight:
  189. iconX = m_ObjectRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2;
  190. break;
  191. case TextAnchor.LowerCenter:
  192. case TextAnchor.UpperCenter:
  193. case TextAnchor.MiddleCenter:
  194. iconX = -m_LabelText.GetPreferredWidth() / 2 - m_IconRect.sizeDelta.x / 2;
  195. break;
  196. }
  197. break;
  198. case Align.Right:
  199. switch (m_LabelText.alignment)
  200. {
  201. case TextAnchor.LowerLeft:
  202. case TextAnchor.UpperLeft:
  203. case TextAnchor.MiddleLeft:
  204. iconX = m_ObjectRect.sizeDelta.x / 2 + m_IconRect.sizeDelta.x / 2;
  205. break;
  206. case TextAnchor.LowerRight:
  207. case TextAnchor.UpperRight:
  208. case TextAnchor.MiddleRight:
  209. iconX = m_IconRect.sizeDelta.x / 2;
  210. break;
  211. case TextAnchor.LowerCenter:
  212. case TextAnchor.UpperCenter:
  213. case TextAnchor.MiddleCenter:
  214. iconX = m_LabelText.GetPreferredWidth() / 2 + m_IconRect.sizeDelta.x / 2;
  215. break;
  216. }
  217. break;
  218. }
  219. m_IconRect.anchoredPosition = m_IconOffest + new Vector3(iconX, 0);
  220. }
  221. }
  222. }
  223. }