LegendItem.cs 6.7 KB

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