Sin descripción
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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.EventSystems;
  4. using UnityEngine.UI;
  5. #if INPUT_SYSTEM_ENABLED
  6. using Input = XCharts.Runtime.InputHelper;
  7. #endif
  8. namespace XCharts.Runtime
  9. {
  10. [RequireComponent(typeof(CanvasRenderer))]
  11. public partial class BaseGraph : MaskableGraphic, IPointerDownHandler, IPointerUpHandler,
  12. IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IPointerClickHandler,
  13. IDragHandler, IEndDragHandler, IScrollHandler
  14. {
  15. [SerializeField] protected bool m_EnableTextMeshPro = false;
  16. protected Painter m_Painter;
  17. protected int m_SiblingIndex;
  18. protected float m_GraphWidth;
  19. protected float m_GraphHeight;
  20. protected float m_GraphX;
  21. protected float m_GraphY;
  22. protected Vector3 m_GraphPosition = Vector3.zero;
  23. protected Vector2 m_GraphMinAnchor;
  24. protected Vector2 m_GraphMaxAnchor;
  25. protected Vector2 m_GraphPivot;
  26. protected Vector2 m_GraphSizeDelta;
  27. protected Vector2 m_GraphAnchoredPosition;
  28. protected Rect m_GraphRect = new Rect(0, 0, 0, 0);
  29. protected bool m_RefreshChart = false;
  30. protected bool m_ForceOpenRaycastTarget;
  31. protected bool m_IsControlledByLayout = false;
  32. protected bool m_PainerDirty = false;
  33. protected bool m_IsOnValidate = false;
  34. protected Vector3 m_LastLocalPosition;
  35. protected Action<PointerEventData, BaseGraph> m_OnPointerClick;
  36. protected Action<PointerEventData, BaseGraph> m_OnPointerDown;
  37. protected Action<PointerEventData, BaseGraph> m_OnPointerUp;
  38. protected Action<PointerEventData, BaseGraph> m_OnPointerEnter;
  39. protected Action<PointerEventData, BaseGraph> m_OnPointerExit;
  40. protected Action<PointerEventData, BaseGraph> m_OnBeginDrag;
  41. protected Action<PointerEventData, BaseGraph> m_OnDrag;
  42. protected Action<PointerEventData, BaseGraph> m_OnEndDrag;
  43. protected Action<PointerEventData, BaseGraph> m_OnScroll;
  44. public virtual HideFlags chartHideFlags { get { return HideFlags.None; } }
  45. private ScrollRect m_ScrollRect;
  46. public Painter painter { get { return m_Painter; } }
  47. protected virtual void InitComponent()
  48. {
  49. InitPainter();
  50. }
  51. protected override void Awake()
  52. {
  53. CheckTextMeshPro();
  54. m_SiblingIndex = 0;
  55. m_LastLocalPosition = transform.localPosition;
  56. UpdateSize();
  57. InitComponent();
  58. CheckIsInScrollRect();
  59. }
  60. protected override void Start()
  61. {
  62. m_RefreshChart = true;
  63. }
  64. protected virtual void Update()
  65. {
  66. CheckSize();
  67. if (m_IsOnValidate)
  68. {
  69. m_IsOnValidate = false;
  70. CheckTextMeshPro();
  71. InitComponent();
  72. RefreshGraph();
  73. }
  74. else
  75. {
  76. CheckComponent();
  77. }
  78. CheckPointerPos();
  79. CheckRefreshChart();
  80. CheckRefreshPainter();
  81. }
  82. protected virtual void SetAllComponentDirty()
  83. {
  84. #if UNITY_EDITOR
  85. if (!Application.isPlaying)
  86. {
  87. m_IsOnValidate = true;
  88. }
  89. #endif
  90. m_PainerDirty = true;
  91. }
  92. protected virtual void CheckComponent()
  93. {
  94. if (m_PainerDirty)
  95. {
  96. InitPainter();
  97. m_PainerDirty = false;
  98. }
  99. }
  100. private void CheckTextMeshPro()
  101. {
  102. #if dUI_TextMeshPro
  103. var enableTextMeshPro = true;
  104. #else
  105. var enableTextMeshPro = false;
  106. #endif
  107. if (m_EnableTextMeshPro != enableTextMeshPro)
  108. {
  109. m_EnableTextMeshPro = enableTextMeshPro;
  110. RebuildChartObject();
  111. }
  112. }
  113. #if UNITY_EDITOR
  114. protected override void Reset() { }
  115. protected override void OnValidate()
  116. {
  117. m_IsOnValidate = true;
  118. }
  119. #endif
  120. protected override void OnDestroy()
  121. {
  122. for (int i = transform.childCount - 1; i >= 0; i--)
  123. {
  124. DestroyImmediate(transform.GetChild(i).gameObject);
  125. }
  126. }
  127. protected override void OnPopulateMesh(VertexHelper vh)
  128. {
  129. vh.Clear();
  130. }
  131. protected virtual void InitPainter()
  132. {
  133. m_Painter = ChartHelper.AddPainterObject("painter_b", transform, m_GraphMinAnchor,
  134. m_GraphMaxAnchor, m_GraphPivot, new Vector2(m_GraphWidth, m_GraphHeight), chartHideFlags, 1);
  135. m_Painter.type = Painter.Type.Base;
  136. m_Painter.onPopulateMesh = OnDrawPainterBase;
  137. m_Painter.transform.SetSiblingIndex(0);
  138. }
  139. private void CheckSize()
  140. {
  141. var currWidth = rectTransform.rect.width;
  142. var currHeight = rectTransform.rect.height;
  143. if (m_GraphWidth == 0 && m_GraphHeight == 0 && (currWidth != 0 || currHeight != 0))
  144. {
  145. Awake();
  146. }
  147. if (m_GraphWidth != currWidth ||
  148. m_GraphHeight != currHeight ||
  149. m_GraphMinAnchor != rectTransform.anchorMin ||
  150. m_GraphMaxAnchor != rectTransform.anchorMax ||
  151. m_GraphAnchoredPosition != rectTransform.anchoredPosition)
  152. {
  153. UpdateSize();
  154. }
  155. if (!ChartHelper.IsValueEqualsVector3(m_LastLocalPosition, transform.localPosition))
  156. {
  157. m_LastLocalPosition = transform.localPosition;
  158. OnLocalPositionChanged();
  159. }
  160. }
  161. protected void UpdateSize()
  162. {
  163. m_GraphWidth = rectTransform.rect.width;
  164. m_GraphHeight = rectTransform.rect.height;
  165. m_GraphMaxAnchor = rectTransform.anchorMax;
  166. m_GraphMinAnchor = rectTransform.anchorMin;
  167. m_GraphSizeDelta = rectTransform.sizeDelta;
  168. m_GraphAnchoredPosition = rectTransform.anchoredPosition;
  169. rectTransform.pivot = LayerHelper.ResetChartPositionAndPivot(m_GraphMinAnchor, m_GraphMaxAnchor,
  170. m_GraphWidth, m_GraphHeight, ref m_GraphX, ref m_GraphY);
  171. m_GraphPivot = rectTransform.pivot;
  172. m_GraphRect.x = m_GraphX;
  173. m_GraphRect.y = m_GraphY;
  174. m_GraphRect.width = m_GraphWidth;
  175. m_GraphRect.height = m_GraphHeight;
  176. m_GraphPosition.x = m_GraphX;
  177. m_GraphPosition.y = m_GraphY;
  178. OnSizeChanged();
  179. }
  180. private void CheckPointerPos()
  181. {
  182. if (!isPointerInChart) return;
  183. if (canvas == null) return;
  184. Vector2 mousePos = Input.mousePosition;
  185. Vector2 local;
  186. if (!ScreenPointToChartPoint(mousePos, out local))
  187. {
  188. pointerPos = Vector2.zero;
  189. }
  190. else
  191. {
  192. pointerPos = local;
  193. }
  194. }
  195. protected virtual void CheckIsInScrollRect()
  196. {
  197. m_ScrollRect = GetComponentInParent<ScrollRect>();
  198. }
  199. protected virtual void CheckRefreshChart()
  200. {
  201. if (m_RefreshChart)
  202. {
  203. m_Painter.Refresh();
  204. m_RefreshChart = false;
  205. }
  206. }
  207. protected virtual void CheckRefreshPainter()
  208. {
  209. m_Painter.CheckRefresh();
  210. }
  211. internal virtual void RefreshPainter(Painter painter)
  212. {
  213. if (painter == null) return;
  214. painter.Refresh();
  215. }
  216. protected virtual void OnSizeChanged()
  217. {
  218. m_RefreshChart = true;
  219. }
  220. protected virtual void OnLocalPositionChanged() { }
  221. protected virtual void OnDrawPainterBase(VertexHelper vh, Painter painter)
  222. {
  223. DrawPainterBase(vh);
  224. }
  225. protected virtual void DrawPainterBase(VertexHelper vh) { }
  226. public virtual void OnPointerClick(PointerEventData eventData)
  227. {
  228. if (m_OnPointerClick != null) m_OnPointerClick(eventData, this);
  229. }
  230. public virtual void OnPointerDown(PointerEventData eventData)
  231. {
  232. if (m_OnPointerDown != null) m_OnPointerDown(eventData, this);
  233. }
  234. public virtual void OnPointerUp(PointerEventData eventData)
  235. {
  236. if (m_OnPointerUp != null) m_OnPointerUp(eventData, this);
  237. }
  238. public virtual void OnPointerEnter(PointerEventData eventData)
  239. {
  240. isPointerInChart = true;
  241. if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this);
  242. }
  243. public virtual void OnPointerExit(PointerEventData eventData)
  244. {
  245. isPointerInChart = false;
  246. if (m_OnPointerExit != null) m_OnPointerExit(eventData, this);
  247. }
  248. public virtual void OnBeginDrag(PointerEventData eventData)
  249. {
  250. if (m_ScrollRect != null) m_ScrollRect.OnBeginDrag(eventData);
  251. if (m_OnBeginDrag != null) m_OnBeginDrag(eventData, this);
  252. }
  253. public virtual void OnEndDrag(PointerEventData eventData)
  254. {
  255. if (m_ScrollRect != null) m_ScrollRect.OnEndDrag(eventData);
  256. if (m_OnEndDrag != null) m_OnEndDrag(eventData, this);
  257. }
  258. public virtual void OnDrag(PointerEventData eventData)
  259. {
  260. if (m_ScrollRect != null) m_ScrollRect.OnDrag(eventData);
  261. if (m_OnDrag != null) m_OnDrag(eventData, this);
  262. }
  263. public virtual void OnScroll(PointerEventData eventData)
  264. {
  265. if (m_ScrollRect != null) m_ScrollRect.OnScroll(eventData);
  266. if (m_OnScroll != null) m_OnScroll(eventData, this);
  267. }
  268. }
  269. }