暫無描述
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.

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