using UnityEngine;
using System.Collections;
///
/// Static utility class that provides common click blocking functionality
/// for UI components that implement IClickBlocker.
/// This avoids the need for inheritance and works with any MonoBehaviour.
///
public static class ClickBlockingHelper
{
///
/// Registers a click blocker with the ClickManager when enabled
/// Call this from your OnEnable method
///
public static void RegisterWithClickManager(IClickBlocker blocker)
{
ClickManager.Instance?.RegisterClickBlocker(blocker);
}
///
/// Unregisters a click blocker from the ClickManager when disabled
/// Call this from your OnDisable method
///
public static void UnregisterWithClickManager(IClickBlocker blocker)
{
ClickManager.Instance?.UnregisterClickBlocker(blocker);
}
///
/// Handles the common click flag logic for UI components.
/// Returns true if a recent UI click should block map clicks.
///
/// Reference to the component's click flag
/// Name of the component for debug logging
/// MonoBehaviour to start coroutines on
/// True if should block due to recent click
public static bool HandleRecentClickCheck(ref bool recentlyHandledClick, string componentName, MonoBehaviour coroutineStarter)
{
if (recentlyHandledClick)
{
Debug.Log($"🚫 {componentName}: Blocking click due to recent UI interaction");
return true;
}
return false;
}
///
/// Sets a temporary click blocking flag and starts the reset coroutine.
/// Call this from UI event handlers (MouseDownEvent, ClickEvent, etc.)
///
/// Name of the component for debug logging
/// MonoBehaviour to start coroutines on
/// Action to set the flag value
/// Coroutine that can be stored by the caller
public static Coroutine SetClickFlag(string componentName, MonoBehaviour coroutineStarter, System.Action flagSetter)
{
flagSetter(true);
Debug.Log($"🚫 {componentName}: Click flag SET - blocking map clicks");
// Stop any existing reset coroutines to prevent premature reset
coroutineStarter.StopAllCoroutines();
// Start the reset coroutine
return coroutineStarter.StartCoroutine(ResetClickFlagCoroutine(componentName, flagSetter));
}
///
/// Coroutine that resets the click blocking flag after a delay
///
private static IEnumerator ResetClickFlagCoroutine(string componentName, System.Action flagSetter)
{
// 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
flagSetter(false);
Debug.Log($"🔓 {componentName}: Click flag reset - map clicks allowed again");
}
///
/// Helper method to check if UI should skip blocking due to being invisible
///
/// Whether the UI is currently visible
/// True if should skip blocking (UI is invisible)
public static bool ShouldSkipDueToInvisibleUI(bool isUIVisible)
{
return !isUIVisible; // Skip blocking if UI is not visible
}
}