AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
BaseGraph.cs
1 /******************************************/
2 /* */
3 /* Copyright (c) 2021 monitor1394 */
4 /* https://github.com/monitor1394 */
5 /* */
6 /******************************************/
7 
8 using UnityEngine;
9 using UnityEngine.UI;
10 using System;
11 using UnityEngine.EventSystems;
12 
13 namespace XCharts
14 {
15  [RequireComponent(typeof(CanvasRenderer))]
16  public partial class BaseGraph : MaskableGraphic, IPointerDownHandler, IPointerUpHandler,
17  IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IPointerClickHandler,
18  IDragHandler, IEndDragHandler, IScrollHandler
19  {
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;
27 
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;
45 
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;
55 
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; } }
60 
61  private ScrollRect m_ScrollRect;
62 
63 
64  protected virtual void InitComponent()
65  {
66  InitPainter();
67  InitBackground();
68  }
69 
70  protected override void Awake()
71  {
72  CheckTextMeshPro();
73  m_SiblingIndex = 0;
74  if (transform.parent != null)
75  {
76  m_IsControlledByLayout = transform.parent.GetComponent<LayoutGroup>() != null;
77  }
78  raycastTarget = false;
79  m_LastLocalPosition = transform.localPosition;
80  UpdateSize();
81  InitComponent();
82  CheckIsInScrollRect();
83  }
84 
85  protected override void Start()
86  {
87  m_RefreshChart = true;
88  }
89 
90  protected virtual void Update()
91  {
92  CheckSize();
93  if (m_IsOnValidate)
94  {
95  m_IsOnValidate = false;
96  m_RefreshChart = true;
97  CheckTextMeshPro();
98  InitComponent();
99  }
100  else
101  {
102  CheckComponent();
103  }
104  CheckPointerPos();
105  CheckRefreshChart();
106  CheckRefreshPainter();
107  }
108 
109  protected virtual void SetAllComponentDirty()
110  {
111 #if UNITY_EDITOR
112  if (!Application.isPlaying)
113  {
114  m_IsOnValidate = true;
115  Update();
116  }
117 #endif
118  m_PainerDirty = true;
119  m_Background.SetAllDirty();
120  }
121 
122  protected virtual void CheckComponent()
123  {
124  CheckComponentDirty(m_Background);
125  if (m_PainerDirty)
126  {
127  InitPainter();
128  m_PainerDirty = false;
129  }
130  }
131 
132 
133 
134  private void CheckTextMeshPro()
135  {
136 #if dUI_TextMeshPro
137  var enableTextMeshPro = true;
138 #else
139  var enableTextMeshPro = false;
140 #endif
141  if (m_EnableTextMeshPro != enableTextMeshPro)
142  {
143  m_EnableTextMeshPro = enableTextMeshPro;
145  }
146  }
147 
148  protected void CheckComponentDirty(ChartComponent component)
149  {
150  if (component.anyDirty)
151  {
152  if (component.componentDirty && component.refreshComponent != null)
153  {
154  component.refreshComponent.Invoke();
155  }
156  if (component.vertsDirty)
157  {
158  if (component.painter != null)
159  {
160  RefreshPainter(component.painter);
161  }
162  }
163  component.ClearDirty();
164  }
165  }
166 
167 #if UNITY_EDITOR
168  protected override void Reset()
169  {
170  }
171 
172  protected override void OnValidate()
173  {
174  m_IsOnValidate = true;
175  }
176 #endif
177 
178  protected override void OnDestroy()
179  {
180  for (int i = transform.childCount - 1; i >= 0; i--)
181  {
182  DestroyImmediate(transform.GetChild(i).gameObject);
183  }
184  }
185 
186  protected override void OnPopulateMesh(VertexHelper vh)
187  {
188  vh.Clear();
189  }
190 
191  private void InitBackground()
192  {
193  m_Background.painter = m_Painter;
194  m_Background.refreshComponent = delegate ()
195  {
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);
208  };
209  m_Background.refreshComponent();
210  }
211 
212  protected virtual void InitPainter()
213  {
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;
218  }
219 
220  private void CheckSize()
221  {
222  var currWidth = rectTransform.rect.width;
223  var currHeight = rectTransform.rect.height;
224 
225  if (m_GraphWidth == 0 && m_GraphHeight == 0 && (currWidth != 0 || currHeight != 0))
226  {
227  Awake();
228  }
229 
230  if (m_GraphWidth != currWidth
231  || m_GraphHeight != currHeight
232  || m_GraphMinAnchor != rectTransform.anchorMin
233  || m_GraphMaxAnchor != rectTransform.anchorMax
234  || m_GraphAnchoredPosition != rectTransform.anchoredPosition)
235  {
236  UpdateSize();
237  }
238  if (!ChartHelper.IsValueEqualsVector3(m_LastLocalPosition, transform.localPosition))
239  {
240  m_LastLocalPosition = transform.localPosition;
241  OnLocalPositionChanged();
242  }
243  }
244 
245  protected void UpdateSize()
246  {
247  m_GraphWidth = rectTransform.rect.width;
248  m_GraphHeight = rectTransform.rect.height;
249 
250  m_GraphMaxAnchor = rectTransform.anchorMax;
251  m_GraphMinAnchor = rectTransform.anchorMin;
252  m_GraphSizeDelta = rectTransform.sizeDelta;
253  m_GraphAnchoredPosition = rectTransform.anchoredPosition;
254 
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;
258 
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;
265 
266  OnSizeChanged();
267  }
268 
269  private void CheckPointerPos()
270  {
271  if (m_ForceOpenRaycastTarget) raycastTarget = true;
272  if (IsNeedCheckPointerPos())
273  {
274  raycastTarget = true;
275  if (canvas == null) return;
276  Vector2 local;
277  if (!ScreenPointToChartPoint(Input.mousePosition, out local))
278  {
279  pointerPos = Vector2.zero;
280  }
281  else
282  {
283  pointerPos = local;
284  }
285  }
286  else
287  {
288  raycastTarget = false;
289  }
290  }
291 
292  protected virtual void CheckIsInScrollRect()
293  {
294  m_ScrollRect = GetComponentInParent<ScrollRect>();
295  }
296 
297  protected virtual bool IsNeedCheckPointerPos()
298  {
299  return raycastTarget;
300  }
301 
302  protected virtual void CheckRefreshChart()
303  {
304  if (m_RefreshChart)
305  {
306  m_Painter.Refresh();
307  m_RefreshChart = false;
308  }
309  }
310 
311  protected virtual void CheckRefreshPainter()
312  {
313  m_Painter.CheckRefresh();
314  }
315 
316  internal virtual void RefreshPainter(Painter painter)
317  {
318  if (painter == null) return;
319  painter.Refresh();
320  }
321 
322  protected virtual void OnSizeChanged()
323  {
324  m_RefreshChart = true;
325  }
326 
327  protected virtual void OnLocalPositionChanged()
328  {
329  }
330 
331  protected virtual void OnDrawPainterBase(VertexHelper vh, Painter painter)
332  {
333  DrawBackground(vh);
334  DrawPainterBase(vh);
335  }
336 
337  protected virtual void DrawPainterBase(VertexHelper vh)
338  {
339  }
340 
341  protected virtual void DrawBackground(VertexHelper vh)
342  {
343  }
344 
345  public virtual void OnPointerClick(PointerEventData eventData)
346  {
347  if (m_OnPointerClick != null) m_OnPointerClick(eventData, this);
348  }
349 
350  public virtual void OnPointerDown(PointerEventData eventData)
351  {
352  if (m_OnPointerDown != null) m_OnPointerDown(eventData, this);
353  }
354 
355  public virtual void OnPointerUp(PointerEventData eventData)
356  {
357  if (m_OnPointerUp != null) m_OnPointerUp(eventData, this);
358  }
359 
360  public virtual void OnPointerEnter(PointerEventData eventData)
361  {
362  isPointerInChart = true;
363  if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this);
364  }
365 
366  public virtual void OnPointerExit(PointerEventData eventData)
367  {
368  isPointerInChart = false;
369  if (m_OnPointerExit != null) m_OnPointerExit(eventData, this);
370  }
371 
372  public virtual void OnBeginDrag(PointerEventData eventData)
373  {
374  if (m_ScrollRect != null) m_ScrollRect.OnBeginDrag(eventData);
375  if (m_OnBeginDrag != null) m_OnBeginDrag(eventData, this);
376  }
377 
378  public virtual void OnEndDrag(PointerEventData eventData)
379  {
380  if (m_ScrollRect != null) m_ScrollRect.OnEndDrag(eventData);
381  if (m_OnEndDrag != null) m_OnEndDrag(eventData, this);
382  }
383 
384  public virtual void OnDrag(PointerEventData eventData)
385  {
386  if (m_ScrollRect != null) m_ScrollRect.OnDrag(eventData);
387  if (m_OnDrag != null) m_OnDrag(eventData, this);
388  }
389 
390  public virtual void OnScroll(PointerEventData eventData)
391  {
392  if (m_ScrollRect != null) m_ScrollRect.OnScroll(eventData);
393  if (m_OnScroll != null) m_OnScroll(eventData, this);
394  }
395  }
396 }
XCharts.BaseGraph.isPointerInChart
bool isPointerInChart
Whether the mouse pointer is in the chart. 鼠标是否在图表内。
Definition: BaseGraph_API.cs:61
XCharts
Definition: RewardChart.cs:14
XCharts.SerieSymbolType.Rect
@ Rect
正方形。可通过设置itemStyle的cornerRadius变成圆角矩形。
XCharts.BaseGraph.pointerPos
Vector2 pointerPos
The postion of pointer. 鼠标位置。
Definition: BaseGraph_API.cs:56
XCharts.BaseGraph.RemoveChartObject
void RemoveChartObject()
移除所有图表子节点,会自动重现初始化。
Definition: BaseGraph_API.cs:161