using UnityEngine;
#if ENABLE_INPUT_SYSTEM
using UnityEngine.InputSystem;
#endif
///
/// Shared low-level input reads used by the camera rigs, the paint controller and the modes. Each
/// method preserves its original per-backend behaviour (new Input System when present, legacy Input
/// Manager otherwise). Sensitivity scaling stays at the call sites.
///
public static class InputUtil
{
/// Raw mouse delta this frame (pixels); the legacy path scales GetAxisRaw by 10 to match.
public static Vector2 ReadMouseDelta()
{
#if ENABLE_INPUT_SYSTEM
return Mouse.current != null ? Mouse.current.delta.ReadValue() : Vector2.zero;
#else
return new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * 10f;
#endif
}
/// Raw mouse-wheel delta this frame (usually only its sign is used).
public static float ReadScroll()
{
#if ENABLE_INPUT_SYSTEM
if (Mouse.current != null) return Mouse.current.scroll.ReadValue().y;
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetAxis("Mouse ScrollWheel");
#else
return 0f;
#endif
}
/// Locks+hides or frees+shows the hardware cursor.
public static void SetCursorLocked(bool locked)
{
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
Cursor.visible = !locked;
}
public static bool ShiftHeld()
{
#if ENABLE_INPUT_SYSTEM
var kb = Keyboard.current;
if (kb != null) return kb.leftShiftKey.isPressed || kb.rightShiftKey.isPressed;
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift);
#else
return false;
#endif
}
public static bool CtrlHeld()
{
#if ENABLE_INPUT_SYSTEM
var kb = Keyboard.current;
if (kb != null) return kb.leftCtrlKey.isPressed || kb.rightCtrlKey.isPressed;
#endif
#if ENABLE_LEGACY_INPUT_MANAGER
return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl);
#else
return false;
#endif
}
public static bool LeftMouseHeld()
{
#if ENABLE_INPUT_SYSTEM
return Mouse.current != null && Mouse.current.leftButton.isPressed;
#else
return Input.GetMouseButton(0);
#endif
}
public static bool RightMouseHeld()
{
#if ENABLE_INPUT_SYSTEM
return Mouse.current != null && Mouse.current.rightButton.isPressed;
#else
return Input.GetMouseButton(1);
#endif
}
public static bool MiddleMouseHeld()
{
#if ENABLE_INPUT_SYSTEM
return Mouse.current != null && Mouse.current.middleButton.isPressed;
#else
return Input.GetMouseButton(2);
#endif
}
/// Left mouse button pressed on this exact frame (edge, not held).
public static bool LeftClickedThisFrame()
{
#if ENABLE_INPUT_SYSTEM
return Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame;
#else
return Input.GetMouseButtonDown(0);
#endif
}
/// Key pressed on this exact frame (edge), regardless of UI focus. The parameter type
/// follows the active input backend, matching how the callers' serialized key fields are declared.
#if ENABLE_INPUT_SYSTEM
public static bool KeyPressedThisFrame(Key key)
=> Keyboard.current != null && Keyboard.current[key].wasPressedThisFrame;
#else
public static bool KeyPressedThisFrame(KeyCode key) => Input.GetKeyDown(key);
#endif
/// Gameplay key pressed on this exact frame (edge), suppressed while a UI text field is
/// focused so typing never triggers gameplay bindings.
#if ENABLE_INPUT_SYSTEM
public static bool GameplayKeyPressed(Key key) => !TypingGuard.IsTyping && KeyPressedThisFrame(key);
#else
public static bool GameplayKeyPressed(KeyCode key) => !TypingGuard.IsTyping && KeyPressedThisFrame(key);
#endif
}