122 lines
4.3 KiB
C#
122 lines
4.3 KiB
C#
using System;
|
|
using UnityEngine;
|
|
|
|
namespace GazeLab.InputSystem
|
|
{
|
|
public sealed class InputModeManager : MonoBehaviour, IAimInputSource
|
|
{
|
|
public static InputModeManager Instance { get; private set; }
|
|
|
|
[Header("Input Sources")]
|
|
public MouseAimInput mouseInput;
|
|
public TobiiGazeInputAdapter tobiiInput;
|
|
public KeyboardAimInput keyboardInput;
|
|
|
|
[Header("Mode & Keyboard Fallback Settings")]
|
|
public AimControlMode activeMode = AimControlMode.Mouse;
|
|
public KeyCode cycleControlModeKey = KeyCode.Tab;
|
|
[Tooltip("Allows WASD / Arrow Keys to override cursor position even while in Mouse or Tobii Eye Tracker mode.")]
|
|
public bool allowUniversalKeyboardAssist = true;
|
|
|
|
private float lastKeyboardAssistTime = -10f;
|
|
private Vector2 lastKnownScreenPos;
|
|
|
|
public event Action<AimControlMode> OnControlModeChanged;
|
|
|
|
public AimControlMode ControlMode => activeMode;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
if (mouseInput == null)
|
|
{
|
|
mouseInput = GetComponent<MouseAimInput>();
|
|
if (mouseInput == null) mouseInput = gameObject.AddComponent<MouseAimInput>();
|
|
}
|
|
if (tobiiInput == null)
|
|
{
|
|
tobiiInput = GetComponent<TobiiGazeInputAdapter>();
|
|
if (tobiiInput == null) tobiiInput = gameObject.AddComponent<TobiiGazeInputAdapter>();
|
|
}
|
|
if (keyboardInput == null)
|
|
{
|
|
keyboardInput = GetComponent<KeyboardAimInput>();
|
|
if (keyboardInput == null) keyboardInput = gameObject.AddComponent<KeyboardAimInput>();
|
|
}
|
|
lastKnownScreenPos = new Vector2(Screen.width * 0.5f, Screen.height * 0.55f);
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (UnityEngine.Input.GetKeyDown(cycleControlModeKey))
|
|
{
|
|
CycleControlMode();
|
|
}
|
|
|
|
if (allowUniversalKeyboardAssist && activeMode != AimControlMode.Keyboard)
|
|
{
|
|
float h = UnityEngine.Input.GetAxisRaw("Horizontal");
|
|
float v = UnityEngine.Input.GetAxisRaw("Vertical");
|
|
if (Mathf.Abs(h) > 0.05f || Mathf.Abs(v) > 0.05f)
|
|
{
|
|
if (Time.unscaledTime - lastKeyboardAssistTime > 0.25f)
|
|
{
|
|
keyboardInput.SyncCursorTo(lastKnownScreenPos);
|
|
}
|
|
lastKeyboardAssistTime = Time.unscaledTime;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void SetControlMode(AimControlMode mode)
|
|
{
|
|
if (activeMode == mode) return;
|
|
activeMode = mode;
|
|
if (keyboardInput != null) keyboardInput.SyncCursorTo(lastKnownScreenPos);
|
|
OnControlModeChanged?.Invoke(activeMode);
|
|
}
|
|
|
|
public void CycleControlMode()
|
|
{
|
|
int next = ((int)activeMode + 1) % 3;
|
|
SetControlMode((AimControlMode)next);
|
|
}
|
|
|
|
public bool TryGetScreenPosition(out Vector2 screenPixelPosition)
|
|
{
|
|
if (allowUniversalKeyboardAssist && activeMode != AimControlMode.Keyboard &&
|
|
(Time.unscaledTime - lastKeyboardAssistTime) < 0.85f)
|
|
{
|
|
if (keyboardInput != null && keyboardInput.TryGetScreenPosition(out screenPixelPosition))
|
|
{
|
|
lastKnownScreenPos = screenPixelPosition;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
bool valid = false;
|
|
screenPixelPosition = lastKnownScreenPos;
|
|
|
|
switch (activeMode)
|
|
{
|
|
case AimControlMode.Mouse:
|
|
valid = mouseInput != null && mouseInput.TryGetScreenPosition(out screenPixelPosition);
|
|
break;
|
|
case AimControlMode.TobiiEyeTracker:
|
|
valid = tobiiInput != null && tobiiInput.TryGetScreenPosition(out screenPixelPosition);
|
|
break;
|
|
case AimControlMode.Keyboard:
|
|
valid = keyboardInput != null && keyboardInput.TryGetScreenPosition(out screenPixelPosition);
|
|
break;
|
|
}
|
|
|
|
if (valid)
|
|
{
|
|
lastKnownScreenPos = screenPixelPosition;
|
|
}
|
|
return valid;
|
|
}
|
|
}
|
|
}
|
|
|