新增对 InputSystem 的支持

Update BaseGraph.cs

Update XCharts.Runtime.asmdef
This commit is contained in:
边上海
2023-01-29 19:43:05 +08:00
parent e90d7d61d9
commit 7fbaf7af70
14 changed files with 444 additions and 288 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

View File

@@ -0,0 +1,17 @@
---
title: 入门教程:从 Input Manager 转到 Input System
sidebar_position: 1
slug: /inputsystem
---
# 教程:从 Input Manager 转到 Input System
## 1. 按图示修改项目配置中输入模式为 Input System
![Project Settings](img/inputsystem02.png)
## 2. 使用 Unity Package Manager 安装 Input System
![UPM](img/inputsystem01.png)
## 3. 选中场景中 EventSystem 游戏对象,更换输入模组
![Input Module](img/inputsystem03.png)

View File

@@ -70,6 +70,7 @@
- 支持万级大数据量绘制,支持采样绘制。
- 支持`TexMeshPro`
- 支持所有`5.6`以上的`Unity`版本。
- 支持 Input System [如何从 Input Manager 转 Input System](Documentation~/zh/inputsystem.md))。
## 截图

View File

@@ -631,12 +631,12 @@ namespace XCharts.Runtime
internal void UpdateStartLabelPosition(Vector3 pos)
{
m_StartLabel.SetPosition(pos);
if (m_StartLabel != null) m_StartLabel.SetPosition(pos);
}
internal void UpdateEndLabelPosition(Vector3 pos)
{
m_EndLabel.SetPosition(pos);
if (m_EndLabel != null) m_EndLabel.SetPosition(pos);
}
public void UpdateRuntimeData(float chartX, float chartY, float chartWidth, float chartHeight)

View File

@@ -1,8 +1,10 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using XUGL;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Runtime
{

View File

@@ -3,7 +3,9 @@ using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using XUGL;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Runtime
{
[UnityEngine.Scripting.Preserve]

View File

@@ -253,7 +253,15 @@ namespace XCharts.Runtime
else
return component;
}
public T EnsureChartComponent<T>() where T : MainComponent
{
var component = GetChartComponent<T>();
if (component == null)
return AddChartComponent<T>();
else
return component;
}
public bool TryGetChartComponent<T>(out T component, int index = 0)
where T : MainComponent
{

View File

@@ -11,7 +11,7 @@ namespace XCharts.Runtime
{
[AddComponentMenu("XCharts/EmptyChart", 10)]
[ExecuteInEditMode]
[RequireComponent(typeof(RectTransform))]
[RequireComponent(typeof(RectTransform),typeof(CanvasRenderer))]
[DisallowMultipleComponent]
public partial class BaseChart : BaseGraph, ISerializationCallbackReceiver
{

View File

@@ -2,6 +2,9 @@ using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
#if INPUT_SYSTEM_ENABLED
using Input = XCharts.Runtime.InputHelper;
#endif
namespace XCharts.Runtime
{
@@ -9,302 +12,303 @@ namespace XCharts.Runtime
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 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()
{
[SerializeField] protected bool m_EnableTextMeshPro = false;
InitPainter();
}
protected Painter m_Painter;
protected int m_SiblingIndex;
protected override void Awake()
{
CheckTextMeshPro();
m_SiblingIndex = 0;
m_LastLocalPosition = transform.localPosition;
UpdateSize();
InitComponent();
CheckIsInScrollRect();
}
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 override void Start()
{
m_RefreshChart = true;
}
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()
protected virtual void Update()
{
CheckSize();
if (m_IsOnValidate)
{
m_IsOnValidate = false;
CheckTextMeshPro();
m_SiblingIndex = 0;
m_LastLocalPosition = transform.localPosition;
UpdateSize();
InitComponent();
CheckIsInScrollRect();
RefreshGraph();
}
protected override void Start()
else
{
m_RefreshChart = true;
CheckComponent();
}
CheckPointerPos();
CheckRefreshChart();
CheckRefreshPainter();
}
protected virtual void Update()
{
CheckSize();
if (m_IsOnValidate)
{
m_IsOnValidate = false;
CheckTextMeshPro();
InitComponent();
RefreshGraph();
}
else
{
CheckComponent();
}
CheckPointerPos();
CheckRefreshChart();
CheckRefreshPainter();
}
protected virtual void SetAllComponentDirty()
{
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() { }
protected override void OnValidate()
if (!Application.isPlaying)
{
m_IsOnValidate = true;
}
#endif
m_PainerDirty = true;
}
protected override void OnDestroy()
protected virtual void CheckComponent()
{
if (m_PainerDirty)
{
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 local;
if (!ScreenPointToChartPoint(Input.mousePosition, 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.Refresh();
m_RefreshChart = false;
}
}
protected virtual void CheckRefreshPainter()
{
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)
{
isPointerInChart = true;
if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this);
}
public virtual void OnPointerExit(PointerEventData eventData)
{
isPointerInChart = false;
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);
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() { }
protected override void OnValidate()
{
m_IsOnValidate = true;
}
#endif
protected override void 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 = Input.mousePosition;
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.Refresh();
m_RefreshChart = false;
}
}
protected virtual void CheckRefreshPainter()
{
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)
{
isPointerInChart = true;
if (m_OnPointerEnter != null) m_OnPointerEnter(eventData, this);
}
public virtual void OnPointerExit(PointerEventData eventData)
{
isPointerInChart = false;
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);
}
}
}

View File

@@ -0,0 +1,111 @@
#if INPUT_SYSTEM_ENABLED
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.LowLevel;
namespace XCharts.Runtime
{
public class InputHelper
{
public static Vector2 mousePosition
{
get
{
var value = Vector2.zero;
if (null != Mouse.current)
{
value = Mouse.current.position.ReadValue();
}
else if (null != Touchscreen.current && Touchscreen.current.touches.Count > 0)
{
value = Touchscreen.current.touches[0].position.ReadValue();
}
return value;
}
}
public static int touchCount
{
get
{
var value = 0;
if (null != Touchscreen.current)
{
value = Touchscreen.current.touches.Count;
}
return value;
}
}
public static Touch GetTouch(int v)
{
UnityEngine.TouchPhase PhaseConvert(TouchState state)
{
UnityEngine.TouchPhase temp = UnityEngine.TouchPhase.Began;
switch (state.phase)
{
case UnityEngine.InputSystem.TouchPhase.Began:
temp = UnityEngine.TouchPhase.Began;
break;
case UnityEngine.InputSystem.TouchPhase.Moved:
temp = UnityEngine.TouchPhase.Moved;
break;
case UnityEngine.InputSystem.TouchPhase.Canceled:
temp = UnityEngine.TouchPhase.Canceled;
break;
case UnityEngine.InputSystem.TouchPhase.Stationary:
temp = UnityEngine.TouchPhase.Stationary;
break;
default:
case UnityEngine.InputSystem.TouchPhase.Ended:
case UnityEngine.InputSystem.TouchPhase.None:
temp = UnityEngine.TouchPhase.Ended;
break;
}
return temp;
}
var touch = Touchscreen.current.touches[v];
var value = touch.ReadValue();
//copy touchcontrol's touchstate data into touch
return new Touch
{
deltaPosition = value.delta,
fingerId = value.touchId,
position = value.position,
phase = PhaseConvert(value),
pressure = value.pressure,
radius = value.radius.magnitude,
radiusVariance = value.radius.sqrMagnitude,
type = value.isPrimaryTouch ? TouchType.Direct : TouchType.Indirect,
tapCount = value.tapCount,
deltaTime = Time.realtimeSinceStartup - (float)value.startTime,
rawPosition = value.startPosition,
};
}
public static bool GetKeyDown(KeyCode keyCode)
{
var value = false;
if (null != Keyboard.current)
{
var key = Keyboard.current.spaceKey;
switch (keyCode)
{
case KeyCode.Space:
key = Keyboard.current.spaceKey;
break;
case KeyCode.L:
key = Keyboard.current.lKey;
break;
default:
Debug.LogError($"{nameof(InputHelper)}: not support {keyCode} yet , please add it yourself if needed");
break;
}
value = key.wasPressedThisFrame;
}
return value;
}
}
}
#endif

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5069defa9fe8c7a43843e1189e2d606c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -1,13 +1,13 @@
{
"name": "XCharts.Runtime",
"references": [
],
"optionalUnityReferences": [],
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": []
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}