maze_switching (#82)

Co-authored-by: Michal Pikulski <michal.a.pikulski@gmail.com>
Reviewed-on: #82
This commit is contained in:
2025-12-15 09:20:15 +00:00
parent 34a6c367cc
commit c62f169d08
32 changed files with 2226 additions and 1155 deletions

View File

@@ -33,6 +33,9 @@ namespace Input
// Track which consumer is handling the current hold operation
private ITouchInputConsumer _activeHoldConsumer;
// Controller registration system
private readonly Dictionary<string, ITouchInputConsumer> _registeredControllers = new Dictionary<string, ITouchInputConsumer>();
/// <summary>
/// Singleton instance of the InputManager. No longer creates an instance if one doesn't exist.
@@ -408,5 +411,126 @@ namespace Input
consumer.OnTap(worldPos);
return true;
}
#region Controller Registration System
/// <summary>
/// Registers a controller with a unique name for later switching.
/// </summary>
/// <param name="controllerName">Unique name for the controller</param>
/// <param name="controller">The controller instance to register</param>
/// <param name="setAsDefaultConsumer">If true, sets this controller as the default input consumer</param>
public void RegisterController(string controllerName, ITouchInputConsumer controller, bool setAsDefaultConsumer = false)
{
if (string.IsNullOrEmpty(controllerName))
{
Debug.LogError("[InputManager] Cannot register controller with null or empty name.");
return;
}
if (controller == null)
{
Debug.LogError($"[InputManager] Cannot register null controller for name: {controllerName}");
return;
}
if (_registeredControllers.ContainsKey(controllerName))
{
Debug.LogWarning($"[InputManager] Controller with name '{controllerName}' is already registered. Overwriting.");
}
_registeredControllers[controllerName] = controller;
Logging.Debug($"Controller registered: {controllerName}");
if (setAsDefaultConsumer)
{
SetDefaultConsumer(controller);
Logging.Debug($"Controller '{controllerName}' set as default consumer.");
}
}
/// <summary>
/// Unregisters a controller by name.
/// </summary>
/// <param name="controllerName">Name of the controller to unregister</param>
public void UnregisterController(string controllerName)
{
if (string.IsNullOrEmpty(controllerName))
{
Debug.LogError("[InputManager] Cannot unregister controller with null or empty name.");
return;
}
if (_registeredControllers.Remove(controllerName))
{
Logging.Debug($"Controller unregistered: {controllerName}");
}
else
{
Debug.LogWarning($"[InputManager] Attempted to unregister non-existent controller: {controllerName}");
}
}
/// <summary>
/// Gets a registered controller by name.
/// </summary>
/// <param name="controllerName">Name of the controller to retrieve</param>
/// <returns>The controller if found, null otherwise</returns>
public ITouchInputConsumer GetController(string controllerName)
{
if (string.IsNullOrEmpty(controllerName))
{
Debug.LogError("[InputManager] Cannot get controller with null or empty name.");
return null;
}
if (_registeredControllers.TryGetValue(controllerName, out ITouchInputConsumer controller))
{
return controller;
}
Debug.LogWarning($"[InputManager] Controller not found: {controllerName}");
return null;
}
/// <summary>
/// Switches to a registered controller by name, setting it as the default consumer.
/// </summary>
/// <param name="controllerName">Name of the controller to switch to</param>
/// <returns>True if the switch was successful, false otherwise</returns>
public bool SwitchToController(string controllerName)
{
ITouchInputConsumer controller = GetController(controllerName);
if (controller != null)
{
SetDefaultConsumer(controller);
Logging.Debug($"Switched to controller: {controllerName}");
return true;
}
return false;
}
/// <summary>
/// Checks if a controller with the given name is registered.
/// </summary>
/// <param name="controllerName">Name to check</param>
/// <returns>True if registered, false otherwise</returns>
public bool IsControllerRegistered(string controllerName)
{
return !string.IsNullOrEmpty(controllerName) && _registeredControllers.ContainsKey(controllerName);
}
/// <summary>
/// Gets the currently active controller (the default consumer).
/// This is the controller that currently has input control.
/// </summary>
/// <returns>The active controller, or null if no default consumer is set</returns>
public ITouchInputConsumer GetActiveController()
{
return defaultConsumer;
}
#endregion
}
}