The orbit rig read the wheel unfiltered for zoom, so scrolling a paint-mode scroll list also zoomed the camera. Gate the zoom behind a shared InputUtil.PointerOverUi() helper (EventSystem raycast); MeshPainter's private IsPointerOverUi() now delegates to the same helper (DRY). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
133 lines
4.3 KiB
C#
133 lines
4.3 KiB
C#
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
#if ENABLE_INPUT_SYSTEM
|
|
using UnityEngine.InputSystem;
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// 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.
|
|
/// </summary>
|
|
public static class InputUtil
|
|
{
|
|
/// <summary>Raw mouse delta this frame (pixels); the legacy path scales GetAxisRaw by 10 to match.</summary>
|
|
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
|
|
}
|
|
|
|
/// <summary>Raw mouse-wheel delta this frame (usually only its sign is used).</summary>
|
|
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
|
|
}
|
|
|
|
/// <summary>Locks+hides or frees+shows the hardware cursor.</summary>
|
|
public static void SetCursorLocked(bool locked)
|
|
{
|
|
Cursor.lockState = locked ? CursorLockMode.Locked : CursorLockMode.None;
|
|
Cursor.visible = !locked;
|
|
}
|
|
|
|
/// <summary>True while the pointer is over an interactive UI element (a raycast target), so the
|
|
/// wheel/click belongs to the UI (e.g. a scroll list) and must not also zoom the camera or paint
|
|
/// the mesh behind it. False when no EventSystem exists.</summary>
|
|
public static bool PointerOverUi()
|
|
{
|
|
var es = EventSystem.current;
|
|
return es && es.IsPointerOverGameObject();
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
/// <summary>Left mouse button pressed on this exact frame (edge, not held).</summary>
|
|
public static bool LeftClickedThisFrame()
|
|
{
|
|
#if ENABLE_INPUT_SYSTEM
|
|
return Mouse.current != null && Mouse.current.leftButton.wasPressedThisFrame;
|
|
#else
|
|
return Input.GetMouseButtonDown(0);
|
|
#endif
|
|
}
|
|
|
|
/// <summary>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.</summary>
|
|
#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
|
|
|
|
/// <summary>Gameplay key pressed on this exact frame (edge), suppressed while a UI text field is
|
|
/// focused so typing never triggers gameplay bindings.</summary>
|
|
#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
|
|
}
|