254 lines
8.1 KiB
C#
254 lines
8.1 KiB
C#
using Invector.vCharacterController;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using InputDevice = Invector.vCharacterController.InputDevice;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
using UnityEngine.InputSystem;
|
|
#endif
|
|
|
|
public class vJoystickMouseInput : BaseInput
|
|
{
|
|
public StandaloneInputModule inputModule;
|
|
public RectTransform cursor;
|
|
public JoystickAxisInput joystickAxisInput;
|
|
|
|
[System.Serializable]
|
|
public class JoystickAxisInput
|
|
{
|
|
public string vertical = "LeftAnalogVertical";
|
|
public string horizontal = "LeftAnalogHorizontal";
|
|
public float horizontalAxis
|
|
{
|
|
get
|
|
{
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetAxis(horizontal);
|
|
#elif ENABLE_INPUT_SYSTEM
|
|
return Gamepad.current != null ? Gamepad.current.leftStick.x.ReadValue() : 0f;
|
|
#else
|
|
return 0f;
|
|
#endif
|
|
}
|
|
}
|
|
public float verticalAxis
|
|
{
|
|
get
|
|
{
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetAxis(vertical);
|
|
#elif ENABLE_INPUT_SYSTEM
|
|
return Gamepad.current != null ? Gamepad.current.leftStick.y.ReadValue() : 0f;
|
|
#else
|
|
return 0f;
|
|
#endif
|
|
}
|
|
}
|
|
}
|
|
public BaseInput oldOverride;
|
|
|
|
protected override void OnEnable()
|
|
{
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
if (inputModule)
|
|
{
|
|
inputModule.inputOverride = this;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
protected override void OnDisable()
|
|
{
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
if (inputModule)
|
|
{
|
|
inputModule.inputOverride = oldOverride;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
protected override void Awake()
|
|
{
|
|
base.Awake();
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
if (!inputModule)
|
|
inputModule = FindAnyObjectByType<StandaloneInputModule>();
|
|
if (inputModule)
|
|
{
|
|
oldOverride = inputModule.inputOverride;
|
|
inputModule.inputOverride = this;
|
|
}
|
|
#endif
|
|
|
|
}
|
|
|
|
protected Vector2 CursorPosition = Vector2.zero;
|
|
public float mouseSpeed = 4;
|
|
|
|
public override Vector2 mousePosition
|
|
{
|
|
get
|
|
{
|
|
if (vInput.instance.inputDevice == InputDevice.Joystick)
|
|
{
|
|
if (cursor && (!cursor.gameObject.activeSelf || Cursor.visible))
|
|
{
|
|
Cursor.visible = false;
|
|
CursorPosition = new Vector2(Screen.width * 0.5f, Screen.height * 0.5f);
|
|
cursor.gameObject.SetActive(true);
|
|
EventSystem.current.SetSelectedGameObject(null);
|
|
}
|
|
CursorPosition.x += joystickAxisInput.horizontalAxis * mouseSpeed;
|
|
CursorPosition.x = Mathf.Clamp(CursorPosition.x, 0, Screen.width);
|
|
CursorPosition.y += joystickAxisInput.verticalAxis * mouseSpeed;
|
|
CursorPosition.y = Mathf.Clamp(CursorPosition.y, 0, Screen.height);
|
|
}
|
|
else
|
|
{
|
|
if (cursor && cursor.gameObject.activeSelf)
|
|
{
|
|
Cursor.visible = true;
|
|
cursor.gameObject.SetActive(false);
|
|
}
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
CursorPosition = base.mousePosition;
|
|
#elif ENABLE_INPUT_SYSTEM
|
|
CursorPosition = Mouse.current != null ? Mouse.current.position.ReadValue() : Vector2.zero;
|
|
#endif
|
|
}
|
|
if (cursor) cursor.position = CursorPosition;
|
|
return this.CursorPosition;
|
|
}
|
|
}
|
|
public virtual string submitButton
|
|
{
|
|
get
|
|
{
|
|
return this.inputModule.submitButton;
|
|
}
|
|
}
|
|
|
|
public override float GetAxisRaw(string axisName)
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
|
|
return 0f;
|
|
#else
|
|
return base.GetAxisRaw(axisName);
|
|
#endif
|
|
}
|
|
|
|
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
|
|
private float GetNavigationAxis(string axisName)
|
|
{
|
|
var isHorizontal = (inputModule != null && axisName == inputModule.horizontalAxis) || axisName == "Horizontal" || axisName == "D-Pad Horizontal";
|
|
var isVertical = (inputModule != null && axisName == inputModule.verticalAxis) || axisName == "Vertical" || axisName == "D-Pad Vertical";
|
|
if (!isHorizontal && !isVertical) return 0f;
|
|
|
|
var gamepad = Gamepad.current;
|
|
if (gamepad != null)
|
|
{
|
|
var value = isHorizontal ? gamepad.dpad.x.ReadValue() : gamepad.dpad.y.ReadValue();
|
|
if (Mathf.Abs(value) > 0.01f) vInput.instance.ReportInputDevice(gamepad);
|
|
return value;
|
|
}
|
|
|
|
var keyboard = Keyboard.current;
|
|
if (keyboard == null) return 0f;
|
|
if (isHorizontal)
|
|
return keyboard.leftArrowKey.isPressed || keyboard.aKey.isPressed ? -1f : keyboard.rightArrowKey.isPressed || keyboard.dKey.isPressed ? 1f : 0f;
|
|
|
|
return keyboard.downArrowKey.isPressed || keyboard.sKey.isPressed ? -1f : keyboard.upArrowKey.isPressed || keyboard.wKey.isPressed ? 1f : 0f;
|
|
}
|
|
#endif
|
|
|
|
public override bool GetMouseButton(int button)
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
|
|
if (vInput.instance.inputDevice == InputDevice.Joystick && button == 0)
|
|
return Gamepad.current != null && Gamepad.current.buttonSouth.isPressed;
|
|
return GetMouseControl(button, 0);
|
|
#else
|
|
switch (vInput.instance.inputDevice)
|
|
{
|
|
case InputDevice.Joystick:
|
|
if (button == 0)
|
|
{
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetButton(submitButton);
|
|
#elif ENABLE_INPUT_SYSTEM
|
|
return Gamepad.current != null && Gamepad.current.buttonSouth.isPressed;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
else return base.GetMouseButton(button);
|
|
|
|
default:
|
|
return base.GetMouseButton(button);
|
|
}
|
|
#endif
|
|
}
|
|
public override bool GetMouseButtonUp(int button)
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
|
|
if (vInput.instance.inputDevice == InputDevice.Joystick && button == 0)
|
|
return Gamepad.current != null && Gamepad.current.buttonSouth.wasReleasedThisFrame;
|
|
return GetMouseControl(button, 1);
|
|
#else
|
|
switch (vInput.instance.inputDevice)
|
|
{
|
|
case InputDevice.Joystick:
|
|
if (button == 0)
|
|
{
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetButtonUp(submitButton);
|
|
#elif ENABLE_INPUT_SYSTEM
|
|
return Gamepad.current != null && Gamepad.current.buttonSouth.wasReleasedThisFrame;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
else return base.GetMouseButtonUp(button);
|
|
|
|
default:
|
|
return base.GetMouseButtonUp(button);
|
|
}
|
|
#endif
|
|
}
|
|
public override bool GetMouseButtonDown(int button)
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM && !ENABLE_LEGACY_INPUT_MANAGER
|
|
if (vInput.instance.inputDevice == InputDevice.Joystick && button == 0)
|
|
return Gamepad.current != null && Gamepad.current.buttonSouth.wasPressedThisFrame;
|
|
return GetMouseControl(button, 2);
|
|
#else
|
|
switch (vInput.instance.inputDevice)
|
|
{
|
|
case InputDevice.Joystick:
|
|
if (button == 0)
|
|
{
|
|
#if ENABLE_LEGACY_INPUT_MANAGER
|
|
return Input.GetButtonDown(submitButton);
|
|
#elif ENABLE_INPUT_SYSTEM
|
|
return Gamepad.current != null && Gamepad.current.buttonSouth.wasPressedThisFrame;
|
|
#else
|
|
return false;
|
|
#endif
|
|
}
|
|
else return base.GetMouseButtonDown(button);
|
|
|
|
default:
|
|
return base.GetMouseButtonDown(button);
|
|
}
|
|
#endif
|
|
}
|
|
|
|
#if ENABLE_INPUT_SYSTEM
|
|
private static bool GetMouseControl(int button, int state)
|
|
{
|
|
if (Mouse.current == null) return false;
|
|
var control = button == 1 ? Mouse.current.rightButton : button == 2 ? Mouse.current.middleButton : Mouse.current.leftButton;
|
|
return state == 1 ? control.wasReleasedThisFrame : state == 2 ? control.wasPressedThisFrame : control.isPressed;
|
|
}
|
|
#endif
|
|
}
|