-
Notifications
You must be signed in to change notification settings - Fork 3
/
VRControllerInputModule.cs
277 lines (226 loc) · 10.5 KB
/
VRControllerInputModule.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
using UnityEngine;
using UnityEngine.EventSystems;
public class VRControllerInputModule : BaseInputModule
{
[Tooltip("A camera mounted on the controller")]
public Camera uiCamera;
[Tooltip("The threshold is a square length at which the cursor will begin drag. Lenght is measured in world coordinates.")]
public float dragThreshold = 0.1f;
// debug
public UnityEngine.UI.Text uiDebugText;
private bool m_useDebugText = false;
private string[] m_debugStrings = new string[5];
// debug
private bool m_isButtonPressed = false; // true if controller's button is currently pressed, false otherwise
private bool m_isButtonPressedChanged = false; // true if controller's button was pressed or released during the last frame
private float m_pressedDistance; // Distance the cursor travelled while pressed.
private Vector2 m_cameraCenter;
private Vector3 m_lastRaycastHitPoint;
private PointerEventData m_pointerEventData;
private bool m_isActive = false;
protected override void Start()
{
base.Start();
if (null != uiCamera)
{
m_isActive = true;
m_cameraCenter = new Vector2(uiCamera.pixelWidth / 2, uiCamera.pixelHeight / 2);
m_useDebugText = null != uiDebugText;
WriteDebug("Camera center: " + m_cameraCenter.ToString());
}
}
public override void Process()
{
if (m_isActive)
{
bool usedEvent = SendUpdateEventToSelectedObject();
MyUpdateControllerData();
ProcessControllerEvent();
}
}
private void MyUpdateControllerData()
{
m_isButtonPressedChanged = false;
if (m_isButtonPressed != VRInputManager.GetIsControllerButtonPressed())
{
m_isButtonPressedChanged = true;
m_isButtonPressed = VRInputManager.GetIsControllerButtonPressed();
}
}
private void ProcessControllerEvent()
{
PointerEventData eventData = GetPointerEventData();
ProcessPress(eventData);
ProcessMove(eventData);
ProcessDrag(eventData);
}
private PointerEventData GetPointerEventData()
{
// Currently the module is made for a single controller with one button.
// That means that we only have a single pointer.
if (null == m_pointerEventData)
m_pointerEventData = new PointerEventData(eventSystem);
if (VRInputManager.GetControllerActive())
{
m_pointerEventData.position = m_cameraCenter;
m_pointerEventData.scrollDelta = Vector2.zero;
m_pointerEventData.button = PointerEventData.InputButton.Left;
eventSystem.RaycastAll(m_pointerEventData, m_RaycastResultCache);
var raycast = FindFirstRaycast(m_RaycastResultCache);
// Delta is used to define if the cursor was moved.
// We will also use it for drag threshold calculation, for which we'll store world distance
// between the last and the current raycasts (will actually use sqrmagnitude for its speed).
Ray ray = new Ray(uiCamera.transform.position, uiCamera.transform.forward);
Vector3 hitPoint = ray.GetPoint(raycast.distance);
m_pointerEventData.delta = new Vector2((hitPoint - m_lastRaycastHitPoint).sqrMagnitude, 0);
m_lastRaycastHitPoint = hitPoint;
m_pointerEventData.pointerCurrentRaycast = raycast;
// Debug
if (m_RaycastResultCache.Count > 0)
WriteDebug("Raycast hit " + raycast.gameObject.name);
m_RaycastResultCache.Clear();
}
return m_pointerEventData;
}
// Copied from PointerInputModule
private void ProcessDrag(PointerEventData eventData)
{
WriteDebug(eventData.delta.sqrMagnitude.ToString());
// If pointer is not moving or if a button is not pressed (or pressed control did not return drag handler), do nothing
if (!eventData.IsPointerMoving() || eventData.pointerDrag == null)
return;
// We are eligible for drag. If drag did not start yet, add drag distance
if (!eventData.dragging)
{
m_pressedDistance += eventData.delta.x;
if (ShouldStartDrag(eventData))
{
ExecuteEvents.Execute(eventData.pointerDrag, eventData, ExecuteEvents.beginDragHandler);
eventData.dragging = true;
}
}
// Drag notification
if (eventData.dragging)
{
// Before doing drag we should cancel any pointer down state
// And clear selection!
if (eventData.pointerPress != eventData.pointerDrag)
{
ExecuteEvents.Execute(eventData.pointerPress, eventData, ExecuteEvents.pointerUpHandler);
eventData.eligibleForClick = false;
eventData.pointerPress = null;
eventData.rawPointerPress = null;
}
ExecuteEvents.Execute(eventData.pointerDrag, eventData, ExecuteEvents.dragHandler);
}
}
private bool ShouldStartDrag(PointerEventData eventData)
{
return !m_isButtonPressedChanged && (m_pressedDistance > dragThreshold);
}
// Copied from PointerInputModule
private void ProcessMove(PointerEventData eventData)
{
var targetGO = eventData.pointerCurrentRaycast.gameObject;
HandlePointerExitAndEnter(eventData, targetGO);
}
// modified StandaloneInputModule
private void ProcessPress(PointerEventData eventData)
{
var currentOverGo = eventData.pointerCurrentRaycast.gameObject;
// PointerDown notification
if (MyIsButtonPressedThisFrame())
{
eventData.eligibleForClick = true;
eventData.delta = Vector2.zero;
eventData.useDragThreshold = true;
eventData.pressPosition = eventData.position;
eventData.pointerPressRaycast = eventData.pointerCurrentRaycast;
DeselectIfSelectionChanged(currentOverGo, eventData);
var newPressed = ExecuteEvents.ExecuteHierarchy(currentOverGo, eventData, ExecuteEvents.pointerDownHandler);
// didnt find a press handler... search for a click handler
if (newPressed == null)
newPressed = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
eventData.pointerPress = newPressed; // TODO:remove?
m_pressedDistance = 0;
eventData.rawPointerPress = currentOverGo;
eventData.clickTime = Time.unscaledTime;
// Save the drag handler as well
eventData.pointerDrag = ExecuteEvents.GetEventHandler<IDragHandler>(currentOverGo);
if (eventData.pointerDrag != null)
ExecuteEvents.Execute(eventData.pointerDrag, eventData, ExecuteEvents.initializePotentialDrag);
}
// PointerUp notification
if (MyIsButtonReleasedThisFrame())
{
ExecuteEvents.Execute(eventData.pointerPress, eventData, ExecuteEvents.pointerUpHandler);
// see if we button up on the same element that we clicked on...
var pointerUpHandler = ExecuteEvents.GetEventHandler<IPointerClickHandler>(currentOverGo);
// PointerClick and Drop events
if (eventData.pointerPress == pointerUpHandler && eventData.eligibleForClick)
{
ExecuteEvents.Execute(eventData.pointerPress, eventData, ExecuteEvents.pointerClickHandler);
}
else if (eventData.pointerDrag != null && eventData.dragging)
{
ExecuteEvents.ExecuteHierarchy(currentOverGo, eventData, ExecuteEvents.dropHandler);
}
eventData.eligibleForClick = false;
eventData.pointerPress = null;
m_pressedDistance = 0; // just in case
eventData.rawPointerPress = null;
if (eventData.pointerDrag != null && eventData.dragging)
ExecuteEvents.Execute(eventData.pointerDrag, eventData, ExecuteEvents.endDragHandler);
eventData.dragging = false;
eventData.pointerDrag = null;
// redo pointer enter / exit to refresh state
// so that if we hovered over something that ignored it before
// due to having pressed on something else
// it now gets it.
if (currentOverGo != eventData.pointerEnter)
{
HandlePointerExitAndEnter(eventData, null);
HandlePointerExitAndEnter(eventData, currentOverGo);
}
}
}
// Copied from PointerInputModule
private void DeselectIfSelectionChanged(GameObject currentOverGo, BaseEventData pointerEvent)
{
// Selection tracking
var selectHandlerGO = ExecuteEvents.GetEventHandler<ISelectHandler>(currentOverGo);
// if we have clicked something new, deselect the old thing
// leave 'selection handling' up to the press event though.
if (selectHandlerGO != eventSystem.currentSelectedGameObject)
eventSystem.SetSelectedGameObject(null, pointerEvent);
}
// Copied from StandaloneInputModule
private bool SendUpdateEventToSelectedObject()
{
if (eventSystem.currentSelectedGameObject == null)
return false;
var data = GetBaseEventData();
ExecuteEvents.Execute(eventSystem.currentSelectedGameObject, data, ExecuteEvents.updateSelectedHandler);
return data.used;
}
private bool MyIsButtonReleasedThisFrame()
{
return (m_isButtonPressedChanged && !m_isButtonPressed);
}
private bool MyIsButtonPressedThisFrame()
{
return (m_isButtonPressedChanged && m_isButtonPressed);
}
// Debug
private void WriteDebug(string text)
{
if (!m_useDebugText)
return;
m_debugStrings[4] = m_debugStrings[3];
m_debugStrings[3] = m_debugStrings[2];
m_debugStrings[2] = m_debugStrings[1];
m_debugStrings[1] = m_debugStrings[0];
m_debugStrings[0] = text;
uiDebugText.text = string.Format("{0}\n{1}\n{2}\n{3}\n{4}", m_debugStrings[0], m_debugStrings[1], m_debugStrings[2], m_debugStrings[3], m_debugStrings[4]);
}
}