using UnityEngine;
using System.Collections;
///
/// Base class for UI components that need to block map clicks.
/// Handles the common click blocking functionality including registration,
/// flag management, and coroutine handling.
///
public abstract class BaseClickBlocker : MonoBehaviour, IClickBlocker
{
///
/// Flag to prevent map clicks when UI handles them
///
[System.NonSerialized]
protected bool recentlyHandledClick = false;
///
/// Whether this UI is currently visible and should block clicks
///
protected virtual bool IsUIVisible => true;
///
/// Auto-register/unregister with ClickManager
///
protected virtual void OnEnable() => ClickManager.Instance?.RegisterClickBlocker(this);
protected virtual void OnDisable() => ClickManager.Instance?.UnregisterClickBlocker(this);
///
/// Abstract method that each UI component must implement to define its specific blocking logic
///
public abstract bool IsBlockingClick(Vector2 screenPosition);
///
/// Sets a temporary flag to block travel system clicks when UI handles a click event.
/// This is much more reliable than trying to calculate coordinates manually.
/// Call this from your UI event handlers (MouseDownEvent, ClickEvent, etc.)
///
protected void SetClickFlag()
{
recentlyHandledClick = true;
Debug.Log($"🚫 {GetType().Name}: Click flag SET - blocking map clicks");
// Also immediately stop any current coroutine to prevent premature reset
StopAllCoroutines();
// Reset the flag after a longer delay to ensure we catch same-frame clicks
StartCoroutine(ResetClickFlag());
}
///
/// Resets the click blocking flag after a short delay
///
private IEnumerator ResetClickFlag()
{
// Wait longer to ensure we block clicks that happen in the same frame
yield return new WaitForEndOfFrame(); // Wait one frame
yield return new WaitForEndOfFrame(); // Wait another frame
yield return new WaitForSeconds(0.1f); // Wait additional time to be extra safe
recentlyHandledClick = false;
Debug.Log($"🔓 {GetType().Name}: Click flag reset - map clicks allowed again");
}
///
/// Helper method to check if a recent UI click should block the current check
/// Use this as the first check in your IsBlockingClick implementation
///
protected bool ShouldBlockDueToRecentClick()
{
if (recentlyHandledClick)
{
Debug.Log($"🚫 {GetType().Name}: Blocking click due to recent UI interaction");
return true;
}
return false;
}
///
/// Helper method to check if UI is visible before doing coordinate checks
/// Use this as the second check in your IsBlockingClick implementation
///
protected bool ShouldSkipDueToInvisibleUI()
{
if (!IsUIVisible)
{
return true; // Skip blocking if UI is not visible
}
return false;
}
}