11 using UnityEngine.EventSystems;
15 [RequireComponent(typeof(CanvasRenderer))]
16 public partial class BaseGraph : MaskableGraphic, IPointerDownHandler, IPointerUpHandler,
17 IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IPointerClickHandler,
18 IDragHandler, IEndDragHandler, IScrollHandler
20 protected static readonly
string s_BackgroundObjectName =
"background";
21 [SerializeField]
protected bool m_MultiComponentMode =
false;
22 [SerializeField]
protected bool m_DebugMode =
false;
23 [SerializeField]
protected bool m_EnableTextMeshPro =
false;
24 [SerializeField]
protected Background m_Background = Background.defaultBackground;
25 protected Painter m_Painter;
26 protected int m_SiblingIndex;
28 protected float m_GraphWidth;
29 protected float m_GraphHeight;
30 protected float m_GraphX;
31 protected float m_GraphY;
32 protected Vector3 m_GraphPosition = Vector3.zero;
33 protected Vector2 m_GraphMinAnchor;
34 protected Vector2 m_GraphMaxAnchor;
35 protected Vector2 m_GraphPivot;
36 protected Vector2 m_GraphSizeDelta;
37 protected Vector2 m_GraphAnchoredPosition;
38 protected Rect m_GraphRect =
new Rect(0, 0, 0, 0);
39 protected bool m_RefreshChart =
false;
40 protected bool m_ForceOpenRaycastTarget;
41 protected bool m_IsControlledByLayout =
false;
42 protected bool m_PainerDirty =
false;
43 protected bool m_IsOnValidate =
false;
44 protected Vector3 m_LastLocalPosition;
46 protected Action<PointerEventData, BaseGraph> m_OnPointerClick;
47 protected Action<PointerEventData, BaseGraph> m_OnPointerDown;
48 protected Action<PointerEventData, BaseGraph> m_OnPointerUp;
49 protected Action<PointerEventData, BaseGraph> m_OnPointerEnter;
50 protected Action<PointerEventData, BaseGraph> m_OnPointerExit;
51 protected Action<PointerEventData, BaseGraph> m_OnBeginDrag;
52 protected Action<PointerEventData, BaseGraph> m_OnDrag;
53 protected Action<PointerEventData, BaseGraph> m_OnEndDrag;
54 protected Action<PointerEventData, BaseGraph> m_OnScroll;
56 protected Vector2 graphAnchorMax {
get {
return m_GraphMinAnchor; } }
57 protected Vector2 graphAnchorMin {
get {
return m_GraphMaxAnchor; } }
58 protected Vector2 graphPivot {
get {
return m_GraphPivot; } }
59 public HideFlags chartHideFlags {
get {
return m_DebugMode ? HideFlags.None : HideFlags.HideInHierarchy; } }
61 private ScrollRect m_ScrollRect;
64 protected virtual void InitComponent()
70 protected override void Awake()
74 if (transform.parent !=
null)
76 m_IsControlledByLayout = transform.parent.GetComponent<LayoutGroup>() !=
null;
78 raycastTarget =
false;
79 m_LastLocalPosition = transform.localPosition;
82 CheckIsInScrollRect();
85 protected override void Start()
87 m_RefreshChart =
true;
90 protected virtual void Update()
95 m_IsOnValidate =
false;
96 m_RefreshChart =
true;
106 CheckRefreshPainter();
109 protected virtual void SetAllComponentDirty()
112 if (!Application.isPlaying)
114 m_IsOnValidate =
true;
118 m_PainerDirty =
true;
119 m_Background.SetAllDirty();
122 protected virtual void CheckComponent()
124 CheckComponentDirty(m_Background);
128 m_PainerDirty =
false;
134 private void CheckTextMeshPro()
137 var enableTextMeshPro =
true;
139 var enableTextMeshPro =
false;
141 if (m_EnableTextMeshPro != enableTextMeshPro)
143 m_EnableTextMeshPro = enableTextMeshPro;
148 protected void CheckComponentDirty(ChartComponent component)
150 if (component.anyDirty)
152 if (component.componentDirty && component.refreshComponent !=
null)
154 component.refreshComponent.Invoke();
156 if (component.vertsDirty)
158 if (component.painter !=
null)
160 RefreshPainter(component.painter);
163 component.ClearDirty();
168 protected override void Reset()
172 protected override void OnValidate()
174 m_IsOnValidate =
true;
178 protected override void OnDestroy()
180 for (
int i = transform.childCount - 1; i >= 0; i--)
182 DestroyImmediate(transform.GetChild(i).gameObject);
186 protected override void OnPopulateMesh(VertexHelper vh)
191 private void InitBackground()
193 m_Background.painter = m_Painter;
194 m_Background.refreshComponent = delegate ()
196 var backgroundObj = ChartHelper.AddObject(s_BackgroundObjectName, transform, m_GraphMinAnchor,
197 m_GraphMaxAnchor, m_GraphPivot, m_GraphSizeDelta);
198 m_Background.gameObject = backgroundObj;
199 backgroundObj.hideFlags = chartHideFlags;
200 var backgroundImage = ChartHelper.GetOrAddComponent<Image>(backgroundObj);
201 ChartHelper.UpdateRectTransform(backgroundObj, m_GraphMinAnchor,
202 m_GraphMaxAnchor, m_GraphPivot, m_GraphSizeDelta);
203 backgroundImage.sprite = m_Background.image;
204 backgroundImage.type = m_Background.imageType;
205 backgroundImage.color = m_Background.imageColor;
206 backgroundObj.transform.SetSiblingIndex(0);
207 backgroundObj.SetActive(m_Background.show);
209 m_Background.refreshComponent();
212 protected virtual void InitPainter()
214 m_Painter = ChartHelper.AddPainterObject(
"painter_b", transform, m_GraphMinAnchor,
215 m_GraphMaxAnchor, m_GraphPivot,
new Vector2(m_GraphWidth, m_GraphHeight), chartHideFlags, 1);
216 m_Painter.type = Painter.Type.Base;
217 m_Painter.onPopulateMesh = OnDrawPainterBase;
220 private void CheckSize()
222 var currWidth = rectTransform.rect.width;
223 var currHeight = rectTransform.rect.height;
225 if (m_GraphWidth == 0 && m_GraphHeight == 0 && (currWidth != 0 || currHeight != 0))
230 if (m_GraphWidth != currWidth
231 || m_GraphHeight != currHeight
232 || m_GraphMinAnchor != rectTransform.anchorMin
233 || m_GraphMaxAnchor != rectTransform.anchorMax
234 || m_GraphAnchoredPosition != rectTransform.anchoredPosition)
238 if (!ChartHelper.IsValueEqualsVector3(m_LastLocalPosition, transform.localPosition))
240 m_LastLocalPosition = transform.localPosition;
241 OnLocalPositionChanged();
245 protected void UpdateSize()
247 m_GraphWidth = rectTransform.rect.width;
248 m_GraphHeight = rectTransform.rect.height;
250 m_GraphMaxAnchor = rectTransform.anchorMax;
251 m_GraphMinAnchor = rectTransform.anchorMin;
252 m_GraphSizeDelta = rectTransform.sizeDelta;
253 m_GraphAnchoredPosition = rectTransform.anchoredPosition;
255 rectTransform.pivot = LayerHelper.ResetChartPositionAndPivot(m_GraphMinAnchor, m_GraphMaxAnchor,
256 m_GraphWidth, m_GraphHeight, ref m_GraphX, ref m_GraphY);
257 m_GraphPivot = rectTransform.pivot;
259 m_GraphRect.x = m_GraphX;
260 m_GraphRect.y = m_GraphY;
261 m_GraphRect.width = m_GraphWidth;
262 m_GraphRect.height = m_GraphHeight;
263 m_GraphPosition.x = m_GraphX;
264 m_GraphPosition.y = m_GraphY;
269 private void CheckPointerPos()
271 if (m_ForceOpenRaycastTarget) raycastTarget =
true;
272 if (IsNeedCheckPointerPos())
274 raycastTarget =
true;
275 if (canvas ==
null)
return;
277 if (!ScreenPointToChartPoint(Input.mousePosition, out local))
288 raycastTarget =
false;
292 protected virtual void CheckIsInScrollRect()
294 m_ScrollRect = GetComponentInParent<ScrollRect>();
297 protected virtual bool IsNeedCheckPointerPos()
299 return raycastTarget;
302 protected virtual void CheckRefreshChart()
307 m_RefreshChart =
false;
311 protected virtual void CheckRefreshPainter()
313 m_Painter.CheckRefresh();
316 internal virtual void RefreshPainter(Painter painter)
318 if (painter ==
null)
return;
322 protected virtual void OnSizeChanged()
324 m_RefreshChart =
true;
327 protected virtual void OnLocalPositionChanged()
331 protected virtual void OnDrawPainterBase(VertexHelper vh, Painter painter)
337 protected virtual void DrawPainterBase(VertexHelper vh)
341 protected virtual void DrawBackground(VertexHelper vh)
345 public virtual void OnPointerClick(PointerEventData eventData)
347 if (m_OnPointerClick !=
null) m_OnPointerClick(eventData,
this);
350 public virtual void OnPointerDown(PointerEventData eventData)
352 if (m_OnPointerDown !=
null) m_OnPointerDown(eventData,
this);
355 public virtual void OnPointerUp(PointerEventData eventData)
357 if (m_OnPointerUp !=
null) m_OnPointerUp(eventData,
this);
360 public virtual void OnPointerEnter(PointerEventData eventData)
363 if (m_OnPointerEnter !=
null) m_OnPointerEnter(eventData,
this);
366 public virtual void OnPointerExit(PointerEventData eventData)
369 if (m_OnPointerExit !=
null) m_OnPointerExit(eventData,
this);
372 public virtual void OnBeginDrag(PointerEventData eventData)
374 if (m_ScrollRect !=
null) m_ScrollRect.OnBeginDrag(eventData);
375 if (m_OnBeginDrag !=
null) m_OnBeginDrag(eventData,
this);
378 public virtual void OnEndDrag(PointerEventData eventData)
380 if (m_ScrollRect !=
null) m_ScrollRect.OnEndDrag(eventData);
381 if (m_OnEndDrag !=
null) m_OnEndDrag(eventData,
this);
384 public virtual void OnDrag(PointerEventData eventData)
386 if (m_ScrollRect !=
null) m_ScrollRect.OnDrag(eventData);
387 if (m_OnDrag !=
null) m_OnDrag(eventData,
this);
390 public virtual void OnScroll(PointerEventData eventData)
392 if (m_ScrollRect !=
null) m_ScrollRect.OnScroll(eventData);
393 if (m_OnScroll !=
null) m_OnScroll(eventData,
this);