BaseGraph.cs 9.9 KB

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