Search Unity

Virtual Mouse Input

Discussion in 'Input System' started by Nanorock, Mar 2, 2020.

  1. Nanorock

    Nanorock

    Joined:
    Dec 2, 2012
    Posts:
    41
    Hi,

    I'm trying to have a seamless mouse/gamepad cursor.
    I studied the virtual mouse from the last update (1.0.0.preview.5)
    While it works for gamepad cursor, the sample scene does not work for system mouse controlled cursor.
    So I tried to modify the Virtual Mouse to listen to the system mouse too if there is one.

    I could get the position to match mouse/gamepad and work:
    upload_2020-3-2_11-16-7.png
    (And I commented out the disabling of system mouse device)

    However I could not get the mouse clicks to pass to the VirtualMouse device.
    I tried the following:
    upload_2020-3-2_11-19-5.png

    But this throws the following error:
    Code (CSharp):
    1. ArgumentException: Cannot change state of bitfield control 'Button:/VirtualMouse/rightButton' using this method
    2. Parameter name: control
    3. UnityEngine.InputSystem.LowLevel.InputState.Change[TState] (UnityEngine.InputSystem.InputControl control, TState state, UnityEngine.InputSystem.LowLevel.InputUpdateType updateType, UnityEngine.InputSystem.LowLevel.InputEventPtr eventPtr) (at Library/PackageCache/com.unity.inputsystem@1.0.0-preview.5/InputSystem/State/InputState.cs:97)
    What would be the proper way to pass the system mouse various clicks events/state to this virtual device ?
     
    Last edited: Mar 2, 2020
  2. Nanorock

    Nanorock

    Joined:
    Dec 2, 2012
    Posts:
    41
    I did not study the code hard enough it seems !
    There is a section that describe this exact bugs which show how for now one can set the buttons.
    Code (CSharp):
    1. // The button controls are bit controls. We can't (yet?) use InputState.Change to state
    2. // the change of those controls as the state update machinery of InputManager only supports
    3. // byte region updates. So we just grab the full state of our virtual mouse, then update
    4. // the button in there and then simply overwrite the entire state.
    5. var isPressed = context.control.IsPressed();
    6. m_VirtualMouse.CopyState<MouseState>(out var mouseState);
    7. mouseState.WithButton(button.Value, isPressed);
    8. InputState.Change(m_VirtualMouse, mouseState);
    9.  
     
    Last edited: Mar 2, 2020
    burakkurkcu likes this.
  3. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    @Nanorock hey did you get this to work? could you post your code as an example to see the changes to the original? Thanks J.
     
  4. Nanorock

    Nanorock

    Joined:
    Dec 2, 2012
    Posts:
    41
    Sure, I add the script. It seamlessly use mouse or gamepad to move the virtual device.
    However this does not work yet on a Xbox build due to a bug from the package.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5. using UnityEngine.InputSystem.Composites;
    6. using UnityEngine.InputSystem.LowLevel;
    7. using UnityEngine.InputSystem.UI;
    8. using UnityEngine.UI;
    9.  
    10. ////TODO: investigate how driving the HW cursor behaves when FPS drops low
    11. ////      (also, maybe we can add support where we turn the gamepad mouse on and off automatically based on whether the system mouse is used)
    12. ////TODO: add support for acceleration
    13. ////TODO: automatically scale mouse speed to resolution such that it stays constant regardless of resolution
    14. ////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)
    15. ////REVIEW: consider this for inclusion directly in the input system
    16.  
    17. namespace App.Platforms
    18. {
    19.     /// <summary>
    20.     /// A component that creates a virtual <see cref="Mouse"/> device and drives its input from gamepad-style inputs. This effectively
    21.     /// adds a software mouse cursor.
    22.     /// </summary>
    23.     /// <remarks>
    24.     /// This component can be used with UIs that are designed for mouse input, i.e. need to be operated with a cursor.
    25.     /// By hooking up the <see cref="InputAction"/>s of this component to gamepad input and directing <see cref="cursorTransform"/>
    26.     /// to the UI transform of the cursor, you can use this component to drive an on-screen cursor.
    27.     ///
    28.     /// Note that this component does not actually trigger UI input itself. Instead, it creates a virtual <see cref="Mouse"/>
    29.     /// device which can then be picked up elsewhere (such as by <see cref="InputSystemUIInputModule"/>) where mouse/pointer input
    30.     /// is expected.
    31.     ///
    32.     /// Also note that if there is a <see cref="Mouse"/> added by the platform, it is not impacted by this component. More specifically,
    33.     /// the system mouse cursor will not be moved or otherwise used by this component.
    34.     /// </remarks>
    35.     /// <seealso cref="Gamepad"/>
    36.     /// <seealso cref="Mouse"/>
    37.     [AddComponentMenu("Input/Virtual Mouse")]
    38.     public class VirtualMouseInput : MonoBehaviour
    39.     {
    40.         /// <summary>
    41.         /// Optional transform that will be updated to correspond to the current mouse position.
    42.         /// </summary>
    43.         /// <value>Transform to update with mouse position.</value>
    44.         /// <remarks>
    45.         /// This is useful for having a UI object that directly represents the mouse cursor. Simply add both the
    46.         /// <c>VirtualMouseInput</c> component and an <a href="https://docs.unity3d.com/Manual/script-Image.html">Image</a>
    47.         /// component and hook the <a href="https://docs.unity3d.com/ScriptReference/RectTransform.html">RectTransform</a>
    48.         /// component for the UI object into here. The object as a whole will then follow the generated mouse cursor
    49.         /// motion.
    50.         /// </remarks>
    51.         public RectTransform cursorTransform
    52.         {
    53.             get => m_CursorTransform;
    54.             set => m_CursorTransform = value;
    55.         }
    56.  
    57.         /// <summary>
    58.         /// How many pixels per second the cursor travels in one axis when the respective axis from
    59.         /// <see cref="stickAction"/> is 1.
    60.         /// </summary>
    61.         /// <value>Mouse speed in pixels per second.</value>
    62.         public float cursorSpeed
    63.         {
    64.             get => m_CursorSpeed;
    65.             set => m_CursorSpeed = value;
    66.         }
    67.  
    68.         /// <summary>
    69.         /// Determines which cursor representation to use. If this is set to <see cref="CursorMode.SoftwareCursor"/>
    70.         /// (the default), then <see cref="cursorGraphic"/> and <see cref="cursorTransform"/> define a software cursor
    71.         /// that is made to correspond to the position of <see cref="virtualMouse"/>. If this is set to <see
    72.         /// cref="CursorMode.HardwareCursorIfAvailable"/> and there is a native <see cref="Mouse"/> device present,
    73.         /// the component will take over that mouse device and disable it (so as for it to not also generate position
    74.         /// updates). It will then use <see cref="Mouse.WarpCursorPosition"/> to move the system mouse cursor to
    75.         /// correspond to the position of the <see cref="virtualMouse"/>. In this case, <see cref="cursorGraphic"/>
    76.         /// will be disabled and <see cref="cursorTransform"/> will not be updated.
    77.         /// </summary>
    78.         /// <value>Whether the system mouse cursor (if present) should be made to correspond with the virtual mouse position.</value>
    79.         /// <remarks>
    80.         /// Note that regardless of which mode is used for the cursor, mouse input is expected to be picked up from <see cref="virtualMouse"/>.
    81.         ///
    82.         /// Note that if <see cref="CursorMode.HardwareCursorIfAvailable"/> is used, the software cursor is still used
    83.         /// if no native <see cref="Mouse"/> device is present.
    84.         /// </remarks>
    85.         public CursorMode cursorMode
    86.         {
    87.             get => m_CursorMode;
    88.             set
    89.             {
    90.                 if (m_CursorMode == value)
    91.                     return;
    92.  
    93.                 // If we're turning it off, make sure we re-enable the system mouse.
    94.                 if (m_CursorMode == CursorMode.HardwareCursorIfAvailable && m_SystemMouse != null)
    95.                 {
    96.                     InputSystem.EnableDevice(m_SystemMouse);
    97.                     m_SystemMouse = null;
    98.                 }
    99.  
    100.                 m_CursorMode = value;
    101.  
    102.                 if (m_CursorMode == CursorMode.HardwareCursorIfAvailable)
    103.                     TryEnableHardwareCursor();
    104.                 else if (m_CursorGraphic != null)
    105.                     m_CursorGraphic.enabled = true;
    106.             }
    107.         }
    108.  
    109.         /// <summary>
    110.         /// The UI graphic element that represents the mouse cursor.
    111.         /// </summary>
    112.         /// <value>Graphic element for the software mouse cursor.</value>
    113.         /// <remarks>
    114.         /// If <see cref="cursorMode"/> is set to <see cref="CursorMode.HardwareCursorIfAvailable"/>, this graphic will
    115.         /// be disabled.
    116.         ///
    117.         /// Also, this UI component implicitly determines the <c>Canvas</c> that defines the screen area for the cursor.
    118.         /// The canvas that this graphic is on will be looked up using <c>GetComponentInParent</c> and then the <c>Canvas.pixelRect</c>
    119.         /// of the canvas is used as the bounds for the cursor motion range.
    120.         /// </remarks>
    121.         /// <seealso cref="CursorMode.SoftwareCursor"/>
    122.         public Graphic cursorGraphic
    123.         {
    124.             get => m_CursorGraphic;
    125.             set
    126.             {
    127.                 m_CursorGraphic = value;
    128.                 TryFindCanvas();
    129.             }
    130.         }
    131.  
    132.         /// <summary>
    133.         /// Multiplier for values received from <see cref="scrollWheelAction"/>.
    134.         /// </summary>
    135.         /// <value>Multiplier for scroll values.</value>
    136.         public float scrollSpeed
    137.         {
    138.             get => m_ScrollSpeed;
    139.             set => m_ScrollSpeed = value;
    140.         }
    141.  
    142.         /// <summary>
    143.         /// The virtual mouse device that the component feeds with input.
    144.         /// </summary>
    145.         /// <value>Instance of virtual mouse or <c>null</c>.</value>
    146.         /// <remarks>
    147.         /// This is only initialized after the component has been enabled for the first time. Note that
    148.         /// when subsequently disabling the component, the property will continue to return the mouse device
    149.         /// but the device will not be added to the system while the component is not enabled.
    150.         /// </remarks>
    151.         public Mouse virtualMouse => m_VirtualMouse;
    152.  
    153.         /// <summary>
    154.         /// The Vector2 stick input that drives the mouse cursor, i.e. <see cref="Mouse.position"/> on
    155.         /// <see cref="virtualMouse"/> and the <a
    156.         /// href="https://docs.unity3d.com/ScriptReference/RectTransform-anchoredPosition.html">anchoredPosition</a>
    157.         /// on <see cref="cursorTransform"/> (if set).
    158.         /// </summary>
    159.         /// <value>Stick input that drives cursor position.</value>
    160.         /// <remarks>
    161.         /// This should normally be bound to controls such as <see cref="Gamepad.leftStick"/> and/or
    162.         /// <see cref="Gamepad.rightStick"/>.
    163.         /// </remarks>
    164.         public InputActionProperty stickAction
    165.         {
    166.             get => m_StickAction;
    167.             set => SetAction(ref m_StickAction, value);
    168.         }
    169.  
    170.         /// <summary>
    171.         /// Optional button input that determines when <see cref="Mouse.leftButton"/> is pressed on
    172.         /// <see cref="virtualMouse"/>.
    173.         /// </summary>
    174.         /// <value>Input for <see cref="Mouse.leftButton"/>.</value>
    175.         public InputActionProperty leftButtonAction
    176.         {
    177.             get => m_LeftButtonAction;
    178.             set
    179.             {
    180.                 if (m_ButtonActionTriggeredDelegate != null)
    181.                     SetActionCallback(m_LeftButtonAction, m_ButtonActionTriggeredDelegate, false);
    182.                 SetAction(ref m_LeftButtonAction, value);
    183.                 if (m_ButtonActionTriggeredDelegate != null)
    184.                     SetActionCallback(m_LeftButtonAction, m_ButtonActionTriggeredDelegate, true);
    185.             }
    186.         }
    187.  
    188.         /// <summary>
    189.         /// Optional button input that determines when <see cref="Mouse.rightButton"/> is pressed on
    190.         /// <see cref="virtualMouse"/>.
    191.         /// </summary>
    192.         /// <value>Input for <see cref="Mouse.rightButton"/>.</value>
    193.         public InputActionProperty rightButtonAction
    194.         {
    195.             get => m_RightButtonAction;
    196.             set
    197.             {
    198.                 if (m_ButtonActionTriggeredDelegate != null)
    199.                     SetActionCallback(m_RightButtonAction, m_ButtonActionTriggeredDelegate, false);
    200.                 SetAction(ref m_RightButtonAction, value);
    201.                 if (m_ButtonActionTriggeredDelegate != null)
    202.                     SetActionCallback(m_RightButtonAction, m_ButtonActionTriggeredDelegate, true);
    203.             }
    204.         }
    205.  
    206.         /// <summary>
    207.         /// Optional button input that determines when <see cref="Mouse.middleButton"/> is pressed on
    208.         /// <see cref="virtualMouse"/>.
    209.         /// </summary>
    210.         /// <value>Input for <see cref="Mouse.middleButton"/>.</value>
    211.         public InputActionProperty middleButtonAction
    212.         {
    213.             get => m_MiddleButtonAction;
    214.             set
    215.             {
    216.                 if (m_ButtonActionTriggeredDelegate != null)
    217.                     SetActionCallback(m_MiddleButtonAction, m_ButtonActionTriggeredDelegate, false);
    218.                 SetAction(ref m_MiddleButtonAction, value);
    219.                 if (m_ButtonActionTriggeredDelegate != null)
    220.                     SetActionCallback(m_MiddleButtonAction, m_ButtonActionTriggeredDelegate, true);
    221.             }
    222.         }
    223.  
    224.         /// <summary>
    225.         /// Optional button input that determines when <see cref="Mouse.forwardButton"/> is pressed on
    226.         /// <see cref="virtualMouse"/>.
    227.         /// </summary>
    228.         /// <value>Input for <see cref="Mouse.forwardButton"/>.</value>
    229.         public InputActionProperty forwardButtonAction
    230.         {
    231.             get => m_ForwardButtonAction;
    232.             set
    233.             {
    234.                 if (m_ButtonActionTriggeredDelegate != null)
    235.                     SetActionCallback(m_ForwardButtonAction, m_ButtonActionTriggeredDelegate, false);
    236.                 SetAction(ref m_ForwardButtonAction, value);
    237.                 if (m_ButtonActionTriggeredDelegate != null)
    238.                     SetActionCallback(m_ForwardButtonAction, m_ButtonActionTriggeredDelegate, true);
    239.             }
    240.         }
    241.  
    242.         /// <summary>
    243.         /// Optional button input that determines when <see cref="Mouse.forwardButton"/> is pressed on
    244.         /// <see cref="virtualMouse"/>.
    245.         /// </summary>
    246.         /// <value>Input for <see cref="Mouse.forwardButton"/>.</value>
    247.         public InputActionProperty backButtonAction
    248.         {
    249.             get => m_BackButtonAction;
    250.             set
    251.             {
    252.                 if (m_ButtonActionTriggeredDelegate != null)
    253.                     SetActionCallback(m_BackButtonAction, m_ButtonActionTriggeredDelegate, false);
    254.                 SetAction(ref m_BackButtonAction, value);
    255.                 if (m_ButtonActionTriggeredDelegate != null)
    256.                     SetActionCallback(m_BackButtonAction, m_ButtonActionTriggeredDelegate, true);
    257.             }
    258.         }
    259.  
    260.         /// <summary>
    261.         /// Optional Vector2 value input that determines the value of <see cref="Mouse.scroll"/> on
    262.         /// <see cref="virtualMouse"/>.
    263.         /// </summary>
    264.         /// <value>Input for <see cref="Mouse.scroll"/>.</value>
    265.         /// <remarks>
    266.         /// In case you want to only bind vertical scrolling, simply have a <see cref="Vector2Composite"/>
    267.         /// with only <c>Up</c> and <c>Down</c> bound and <c>Left</c> and <c>Right</c> deleted or bound to nothing.
    268.         /// </remarks>
    269.         public InputActionProperty scrollWheelAction
    270.         {
    271.             get => m_ScrollWheelAction;
    272.             set => SetAction(ref m_ScrollWheelAction, value);
    273.         }
    274.  
    275.         // IEnumerator DelayEventToggle()
    276.         // {
    277.         //     yield return new WaitForSeconds(0.5f);
    278.         //     var input = transform.parent.parent.GetComponent<InputSystemUIInputModule>();
    279.         //     input.DeactivateModule();
    280.         //     yield return new WaitForSeconds(0.1f);
    281.         //     input.ActivateModule();
    282.         // }
    283.        
    284.         protected void OnEnable()
    285.         {
    286.             //StartCoroutine(DelayEventToggle());
    287.            
    288.             // Hijack system mouse, if enabled.
    289.             if (m_CursorMode == CursorMode.HardwareCursorIfAvailable)
    290.                 TryEnableHardwareCursor();
    291.            
    292.             // Add mouse device.
    293.             if (m_VirtualMouse == null)
    294.                 m_VirtualMouse = (Mouse)InputSystem.AddDevice("VirtualMouse");
    295.             else if (!m_VirtualMouse.added)
    296.                 InputSystem.AddDevice(m_VirtualMouse);
    297.  
    298.             // Set initial cursor position.
    299.             if (m_CursorTransform != null)
    300.             {
    301.                 var position = m_CursorTransform.anchoredPosition;
    302.                 InputState.Change(m_VirtualMouse.position, position);
    303.                 m_SystemMouse?.WarpCursorPosition(position);
    304.             }
    305.  
    306.             // Hook into input update.
    307.             if (m_AfterInputUpdateDelegate == null)
    308.                 m_AfterInputUpdateDelegate = OnAfterInputUpdate;
    309.             InputSystem.onAfterUpdate += m_AfterInputUpdateDelegate;
    310.  
    311.             // Hook into actions.
    312.             if (m_ButtonActionTriggeredDelegate == null)
    313.                 m_ButtonActionTriggeredDelegate = OnButtonActionTriggered;
    314.             SetActionCallback(m_LeftButtonAction, m_ButtonActionTriggeredDelegate, true);
    315.             SetActionCallback(m_RightButtonAction, m_ButtonActionTriggeredDelegate, true);
    316.             SetActionCallback(m_MiddleButtonAction, m_ButtonActionTriggeredDelegate, true);
    317.             SetActionCallback(m_ForwardButtonAction, m_ButtonActionTriggeredDelegate, true);
    318.             SetActionCallback(m_BackButtonAction, m_ButtonActionTriggeredDelegate, true);
    319.  
    320.             // Enable actions.
    321.             m_StickAction.action?.Enable();
    322.             m_LeftButtonAction.action?.Enable();
    323.             m_RightButtonAction.action?.Enable();
    324.             m_MiddleButtonAction.action?.Enable();
    325.             m_ForwardButtonAction.action?.Enable();
    326.             m_BackButtonAction.action?.Enable();
    327.             m_ScrollWheelAction.action?.Enable();
    328.         }
    329.  
    330.         protected void OnDisable()
    331.         {
    332.             // Remove mouse device.
    333.             if (m_VirtualMouse != null && m_VirtualMouse.added)
    334.                 InputSystem.RemoveDevice(m_VirtualMouse);
    335.  
    336.             // Let go of system mouse.
    337.             if (m_SystemMouse != null)
    338.             {
    339.                 InputSystem.EnableDevice(m_SystemMouse);
    340.                 m_SystemMouse = null;
    341.             }
    342.  
    343.             // Remove ourselves from input update.
    344.             if (m_AfterInputUpdateDelegate != null)
    345.                 InputSystem.onAfterUpdate -= m_AfterInputUpdateDelegate;
    346.  
    347.             // Disable actions.
    348.             m_StickAction.action?.Disable();
    349.             m_LeftButtonAction.action?.Disable();
    350.             m_RightButtonAction.action?.Disable();
    351.             m_MiddleButtonAction.action?.Disable();
    352.             m_ForwardButtonAction.action?.Disable();
    353.             m_BackButtonAction.action?.Disable();
    354.             m_ScrollWheelAction.action?.Disable();
    355.  
    356.             // Unhock from actions.
    357.             if (m_ButtonActionTriggeredDelegate != null)
    358.             {
    359.                 SetActionCallback(m_LeftButtonAction, m_ButtonActionTriggeredDelegate, false);
    360.                 SetActionCallback(m_RightButtonAction, m_ButtonActionTriggeredDelegate, false);
    361.                 SetActionCallback(m_MiddleButtonAction, m_ButtonActionTriggeredDelegate, false);
    362.                 SetActionCallback(m_ForwardButtonAction, m_ButtonActionTriggeredDelegate, false);
    363.                 SetActionCallback(m_BackButtonAction, m_ButtonActionTriggeredDelegate, false);
    364.             }
    365.  
    366.             m_LastTime = default;
    367.             m_LastStickValue = default;
    368.         }
    369.  
    370.         private void TryFindCanvas()
    371.         {
    372.             m_Canvas = m_CursorGraphic?.GetComponentInParent<Canvas>();
    373.         }
    374.  
    375.         private void TryEnableHardwareCursor()
    376.         {
    377.             var devices = InputSystem.devices;
    378.             for (var i = 0; i < devices.Count; ++i)
    379.             {
    380.                 var device = devices[i];
    381.                 if (device.native && device is Mouse mouse)
    382.                 {
    383.                     m_SystemMouse = mouse;
    384.                     break;
    385.                 }
    386.             }
    387.  
    388.             if (m_SystemMouse == null)
    389.             {
    390.                 if (m_CursorGraphic != null)
    391.                     m_CursorGraphic.enabled = true;
    392.                 return;
    393.             }
    394.  
    395.             //InputSystem.DisableDevice(m_SystemMouse);
    396.  
    397.             // Sync position.
    398.             if (m_VirtualMouse != null)
    399.                 m_SystemMouse.WarpCursorPosition(m_VirtualMouse.position.ReadValue());
    400.  
    401.             // Turn off mouse cursor image.
    402.             if (m_CursorGraphic != null)
    403.                 m_CursorGraphic.enabled = false;
    404.         }
    405.  
    406.         void UpdateMotion()
    407.         {
    408.             if (m_VirtualMouse == null)
    409.                 return;
    410.            
    411.             if (m_VirtualMouse != null && m_SystemMouse != null)
    412.             {
    413.                 var cursorPos = m_SystemMouse.position.ReadValue();
    414.                 var cursorDt = m_SystemMouse.delta.ReadValue();
    415.                
    416.                 InputState.Change(m_VirtualMouse.position, cursorPos);
    417.                 InputState.Change(m_VirtualMouse.delta, cursorDt);
    418.                
    419.                 // Update software cursor transform, if any.
    420.                 if (m_CursorTransform != null)
    421.                     m_CursorTransform.anchoredPosition = cursorPos;
    422.             }
    423.             // Read current stick value.
    424.             var stickAction = m_StickAction.action;
    425.             if (stickAction == null)
    426.                 return;
    427.             var stickValue = stickAction.ReadValue<Vector2>();
    428.             if (Mathf.Approximately(0, stickValue.x) && Mathf.Approximately(0, stickValue.y))
    429.             {
    430.                 // Motion has stopped.
    431.                 m_LastTime = default;
    432.                 m_LastStickValue = default;
    433.             }
    434.             else
    435.             {
    436.                 var currentTime = InputState.currentTime;
    437.                 if (Mathf.Approximately(0, m_LastStickValue.x) && Mathf.Approximately(0, m_LastStickValue.y))
    438.                 {
    439.                     // Motion has started.
    440.                     m_LastTime = currentTime;
    441.                 }
    442.  
    443.                 // Compute delta.
    444.                 var deltaTime = (float)(currentTime - m_LastTime);
    445.                 var delta = new Vector2(m_CursorSpeed * stickValue.x * deltaTime, m_CursorSpeed * stickValue.y * deltaTime);
    446.  
    447.                 // Update position.
    448.                 var currentPosition = m_VirtualMouse.position.ReadValue();
    449.                 var newPosition = currentPosition + delta;
    450.  
    451.                 ////REVIEW: for the hardware cursor, clamp to something else?
    452.                 // Clamp to canvas.
    453.                 if (m_Canvas != null)
    454.                 {
    455.                     // Clamp to canvas.
    456.                     var pixelRect = m_Canvas.pixelRect;
    457.                     newPosition.x = Mathf.Clamp(newPosition.x, pixelRect.xMin, pixelRect.xMax);
    458.                     newPosition.y = Mathf.Clamp(newPosition.y, pixelRect.yMin, pixelRect.yMax);
    459.                 }
    460.  
    461.                 ////REVIEW: the fact we have no events on these means that actions won't have an event ID to go by; problem?
    462.                 InputState.Change(m_VirtualMouse.position, newPosition);
    463.                 InputState.Change(m_VirtualMouse.delta, delta);
    464.  
    465.                 InputState.Change(m_SystemMouse.position, newPosition);
    466.                 InputState.Change(m_SystemMouse.delta, delta);
    467.                
    468.                 // Update software cursor transform, if any.
    469.                 if (m_CursorTransform != null && m_CursorMode == CursorMode.SoftwareCursor)
    470.                     m_CursorTransform.anchoredPosition = newPosition;
    471.  
    472.                 m_LastStickValue = stickValue;
    473.                 m_LastTime = currentTime;
    474.  
    475.                 // Update hardware cursor.
    476.                 m_SystemMouse?.WarpCursorPosition(newPosition);
    477.             }
    478.  
    479.             // Update scroll wheel.
    480.             var scrollAction = m_ScrollWheelAction.action;
    481.             if (scrollAction != null)
    482.             {
    483.                 var scrollValue = scrollAction.ReadValue<Vector2>();
    484.                 scrollValue.x *= m_ScrollSpeed;
    485.                 scrollValue.y *= m_ScrollSpeed;
    486.  
    487.                 InputState.Change(m_VirtualMouse.scroll, scrollValue);
    488.             }
    489.            
    490.             if (m_SystemMouse != null)
    491.             {
    492.                 var mouseRightButton = m_SystemMouse.rightButton.isPressed;
    493.                 mouseRightButton = mouseRightButton || rightButtonAction.action.triggered;
    494.                 AddButtonToVirtualDevice(MouseButton.Right, mouseRightButton);
    495.  
    496.                 var mouseLeftButton = m_SystemMouse.leftButton.isPressed;
    497.                 mouseLeftButton = mouseLeftButton || leftButtonAction.action.triggered;
    498.                 AddButtonToVirtualDevice(MouseButton.Left, mouseLeftButton);
    499.             }
    500.            
    501.         }
    502.  
    503.         [Header("Cursor")]
    504.         [SerializeField] private CursorMode m_CursorMode;
    505.         [SerializeField] private Graphic m_CursorGraphic;
    506.         [SerializeField] private RectTransform m_CursorTransform;
    507.  
    508.         [Header("Motion")]
    509.         [SerializeField] private float m_CursorSpeed = 400;
    510.         [SerializeField] private float m_ScrollSpeed = 45;
    511.  
    512.         [Space(10)]
    513.         [SerializeField] private InputActionProperty m_StickAction;
    514.         [SerializeField] private InputActionProperty m_LeftButtonAction;
    515.         [SerializeField] private InputActionProperty m_MiddleButtonAction;
    516.         [SerializeField] private InputActionProperty m_RightButtonAction;
    517.         [SerializeField] private InputActionProperty m_ForwardButtonAction;
    518.         [SerializeField] private InputActionProperty m_BackButtonAction;
    519.         [SerializeField] private InputActionProperty m_ScrollWheelAction;
    520.  
    521.         private Canvas m_Canvas; // Canvas that gives the motion range for the software cursor.
    522.         private Mouse m_VirtualMouse;
    523.         private Mouse m_SystemMouse;
    524.         private Action m_AfterInputUpdateDelegate;
    525.         private Action<InputAction.CallbackContext> m_ButtonActionTriggeredDelegate;
    526.         private double m_LastTime;
    527.         private Vector2 m_LastStickValue;
    528.  
    529.         private void OnButtonActionTriggered(InputAction.CallbackContext context)
    530.         {
    531.             if (m_VirtualMouse == null)
    532.                 return;
    533.  
    534.             // The button controls are bit controls. We can't (yet?) use InputState.Change to state
    535.             // the change of those controls as the state update machinery of InputManager only supports
    536.             // byte region updates. So we just grab the full state of our virtual mouse, then update
    537.             // the button in there and then simply overwrite the entire state.
    538.  
    539.             var action = context.action;
    540.             MouseButton? button = null;
    541.             if (action == m_LeftButtonAction.action)
    542.                 button = MouseButton.Left;
    543.             else if (action == m_RightButtonAction.action)
    544.                 button = MouseButton.Right;
    545.             else if (action == m_MiddleButtonAction.action)
    546.                 button = MouseButton.Middle;
    547.             else if (action == m_ForwardButtonAction.action)
    548.                 button = MouseButton.Forward;
    549.             else if (action == m_BackButtonAction.action)
    550.                 button = MouseButton.Back;
    551.  
    552.             if (button != null)
    553.             {
    554.                 var isPressed = context.control.IsPressed();
    555.                 AddButtonToVirtualDevice(button, isPressed);
    556.             }
    557.         }
    558.  
    559.         void AddButtonToVirtualDevice(MouseButton? button, bool isPressed)
    560.         {
    561.             if (m_VirtualMouse == null || !button.HasValue)
    562.                 return;
    563.  
    564.             // The button controls are bit controls. We can't (yet?) use InputState.Change to state
    565.             // the change of those controls as the state update machinery of InputManager only supports
    566.             // byte region updates. So we just grab the full state of our virtual mouse, then update
    567.             // the button in there and then simply overwrite the entire state.
    568.  
    569.             m_VirtualMouse.CopyState<MouseState>(out var mouseState);
    570.             mouseState.WithButton(button.Value, isPressed);
    571.  
    572.             InputState.Change(m_VirtualMouse, mouseState);
    573.  
    574.         }
    575.  
    576.         private static void SetActionCallback(InputActionProperty field, Action<InputAction.CallbackContext> callback, bool install = true)
    577.         {
    578.             var action = field.action;
    579.             if (action == null)
    580.                 return;
    581.  
    582.             // We don't need the performed callback as our mouse buttons are binary and thus
    583.             // we only care about started (1) and canceled (0).
    584.  
    585.             if (install)
    586.             {
    587.                 action.started += callback;
    588.                 action.canceled += callback;
    589.             }
    590.             else
    591.             {
    592.                 action.started -= callback;
    593.                 action.canceled -= callback;
    594.             }
    595.         }
    596.  
    597.         private static void SetAction(ref InputActionProperty field, InputActionProperty value)
    598.         {
    599.             var oldValue = field;
    600.             field = value;
    601.  
    602.             if (oldValue.reference == null)
    603.             {
    604.                 var oldAction = oldValue.action;
    605.                 if (oldAction != null && oldAction.enabled)
    606.                 {
    607.                     oldAction.Disable();
    608.                     if (value.reference == null)
    609.                         value.action?.Enable();
    610.                 }
    611.             }
    612.         }
    613.  
    614.         private void OnAfterInputUpdate()
    615.         {
    616.             UpdateMotion();
    617.         }
    618.  
    619.         /// <summary>
    620.         /// Determines how the cursor for the virtual mouse is represented.
    621.         /// </summary>
    622.         /// <seealso cref="cursorMode"/>
    623.         public enum CursorMode
    624.         {
    625.             /// <summary>
    626.             /// The cursor is represented as a UI element. See <see cref="cursorGraphic"/>.
    627.             /// </summary>
    628.             SoftwareCursor,
    629.  
    630.             /// <summary>
    631.             /// If a native <see cref="Mouse"/> device is present, its cursor will be used and driven
    632.             /// by the virtual mouse using <see cref="Mouse.WarpCursorPosition"/>. The software cursor
    633.             /// referenced by <see cref="cursorGraphic"/> will be disabled.
    634.             ///
    635.             /// Note that if no native <see cref="Mouse"/> is present, behavior will fall back to
    636.             /// <see cref="SoftwareCursor"/>.
    637.             /// </summary>
    638.             HardwareCursorIfAvailable,
    639.         }
    640.     }
    641. }
    642.  
     
    Sarahnade and jister like this.
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    ah thanks good to know, I tested it on PS4 and assumed that if it works there, it would work on XBox1
    I'll test on XBox1 and see if i can find a fix and post it here.
    also I had to do some checks to see which one is active, i see yours is different. Will try your approach to see the difference
    Code (CSharp):
    1. private void UpdateMotion()
    2.         {
    3.             if (m_VirtualMouse == null)
    4.                 return;
    5.            
    6.             // Read current stick value.
    7.             var stickAction = m_StickAction.action;
    8.             if (stickAction == null)
    9.                 return;
    10.             var stickValue = stickAction.ReadValue<Vector2>();
    11.             if (Mathf.Approximately(0, stickValue.x) && Mathf.Approximately(0, stickValue.y))
    12.             {
    13.                 // Motion has stopped.
    14.                 m_LastTime = default;
    15.                 m_LastStickValue = default;
    16.                 if (m_SystemMouse != null && m_SystemMouse.delta.ReadValue() != Vector2.zero)
    17.                 {
    18.                     var cursorPos = m_SystemMouse.position.ReadValue();
    19.                     var cursorDt = m_SystemMouse.delta.ReadValue();
    20.                     InputState.Change(m_VirtualMouse.position, cursorPos);
    21.                     InputState.Change(m_VirtualMouse.delta, cursorDt);
    22.  
    23.                     if (m_CursorTransform != null)
    24.                         m_CursorTransform.anchoredPosition = cursorPos;
    25.                 }
    26.             }
    27.             else
    28.             {
    29.                 var currentTime = InputState.currentTime;
    30.                 if (Mathf.Approximately(0, m_LastStickValue.x) && Mathf.Approximately(0, m_LastStickValue.y))
    31.                 {
    32.                     // Motion has started.
    33.                     m_LastTime = currentTime;
    34.                 }
    35.  
    36.                 // Compute delta.
    37.                 var deltaTime = (float)(currentTime - m_LastTime);
    38.                 var delta = new Vector2(m_CursorSpeed * stickValue.x * deltaTime, m_CursorSpeed * stickValue.y * deltaTime);
    39.  
    40.                 // Update position.
    41.                 var currentPosition = m_VirtualMouse.position.ReadValue();
    42.                 var newPosition = currentPosition + delta;
    43.  
    44.                 ////REVIEW: for the hardware cursor, clamp to something else?
    45.                 // Clamp to canvas.
    46.                 if (m_Canvas != null)
    47.                 {
    48.                     // Clamp to canvas.
    49.                     var pixelRect = m_Canvas.pixelRect;
    50.                     newPosition.x = Mathf.Clamp(newPosition.x, pixelRect.xMin, pixelRect.xMax);
    51.                     newPosition.y = Mathf.Clamp(newPosition.y, pixelRect.yMin, pixelRect.yMax);
    52.                 }
    53.  
    54.                 ////REVIEW: the fact we have no events on these means that actions won't have an event ID to go by; problem?
    55.                 InputState.Change(m_VirtualMouse.position, newPosition);
    56.                 InputState.Change(m_VirtualMouse.delta, delta);
    57.  
    58.                 // Update software cursor transform, if any.
    59.                 if (m_CursorTransform != null && m_CursorMode == CursorMode.SoftwareCursor)
    60.                     m_CursorTransform.anchoredPosition = newPosition;
    61.  
    62.                 m_LastStickValue = stickValue;
    63.                 m_LastTime = currentTime;
    64.  
    65.                 // Update hardware cursor.
    66.                 m_SystemMouse?.WarpCursorPosition(newPosition);
    67.                 s_CursorPosition = newPosition;
    68.             }
    69.  
    70.             // Update scroll wheel.
    71.             var scrollAction = m_ScrollWheelAction.action;
    72.             if (scrollAction != null)
    73.             {
    74.                 var scrollValue = scrollAction.ReadValue<Vector2>();
    75.                 scrollValue.x *= m_ScrollSpeed;
    76.                 scrollValue.y *= m_ScrollSpeed;
    77.  
    78.                 InputState.Change(m_VirtualMouse.scroll, scrollValue);
    79.             }
    80.         }
     
    Last edited: Mar 5, 2020
  6. astearon

    astearon

    Joined:
    May 9, 2016
    Posts:
    18
    Id like to do something similar to this, I want to have a virtual touchpad that I can drive with the new input system with a /touch*/position event, would you have any pointers for me ?
    I initially wanted to modify the already made on-screen stick but that works like an actual joystick. Once I stop moving my finger it will not reset the joystick's origin to my current position so I'd have to move back to the initial center then in the direction i actually want to move in order to achieve the desired effect.
     
  7. AlienFreak

    AlienFreak

    Joined:
    Jul 2, 2012
    Posts:
    40
    In case you came here because Cursor.lockstate doesn't move or lock the cursor in Editor mode but works find in build mode here's a fix.

    For the Editor, I came up with a very legit way to move the mouse cursor and another to force a mouse click event because Unity is so annoying in not letting you do this when using Cursor.lockState. Now you can set Cursor.lockState and/or Cursor.visibility in Start() and call either of the following below to force the mouse to behave. (Verified working in macOS Big Sur, Unity 2021.1.17)

    Stupid easy way to force mouse cursor position to center of game window in editor only from code:
    Code (CSharp):
    1. using UnityEngine.InputSystem;
    2. using UnityEngine.InputSystem.LowLevel;
    3.  
    4. public static void ForceMousePositionToCenterOfGameWindow()
    5.     {
    6. #if UNITY_EDITOR
    7.         // Force the mouse to be in the middle of the game screen
    8.         var game = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView"));
    9.         Vector2 warpPosition = game.rootVisualElement.contentRect.center;  // never let it move
    10.         Mouse.current.WarpCursorPosition(warpPosition);
    11.         InputState.Change(Mouse.current.position, warpPosition);
    12. #endif
    13.     }

    Stupid easy way to force click in game window in editor only from code:
    Code (CSharp):
    1. using UnityEngine.InputSystem;
    2. using UnityEngine.InputSystem.LowLevel;
    3.  
    4. public static void ForceClickMouseButtonInCenterOfGameWindow()
    5.     {
    6. #if UNITY_EDITOR
    7.         var game = UnityEditor.EditorWindow.GetWindow(typeof(UnityEditor.EditorWindow).Assembly.GetType("UnityEditor.GameView"));
    8.         Vector2 gameWindowCenter = game.rootVisualElement.contentRect.center;
    9.  
    10.         Event leftClickDown = new Event();
    11.         leftClickDown.button = 0;
    12.         leftClickDown.clickCount = 1;
    13.         leftClickDown.type = EventType.MouseDown;
    14.         leftClickDown.mousePosition = gameWindow;
    15.  
    16.         game.SendEvent(leftClickDown);
    17. #endif
    18.     }