forked from Unity-Technologies/com.unity.reflect.viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollInputController.cs
More file actions
97 lines (84 loc) · 3.23 KB
/
Copy pathScrollInputController.cs
File metadata and controls
97 lines (84 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
using System;
using Unity.SpatialFramework.Input;
using Unity.XRTools.ModuleLoader;
using UnityEngine;
using UnityEngine.InputSystem;
namespace Unity.SpatialFramework.Interaction
{
/// <summary>
/// Component fires a scroll input event from an input action such as a joystick.
/// The scroll is constrained to be either horizontal or vertical, but not both, based on whichever one passes a threshold first
/// </summary>
public class ScrollInputController : MonoBehaviour, IUsesInputActions
{
const float k_InputSmoothTime = 0.1f;
const float k_ReleaseThreshold = 0.05f;
enum StickAxis
{
None,
Horizontal,
Vertical,
}
#pragma warning disable 649
[SerializeField, Tooltip("An action map that contains an action named Scroll")]
InputActionAsset m_ActionsAsset;
#pragma warning restore 649
InputAction m_ScrollAction;
StickAxis m_CurrentDirection = StickAxis.None;
Vector2 m_InputValue;
Vector2 m_InputValueVelocity;
IProvidesInputActions IFunctionalitySubscriber<IProvidesInputActions>.provider { get; set; }
public InputActionAsset inputActionsAsset { get => m_ActionsAsset; }
/// <summary>
/// Action that is called when scrolling occurs
/// </summary>
public Action<Vector2> onInput { private get; set; }
void IUsesInputActions.OnActionsCreated(InputActionAsset input)
{
m_ScrollAction = input.FindAction("Scroll");
m_CurrentDirection = StickAxis.None;
}
void IUsesInputActions.ProcessInput(InputActionAsset input)
{
var value = m_ScrollAction.ReadValue<Vector2>();
var x = value.x;
var y = value.y;
var absX = Mathf.Abs(x);
var absY = Mathf.Abs(y);
// Stick dead zone will already be processed by input system, but this threshold is to release from one axis and switch to the other
if (m_CurrentDirection == StickAxis.None)
{
if (absX > absY)
{
m_CurrentDirection = StickAxis.Horizontal;
}
else if (absY > absX)
{
m_CurrentDirection = StickAxis.Vertical;
}
}
else if (absX <= k_ReleaseThreshold && absY <= k_ReleaseThreshold)
{
m_CurrentDirection = StickAxis.None;
}
if (m_CurrentDirection == StickAxis.Horizontal)
{
y = 0f;
}
else if (m_CurrentDirection == StickAxis.Vertical)
{
x = 0f;
}
if (m_CurrentDirection != StickAxis.None)
{
m_InputValue = Vector2.SmoothDamp(current: m_InputValue, target: new Vector2(x, y), currentVelocity: ref m_InputValueVelocity, smoothTime: k_InputSmoothTime, maxSpeed: Mathf.Infinity, deltaTime: Time.unscaledDeltaTime);
this.ConsumeControl(m_ScrollAction.activeControl);
}
else
{
m_InputValue = Vector2.zero;
}
onInput?.Invoke(m_InputValue);
}
}
}