472 lines
18 KiB
C#
472 lines
18 KiB
C#
using System;
|
|
using GazeLab.Core;
|
|
using GazeLab.InputSystem;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using UnityEngine.UI;
|
|
|
|
namespace GazeLab.UI
|
|
{
|
|
/// <summary>
|
|
/// Universal UI Button enhancement that supports standard Mouse Clicks, Keyboard hotkeys,
|
|
/// AND hands-free Eye-Tracker / Cursor Dwell activation (using unscaled time so it works while paused).
|
|
/// Also supports slowing down game time during active combat when lingering on buttons.
|
|
/// </summary>
|
|
[RequireComponent(typeof(RectTransform))]
|
|
public sealed class DwellButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerClickHandler
|
|
{
|
|
public static float CurrentHoveredDwellProgress { get; private set; }
|
|
public static bool IsAnyButtonHovered { get; private set; }
|
|
public static DwellButton ActiveHoveredButton { get; private set; }
|
|
|
|
public bool isDwellEnabled = true;
|
|
public bool showDwellBar = true;
|
|
|
|
private static Sprite s_WhiteFillSprite;
|
|
public static Sprite GetOrCreateWhiteFillSprite()
|
|
{
|
|
if (s_WhiteFillSprite == null)
|
|
{
|
|
Texture2D tex = new Texture2D(2, 2, TextureFormat.RGBA32, false);
|
|
tex.SetPixels(new Color[] { Color.white, Color.white, Color.white, Color.white });
|
|
tex.Apply();
|
|
s_WhiteFillSprite = Sprite.Create(tex, new Rect(0, 0, 2, 2), new Vector2(0.5f, 0.5f));
|
|
}
|
|
return s_WhiteFillSprite;
|
|
}
|
|
|
|
public bool IsInteractable
|
|
{
|
|
get
|
|
{
|
|
if (unityButton != null && !unityButton.interactable) return false;
|
|
if (!isDwellEnabled) return false;
|
|
|
|
// If an overlay/modal is active, only buttons inside the topmost modal can be interacted with
|
|
if (GazeLabHUD.Instance != null)
|
|
{
|
|
GameObject topModal = GazeLabHUD.Instance.GetTopmostActiveModal();
|
|
if (topModal != null && !transform.IsChildOf(topModal.transform))
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public static bool IsAnyMenuOpen()
|
|
{
|
|
if (GazeLabHUD.Instance != null && GazeLabHUD.Instance.IsAnyModalOpen())
|
|
return true;
|
|
|
|
GazeLabGameManager gm = GazeLabGameManager.Instance;
|
|
if (gm == null) return false;
|
|
return gm.currentState == GameFlowState.InterWaveShop ||
|
|
gm.currentState == GameFlowState.PausedModal ||
|
|
gm.currentState == GameFlowState.GameOver ||
|
|
gm.currentState == GameFlowState.TitleMenu;
|
|
}
|
|
|
|
public static void ResetAllDwellState()
|
|
{
|
|
if (ActiveHoveredButton != null)
|
|
{
|
|
ActiveHoveredButton.currentDwellTime = 0f;
|
|
if (ActiveHoveredButton.dwellProgressFillImage != null)
|
|
{
|
|
ActiveHoveredButton.dwellProgressFillImage.fillAmount = 0f;
|
|
ActiveHoveredButton.dwellProgressFillImage.enabled = false;
|
|
}
|
|
ActiveHoveredButton.isPointerInside = false;
|
|
ActiveHoveredButton = null;
|
|
}
|
|
IsAnyButtonHovered = false;
|
|
CurrentHoveredDwellProgress = 0f;
|
|
}
|
|
|
|
[Range(0.35f, 2.5f)] public float dwellActivationSeconds = 0.95f;
|
|
public bool enableDwellInMouseMode = true;
|
|
[Tooltip("If true, slows game time down to 0.3x when gazing at this button during active combat.")]
|
|
public bool slowTimeDuringActiveCombat = true;
|
|
public Image dwellProgressFillImage;
|
|
public KeyCode optionalShortcutKey = KeyCode.None;
|
|
|
|
private RectTransform rectTransform;
|
|
private Button unityButton;
|
|
private bool isPointerInside;
|
|
private float currentDwellTime;
|
|
private Action clickCallback;
|
|
public Action hoverCallback;
|
|
|
|
public void Bind(Action onClick, float dwellTime = 0.95f, KeyCode shortcut = KeyCode.None, Action onHover = null)
|
|
{
|
|
clickCallback = onClick;
|
|
dwellActivationSeconds = dwellTime;
|
|
optionalShortcutKey = shortcut;
|
|
hoverCallback = onHover;
|
|
}
|
|
|
|
private void Awake()
|
|
{
|
|
rectTransform = GetComponent<RectTransform>();
|
|
unityButton = GetComponent<Button>();
|
|
if (unityButton != null)
|
|
{
|
|
unityButton.onClick.AddListener(TriggerButtonAction);
|
|
}
|
|
|
|
// Auto-disable dwell fill bars on circular sockets or ability rings
|
|
string n = gameObject.name;
|
|
if (n.Contains("Socket") || n.Contains("Slot") || n.Contains("Ring"))
|
|
{
|
|
showDwellBar = false;
|
|
}
|
|
|
|
EnsureDwellFillBar();
|
|
}
|
|
|
|
private void OnDisable()
|
|
{
|
|
if (ActiveHoveredButton == this)
|
|
{
|
|
ActiveHoveredButton = null;
|
|
IsAnyButtonHovered = false;
|
|
CurrentHoveredDwellProgress = 0f;
|
|
RestoreCombatTimeScale();
|
|
}
|
|
isPointerInside = false;
|
|
currentDwellTime = 0f;
|
|
if (dwellProgressFillImage != null)
|
|
{
|
|
dwellProgressFillImage.fillAmount = 0f;
|
|
dwellProgressFillImage.enabled = false;
|
|
}
|
|
}
|
|
|
|
private void EnsureDwellFillBar()
|
|
{
|
|
if (!showDwellBar)
|
|
{
|
|
Transform old = transform.Find("DwellFillBar");
|
|
if (old != null) Destroy(old.gameObject);
|
|
dwellProgressFillImage = null;
|
|
return;
|
|
}
|
|
|
|
if (dwellProgressFillImage != null)
|
|
{
|
|
if (dwellProgressFillImage.sprite == null)
|
|
dwellProgressFillImage.sprite = GetOrCreateWhiteFillSprite();
|
|
dwellProgressFillImage.enabled = currentDwellTime > 0.001f;
|
|
return;
|
|
}
|
|
|
|
Transform existing = transform.Find("DwellFillBar");
|
|
if (existing != null)
|
|
{
|
|
dwellProgressFillImage = existing.GetComponent<Image>();
|
|
if (dwellProgressFillImage != null)
|
|
{
|
|
if (dwellProgressFillImage.sprite == null)
|
|
dwellProgressFillImage.sprite = GetOrCreateWhiteFillSprite();
|
|
dwellProgressFillImage.enabled = currentDwellTime > 0.001f;
|
|
return;
|
|
}
|
|
}
|
|
|
|
GameObject barGo = new GameObject("DwellFillBar", typeof(RectTransform), typeof(Image));
|
|
barGo.transform.SetParent(transform, false);
|
|
RectTransform rt = barGo.GetComponent<RectTransform>();
|
|
rt.anchorMin = new Vector2(0.04f, 0.04f);
|
|
rt.anchorMax = new Vector2(0.96f, 0.12f);
|
|
rt.offsetMin = Vector2.zero;
|
|
rt.offsetMax = Vector2.zero;
|
|
|
|
dwellProgressFillImage = barGo.GetComponent<Image>();
|
|
dwellProgressFillImage.sprite = GetOrCreateWhiteFillSprite();
|
|
dwellProgressFillImage.color = new Color(0.20f, 0.95f, 0.65f, 0.95f);
|
|
dwellProgressFillImage.type = Image.Type.Filled;
|
|
dwellProgressFillImage.fillMethod = Image.FillMethod.Horizontal;
|
|
dwellProgressFillImage.fillAmount = 0f;
|
|
dwellProgressFillImage.enabled = false;
|
|
dwellProgressFillImage.raycastTarget = false;
|
|
}
|
|
|
|
private static readonly System.Collections.Generic.List<RaycastResult> s_RaycastResults = new System.Collections.Generic.List<RaycastResult>();
|
|
private static PointerEventData s_CachedPointerData;
|
|
private static int s_LastRaycastFrame = -1;
|
|
private static Vector2 s_LastRaycastScreenPt = new Vector2(-9999f, -9999f);
|
|
private static GameObject s_CachedTopHit;
|
|
|
|
/// <summary>
|
|
/// Validates that this button is genuinely the topmost active UI element at the given screen coordinates.
|
|
/// Caches the EventSystem.RaycastAll result per frame/point so 25+ buttons only perform 1 raycast per frame.
|
|
/// </summary>
|
|
public bool IsTopmostAtScreenPoint(Vector2 screenPt)
|
|
{
|
|
if (EventSystem.current == null) return true;
|
|
|
|
int frame = Time.frameCount;
|
|
if (frame != s_LastRaycastFrame || (screenPt - s_LastRaycastScreenPt).sqrMagnitude > 0.25f)
|
|
{
|
|
s_LastRaycastFrame = frame;
|
|
s_LastRaycastScreenPt = screenPt;
|
|
|
|
if (s_CachedPointerData == null)
|
|
s_CachedPointerData = new PointerEventData(EventSystem.current);
|
|
s_CachedPointerData.position = screenPt;
|
|
|
|
s_RaycastResults.Clear();
|
|
EventSystem.current.RaycastAll(s_CachedPointerData, s_RaycastResults);
|
|
s_CachedTopHit = s_RaycastResults.Count > 0 ? s_RaycastResults[0].gameObject : null;
|
|
}
|
|
|
|
if (s_CachedTopHit == null) return true;
|
|
return s_CachedTopHit == gameObject || s_CachedTopHit.transform.IsChildOf(transform);
|
|
}
|
|
|
|
public void OnPointerEnter(PointerEventData eventData)
|
|
{
|
|
if (!IsTopmostAtScreenPoint(eventData.position)) return;
|
|
isPointerInside = true;
|
|
if (!IsInteractable)
|
|
{
|
|
hoverCallback?.Invoke();
|
|
}
|
|
}
|
|
|
|
public void OnPointerExit(PointerEventData eventData)
|
|
{
|
|
isPointerInside = false;
|
|
currentDwellTime = 0f;
|
|
if (dwellProgressFillImage != null)
|
|
{
|
|
dwellProgressFillImage.fillAmount = 0f;
|
|
dwellProgressFillImage.enabled = false;
|
|
}
|
|
if (ActiveHoveredButton == this)
|
|
{
|
|
ActiveHoveredButton = null;
|
|
IsAnyButtonHovered = false;
|
|
CurrentHoveredDwellProgress = 0f;
|
|
RestoreCombatTimeScale();
|
|
}
|
|
}
|
|
|
|
public void OnPointerClick(PointerEventData eventData)
|
|
{
|
|
if (!IsTopmostAtScreenPoint(eventData.position)) return;
|
|
|
|
if (!IsInteractable)
|
|
{
|
|
// If not interactable (e.g. unaffordable tech node), still invoke hoverCallback to select for inspection,
|
|
// provided we aren't blocked by a separate topmost modal.
|
|
if (GazeLabHUD.Instance != null)
|
|
{
|
|
GameObject topModal = GazeLabHUD.Instance.GetTopmostActiveModal();
|
|
if (topModal != null && !transform.IsChildOf(topModal.transform))
|
|
return;
|
|
}
|
|
hoverCallback?.Invoke();
|
|
return;
|
|
}
|
|
|
|
if (unityButton == null)
|
|
{
|
|
TriggerButtonAction();
|
|
}
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (!IsInteractable)
|
|
{
|
|
currentDwellTime = 0f;
|
|
if (dwellProgressFillImage != null)
|
|
{
|
|
dwellProgressFillImage.fillAmount = 0f;
|
|
dwellProgressFillImage.enabled = false;
|
|
}
|
|
if (ActiveHoveredButton == this)
|
|
{
|
|
ActiveHoveredButton = null;
|
|
IsAnyButtonHovered = false;
|
|
CurrentHoveredDwellProgress = 0f;
|
|
RestoreCombatTimeScale();
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (optionalShortcutKey != KeyCode.None && UnityEngine.Input.GetKeyDown(optionalShortcutKey))
|
|
{
|
|
TriggerButtonAction();
|
|
return;
|
|
}
|
|
|
|
Vector2 aimScreenPos;
|
|
if (InputModeManager.Instance != null && InputModeManager.Instance.TryGetScreenPosition(out aimScreenPos))
|
|
{
|
|
// active custom/gaze cursor position
|
|
}
|
|
else
|
|
{
|
|
aimScreenPos = UnityEngine.Input.mousePosition;
|
|
}
|
|
|
|
bool isTopmost = IsTopmostAtScreenPoint(aimScreenPos);
|
|
bool pointerInsideRect = rectTransform != null && RectTransformUtility.RectangleContainsScreenPoint(rectTransform, aimScreenPos, null);
|
|
if (!pointerInsideRect || !isTopmost)
|
|
{
|
|
isPointerInside = false;
|
|
}
|
|
|
|
bool gazeHovered = isTopmost && CheckCustomAimCursorInside(aimScreenPos);
|
|
bool activeHover = (isPointerInside && isTopmost) || gazeHovered;
|
|
|
|
if (activeHover)
|
|
{
|
|
if (ActiveHoveredButton != this)
|
|
{
|
|
hoverCallback?.Invoke();
|
|
}
|
|
|
|
if (!IsInteractable)
|
|
{
|
|
currentDwellTime = 0f;
|
|
if (dwellProgressFillImage != null)
|
|
{
|
|
dwellProgressFillImage.fillAmount = 0f;
|
|
dwellProgressFillImage.enabled = false;
|
|
}
|
|
if (ActiveHoveredButton == this)
|
|
{
|
|
ActiveHoveredButton = null;
|
|
IsAnyButtonHovered = false;
|
|
CurrentHoveredDwellProgress = 0f;
|
|
RestoreCombatTimeScale();
|
|
}
|
|
return;
|
|
}
|
|
|
|
ActiveHoveredButton = this;
|
|
IsAnyButtonHovered = true;
|
|
|
|
// Slow down combat time when hovering over a button during active wave
|
|
ApplyCombatSlowMotion();
|
|
|
|
currentDwellTime += Time.unscaledDeltaTime;
|
|
float ratio = Mathf.Clamp01(currentDwellTime / Mathf.Max(0.2f, dwellActivationSeconds));
|
|
CurrentHoveredDwellProgress = ratio;
|
|
|
|
if (dwellProgressFillImage != null)
|
|
{
|
|
dwellProgressFillImage.enabled = ratio > 0.001f;
|
|
dwellProgressFillImage.fillAmount = ratio;
|
|
}
|
|
|
|
if (currentDwellTime >= dwellActivationSeconds)
|
|
{
|
|
TriggerButtonAction();
|
|
}
|
|
}
|
|
else if (currentDwellTime > 0f)
|
|
{
|
|
currentDwellTime = Mathf.MoveTowards(currentDwellTime, 0f, Time.unscaledDeltaTime * 2.5f);
|
|
float ratio = Mathf.Clamp01(currentDwellTime / Mathf.Max(0.2f, dwellActivationSeconds));
|
|
if (dwellProgressFillImage != null)
|
|
{
|
|
dwellProgressFillImage.enabled = ratio > 0.001f;
|
|
dwellProgressFillImage.fillAmount = ratio;
|
|
}
|
|
|
|
if (ActiveHoveredButton == this)
|
|
{
|
|
CurrentHoveredDwellProgress = ratio;
|
|
if (ratio <= 0.001f)
|
|
{
|
|
ActiveHoveredButton = null;
|
|
IsAnyButtonHovered = false;
|
|
RestoreCombatTimeScale();
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (dwellProgressFillImage != null && dwellProgressFillImage.enabled)
|
|
{
|
|
dwellProgressFillImage.fillAmount = 0f;
|
|
dwellProgressFillImage.enabled = false;
|
|
}
|
|
|
|
if (ActiveHoveredButton == this)
|
|
{
|
|
ActiveHoveredButton = null;
|
|
IsAnyButtonHovered = false;
|
|
CurrentHoveredDwellProgress = 0f;
|
|
RestoreCombatTimeScale();
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ApplyCombatSlowMotion()
|
|
{
|
|
if (!slowTimeDuringActiveCombat || IsAnyMenuOpen()) return;
|
|
GazeLabGameManager gm = GazeLabGameManager.Instance;
|
|
if (gm != null && gm.currentState == GameFlowState.WaveCombat)
|
|
{
|
|
Time.timeScale = 0.32f;
|
|
}
|
|
}
|
|
|
|
private static void RestoreCombatTimeScale()
|
|
{
|
|
if (IsAnyMenuOpen()) return;
|
|
GazeLabGameManager gm = GazeLabGameManager.Instance;
|
|
if (gm != null && gm.currentState == GameFlowState.WaveCombat)
|
|
{
|
|
Time.timeScale = 1.0f;
|
|
}
|
|
}
|
|
|
|
private bool CheckCustomAimCursorInside(Vector2 screenPt)
|
|
{
|
|
if (!IsInteractable) return false;
|
|
if (InputModeManager.Instance == null || rectTransform == null) return false;
|
|
if (InputModeManager.Instance.ControlMode == AimControlMode.Mouse && !enableDwellInMouseMode)
|
|
return false;
|
|
|
|
return RectTransformUtility.RectangleContainsScreenPoint(rectTransform, screenPt, null);
|
|
}
|
|
|
|
private void TriggerButtonAction()
|
|
{
|
|
if (!IsInteractable) return;
|
|
|
|
isPointerInside = false;
|
|
currentDwellTime = 0f;
|
|
CurrentHoveredDwellProgress = 0f;
|
|
if (dwellProgressFillImage != null)
|
|
{
|
|
dwellProgressFillImage.fillAmount = 0f;
|
|
dwellProgressFillImage.enabled = false;
|
|
}
|
|
|
|
if (ActiveHoveredButton == this)
|
|
{
|
|
ActiveHoveredButton = null;
|
|
IsAnyButtonHovered = false;
|
|
RestoreCombatTimeScale();
|
|
}
|
|
|
|
if (SpaceAudioController.Instance != null)
|
|
SpaceAudioController.Instance.PlayClick();
|
|
|
|
if (!IsAnyMenuOpen())
|
|
{
|
|
RestoreCombatTimeScale();
|
|
}
|
|
clickCallback?.Invoke();
|
|
}
|
|
}
|
|
}
|