Files
XCharts/Runtime/Internal/BaseGraph.API.cs

223 lines
8.3 KiB
C#
Raw Normal View History

2020-06-05 08:52:36 +08:00
using System;
2022-12-22 21:51:08 +08:00
using System.Collections;
2022-05-22 22:17:38 +08:00
using UnityEngine;
2020-06-05 08:52:36 +08:00
using UnityEngine.EventSystems;
2022-02-19 22:37:57 +08:00
namespace XCharts.Runtime
2020-06-05 08:52:36 +08:00
{
/// <summary>
/// The base class of all graphs or components.
2023-11-11 23:32:24 +08:00
/// ||所有图形的基类。
2020-06-05 08:52:36 +08:00
/// </summary>
public partial class BaseGraph
{
/// <summary>
2022-03-24 08:37:06 +08:00
/// The x of graph.
2023-11-11 23:32:24 +08:00
/// ||图形的X
2020-06-05 08:52:36 +08:00
/// </summary>
public float graphX { get { return m_GraphX; } }
/// <summary>
2022-03-24 08:37:06 +08:00
/// The y of graph.
2023-11-11 23:32:24 +08:00
/// ||图形的Y
2020-06-05 08:52:36 +08:00
/// </summary>
public float graphY { get { return m_GraphY; } }
/// <summary>
2022-03-24 08:37:06 +08:00
/// The width of graph.
2023-11-11 23:32:24 +08:00
/// ||图形的宽
2020-06-05 08:52:36 +08:00
/// </summary>
public float graphWidth { get { return m_GraphWidth; } }
/// <summary>
2022-03-24 08:37:06 +08:00
/// The height of graph.
2023-11-11 23:32:24 +08:00
/// ||图形的高
2020-06-05 08:52:36 +08:00
/// </summary>
public float graphHeight { get { return m_GraphHeight; } }
/// <summary>
/// The position of graph.
2023-11-11 23:32:24 +08:00
/// ||图形的左下角起始坐标。
2020-06-05 08:52:36 +08:00
/// </summary>
public Vector3 graphPosition { get { return m_GraphPosition; } }
public Rect graphRect { get { return m_GraphRect; } }
2022-12-07 13:16:06 +08:00
public Vector2 graphSizeDelta { get { return m_GraphSizeDelta; } }
public Vector2 graphPivot { get { return m_GraphPivot; } }
public Vector2 graphMinAnchor { get { return m_GraphMinAnchor; } }
public Vector2 graphMaxAnchor { get { return m_GraphMaxAnchor; } }
public Vector2 graphAnchoredPosition { get { return m_GraphAnchoredPosition; } }
2020-06-05 08:52:36 +08:00
/// <summary>
/// The postion of pointer move.
2023-11-11 23:32:24 +08:00
/// ||鼠标位置。
2020-06-05 08:52:36 +08:00
/// </summary>
public Vector2 pointerPos { get; protected set; }
public Vector2 clickPos { get; protected set; }
2020-06-05 08:52:36 +08:00
/// <summary>
/// Whether the mouse pointer is in the chart.
2023-11-11 23:32:24 +08:00
/// ||鼠标是否在图表内。
/// </summary>
public bool isPointerInChart { get { return pointerMoveEventData != null; } }
/// <summary>
/// Whether the mouse click the chart.
/// ||鼠标是否点击了图表。
/// </summary>
public bool isPointerClick { get { return pointerClickEventData != null; } }
/// <summary>
2020-06-05 08:52:36 +08:00
/// 警告信息。
/// </summary>
public string warningInfo { get; protected set; }
/// <summary>
/// 强制开启鼠标事件检测。
/// </summary>
public bool forceOpenRaycastTarget { get { return m_ForceOpenRaycastTarget; } set { m_ForceOpenRaycastTarget = value; } }
/// <summary>
/// 鼠标点击回调。
/// </summary>
public Action<PointerEventData, BaseGraph> onPointerClick { set { m_OnPointerClick = value; m_ForceOpenRaycastTarget = true; } }
2020-06-05 08:52:36 +08:00
/// <summary>
/// 鼠标按下回调。
/// </summary>
public Action<PointerEventData, BaseGraph> onPointerDown { set { m_OnPointerDown = value; m_ForceOpenRaycastTarget = true; } }
2020-06-05 08:52:36 +08:00
/// <summary>
/// 鼠标弹起回调。
/// </summary>
public Action<PointerEventData, BaseGraph> onPointerUp { set { m_OnPointerUp = value; m_ForceOpenRaycastTarget = true; } }
2020-06-05 08:52:36 +08:00
/// <summary>
/// 鼠标进入回调。
/// </summary>
public Action<PointerEventData, BaseGraph> onPointerEnter { set { m_OnPointerEnter = value; m_ForceOpenRaycastTarget = true; } }
2020-06-05 08:52:36 +08:00
/// <summary>
/// 鼠标退出回调。
/// </summary>
public Action<PointerEventData, BaseGraph> onPointerExit { set { m_OnPointerExit = value; m_ForceOpenRaycastTarget = true; } }
2020-06-05 08:52:36 +08:00
/// <summary>
/// 鼠标开始拖拽回调。
/// </summary>
public Action<PointerEventData, BaseGraph> onBeginDrag { set { m_OnBeginDrag = value; m_ForceOpenRaycastTarget = true; } }
2020-06-05 08:52:36 +08:00
/// <summary>
/// 鼠标拖拽回调。
/// </summary>
public Action<PointerEventData, BaseGraph> onDrag { set { m_OnDrag = value; m_ForceOpenRaycastTarget = true; } }
2020-06-05 08:52:36 +08:00
/// <summary>
/// 鼠标结束拖拽回调。
/// </summary>
public Action<PointerEventData, BaseGraph> onEndDrag { set { m_OnEndDrag = value; m_ForceOpenRaycastTarget = true; } }
2020-06-05 08:52:36 +08:00
/// <summary>
/// 鼠标滚动回调。
/// </summary>
public Action<PointerEventData, BaseGraph> onScroll { set { m_OnScroll = value; m_ForceOpenRaycastTarget = true; } }
2020-06-05 08:52:36 +08:00
/// <summary>
2020-06-05 09:27:51 +08:00
/// 设置图形的宽高在非stretch pivot下才有效其他情况需要自己调整RectTransform
2020-06-05 08:52:36 +08:00
/// </summary>
/// <param name="width"></param>
/// <param name="height"></param>
public virtual void SetSize(float width, float height)
{
if (LayerHelper.IsFixedWidthHeight(rectTransform))
{
rectTransform.sizeDelta = new Vector2(width, height);
}
else
{
Debug.LogError("Can't set size on stretch pivot,you need to modify rectTransform by yourself.");
}
}
/// <summary>
/// 重新初始化Painter
/// </summary>
public void SetPainterDirty()
{
m_PainerDirty = true;
}
2020-06-05 08:52:36 +08:00
/// <summary>
/// Redraw graph in next frame.
2023-11-11 23:32:24 +08:00
/// ||在下一帧刷新图形。
2020-06-05 08:52:36 +08:00
/// </summary>
2022-09-09 13:19:22 +08:00
public virtual void RefreshGraph()
2020-06-05 08:52:36 +08:00
{
m_RefreshChart = true;
}
2021-01-11 08:54:28 +08:00
public void RefreshAllComponent()
{
SetAllComponentDirty();
2021-03-25 12:55:52 +08:00
RefreshGraph();
2021-01-11 08:54:28 +08:00
}
2020-06-05 08:52:36 +08:00
/// <summary>
/// 检测警告信息。
/// </summary>
/// <returns></returns>
public string CheckWarning()
{
warningInfo = CheckHelper.CheckChart(this);
return warningInfo;
}
/// <summary>
2022-03-09 07:26:15 +08:00
/// 移除并重新创建所有图表的Object。
/// </summary>
2022-03-09 07:26:15 +08:00
public void RebuildChartObject()
{
ChartHelper.DestroyAllChildren(transform);
2022-02-25 08:10:09 +08:00
SetAllComponentDirty();
}
2021-05-13 09:38:32 +08:00
public bool ScreenPointToChartPoint(Vector2 screenPoint, out Vector2 chartPoint)
{
#if UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
var relative = Display.RelativeMouseAt(screenPoint);
2022-05-22 22:17:38 +08:00
if (relative != Vector3.zero)
screenPoint = relative;
2021-06-13 11:50:41 +08:00
#endif
2021-05-13 09:38:32 +08:00
var cam = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform,
2022-05-22 22:17:38 +08:00
screenPoint, cam, out chartPoint))
2021-05-13 09:38:32 +08:00
{
return false;
}
return true;
}
2022-12-22 21:51:08 +08:00
/// <summary>
/// chart local point to screen point.
2023-11-11 23:32:24 +08:00
/// ||图表内坐标转屏幕坐标。
/// </summary>
/// <param name="localPoint">图表内的坐标</param>
/// <returns>屏幕坐标</returns>
[Since("v3.7.0")]
public Vector2 LocalPointToScreenPoint(Vector2 localPoint)
{
var cam = canvas.renderMode == RenderMode.ScreenSpaceOverlay ? null : canvas.worldCamera;
var wordPoint = rectTransform.TransformPoint(localPoint);
return RectTransformUtility.WorldToScreenPoint(cam, wordPoint);
}
/// <summary>
/// chart local point to world point.
2023-11-11 23:32:24 +08:00
/// ||图表内坐标转世界坐标。
/// </summary>
/// <param name="localPoint">图表内的坐标</param>
/// <returns>世界坐标</returns>
[Since("v3.7.0")]
public Vector2 LocalPointToWorldPoint(Vector2 localPoint)
{
return rectTransform.TransformPoint(localPoint);
}
2022-12-22 21:51:08 +08:00
/// <summary>
/// 保存图表为图片。
/// </summary>
/// <param name="imageType">type of image: png, jpg, exr</param>
/// <param name="savePath">save path</param>
public void SaveAsImage(string imageType = "png", string savePath = "")
{
StartCoroutine(SaveAsImageSync(imageType, savePath));
}
private IEnumerator SaveAsImageSync(string imageType, string path)
{
yield return new WaitForEndOfFrame();
ChartHelper.SaveAsImage(rectTransform, canvas, imageType, path);
}
2020-06-05 08:52:36 +08:00
}
2022-05-22 22:17:38 +08:00
}