ClickBlockingHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// Static utility class that provides common click blocking functionality
  5. /// for UI components that implement IClickBlocker.
  6. /// This avoids the need for inheritance and works with any MonoBehaviour.
  7. /// </summary>
  8. public static class ClickBlockingHelper
  9. {
  10. /// <summary>
  11. /// Registers a click blocker with the ClickManager when enabled
  12. /// Call this from your OnEnable method
  13. /// </summary>
  14. public static void RegisterWithClickManager(IClickBlocker blocker)
  15. {
  16. ClickManager.Instance?.RegisterClickBlocker(blocker);
  17. }
  18. /// <summary>
  19. /// Unregisters a click blocker from the ClickManager when disabled
  20. /// Call this from your OnDisable method
  21. /// </summary>
  22. public static void UnregisterWithClickManager(IClickBlocker blocker)
  23. {
  24. ClickManager.Instance?.UnregisterClickBlocker(blocker);
  25. }
  26. /// <summary>
  27. /// Handles the common click flag logic for UI components.
  28. /// Returns true if a recent UI click should block map clicks.
  29. /// </summary>
  30. /// <param name="recentlyHandledClick">Reference to the component's click flag</param>
  31. /// <param name="componentName">Name of the component for debug logging</param>
  32. /// <param name="coroutineStarter">MonoBehaviour to start coroutines on</param>
  33. /// <returns>True if should block due to recent click</returns>
  34. public static bool HandleRecentClickCheck(ref bool recentlyHandledClick, string componentName, MonoBehaviour coroutineStarter)
  35. {
  36. if (recentlyHandledClick)
  37. {
  38. Debug.Log($"🚫 {componentName}: Blocking click due to recent UI interaction");
  39. return true;
  40. }
  41. return false;
  42. }
  43. /// <summary>
  44. /// Sets a temporary click blocking flag and starts the reset coroutine.
  45. /// Call this from UI event handlers (MouseDownEvent, ClickEvent, etc.)
  46. /// </summary>
  47. /// <param name="componentName">Name of the component for debug logging</param>
  48. /// <param name="coroutineStarter">MonoBehaviour to start coroutines on</param>
  49. /// <param name="flagSetter">Action to set the flag value</param>
  50. /// <returns>Coroutine that can be stored by the caller</returns>
  51. public static Coroutine SetClickFlag(string componentName, MonoBehaviour coroutineStarter, System.Action<bool> flagSetter)
  52. {
  53. flagSetter(true);
  54. Debug.Log($"🚫 {componentName}: Click flag SET - blocking map clicks");
  55. // Stop any existing reset coroutines to prevent premature reset
  56. coroutineStarter.StopAllCoroutines();
  57. // Start the reset coroutine
  58. return coroutineStarter.StartCoroutine(ResetClickFlagCoroutine(componentName, flagSetter));
  59. }
  60. /// <summary>
  61. /// Coroutine that resets the click blocking flag after a delay
  62. /// </summary>
  63. private static IEnumerator ResetClickFlagCoroutine(string componentName, System.Action<bool> flagSetter)
  64. {
  65. // Wait longer to ensure we block clicks that happen in the same frame
  66. yield return new WaitForEndOfFrame(); // Wait one frame
  67. yield return new WaitForEndOfFrame(); // Wait another frame
  68. yield return new WaitForSeconds(0.1f); // Wait additional time to be extra safe
  69. flagSetter(false);
  70. Debug.Log($"🔓 {componentName}: Click flag reset - map clicks allowed again");
  71. }
  72. /// <summary>
  73. /// Helper method to check if UI should skip blocking due to being invisible
  74. /// </summary>
  75. /// <param name="isUIVisible">Whether the UI is currently visible</param>
  76. /// <returns>True if should skip blocking (UI is invisible)</returns>
  77. public static bool ShouldSkipDueToInvisibleUI(bool isUIVisible)
  78. {
  79. return !isUIVisible; // Skip blocking if UI is not visible
  80. }
  81. }