-
Notifications
You must be signed in to change notification settings - Fork 324
/
Copy pathVirtualMouseInput.cs
649 lines (576 loc) · 28.6 KB
/
VirtualMouseInput.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
using System;
using UnityEngine.InputSystem.LowLevel;
using UnityEngine.UI;
////TODO: respect cursor lock mode
////TODO: find a way to automatically turn the gamepad cursor on/off based on gamepad use
////TODO: investigate how driving the HW cursor behaves when FPS drops low
//// (also, maybe we can add support where we turn the gamepad mouse on and off automatically based on whether the system mouse is used)
////TODO: add support for acceleration
////TODO: automatically scale mouse speed to resolution such that it stays constant regardless of resolution
////TODO: make it work with PlayerInput such that it will automatically look up actions in the actual PlayerInput instance it is used with (based on the action IDs it has)
////FIXME: make this work with world-space canvases
////REVIEW: should we default the SW cursor position to the center of the screen?
////REVIEW: consider this for inclusion directly in the input system
namespace UnityEngine.InputSystem.UI
{
/// <summary>
/// A component that creates a virtual <see cref="Mouse"/> device and drives its input from gamepad-style inputs. This effectively
/// adds a software mouse cursor.
/// </summary>
/// <remarks>
/// This component can be used with UIs that are designed for mouse input, i.e. need to be operated with a cursor.
/// By hooking up the <see cref="InputAction"/>s of this component to gamepad input and directing <see cref="cursorTransform"/>
/// to the UI transform of the cursor, you can use this component to drive an on-screen cursor.
///
/// Note that this component does not actually trigger UI input itself. Instead, it creates a virtual <see cref="Mouse"/>
/// device which can then be picked up elsewhere (such as by <see cref="InputSystemUIInputModule"/>) where mouse/pointer input
/// is expected.
///
/// Also note that if there is a <see cref="Mouse"/> added by the platform, it is not impacted by this component. More specifically,
/// the system mouse cursor will not be moved or otherwise used by this component.
/// </remarks>
/// <seealso cref="Gamepad"/>
/// <seealso cref="Mouse"/>
[AddComponentMenu("Input/Virtual Mouse")]
public class VirtualMouseInput : MonoBehaviour
{
/// <summary>
/// Optional transform that will be updated to correspond to the current mouse position.
/// </summary>
/// <value>Transform to update with mouse position.</value>
/// <remarks>
/// This is useful for having a UI object that directly represents the mouse cursor. Simply add both the
/// <c>VirtualMouseInput</c> component and an <a href="https://docs.unity3d.com/Manual/script-Image.html">Image</a>
/// component and hook the <a href="https://docs.unity3d.com/ScriptReference/RectTransform.html">RectTransform</a>
/// component for the UI object into here. The object as a whole will then follow the generated mouse cursor
/// motion.
/// </remarks>
public RectTransform cursorTransform
{
get => m_CursorTransform;
set => m_CursorTransform = value;
}
/// <summary>
/// How many pixels per second the cursor travels in one axis when the respective axis from
/// <see cref="stickAction"/> is 1.
/// </summary>
/// <value>Mouse speed in pixels per second.</value>
public float cursorSpeed
{
get => m_CursorSpeed;
set => m_CursorSpeed = value;
}
/// <summary>
/// Determines which cursor representation to use. If this is set to <see cref="CursorMode.SoftwareCursor"/>
/// (the default), then <see cref="cursorGraphic"/> and <see cref="cursorTransform"/> define a software cursor
/// that is made to correspond to the position of <see cref="virtualMouse"/>. If this is set to <see
/// cref="CursorMode.HardwareCursorIfAvailable"/> and there is a native <see cref="Mouse"/> device present,
/// the component will take over that mouse device and disable it (so as for it to not also generate position
/// updates) except when this is explicitly disabled via <see cref="disableSystemMouse"/>. It will then use
/// <see cref="Mouse.WarpCursorPosition"/> to move the system mouse cursor to correspond to the position of the
/// <see cref="virtualMouse"/>. In this case, <see cref="cursorGraphic"/> will be disabled and
/// <see cref="cursorTransform"/> will not be updated.
/// </summary>
/// <value>Whether the system mouse cursor (if present) should be made to correspond with the virtual mouse position.</value>
/// <remarks>
/// Note that regardless of which mode is used for the cursor, mouse input is expected to be picked up from <see cref="virtualMouse"/>.
///
/// Note that if <see cref="CursorMode.HardwareCursorIfAvailable"/> is used, the software cursor is still used
/// if no native <see cref="Mouse"/> device is present.
/// </remarks>
public CursorMode cursorMode
{
get => m_CursorMode;
set
{
if (m_CursorMode == value)
return;
// If we're turning it off, make sure we re-enable the system mouse.
if (m_CursorMode == CursorMode.HardwareCursorIfAvailable && m_SystemMouse != null)
{
if (m_DisableSystemMouse)
InputSystem.EnableDevice(m_SystemMouse);
m_SystemMouse = null;
}
m_CursorMode = value;
if (m_CursorMode == CursorMode.HardwareCursorIfAvailable)
TryEnableHardwareCursor();
else if (m_CursorGraphic != null)
m_CursorGraphic.enabled = true;
}
}
/// <summary>
/// The UI graphic element that represents the mouse cursor.
/// </summary>
/// <value>Graphic element for the software mouse cursor.</value>
/// <remarks>
/// If <see cref="cursorMode"/> is set to <see cref="CursorMode.HardwareCursorIfAvailable"/>, this graphic will
/// be disabled.
///
/// Also, this UI component implicitly determines the <c>Canvas</c> that defines the screen area for the cursor.
/// The canvas that this graphic is on will be looked up using <c>GetComponentInParent</c> and then the <c>Canvas.pixelRect</c>
/// of the canvas is used as the bounds for the cursor motion range.
/// </remarks>
/// <seealso cref="CursorMode.SoftwareCursor"/>
public Graphic cursorGraphic
{
get => m_CursorGraphic;
set
{
m_CursorGraphic = value;
TryFindCanvas();
}
}
/// <summary>
/// Multiplier for values received from <see cref="scrollWheelAction"/>.
/// </summary>
/// <value>Multiplier for scroll values.</value>
public float scrollSpeed
{
get => m_ScrollSpeed;
set => m_ScrollSpeed = value;
}
/// <summary>
/// The virtual mouse device that the component feeds with input.
/// </summary>
/// <value>Instance of virtual mouse or <c>null</c>.</value>
/// <remarks>
/// This is only initialized after the component has been enabled for the first time. Note that
/// when subsequently disabling the component, the property will continue to return the mouse device
/// but the device will not be added to the system while the component is not enabled.
/// </remarks>
public Mouse virtualMouse => m_VirtualMouse;
/// <summary>
/// If <see cref="cursorMode"/> is set to <see cref="CursorMode.HardwareCursorIfAvailable"/>, whether to
/// disable the system mouse, if present. This is on by default.
/// </summary>
/// <value>If true, the system mouse will be disabled if present and while the VirtualMouseInput component is active.</value>
/// <remarks>
/// If the component drives the system mouse cursor rather than using a software cursor, the system mouse cursor position will
/// get warped around using <see cref="Mouse.WarpCursorPosition"/>. This, however, has the side-effect of generating input
/// on the system <see cref="Mouse"/> device. In other words, mouse motion from the gamepad will come in through both
/// the VirtualMouse created for the gamepad and the <see cref="Mouse"/> added by the system (with a one frame lag for
/// the latter).
///
/// To avoid this, the system mouse will by default get disabled while the VirtualMouseInput component is enabled. Thus,
/// the system <see cref="Mouse"/> will not receive input while the VirtualMouseInput component is enabled. The idea here
/// is that the application should determine when the gamepad is used and only turn on the gamepad cursor while this is
/// the case. One possible approach is to set up a control scheme for gamepads and enable the gamepad mouse cursor only
/// while the gamepad scheme is active.
///
/// However, by setting this property to false, the disabling of the system <see cref="Mouse"/> device can be suppressed.
/// This means the system mouse will stay fully functional. You will, however, see concurrent input on both the VirtualMouse
/// and the system mouse. This may, for example, interfere with automatic control scheme switching (which is why the disabling
/// is on by default).
/// </remarks>
public bool disableSystemMouse
{
get => m_DisableSystemMouse;
set
{
if (value == m_DisableSystemMouse)
return;
m_DisableSystemMouse = value;
if (m_SystemMouse != null)
{
if (value)
InputSystem.DisableDevice(m_SystemMouse);
else
InputSystem.EnableDevice(m_SystemMouse);
}
}
}
/// <summary>
/// The Vector2 stick input that drives the mouse cursor, i.e. <see cref="Mouse.position"/> on
/// <see cref="virtualMouse"/> and the position on <see cref="cursorTransform"/> (if set).
/// </summary>
/// <value>Stick input that drives cursor position.</value>
/// <remarks>
/// This should normally be bound to controls such as <see cref="Gamepad.leftStick"/> and/or
/// <see cref="Gamepad.rightStick"/>.
/// </remarks>
public InputActionProperty stickAction
{
get => m_StickAction;
set => SetAction(ref m_StickAction, value);
}
/// <summary>
/// Optional button input that determines when <see cref="Mouse.leftButton"/> is pressed on
/// <see cref="virtualMouse"/>.
/// </summary>
/// <value>Input for <see cref="Mouse.leftButton"/>.</value>
public InputActionProperty leftButtonAction
{
get => m_LeftButtonAction;
set
{
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_LeftButtonAction, m_ButtonActionTriggeredDelegate, false);
SetAction(ref m_LeftButtonAction, value);
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_LeftButtonAction, m_ButtonActionTriggeredDelegate, true);
}
}
/// <summary>
/// Optional button input that determines when <see cref="Mouse.rightButton"/> is pressed on
/// <see cref="virtualMouse"/>.
/// </summary>
/// <value>Input for <see cref="Mouse.rightButton"/>.</value>
public InputActionProperty rightButtonAction
{
get => m_RightButtonAction;
set
{
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_RightButtonAction, m_ButtonActionTriggeredDelegate, false);
SetAction(ref m_RightButtonAction, value);
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_RightButtonAction, m_ButtonActionTriggeredDelegate, true);
}
}
/// <summary>
/// Optional button input that determines when <see cref="Mouse.middleButton"/> is pressed on
/// <see cref="virtualMouse"/>.
/// </summary>
/// <value>Input for <see cref="Mouse.middleButton"/>.</value>
public InputActionProperty middleButtonAction
{
get => m_MiddleButtonAction;
set
{
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_MiddleButtonAction, m_ButtonActionTriggeredDelegate, false);
SetAction(ref m_MiddleButtonAction, value);
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_MiddleButtonAction, m_ButtonActionTriggeredDelegate, true);
}
}
/// <summary>
/// Optional button input that determines when <see cref="Mouse.forwardButton"/> is pressed on
/// <see cref="virtualMouse"/>.
/// </summary>
/// <value>Input for <see cref="Mouse.forwardButton"/>.</value>
public InputActionProperty forwardButtonAction
{
get => m_ForwardButtonAction;
set
{
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_ForwardButtonAction, m_ButtonActionTriggeredDelegate, false);
SetAction(ref m_ForwardButtonAction, value);
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_ForwardButtonAction, m_ButtonActionTriggeredDelegate, true);
}
}
/// <summary>
/// Optional button input that determines when <see cref="Mouse.forwardButton"/> is pressed on
/// <see cref="virtualMouse"/>.
/// </summary>
/// <value>Input for <see cref="Mouse.forwardButton"/>.</value>
public InputActionProperty backButtonAction
{
get => m_BackButtonAction;
set
{
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_BackButtonAction, m_ButtonActionTriggeredDelegate, false);
SetAction(ref m_BackButtonAction, value);
if (m_ButtonActionTriggeredDelegate != null)
SetActionCallback(m_BackButtonAction, m_ButtonActionTriggeredDelegate, true);
}
}
/// <summary>
/// Optional Vector2 value input that determines the value of <see cref="Mouse.scroll"/> on
/// <see cref="virtualMouse"/>.
/// </summary>
/// <value>Input for <see cref="Mouse.scroll"/>.</value>
/// <remarks>
/// In case you want to only bind vertical scrolling, simply have a <see cref="Composites.Vector2Composite"/>
/// with only <c>Up</c> and <c>Down</c> bound and <c>Left</c> and <c>Right</c> deleted or bound to nothing.
/// </remarks>
public InputActionProperty scrollWheelAction
{
get => m_ScrollWheelAction;
set => SetAction(ref m_ScrollWheelAction, value);
}
protected void OnEnable()
{
// Hijack system mouse, if enabled.
if (m_CursorMode == CursorMode.HardwareCursorIfAvailable)
TryEnableHardwareCursor();
// Add mouse device.
if (m_VirtualMouse == null)
m_VirtualMouse = (Mouse)InputSystem.AddDevice("VirtualMouse");
else if (!m_VirtualMouse.added)
InputSystem.AddDevice(m_VirtualMouse);
// Set initial cursor position.
if (m_CursorTransform != null)
{
var position = m_CursorTransform.position;
InputState.Change(m_VirtualMouse.position, position);
m_SystemMouse?.WarpCursorPosition(position);
}
// Hook into input update.
if (m_AfterInputUpdateDelegate == null)
m_AfterInputUpdateDelegate = OnAfterInputUpdate;
InputSystem.onAfterUpdate += m_AfterInputUpdateDelegate;
// Hook into actions.
if (m_ButtonActionTriggeredDelegate == null)
m_ButtonActionTriggeredDelegate = OnButtonActionTriggered;
SetActionCallback(m_LeftButtonAction, m_ButtonActionTriggeredDelegate, true);
SetActionCallback(m_RightButtonAction, m_ButtonActionTriggeredDelegate, true);
SetActionCallback(m_MiddleButtonAction, m_ButtonActionTriggeredDelegate, true);
SetActionCallback(m_ForwardButtonAction, m_ButtonActionTriggeredDelegate, true);
SetActionCallback(m_BackButtonAction, m_ButtonActionTriggeredDelegate, true);
// Enable actions.
m_StickAction.action?.Enable();
m_LeftButtonAction.action?.Enable();
m_RightButtonAction.action?.Enable();
m_MiddleButtonAction.action?.Enable();
m_ForwardButtonAction.action?.Enable();
m_BackButtonAction.action?.Enable();
m_ScrollWheelAction.action?.Enable();
}
protected void OnDisable()
{
// Remove mouse device.
if (m_VirtualMouse != null && m_VirtualMouse.added)
InputSystem.RemoveDevice(m_VirtualMouse);
// Let go of system mouse.
if (m_SystemMouse != null)
{
InputSystem.EnableDevice(m_SystemMouse);
m_SystemMouse = null;
}
// Remove ourselves from input update.
if (m_AfterInputUpdateDelegate != null)
InputSystem.onAfterUpdate -= m_AfterInputUpdateDelegate;
// Disable actions.
m_StickAction.action?.Disable();
m_LeftButtonAction.action?.Disable();
m_RightButtonAction.action?.Disable();
m_MiddleButtonAction.action?.Disable();
m_ForwardButtonAction.action?.Disable();
m_BackButtonAction.action?.Disable();
m_ScrollWheelAction.action?.Disable();
// Unhock from actions.
if (m_ButtonActionTriggeredDelegate != null)
{
SetActionCallback(m_LeftButtonAction, m_ButtonActionTriggeredDelegate, false);
SetActionCallback(m_RightButtonAction, m_ButtonActionTriggeredDelegate, false);
SetActionCallback(m_MiddleButtonAction, m_ButtonActionTriggeredDelegate, false);
SetActionCallback(m_ForwardButtonAction, m_ButtonActionTriggeredDelegate, false);
SetActionCallback(m_BackButtonAction, m_ButtonActionTriggeredDelegate, false);
}
m_LastTime = default;
m_LastStickValue = default;
}
private void TryFindCanvas()
{
m_Canvas = m_CursorGraphic?.GetComponentInParent<Canvas>();
}
private void TryEnableHardwareCursor()
{
var devices = InputSystem.devices;
for (var i = 0; i < devices.Count; ++i)
{
var device = devices[i];
if (device.native && device is Mouse mouse)
{
m_SystemMouse = mouse;
break;
}
}
if (m_SystemMouse == null)
{
if (m_CursorGraphic != null)
m_CursorGraphic.enabled = true;
return;
}
if (m_DisableSystemMouse)
InputSystem.DisableDevice(m_SystemMouse);
// Sync position.
if (m_VirtualMouse != null)
m_SystemMouse.WarpCursorPosition(m_VirtualMouse.position.ReadValue());
// Turn off mouse cursor image.
if (m_CursorGraphic != null)
m_CursorGraphic.enabled = false;
}
private void UpdateMotion()
{
if (m_VirtualMouse == null)
return;
// Read current stick value.
var stickAction = m_StickAction.action;
if (stickAction == null)
return;
var stickValue = stickAction.ReadValue<Vector2>();
if (Mathf.Approximately(0, stickValue.x) && Mathf.Approximately(0, stickValue.y))
{
// Motion has stopped.
m_LastTime = default;
m_LastStickValue = default;
}
else
{
var currentTime = InputState.currentTime;
if (Mathf.Approximately(0, m_LastStickValue.x) && Mathf.Approximately(0, m_LastStickValue.y))
{
// Motion has started.
m_LastTime = currentTime;
}
// Compute delta.
var deltaTime = (float)(currentTime - m_LastTime);
var delta = new Vector2(m_CursorSpeed * stickValue.x * deltaTime, m_CursorSpeed * stickValue.y * deltaTime);
// Update position.
var currentPosition = m_VirtualMouse.position.ReadValue();
var newPosition = currentPosition + delta;
////REVIEW: for the hardware cursor, clamp to something else?
// Clamp to canvas.
if (m_Canvas != null)
{
// Clamp to canvas.
var pixelRect = m_Canvas.pixelRect;
newPosition.x = Mathf.Clamp(newPosition.x, pixelRect.xMin, pixelRect.xMax);
newPosition.y = Mathf.Clamp(newPosition.y, pixelRect.yMin, pixelRect.yMax);
}
////REVIEW: the fact we have no events on these means that actions won't have an event ID to go by; problem?
InputState.Change(m_VirtualMouse.position, newPosition);
InputState.Change(m_VirtualMouse.delta, delta);
// Update software cursor transform, if any.
if (m_CursorTransform != null &&
(m_CursorMode == CursorMode.SoftwareCursor ||
(m_CursorMode == CursorMode.HardwareCursorIfAvailable && m_SystemMouse == null)))
m_CursorTransform.position = newPosition;
m_LastStickValue = stickValue;
m_LastTime = currentTime;
// Update hardware cursor.
m_SystemMouse?.WarpCursorPosition(newPosition);
}
// Update scroll wheel.
var scrollAction = m_ScrollWheelAction.action;
if (scrollAction != null)
{
var scrollValue = scrollAction.ReadValue<Vector2>();
scrollValue.x *= m_ScrollSpeed;
scrollValue.y *= m_ScrollSpeed;
InputState.Change(m_VirtualMouse.scroll, scrollValue);
}
}
[Header("Cursor")]
[Tooltip("Whether to use the system mouse cursor, if available. Or whether to use a pure software cursor only.")]
[SerializeField] private CursorMode m_CursorMode;
[Tooltip("With 'Cursor Mode' set to 'Hardware Cursor If Available', whether to disable the system mouse. The cursor warping that is used "
+ "to drive the HW cursor will generate input on the mouse device which mirrors the input on the virtual mouse. By enabling this flag, "
+ "this input can be suppressed by disabling the system mouse altogether. Note that this will make *no* input from the system mouse come "
+ "through while the VirtualMouseInput component is active.")]
[SerializeField] private bool m_DisableSystemMouse = true;
[Tooltip("The UI graphic that represents the software cursor.")]
[SerializeField] private Graphic m_CursorGraphic;
[Tooltip("The UI transform for the UI object that represents the software cursor.")]
[SerializeField] private RectTransform m_CursorTransform;
[Header("Motion")]
[SerializeField] private float m_CursorSpeed = 400;
[SerializeField] private float m_ScrollSpeed = 45;
[Space(10)]
[SerializeField] private InputActionProperty m_StickAction;
[SerializeField] private InputActionProperty m_LeftButtonAction;
[SerializeField] private InputActionProperty m_MiddleButtonAction;
[SerializeField] private InputActionProperty m_RightButtonAction;
[SerializeField] private InputActionProperty m_ForwardButtonAction;
[SerializeField] private InputActionProperty m_BackButtonAction;
[SerializeField] private InputActionProperty m_ScrollWheelAction;
private Canvas m_Canvas; // Canvas that gives the motion range for the software cursor.
private Mouse m_VirtualMouse;
private Mouse m_SystemMouse;
private Action m_AfterInputUpdateDelegate;
private Action<InputAction.CallbackContext> m_ButtonActionTriggeredDelegate;
private double m_LastTime;
private Vector2 m_LastStickValue;
private void OnButtonActionTriggered(InputAction.CallbackContext context)
{
if (m_VirtualMouse == null)
return;
// The button controls are bit controls. We can't (yet?) use InputState.Change to state
// the change of those controls as the state update machinery of InputManager only supports
// byte region updates. So we just grab the full state of our virtual mouse, then update
// the button in there and then simply overwrite the entire state.
var action = context.action;
MouseButton? button = null;
if (action == m_LeftButtonAction.action)
button = MouseButton.Left;
else if (action == m_RightButtonAction.action)
button = MouseButton.Right;
else if (action == m_MiddleButtonAction.action)
button = MouseButton.Middle;
else if (action == m_ForwardButtonAction.action)
button = MouseButton.Forward;
else if (action == m_BackButtonAction.action)
button = MouseButton.Back;
if (button != null)
{
var isPressed = context.control.IsPressed();
m_VirtualMouse.CopyState<MouseState>(out var mouseState);
mouseState.WithButton(button.Value, isPressed);
InputState.Change(m_VirtualMouse, mouseState);
}
}
private static void SetActionCallback(InputActionProperty field, Action<InputAction.CallbackContext> callback, bool install = true)
{
var action = field.action;
if (action == null)
return;
// We don't need the performed callback as our mouse buttons are binary and thus
// we only care about started (1) and canceled (0).
if (install)
{
action.started += callback;
action.canceled += callback;
}
else
{
action.started -= callback;
action.canceled -= callback;
}
}
private static void SetAction(ref InputActionProperty field, InputActionProperty value)
{
var oldValue = field;
field = value;
if (oldValue.reference == null)
{
var oldAction = oldValue.action;
if (oldAction != null && oldAction.enabled)
{
oldAction.Disable();
if (value.reference == null)
value.action?.Enable();
}
}
}
private void OnAfterInputUpdate()
{
UpdateMotion();
}
/// <summary>
/// Determines how the cursor for the virtual mouse is represented.
/// </summary>
/// <seealso cref="cursorMode"/>
public enum CursorMode
{
/// <summary>
/// The cursor is represented as a UI element. See <see cref="cursorGraphic"/>.
/// </summary>
SoftwareCursor,
/// <summary>
/// If a native <see cref="Mouse"/> device is present, its cursor will be used and driven
/// by the virtual mouse using <see cref="Mouse.WarpCursorPosition"/>. The software cursor
/// referenced by <see cref="cursorGraphic"/> will be disabled.
///
/// Note that if no native <see cref="Mouse"/> is present, behavior will fall back to
/// <see cref="SoftwareCursor"/>.
/// </summary>
HardwareCursorIfAvailable,
}
}
}