Няма описание
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 SetRotate(float rotate)
  154. {
  155. transform.localEulerAngles = new Vector3(0, 0, rotate);
  156. }
  157. public void SetTextRotate(float rotate)
  158. {
  159. if (m_LabelText != null) m_LabelText.SetLocalEulerAngles(new Vector3(0, 0, rotate));
  160. }
  161. public void SetPosition(Vector3 position)
  162. {
  163. transform.localPosition = position;
  164. }
  165. public void SetRectPosition(Vector3 position)
  166. {
  167. objectRect.anchoredPosition3D = position;
  168. }
  169. public Vector3 GetPosition()
  170. {
  171. return transform.localPosition;
  172. }
  173. public override bool IsActive()
  174. {
  175. return m_Active;
  176. }
  177. public void SetActive(bool flag)
  178. {
  179. m_Active = flag;
  180. ChartHelper.SetActive(gameObject, flag);
  181. }
  182. public void SetTextActive(bool flag)
  183. {
  184. if (m_LabelText != null) m_LabelText.SetActive(flag);
  185. }
  186. public void SetIconActive(bool flag)
  187. {
  188. isIconActive = flag;
  189. if (m_IconImage) ChartHelper.SetActive(m_IconImage, flag);
  190. }
  191. public bool SetText(string text)
  192. {
  193. if (m_TextRect == null || m_LabelText == null)
  194. return false;
  195. if (text == null)
  196. text = "";
  197. if (!m_LabelText.GetText().Equals(text))
  198. {
  199. m_LabelText.SetText(text);
  200. if (m_AutoSize)
  201. {
  202. var newSize = string.IsNullOrEmpty(text) ? Vector2.zero :
  203. new Vector2(m_LabelText.GetPreferredWidth(),
  204. m_LabelText.GetPreferredHeight());
  205. var sizeChange = newSize.x != m_TextRect.sizeDelta.x || newSize.y != m_TextRect.sizeDelta.y;
  206. this.m_Width = newSize.x;
  207. this.m_Height = newSize.y;
  208. if (sizeChange)
  209. {
  210. m_TextRect.sizeDelta = newSize;
  211. UpdateSize();
  212. UpdatePadding();
  213. AdjustIconPos();
  214. }
  215. return sizeChange;
  216. }
  217. AdjustIconPos();
  218. if (m_HideIconIfTextEmpty && isIconActive)
  219. {
  220. ChartHelper.SetActive(m_IconImage.gameObject, !string.IsNullOrEmpty(text));
  221. }
  222. }
  223. return false;
  224. }
  225. private void UpdateSize()
  226. {
  227. if (m_AutoSize)
  228. {
  229. var sizeDelta = m_TextRect.sizeDelta;
  230. m_Width = sizeDelta.x + m_PaddingLeft + m_PaddingRight;
  231. m_Height = sizeDelta.y + m_PaddingTop + m_PaddingBottom;
  232. objectRect.sizeDelta = new Vector2(m_Width, m_Height);
  233. }
  234. }
  235. private void UpdatePadding()
  236. {
  237. if (m_TextRect == null) return;
  238. switch (text.alignment)
  239. {
  240. case TextAnchor.LowerLeft:
  241. m_TextRect.anchoredPosition = new Vector2(m_PaddingLeft, m_PaddingBottom);
  242. break;
  243. case TextAnchor.UpperLeft:
  244. m_TextRect.anchoredPosition = new Vector2(m_PaddingLeft, -m_PaddingTop);
  245. break;
  246. case TextAnchor.MiddleLeft:
  247. m_TextRect.anchoredPosition = new Vector2(m_PaddingLeft, m_Height / 2 - m_PaddingTop - m_TextRect.sizeDelta.y / 2);
  248. break;
  249. case TextAnchor.LowerRight:
  250. m_TextRect.anchoredPosition = new Vector2(-m_PaddingRight, m_PaddingBottom);
  251. break;
  252. case TextAnchor.UpperRight:
  253. m_TextRect.anchoredPosition = new Vector2(-m_PaddingRight, -m_PaddingTop);
  254. break;
  255. case TextAnchor.MiddleRight:
  256. m_TextRect.anchoredPosition = new Vector2(-m_PaddingRight, m_Height / 2 - m_PaddingTop - m_TextRect.sizeDelta.y / 2);
  257. break;
  258. case TextAnchor.LowerCenter:
  259. m_TextRect.anchoredPosition = new Vector2(-(m_Width / 2 - m_PaddingLeft - m_TextRect.sizeDelta.x / 2), m_PaddingBottom);
  260. break;
  261. case TextAnchor.UpperCenter:
  262. m_TextRect.anchoredPosition = new Vector2(-(m_Width / 2 - m_PaddingLeft - m_TextRect.sizeDelta.x / 2), -m_PaddingTop);
  263. break;
  264. case TextAnchor.MiddleCenter:
  265. 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);
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. private void AdjustIconPos()
  272. {
  273. if (m_IconImage && m_IconRect && m_LabelText != null && m_TextRect != null)
  274. {
  275. var iconX = 0f;
  276. switch (m_Align)
  277. {
  278. case Align.Left:
  279. switch (m_LabelText.alignment)
  280. {
  281. case TextAnchor.LowerLeft:
  282. case TextAnchor.UpperLeft:
  283. case TextAnchor.MiddleLeft:
  284. iconX = -m_TextRect.sizeDelta.x / 2 - m_IconRect.sizeDelta.x / 2;
  285. break;
  286. case TextAnchor.LowerRight:
  287. case TextAnchor.UpperRight:
  288. case TextAnchor.MiddleRight:
  289. iconX = m_TextRect.sizeDelta.x / 2 - m_LabelText.GetPreferredWidth() - m_IconRect.sizeDelta.x / 2;
  290. break;
  291. case TextAnchor.LowerCenter:
  292. case TextAnchor.UpperCenter:
  293. case TextAnchor.MiddleCenter:
  294. iconX = -m_LabelText.GetPreferredWidth() / 2 - m_IconRect.sizeDelta.x / 2;
  295. break;
  296. }
  297. break;
  298. case Align.Right:
  299. switch (m_LabelText.alignment)
  300. {
  301. case TextAnchor.LowerLeft:
  302. case TextAnchor.UpperLeft:
  303. case TextAnchor.MiddleLeft:
  304. iconX = m_TextRect.sizeDelta.x / 2 + m_IconRect.sizeDelta.x / 2;
  305. break;
  306. case TextAnchor.LowerRight:
  307. case TextAnchor.UpperRight:
  308. case TextAnchor.MiddleRight:
  309. iconX = m_IconRect.sizeDelta.x / 2;
  310. break;
  311. case TextAnchor.LowerCenter:
  312. case TextAnchor.UpperCenter:
  313. case TextAnchor.MiddleCenter:
  314. iconX = m_LabelText.GetPreferredWidth() / 2 + m_IconRect.sizeDelta.x / 2;
  315. break;
  316. }
  317. break;
  318. }
  319. m_IconRect.anchoredPosition = m_IconOffest + new Vector3(iconX, 0);
  320. }
  321. }
  322. }
  323. }