LegendItem.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /************************************************/
  2. /* */
  3. /* Copyright (c) 2018 - 2021 monitor1394 */
  4. /* https://github.com/monitor1394 */
  5. /* */
  6. /************************************************/
  7. using System;
  8. using UnityEngine;
  9. using UnityEngine.UI;
  10. namespace XCharts
  11. {
  12. public class LegendItem
  13. {
  14. private int m_Index;
  15. private string m_Name;
  16. private string m_LegendName;
  17. private GameObject m_GameObject;
  18. private Button m_Button;
  19. private Image m_Icon;
  20. private ChartText m_Text;
  21. private Image m_TextBackground;
  22. private RectTransform m_Rect;
  23. private RectTransform m_IconRect;
  24. private RectTransform m_TextRect;
  25. private RectTransform m_TextBackgroundRect;
  26. private float m_Gap = 0f;
  27. private float m_LabelPaddingLeftRight = 0f;
  28. private float m_LabelPaddingTopBottom = 0f;
  29. private bool m_LabelAutoSize = true;
  30. public int index { get { return m_Index; } set { m_Index = value; } }
  31. public string name { get { return m_Name; } set { m_Name = value; } }
  32. public string legendName { get { return m_LegendName; } set { m_LegendName = value; } }
  33. public GameObject gameObject { get { return m_GameObject; } }
  34. public Button button { get { return m_Button; } }
  35. public float width
  36. {
  37. get
  38. {
  39. if (m_IconRect && m_TextBackgroundRect)
  40. {
  41. return m_IconRect.sizeDelta.x + m_Gap + m_TextBackgroundRect.sizeDelta.x;
  42. }
  43. else
  44. {
  45. return 0;
  46. }
  47. }
  48. }
  49. public float height
  50. {
  51. get
  52. {
  53. if (m_IconRect && m_TextBackgroundRect)
  54. {
  55. return Mathf.Max(m_IconRect.sizeDelta.y, m_TextBackgroundRect.sizeDelta.y);
  56. }
  57. else
  58. {
  59. return 0;
  60. }
  61. }
  62. }
  63. public void SetObject(GameObject obj)
  64. {
  65. m_GameObject = obj;
  66. m_Button = obj.GetComponent<Button>();
  67. m_Rect = obj.GetComponent<RectTransform>();
  68. m_Icon = obj.transform.Find("icon").gameObject.GetComponent<Image>();
  69. m_TextBackground = obj.transform.Find("content").gameObject.GetComponent<Image>();
  70. m_Text = new ChartText(obj);
  71. m_IconRect = m_Icon.gameObject.GetComponent<RectTransform>();
  72. m_TextRect = m_Text.gameObject.GetComponent<RectTransform>();
  73. m_TextBackgroundRect = m_TextBackground.gameObject.GetComponent<RectTransform>();
  74. }
  75. public void SetButton(Button button)
  76. {
  77. m_Button = button;
  78. }
  79. public void SetIcon(Image icon)
  80. {
  81. m_Icon = icon;
  82. }
  83. public void SetText(ChartText text)
  84. {
  85. m_Text = text;
  86. }
  87. public void SetTextBackground(Image image)
  88. {
  89. m_TextBackground = image;
  90. }
  91. public void SetIconSize(float width, float height)
  92. {
  93. if (m_IconRect)
  94. {
  95. m_IconRect.sizeDelta = new Vector2(width, height);
  96. }
  97. }
  98. public Rect GetIconRect()
  99. {
  100. if (m_GameObject && m_IconRect)
  101. {
  102. var pos = m_GameObject.transform.localPosition;
  103. var sizeDelta = m_IconRect.sizeDelta;
  104. var y = pos.y - (m_Rect.sizeDelta.y - sizeDelta.y) / 2 - sizeDelta.y;
  105. return new Rect(pos.x, y, m_IconRect.sizeDelta.x, m_IconRect.sizeDelta.y);
  106. }
  107. else
  108. {
  109. return Rect.zero;
  110. }
  111. }
  112. public Color GetIconColor()
  113. {
  114. if (m_Icon) return m_Icon.color;
  115. else return Color.clear;
  116. }
  117. public void SetIconColor(Color color)
  118. {
  119. if (m_Icon)
  120. {
  121. m_Icon.color = color;
  122. }
  123. }
  124. public void SetIconImage(Sprite image)
  125. {
  126. if (m_Icon)
  127. {
  128. m_Icon.sprite = image;
  129. }
  130. }
  131. public void SetIconActive(bool active)
  132. {
  133. if (m_Icon)
  134. {
  135. m_Icon.gameObject.SetActive(active);
  136. }
  137. }
  138. public void SetContentColor(Color color)
  139. {
  140. if (m_Text != null)
  141. {
  142. m_Text.SetColor(color);
  143. }
  144. }
  145. public void SetContentBackgroundColor(Color color)
  146. {
  147. if (m_TextBackground)
  148. {
  149. m_TextBackground.color = color;
  150. }
  151. }
  152. public void SetContentPosition(Vector3 offset)
  153. {
  154. m_Gap = offset.x;
  155. if (m_TextBackgroundRect)
  156. {
  157. var posX = m_IconRect.sizeDelta.x + offset.x;
  158. m_TextBackgroundRect.anchoredPosition3D = new Vector3(posX, offset.y, 0);
  159. }
  160. }
  161. public bool SetContent(string content)
  162. {
  163. if (m_Text != null && !m_Text.GetText().Equals(content))
  164. {
  165. m_Text.SetText(content);
  166. if (m_LabelAutoSize)
  167. {
  168. var newSize = string.IsNullOrEmpty(content) ? Vector2.zero :
  169. new Vector2(m_Text.GetPreferredWidth(), m_Text.GetPreferredHeight());
  170. var sizeChange = newSize.x != m_TextRect.sizeDelta.x || newSize.y != m_TextRect.sizeDelta.y;
  171. if (sizeChange)
  172. {
  173. m_TextRect.sizeDelta = newSize;
  174. m_TextRect.anchoredPosition3D = new Vector3(m_LabelPaddingLeftRight, 0);
  175. m_TextBackgroundRect.sizeDelta = new Vector2(m_Text.GetPreferredWidth() + m_LabelPaddingLeftRight * 2,
  176. m_Text.GetPreferredHeight() + m_LabelPaddingTopBottom * 2 - 4);
  177. m_Rect.sizeDelta = new Vector3(width, height);
  178. }
  179. return sizeChange;
  180. }
  181. }
  182. return false;
  183. }
  184. public void SetPosition(Vector3 position)
  185. {
  186. if (m_GameObject)
  187. {
  188. m_GameObject.transform.localPosition = position;
  189. }
  190. }
  191. public void SetActive(bool active)
  192. {
  193. if (m_GameObject)
  194. {
  195. m_GameObject.SetActive(active);
  196. }
  197. }
  198. }
  199. }