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.

BaseGraph.cs 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. protected override void OnValidate()
  117. {
  118. m_IsOnValidate = true;
  119. }
  120. #endif
  121. protected override void OnDestroy()
  122. {
  123. for (int i = transform.childCount - 1; i >= 0; i--)
  124. {
  125. DestroyImmediate(transform.GetChild(i).gameObject);
  126. }
  127. }
  128. protected override void OnPopulateMesh(VertexHelper vh)
  129. {
  130. vh.Clear();
  131. }
  132. protected virtual void InitPainter()
  133. {
  134. m_Painter = ChartHelper.AddPainterObject("painter_b", transform, m_GraphMinAnchor,
  135. m_GraphMaxAnchor, m_GraphPivot, new Vector2(m_GraphWidth, m_GraphHeight), chartHideFlags, 1);
  136. m_Painter.type = Painter.Type.Base;
  137. m_Painter.onPopulateMesh = OnDrawPainterBase;
  138. m_Painter.transform.SetSiblingIndex(0);
  139. }
  140. private void CheckSize()
  141. {
  142. var currWidth = rectTransform.rect.width;
  143. var currHeight = rectTransform.rect.height;
  144. if (m_GraphWidth == 0 && m_GraphHeight == 0 && (currWidth != 0 || currHeight != 0))
  145. {
  146. Awake();
  147. }
  148. if (m_GraphWidth != currWidth ||
  149. m_GraphHeight != currHeight ||
  150. m_GraphMinAnchor != rectTransform.anchorMin ||
  151. m_GraphMaxAnchor != rectTransform.anchorMax ||
  152. m_GraphAnchoredPosition != rectTransform.anchoredPosition)
  153. {
  154. UpdateSize();
  155. }
  156. if (!ChartHelper.IsValueEqualsVector3(m_LastLocalPosition, transform.localPosition))
  157. {
  158. m_LastLocalPosition = transform.localPosition;
  159. OnLocalPositionChanged();
  160. }
  161. }
  162. protected void UpdateSize()
  163. {
  164. m_GraphWidth = rectTransform.rect.width;
  165. m_GraphHeight = rectTransform.rect.height;
  166. m_GraphMaxAnchor = rectTransform.anchorMax;
  167. m_GraphMinAnchor = rectTransform.anchorMin;
  168. m_GraphSizeDelta = rectTransform.sizeDelta;
  169. m_GraphAnchoredPosition = rectTransform.anchoredPosition;
  170. rectTransform.pivot = LayerHelper.ResetChartPositionAndPivot(m_GraphMinAnchor, m_GraphMaxAnchor,
  171. m_GraphWidth, m_GraphHeight, ref m_GraphX, ref m_GraphY);
  172. m_GraphPivot = rectTransform.pivot;
  173. m_GraphRect.x = m_GraphX;
  174. m_GraphRect.y = m_GraphY;
  175. m_GraphRect.width = m_GraphWidth;
  176. m_GraphRect.height = m_GraphHeight;
  177. m_GraphPosition.x = m_GraphX;
  178. m_GraphPosition.y = m_GraphY;
  179. OnSizeChanged();
  180. }
  181. private void CheckPointerPos()
  182. {
  183. if (!isPointerInChart) return;
  184. if (canvas == null) return;
  185. Vector2 mousePos = m_PointerEventData.position;
  186. Vector2 local;
  187. if (!ScreenPointToChartPoint(mousePos, out local))
  188. {
  189. pointerPos = Vector2.zero;
  190. }
  191. else
  192. {
  193. pointerPos = local;
  194. }
  195. }
  196. protected virtual void CheckIsInScrollRect()
  197. {
  198. m_ScrollRect = GetComponentInParent<ScrollRect>();
  199. }
  200. protected virtual void CheckRefreshChart()
  201. {
  202. if (m_RefreshChart && m_Painter != null)
  203. {
  204. m_Painter.Refresh();
  205. m_RefreshChart = false;
  206. }
  207. }
  208. protected virtual void CheckRefreshPainter()
  209. {
  210. if (m_Painter == null) return;
  211. m_Painter.CheckRefresh();
  212. }
  213. internal virtual void RefreshPainter(Painter painter)
  214. {
  215. if (painter == null) return;
  216. painter.Refresh();
  217. }
  218. protected virtual void OnSizeChanged()
  219. {
  220. m_RefreshChart = true;
  221. }
  222. protected virtual void OnLocalPositionChanged() { }
  223. protected virtual void OnDrawPainterBase(VertexHelper vh, Painter painter)
  224. {
  225. DrawPainterBase(vh);
  226. }
  227. protected virtual void DrawPainterBase(VertexHelper vh) { }
  228. public virtual void OnPointerClick(PointerEventData eventData)
  229. {
  230. if (m_OnPointerClick != null) m_OnPointerClick(eventData, this);
  231. }
  232. public virtual void OnPointerDown(PointerEventData eventData)
  233. {
  234. if (m_OnPointerDown != null) m_OnPointerDown(eventData, this);
  235. }
  236. public virtual void OnPointerUp(PointerEventData eventData)
  237. {
  238. if (m_OnPointerUp != null) m_OnPointerUp(eventData, this);
  239. }
  240. public virtual void OnPointerEnter(PointerEventData eventData)
  241. {
  242. m_PointerEventData = eventData;
  243. if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this);
  244. }
  245. public virtual void OnPointerExit(PointerEventData eventData)
  246. {
  247. m_PointerEventData = null;
  248. if (m_OnPointerExit != null) m_OnPointerExit(eventData, this);
  249. }
  250. public virtual void OnBeginDrag(PointerEventData eventData)
  251. {
  252. if (m_ScrollRect != null) m_ScrollRect.OnBeginDrag(eventData);
  253. if (m_OnBeginDrag != null) m_OnBeginDrag(eventData, this);
  254. }
  255. public virtual void OnEndDrag(PointerEventData eventData)
  256. {
  257. if (m_ScrollRect != null) m_ScrollRect.OnEndDrag(eventData);
  258. if (m_OnEndDrag != null) m_OnEndDrag(eventData, this);
  259. }
  260. public virtual void OnDrag(PointerEventData eventData)
  261. {
  262. if (m_ScrollRect != null) m_ScrollRect.OnDrag(eventData);
  263. if (m_OnDrag != null) m_OnDrag(eventData, this);
  264. }
  265. public virtual void OnScroll(PointerEventData eventData)
  266. {
  267. if (m_ScrollRect != null) m_ScrollRect.OnScroll(eventData);
  268. if (m_OnScroll != null) m_OnScroll(eventData, this);
  269. }
  270. }
  271. }