No Description
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.

LegendItem.cs 6.6KB

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