Skip to content

Commit

Permalink
FIX: Sync enable/disable system mouse dynamically.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmytro Ivanov committed Feb 14, 2023
1 parent 5ffb592 commit 4fb1a84
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
{
"name": "",
"id": "39af39e2-c75c-421e-84bf-1e4ff9793ab4",
"path": "<VirtualMouse>/position",
"path": "<Mouse>/position",
"interactions": "",
"processors": "",
"groups": "",
Expand All @@ -45,7 +45,7 @@
{
"name": "",
"id": "f9d5a98e-68a3-44c8-a096-a1a30237ca81",
"path": "<VirtualMouse>/leftButton",
"path": "<Mouse>/leftButton",
"interactions": "",
"processors": "",
"groups": "",
Expand All @@ -56,7 +56,7 @@
{
"name": "",
"id": "7ffd960c-11ef-4fc0-b91b-4cbd02eba1b8",
"path": "<VirtualMouse>/scroll",
"path": "<Mouse>/scroll",
"interactions": "",
"processors": "",
"groups": "",
Expand Down
9 changes: 5 additions & 4 deletions Assets/Samples/GamepadMouseCursor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ This sample demonstrates how to set this up with the input system.
1) It uses a custom [actions file](./GamepadMouseCursorUIActions.inputactions) for feeding input to the UI as the default actions are set up for gamepad navigation &ndash; something we don't want here as it would conflict with gamepad input being used for virtual cursor navigation.
2) Note how `InputSystemUIInputModule` on the `EventSystem` GameObject is set up to reference actions from that file.
3) The key component to take a look at is `VirtualMouseInput` on `Canvas >> Cursor`. The component is set up to receive input from the gamepad and translates it into motion on the `RectTransform` it is given. When going into play mode, you should also see a `Virtual Mouse` being added to the devices by the component.
4) Note how the anchor position on the `RectTransform` is set to bottom left. This way the coordinate system responds to how mouse screen space operates.
5) Note how `Cursor` is the last child of `Canvas` so that it draws on top of everything else.
6) Note that `Raycast Target` on the `Image` component of the cursor is turned off to avoid raycasts from the mouse cursor hitting the cursor itself.
7) Note that `Cursor Mode` on the `VirtualMouseInput` component is set to `Hardware Cursor If Available`. This will cause the component to look for a system mouse. If present, the system mouse is disabled and the system mouse cursor is warped to the virtual mouse position using `Mouse.WarpCursorPosition`. If no system mouse is present, `Cursor Graphic` will be used as a software mouse cursor.
4) Note how `Cursor` is the last child of `Canvas` so that it draws on top of everything else.
5) Note that `Raycast Target` on the `Image` component of the cursor is turned off to avoid raycasts from the mouse cursor hitting the cursor itself.
6) Note that `Cursor Mode` on the `VirtualMouseInput` component is set to `Hardware Cursor If Available`. This will cause the component to look for a system mouse. If present, the system mouse is disabled and the system mouse cursor is warped to the virtual mouse position using `Mouse.WarpCursorPosition`. If no system mouse is present, `Cursor Graphic` will be used as a software mouse cursor.

>NOTE: The `VirtualMouseInput` component currently does not work correctly with world-space canvases.
# Licenses

Expand Down
5 changes: 5 additions & 0 deletions Packages/com.unity.inputsystem/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ however, it has to be formatted properly to pass verification tests.
### Fixed

- Fixed unclosed profiler marker in `InvokeCallbacksSafe_AnyCallbackReturnsTrue` which would lead to eventually broken profiler traces in some cases like using `PlayerInput` (case ISXB-393).
- `VirtualMouseInput` not working correctly with `CanvasScaler` settings other than `ConstantPixelSize`. Fix contributed by [ad-walker](https://github.com/ad-walker) in [#1078](https://github.com/Unity-Technologies/InputSystem/pull/1078).

### Added

- Added an explicit `Disable System Mouse` setting to `VirtualMouseInput` to allow toggling the default behavior off which makes the component turn off the system mouse while it is enabled (so as to suppress the system mouse doubling the input activity on the VirtualMouse).

## [1.5.0] - 2023-01-24

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

////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)

Expand All @@ -14,6 +16,8 @@

////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
Expand Down Expand Up @@ -78,9 +82,10 @@ public float cursorSpeed
/// 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). 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.
/// 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>
Expand All @@ -100,7 +105,8 @@ public CursorMode cursorMode
// If we're turning it off, make sure we re-enable the system mouse.
if (m_CursorMode == CursorMode.HardwareCursorIfAvailable && m_SystemMouse != null)
{
InputSystem.EnableDevice(m_SystemMouse);
if (m_DisableSystemMouse)
InputSystem.EnableDevice(m_SystemMouse);
m_SystemMouse = null;
}

Expand Down Expand Up @@ -157,11 +163,51 @@ public float scrollSpeed
/// </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="Pointer.position"/> on
/// <see cref="virtualMouse"/> and the <a
/// href="https://docs.unity3d.com/ScriptReference/RectTransform-anchoredPosition.html">anchoredPosition</a>
/// on <see cref="cursorTransform"/> (if set).
/// <see cref="virtualMouse"/> and the position on <see cref="cursorTransform"/> (if set).
/// </summary>
/// <value>Stick input that drives cursor position.</value>
/// <remarks>
Expand Down Expand Up @@ -294,7 +340,7 @@ protected void OnEnable()
// Set initial cursor position.
if (m_CursorTransform != null)
{
var position = m_CursorTransform.anchoredPosition;
var position = m_CursorTransform.position;
InputState.Change(m_VirtualMouse.position, position);
m_SystemMouse?.WarpCursorPosition(position);
}
Expand Down Expand Up @@ -388,7 +434,8 @@ private void TryEnableHardwareCursor()
return;
}

InputSystem.DisableDevice(m_SystemMouse);
if (m_DisableSystemMouse)
InputSystem.DisableDevice(m_SystemMouse);

// Sync position.
if (m_VirtualMouse != null)
Expand Down Expand Up @@ -450,7 +497,7 @@ private void UpdateMotion()
if (m_CursorTransform != null &&
(m_CursorMode == CursorMode.SoftwareCursor ||
(m_CursorMode == CursorMode.HardwareCursorIfAvailable && m_SystemMouse == null)))
m_CursorTransform.anchoredPosition = newPosition;
m_CursorTransform.position = newPosition;

m_LastStickValue = stickValue;
m_LastTime = currentTime;
Expand All @@ -475,6 +522,11 @@ private void UpdateMotion()
[Tooltip("Whether the component should set the cursor position of the hardware mouse cursor, if one is available. If so, "
+ "the software cursor pointed (to by 'Cursor Graphic') will be hidden.")]
[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 graphic that represents the software cursor. This is hidden if a hardware cursor (see 'Cursor Mode') is used.")]
[SerializeField] private Graphic m_CursorGraphic;
[Tooltip("The transform for the software cursor. Will only be set if a software cursor is used (see 'Cursor Mode'). Moving the cursor "
Expand Down

0 comments on commit 4fb1a84

Please sign in to comment.