| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using UnityEngine;
- #if ENABLE_INPUT_SYSTEM
- using UnityEngine.InputSystem;
- #endif
- namespace MazeGen
- {
- /// <summary>
- /// Small input-backend-agnostic helpers. This project has the Input System package
- /// installed, and Active Input Handling may be set to "Input System Package (New)"
- /// only - which silently breaks legacy-only APIs like OnMouseDown/Input.GetKey. These
- /// helpers pick the right backend at compile time via the ENABLE_INPUT_SYSTEM symbol
- /// Unity defines automatically when the package is present.
- /// </summary>
- public static class MazeInput
- {
- public static bool WasLeftClickedThisFrame()
- {
- #if ENABLE_INPUT_SYSTEM
- return Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame;
- #else
- return Input.GetMouseButtonDown(0);
- #endif
- }
- public static bool IsMiddleMouseHeld()
- {
- #if ENABLE_INPUT_SYSTEM
- return Mouse.current != null && Mouse.current.middleButton.isPressed;
- #else
- return Input.GetMouseButton(2);
- #endif
- }
- public static Vector2 GetPointerScreenPosition()
- {
- #if ENABLE_INPUT_SYSTEM
- return Mouse.current != null ? Mouse.current.position.ReadValue() : Vector2.zero;
- #else
- return Input.mousePosition;
- #endif
- }
- /// <summary>Mouse wheel movement this frame, normalized to roughly "notches" on both input backends (positive = scroll up/away from the user).</summary>
- public static float GetScrollDelta()
- {
- #if ENABLE_INPUT_SYSTEM
- // The new Input System reports raw OS wheel delta (~120 per notch on Windows),
- // not pre-normalized like the legacy API - scale it down to match.
- return Mouse.current != null ? Mouse.current.scroll.ReadValue().y / 120f : 0f;
- #else
- return Input.mouseScrollDelta.y;
- #endif
- }
- /// <summary>Normalized WASD movement axis: (right/left, up/down).</summary>
- public static Vector2 ReadWasdAxis()
- {
- float x = 0f, y = 0f;
- #if ENABLE_INPUT_SYSTEM
- var kb = Keyboard.current;
- if (kb != null)
- {
- if (kb.aKey.isPressed) x -= 1f;
- if (kb.dKey.isPressed) x += 1f;
- if (kb.sKey.isPressed) y -= 1f;
- if (kb.wKey.isPressed) y += 1f;
- }
- #else
- if (Input.GetKey(KeyCode.A)) x -= 1f;
- if (Input.GetKey(KeyCode.D)) x += 1f;
- if (Input.GetKey(KeyCode.S)) y -= 1f;
- if (Input.GetKey(KeyCode.W)) y += 1f;
- #endif
- Vector2 v = new Vector2(x, y);
- return v.sqrMagnitude > 1f ? v.normalized : v;
- }
- }
- }
|