-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInputHandler.cs
175 lines (149 loc) · 5.24 KB
/
InputHandler.cs
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;
namespace XenoWare
{
public class InputHandler : MonoBehaviour, IInputHandler
{
[SerializeField]
Camera _camera;
[SerializeField]
LayerMask directionHelperMask;
public event EventHandler QuitApplication;
public event EventHandler<InputButtonEventArgs> OnButtonClick;
public event EventHandler<InputMouseEventArgs> OnMouseClick;
BasicControls inputActions;
Vector2 mousePosition;
Vector2 mouseAxis;
Vector2 cameraMovementVector = Vector2.zero;
public float screenMouseX, screenMouseY;
public Vector2 mouseScroll;
Vector2 movementInput;
public RaycastHit InteractionClickedTarget { get; private set; }
public bool leftInteractionClickMultitapped = false;
public bool leftInteractionHold = false;
public Vector2 CameraMovementVector => cameraMovementVector;
public float right;
public float forward;
public float up;
public float movementAmount;
public bool boost;
public void Start()
{
if (_camera == null)
{
_camera = Camera.main;
}
mousePosition = Vector2.zero;
mouseAxis = Vector2.zero;
}
public void OnEnable()
{
if (inputActions == null)
{
inputActions = new BasicControls();
inputActions.Mouse.Interactions.started += Interactions_started;
inputActions.Mouse.Interactions.canceled += Interactions_canceled;
inputActions.Mouse.Interactions.performed += Interaction_performed;
inputActions.Mouse.MousePosition.performed += p => mousePosition = p.ReadValue<Vector2>();
inputActions.Mouse.CameraZoom.performed += i => mouseScroll = i.ReadValue<Vector2>();
inputActions.Controller.Quit.performed += i => QuitApplication?.Invoke(this, new EventArgs());
inputActions.Controller.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
inputActions.Controller.Thruster.performed += inputActions => up = inputActions.ReadValue<float>();
}
inputActions.Enable();
}
private void Interactions_canceled(InputAction.CallbackContext context)
{
leftInteractionHold = false;
Debug.Log("Interactions_canceled");
}
private void Interactions_started(InputAction.CallbackContext context)
{
Debug.Log("Interactions_started");
leftInteractionHold = false;
HoldAction(context);
}
public float getMouseScroll()
{
float mouseScrollValue = mouseScroll.normalized.y;
mouseScroll = Vector2.zero;
return mouseScrollValue;
}
public Vector2 GetMousePosition()
{
return mousePosition;
}
public float GetMouseAxisX()
{
return mouseAxis.x;
}
public float GetMouseAxisY()
{
return mouseAxis.y;
}
//These run only after Canceled event.
private void Interaction_performed(InputAction.CallbackContext context)
{
if (context.interaction is TapInteraction)
{
return;
}
if (context.interaction is MultiTapInteraction)
{
leftInteractionClickMultitapped = true;
return;
}
}
private void HoldAction(InputAction.CallbackContext context)
{
if (context.interaction is HoldInteraction)
{
Debug.Log("hold action");
cameraMovementVector = Vector2.zero;
leftInteractionHold = true;
}
}
private void OnDisable()
{
inputActions.Disable();
}
public void TickInput(float delta)
{
MoveInput(delta);
MoveCamera(delta);
boost = inputActions.Controller.Boost.IsPressed();
mouseAxis = inputActions.Mouse.MouseAxis.ReadValue<Vector2>();
if (inputActions.Mouse.Interactions.IsPressed())
{
Ray ray = _camera.ScreenPointToRay(mousePosition);
if (Physics.Raycast(ray, out RaycastHit hit))
{
InteractionClickedTarget = hit;
}
}
}
private void MoveInput(float delta)
{
right = movementInput.x;
forward = movementInput.y;
movementAmount = Mathf.Clamp01(Mathf.Abs(right) + Mathf.Abs(forward));
}
private void MoveCamera(float delta)
{
if (leftInteractionHold)
{
cameraMovementVector.x = mouseAxis.x;
cameraMovementVector.y = mouseAxis.y;
}
else
{
cameraMovementVector.x = 0;
cameraMovementVector.y = 0;
}
}
}
}