| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- using UnityEngine;
- using System.Collections;
- /// <summary>
- /// Base class for UI components that need to block map clicks.
- /// Handles the common click blocking functionality including registration,
- /// flag management, and coroutine handling.
- /// </summary>
- public abstract class BaseClickBlocker : MonoBehaviour, IClickBlocker
- {
- /// <summary>
- /// Flag to prevent map clicks when UI handles them
- /// </summary>
- [System.NonSerialized]
- protected bool recentlyHandledClick = false;
- /// <summary>
- /// Whether this UI is currently visible and should block clicks
- /// </summary>
- protected virtual bool IsUIVisible => true;
- /// <summary>
- /// Auto-register/unregister with ClickManager
- /// </summary>
- protected virtual void OnEnable() => ClickManager.Instance?.RegisterClickBlocker(this);
- protected virtual void OnDisable() => ClickManager.Instance?.UnregisterClickBlocker(this);
- /// <summary>
- /// Abstract method that each UI component must implement to define its specific blocking logic
- /// </summary>
- public abstract bool IsBlockingClick(Vector2 screenPosition);
- /// <summary>
- /// 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.)
- /// </summary>
- 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());
- }
- /// <summary>
- /// Resets the click blocking flag after a short delay
- /// </summary>
- 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");
- }
- /// <summary>
- /// Helper method to check if a recent UI click should block the current check
- /// Use this as the first check in your IsBlockingClick implementation
- /// </summary>
- protected bool ShouldBlockDueToRecentClick()
- {
- if (recentlyHandledClick)
- {
- Debug.Log($"🚫 {GetType().Name}: Blocking click due to recent UI interaction");
- return true;
- }
- return false;
- }
- /// <summary>
- /// Helper method to check if UI is visible before doing coordinate checks
- /// Use this as the second check in your IsBlockingClick implementation
- /// </summary>
- protected bool ShouldSkipDueToInvisibleUI()
- {
- if (!IsUIVisible)
- {
- return true; // Skip blocking if UI is not visible
- }
- return false;
- }
- }
|