BaseGraph.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /******************************************/
  2. /* */
  3. /* Copyright (c) 2021 monitor1394 */
  4. /* https://github.com/monitor1394 */
  5. /* */
  6. /******************************************/
  7. using UnityEngine;
  8. using UnityEngine.UI;
  9. using System;
  10. using UnityEngine.EventSystems;
  11. namespace XCharts
  12. {
  13. [RequireComponent(typeof(CanvasRenderer))]
  14. public partial class BaseGraph : MaskableGraphic, IPointerDownHandler, IPointerUpHandler,
  15. IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IPointerClickHandler,
  16. IDragHandler, IEndDragHandler, IScrollHandler
  17. {
  18. protected static readonly string s_BackgroundObjectName = "background";
  19. [SerializeField] protected bool m_MultiComponentMode = false;
  20. [SerializeField] protected bool m_DebugMode = false;
  21. [SerializeField] protected bool m_EnableTextMeshPro = false;
  22. [SerializeField] protected Background m_Background = Background.defaultBackground;
  23. protected Painter m_Painter;
  24. protected int m_SiblingIndex;
  25. protected float m_GraphWidth;
  26. protected float m_GraphHeight;
  27. protected float m_GraphX;
  28. protected float m_GraphY;
  29. protected Vector3 m_GraphPosition = Vector3.zero;
  30. protected Vector2 m_GraphMinAnchor;
  31. protected Vector2 m_GraphMaxAnchor;
  32. protected Vector2 m_GraphPivot;
  33. protected Vector2 m_GraphSizeDelta;
  34. protected Vector2 m_GraphAnchoredPosition;
  35. protected Rect m_GraphRect = new Rect(0, 0, 0, 0);
  36. protected bool m_RefreshChart = false;
  37. protected bool m_ForceOpenRaycastTarget;
  38. protected bool m_IsControlledByLayout = false;
  39. protected bool m_PainerDirty = false;
  40. protected bool m_IsOnValidate = false;
  41. protected Vector3 m_LastLocalPosition;
  42. protected Action<PointerEventData, BaseGraph> m_OnPointerClick;
  43. protected Action<PointerEventData, BaseGraph> m_OnPointerDown;
  44. protected Action<PointerEventData, BaseGraph> m_OnPointerUp;
  45. protected Action<PointerEventData, BaseGraph> m_OnPointerEnter;
  46. protected Action<PointerEventData, BaseGraph> m_OnPointerExit;
  47. protected Action<PointerEventData, BaseGraph> m_OnBeginDrag;
  48. protected Action<PointerEventData, BaseGraph> m_OnDrag;
  49. protected Action<PointerEventData, BaseGraph> m_OnEndDrag;
  50. protected Action<PointerEventData, BaseGraph> m_OnScroll;
  51. protected Vector2 graphAnchorMax { get { return m_GraphMinAnchor; } }
  52. protected Vector2 graphAnchorMin { get { return m_GraphMaxAnchor; } }
  53. protected Vector2 graphPivot { get { return m_GraphPivot; } }
  54. public HideFlags chartHideFlags { get { return m_DebugMode ? HideFlags.None : HideFlags.HideInHierarchy; } }
  55. private ScrollRect m_ScrollRect;
  56. protected virtual void InitComponent()
  57. {
  58. InitPainter();
  59. InitBackground();
  60. }
  61. protected override void Awake()
  62. {
  63. CheckTextMeshPro();
  64. m_SiblingIndex = 0;
  65. if (transform.parent != null)
  66. {
  67. m_IsControlledByLayout = transform.parent.GetComponent<LayoutGroup>() != null;
  68. }
  69. raycastTarget = false;
  70. m_LastLocalPosition = transform.localPosition;
  71. UpdateSize();
  72. InitComponent();
  73. CheckIsInScrollRect();
  74. }
  75. protected override void Start()
  76. {
  77. m_RefreshChart = true;
  78. }
  79. protected virtual void Update()
  80. {
  81. CheckSize();
  82. if (m_IsOnValidate)
  83. {
  84. m_IsOnValidate = false;
  85. m_RefreshChart = true;
  86. CheckTextMeshPro();
  87. InitComponent();
  88. }
  89. else
  90. {
  91. CheckComponent();
  92. }
  93. CheckPointerPos();
  94. CheckRefreshChart();
  95. CheckRefreshPainter();
  96. }
  97. protected virtual void SetAllComponentDirty()
  98. {
  99. #if UNITY_EDITOR
  100. if (!Application.isPlaying)
  101. {
  102. m_IsOnValidate = true;
  103. Update();
  104. }
  105. #endif
  106. m_PainerDirty = true;
  107. m_Background.SetAllDirty();
  108. }
  109. protected virtual void CheckComponent()
  110. {
  111. CheckComponentDirty(m_Background);
  112. if (m_PainerDirty)
  113. {
  114. InitPainter();
  115. m_PainerDirty = false;
  116. }
  117. }
  118. private void CheckTextMeshPro()
  119. {
  120. #if dUI_TextMeshPro
  121. var enableTextMeshPro = true;
  122. #else
  123. var enableTextMeshPro = false;
  124. #endif
  125. if (m_EnableTextMeshPro != enableTextMeshPro)
  126. {
  127. m_EnableTextMeshPro = enableTextMeshPro;
  128. RemoveChartObject();
  129. }
  130. }
  131. protected void CheckComponentDirty(ChartComponent component)
  132. {
  133. if (component.anyDirty)
  134. {
  135. if (component.componentDirty && component.refreshComponent != null)
  136. {
  137. component.refreshComponent.Invoke();
  138. }
  139. if (component.vertsDirty)
  140. {
  141. if (component.painter != null)
  142. {
  143. RefreshPainter(component.painter);
  144. }
  145. }
  146. component.ClearDirty();
  147. }
  148. }
  149. #if UNITY_EDITOR
  150. protected override void Reset()
  151. {
  152. }
  153. protected override void OnValidate()
  154. {
  155. m_IsOnValidate = true;
  156. }
  157. #endif
  158. protected override void OnDestroy()
  159. {
  160. for (int i = transform.childCount - 1; i >= 0; i--)
  161. {
  162. DestroyImmediate(transform.GetChild(i).gameObject);
  163. }
  164. }
  165. protected override void OnPopulateMesh(VertexHelper vh)
  166. {
  167. vh.Clear();
  168. }
  169. private void InitBackground()
  170. {
  171. m_Background.painter = m_Painter;
  172. m_Background.refreshComponent = delegate ()
  173. {
  174. var backgroundObj = ChartHelper.AddObject(s_BackgroundObjectName, transform, m_GraphMinAnchor,
  175. m_GraphMaxAnchor, m_GraphPivot, m_GraphSizeDelta);
  176. m_Background.gameObject = backgroundObj;
  177. backgroundObj.hideFlags = chartHideFlags;
  178. var backgroundImage = ChartHelper.GetOrAddComponent<Image>(backgroundObj);
  179. ChartHelper.UpdateRectTransform(backgroundObj, m_GraphMinAnchor,
  180. m_GraphMaxAnchor, m_GraphPivot, m_GraphSizeDelta);
  181. backgroundImage.sprite = m_Background.image;
  182. backgroundImage.type = m_Background.imageType;
  183. backgroundImage.color = m_Background.imageColor;
  184. backgroundObj.transform.SetSiblingIndex(0);
  185. backgroundObj.SetActive(m_Background.show);
  186. };
  187. m_Background.refreshComponent();
  188. }
  189. protected virtual void InitPainter()
  190. {
  191. m_Painter = ChartHelper.AddPainterObject("painter_b", transform, m_GraphMinAnchor,
  192. m_GraphMaxAnchor, m_GraphPivot, new Vector2(m_GraphWidth, m_GraphHeight), chartHideFlags, 1);
  193. m_Painter.type = Painter.Type.Base;
  194. m_Painter.onPopulateMesh = OnDrawPainterBase;
  195. }
  196. private void CheckSize()
  197. {
  198. var currWidth = rectTransform.rect.width;
  199. var currHeight = rectTransform.rect.height;
  200. if (m_GraphWidth == 0 && m_GraphHeight == 0 && (currWidth != 0 || currHeight != 0))
  201. {
  202. Awake();
  203. }
  204. if (m_GraphWidth != currWidth
  205. || m_GraphHeight != currHeight
  206. || m_GraphMinAnchor != rectTransform.anchorMin
  207. || m_GraphMaxAnchor != rectTransform.anchorMax
  208. || m_GraphAnchoredPosition != rectTransform.anchoredPosition)
  209. {
  210. UpdateSize();
  211. }
  212. if (!ChartHelper.IsValueEqualsVector3(m_LastLocalPosition, transform.localPosition))
  213. {
  214. m_LastLocalPosition = transform.localPosition;
  215. OnLocalPositionChanged();
  216. }
  217. }
  218. protected void UpdateSize()
  219. {
  220. m_GraphWidth = rectTransform.rect.width;
  221. m_GraphHeight = rectTransform.rect.height;
  222. m_GraphMaxAnchor = rectTransform.anchorMax;
  223. m_GraphMinAnchor = rectTransform.anchorMin;
  224. m_GraphSizeDelta = rectTransform.sizeDelta;
  225. m_GraphAnchoredPosition = rectTransform.anchoredPosition;
  226. rectTransform.pivot = LayerHelper.ResetChartPositionAndPivot(m_GraphMinAnchor, m_GraphMaxAnchor,
  227. m_GraphWidth, m_GraphHeight, ref m_GraphX, ref m_GraphY);
  228. m_GraphPivot = rectTransform.pivot;
  229. m_GraphRect.x = m_GraphX;
  230. m_GraphRect.y = m_GraphY;
  231. m_GraphRect.width = m_GraphWidth;
  232. m_GraphRect.height = m_GraphHeight;
  233. m_GraphPosition.x = m_GraphX;
  234. m_GraphPosition.y = m_GraphY;
  235. OnSizeChanged();
  236. }
  237. private void CheckPointerPos()
  238. {
  239. if (m_ForceOpenRaycastTarget) raycastTarget = true;
  240. if (IsNeedCheckPointerPos())
  241. {
  242. raycastTarget = true;
  243. if (canvas == null) return;
  244. Vector2 local;
  245. if (!ScreenPointToChartPoint(Input.mousePosition, out local))
  246. {
  247. pointerPos = Vector2.zero;
  248. }
  249. else
  250. {
  251. pointerPos = local;
  252. }
  253. }
  254. else
  255. {
  256. raycastTarget = false;
  257. }
  258. }
  259. protected virtual void CheckIsInScrollRect()
  260. {
  261. m_ScrollRect = GetComponentInParent<ScrollRect>();
  262. }
  263. protected virtual bool IsNeedCheckPointerPos()
  264. {
  265. return raycastTarget;
  266. }
  267. protected virtual void CheckRefreshChart()
  268. {
  269. if (m_RefreshChart)
  270. {
  271. m_Painter.Refresh();
  272. m_RefreshChart = false;
  273. }
  274. }
  275. protected virtual void CheckRefreshPainter()
  276. {
  277. m_Painter.CheckRefresh();
  278. }
  279. internal virtual void RefreshPainter(Painter painter)
  280. {
  281. if (painter == null) return;
  282. painter.Refresh();
  283. }
  284. protected virtual void OnSizeChanged()
  285. {
  286. m_RefreshChart = true;
  287. }
  288. protected virtual void OnLocalPositionChanged()
  289. {
  290. }
  291. protected virtual void OnDrawPainterBase(VertexHelper vh, Painter painter)
  292. {
  293. DrawBackground(vh);
  294. DrawPainterBase(vh);
  295. }
  296. protected virtual void DrawPainterBase(VertexHelper vh)
  297. {
  298. }
  299. protected virtual void DrawBackground(VertexHelper vh)
  300. {
  301. }
  302. public virtual void OnPointerClick(PointerEventData eventData)
  303. {
  304. if (m_OnPointerClick != null) m_OnPointerClick(eventData, this);
  305. }
  306. public virtual void OnPointerDown(PointerEventData eventData)
  307. {
  308. if (m_OnPointerDown != null) m_OnPointerDown(eventData, this);
  309. }
  310. public virtual void OnPointerUp(PointerEventData eventData)
  311. {
  312. if (m_OnPointerUp != null) m_OnPointerUp(eventData, this);
  313. }
  314. public virtual void OnPointerEnter(PointerEventData eventData)
  315. {
  316. isPointerInChart = true;
  317. if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this);
  318. }
  319. public virtual void OnPointerExit(PointerEventData eventData)
  320. {
  321. isPointerInChart = false;
  322. if (m_OnPointerExit != null) m_OnPointerExit(eventData, this);
  323. }
  324. public virtual void OnBeginDrag(PointerEventData eventData)
  325. {
  326. if (m_ScrollRect != null) m_ScrollRect.OnBeginDrag(eventData);
  327. if (m_OnBeginDrag != null) m_OnBeginDrag(eventData, this);
  328. }
  329. public virtual void OnEndDrag(PointerEventData eventData)
  330. {
  331. if (m_ScrollRect != null) m_ScrollRect.OnEndDrag(eventData);
  332. if (m_OnEndDrag != null) m_OnEndDrag(eventData, this);
  333. }
  334. public virtual void OnDrag(PointerEventData eventData)
  335. {
  336. if (m_ScrollRect != null) m_ScrollRect.OnDrag(eventData);
  337. if (m_OnDrag != null) m_OnDrag(eventData, this);
  338. }
  339. public virtual void OnScroll(PointerEventData eventData)
  340. {
  341. if (m_ScrollRect != null) m_ScrollRect.OnScroll(eventData);
  342. if (m_OnScroll != null) m_OnScroll(eventData, this);
  343. }
  344. }
  345. }