using System;
using GazeLab.Core;
using GazeLab.InputSystem;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace GazeLab.UI
{
///
/// 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.
///
[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();
unityButton = GetComponent