Working item switcher
This commit is contained in:
@@ -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,116 @@ 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);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user