| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321 | using System;using UnityEngine;using UnityEngine.EventSystems;using UnityEngine.UI;#if INPUT_SYSTEM_ENABLEDusing Input = XCharts.Runtime.InputHelper;#endifnamespace XCharts.Runtime{    [RequireComponent(typeof(CanvasRenderer))]    public partial class BaseGraph : MaskableGraphic, IPointerDownHandler, IPointerUpHandler,        IPointerEnterHandler, IPointerExitHandler, IBeginDragHandler, IPointerClickHandler,        IDragHandler, IEndDragHandler, IScrollHandler    {        [SerializeField] protected bool m_EnableTextMeshPro = false;        protected Painter m_Painter;        protected int m_SiblingIndex;        protected float m_GraphWidth;        protected float m_GraphHeight;        protected float m_GraphX;        protected float m_GraphY;        protected Vector3 m_GraphPosition = Vector3.zero;        protected Vector2 m_GraphMinAnchor;        protected Vector2 m_GraphMaxAnchor;        protected Vector2 m_GraphPivot;        protected Vector2 m_GraphSizeDelta;        protected Vector2 m_GraphAnchoredPosition;        protected Rect m_GraphRect = new Rect(0, 0, 0, 0);        protected bool m_RefreshChart = false;        protected bool m_ForceOpenRaycastTarget;        protected bool m_IsControlledByLayout = false;        protected bool m_PainerDirty = false;        protected bool m_IsOnValidate = false;        protected Vector3 m_LastLocalPosition;        protected PointerEventData m_PointerEventData;        protected Action<PointerEventData, BaseGraph> m_OnPointerClick;        protected Action<PointerEventData, BaseGraph> m_OnPointerDown;        protected Action<PointerEventData, BaseGraph> m_OnPointerUp;        protected Action<PointerEventData, BaseGraph> m_OnPointerEnter;        protected Action<PointerEventData, BaseGraph> m_OnPointerExit;        protected Action<PointerEventData, BaseGraph> m_OnBeginDrag;        protected Action<PointerEventData, BaseGraph> m_OnDrag;        protected Action<PointerEventData, BaseGraph> m_OnEndDrag;        protected Action<PointerEventData, BaseGraph> m_OnScroll;        public virtual HideFlags chartHideFlags { get { return HideFlags.None; } }        private ScrollRect m_ScrollRect;        public Painter painter { get { return m_Painter; } }        protected virtual void InitComponent()        {            InitPainter();        }        protected override void Awake()        {            CheckTextMeshPro();            m_SiblingIndex = 0;            m_LastLocalPosition = transform.localPosition;            UpdateSize();            InitComponent();            CheckIsInScrollRect();        }        protected override void Start()        {            m_RefreshChart = true;        }        protected virtual void Update()        {            CheckSize();            if (m_IsOnValidate)            {                m_IsOnValidate = false;                CheckTextMeshPro();                InitComponent();                RefreshGraph();            }            else            {                CheckComponent();            }            CheckPointerPos();            CheckRefreshChart();            CheckRefreshPainter();        }        protected virtual void SetAllComponentDirty()        {#if UNITY_EDITOR            if (!Application.isPlaying)            {                m_IsOnValidate = true;            }#endif            m_PainerDirty = true;        }        protected virtual void CheckComponent()        {            if (m_PainerDirty)            {                InitPainter();                m_PainerDirty = false;            }        }        private void CheckTextMeshPro()        {#if dUI_TextMeshPro                var enableTextMeshPro = true;#else            var enableTextMeshPro = false;#endif            if (m_EnableTextMeshPro != enableTextMeshPro)            {                m_EnableTextMeshPro = enableTextMeshPro;                RebuildChartObject();            }        }#if UNITY_EDITOR        protected override void Reset()        {            base.Reset();        }        protected override void OnValidate()        {            base.OnValidate();            m_IsOnValidate = true;        }#endif        protected override void OnDestroy()        {            base.OnDestroy();            for (int i = transform.childCount - 1; i >= 0; i--)            {                DestroyImmediate(transform.GetChild(i).gameObject);            }        }        protected override void OnPopulateMesh(VertexHelper vh)        {            vh.Clear();        }        protected virtual void InitPainter()        {            m_Painter = ChartHelper.AddPainterObject("painter_b", transform, m_GraphMinAnchor,                m_GraphMaxAnchor, m_GraphPivot, new Vector2(m_GraphWidth, m_GraphHeight), chartHideFlags, 1);            m_Painter.type = Painter.Type.Base;            m_Painter.onPopulateMesh = OnDrawPainterBase;            m_Painter.transform.SetSiblingIndex(0);        }        private void CheckSize()        {            var currWidth = rectTransform.rect.width;            var currHeight = rectTransform.rect.height;            if (m_GraphWidth == 0 && m_GraphHeight == 0 && (currWidth != 0 || currHeight != 0))            {                Awake();            }            if (m_GraphWidth != currWidth ||                m_GraphHeight != currHeight ||                m_GraphMinAnchor != rectTransform.anchorMin ||                m_GraphMaxAnchor != rectTransform.anchorMax ||                m_GraphAnchoredPosition != rectTransform.anchoredPosition)            {                UpdateSize();            }            if (!ChartHelper.IsValueEqualsVector3(m_LastLocalPosition, transform.localPosition))            {                m_LastLocalPosition = transform.localPosition;                OnLocalPositionChanged();            }        }        protected void UpdateSize()        {            m_GraphWidth = rectTransform.rect.width;            m_GraphHeight = rectTransform.rect.height;            m_GraphMaxAnchor = rectTransform.anchorMax;            m_GraphMinAnchor = rectTransform.anchorMin;            m_GraphSizeDelta = rectTransform.sizeDelta;            m_GraphAnchoredPosition = rectTransform.anchoredPosition;            rectTransform.pivot = LayerHelper.ResetChartPositionAndPivot(m_GraphMinAnchor, m_GraphMaxAnchor,                m_GraphWidth, m_GraphHeight, ref m_GraphX, ref m_GraphY);            m_GraphPivot = rectTransform.pivot;            m_GraphRect.x = m_GraphX;            m_GraphRect.y = m_GraphY;            m_GraphRect.width = m_GraphWidth;            m_GraphRect.height = m_GraphHeight;            m_GraphPosition.x = m_GraphX;            m_GraphPosition.y = m_GraphY;            OnSizeChanged();        }        private void CheckPointerPos()        {            if (!isPointerInChart) return;            if (canvas == null) return;            Vector2 mousePos = m_PointerEventData.position;            Vector2 local;            if (!ScreenPointToChartPoint(mousePos, out local))            {                pointerPos = Vector2.zero;            }            else            {                pointerPos = local;            }        }        protected virtual void CheckIsInScrollRect()        {            m_ScrollRect = GetComponentInParent<ScrollRect>();        }        protected virtual void CheckRefreshChart()        {            if (m_RefreshChart && m_Painter != null)            {                m_Painter.Refresh();                m_RefreshChart = false;            }        }        protected virtual void CheckRefreshPainter()        {            if (m_Painter == null) return;            m_Painter.CheckRefresh();        }        internal virtual void RefreshPainter(Painter painter)        {            if (painter == null) return;            painter.Refresh();        }        protected virtual void OnSizeChanged()        {            m_RefreshChart = true;        }        protected virtual void OnLocalPositionChanged() { }        protected virtual void OnDrawPainterBase(VertexHelper vh, Painter painter)        {            DrawPainterBase(vh);        }        protected virtual void DrawPainterBase(VertexHelper vh) { }        public virtual void OnPointerClick(PointerEventData eventData)        {            if (m_OnPointerClick != null) m_OnPointerClick(eventData, this);        }        public virtual void OnPointerDown(PointerEventData eventData)        {            if (m_OnPointerDown != null) m_OnPointerDown(eventData, this);        }        public virtual void OnPointerUp(PointerEventData eventData)        {            if (m_OnPointerUp != null) m_OnPointerUp(eventData, this);        }        public virtual void OnPointerEnter(PointerEventData eventData)        {            m_PointerEventData = eventData;            if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this);        }        public virtual void OnPointerExit(PointerEventData eventData)        {            m_PointerEventData = null;            if (m_OnPointerExit != null) m_OnPointerExit(eventData, this);        }        public virtual void OnBeginDrag(PointerEventData eventData)        {            if (m_ScrollRect != null) m_ScrollRect.OnBeginDrag(eventData);            if (m_OnBeginDrag != null) m_OnBeginDrag(eventData, this);        }        public virtual void OnEndDrag(PointerEventData eventData)        {            if (m_ScrollRect != null) m_ScrollRect.OnEndDrag(eventData);            if (m_OnEndDrag != null) m_OnEndDrag(eventData, this);        }        public virtual void OnDrag(PointerEventData eventData)        {            if (m_ScrollRect != null) m_ScrollRect.OnDrag(eventData);            if (m_OnDrag != null) m_OnDrag(eventData, this);        }        public virtual void OnScroll(PointerEventData eventData)        {            if (m_ScrollRect != null) m_ScrollRect.OnScroll(eventData);            if (m_OnScroll != null) m_OnScroll(eventData, this);        }    }}
 |