namespace VRTK.Prefabs.CameraRig.SimulatedCameraRig.Input { using UnityEngine; using Malimbe.PropertySerializationAttribute; using Malimbe.XmlDocumentationAttribute; using Zinnia.Action; /// /// Listens for input from the mouse and converts into Vector2 data. /// public class MouseVector2DAction : Vector2Action { /// /// The named x axis of the mouse. /// [Serialized] [field: DocumentedByXml] public string XAxisName { get; set; } = "Mouse X"; /// /// The named y axis of the mouse. /// [Serialized] [field: DocumentedByXml] public string YAxisName { get; set; } = "Mouse Y"; /// /// Determines whether to lock the cursor in the game window. /// [Serialized] [field: DocumentedByXml] public bool LockCursor { get; set; } /// /// Multiplies the speed at which the unlocked cursor moves the axis. /// [Serialized] [field: DocumentedByXml] public float CursorMultiplier { get; set; } = 1f; /// /// Multiplies the speed at which the locked cursor moves the axis. /// [Serialized] [field: DocumentedByXml] public float LockedCursorMultiplier { get; set; } = 2f; /// /// The previous axis position of the mouse pointer. /// protected Vector3 previousMousePosition; protected override void OnEnable() { previousMousePosition = Input.mousePosition; base.OnEnable(); } protected virtual void Update() { Cursor.lockState = LockCursor ? CursorLockMode.Locked : CursorLockMode.None; Vector3 mouseData = GetMouseDelta(); Receive(new Vector2(mouseData.x, mouseData.y)); } /// /// Gets the difference in axis position of the mouse between the previous frame and current frame. /// /// The difference in mouse axis position. protected virtual Vector3 GetMouseDelta() { Vector3 difference = Input.mousePosition - previousMousePosition; previousMousePosition = Input.mousePosition; return Cursor.lockState == CursorLockMode.Locked ? new Vector3(Input.GetAxis(XAxisName), Input.GetAxis(YAxisName)) * LockedCursorMultiplier : difference * CursorMultiplier; } } }