using System.Collections;
using System.Collections.Generic;
namespace Robust.Shared.Input
{
///
/// An Input Context to determine which key binds are currently available to the player.
///
public interface IInputCmdContext : IEnumerable
{
///
/// Adds a key function to the set of available functions.
///
///
void AddFunction(BoundKeyFunction function);
///
/// Checks if a key function is available in THIS context (DOES NOT CHECK PARENTS).
///
/// Function to look for.
/// If the function is available.
bool FunctionExists(BoundKeyFunction function);
///
/// Checks if a key function is available in this and ALL parent contexts.
///
/// Function to look for.
/// If the function is available.
bool FunctionExistsHierarchy(BoundKeyFunction function);
///
/// Removes a function from THIS context.
///
/// Function to remove.
void RemoveFunction(BoundKeyFunction function);
string Name { get; }
}
///
internal sealed class InputCmdContext : IInputCmdContext
{
private readonly List _commands = new();
private readonly IInputCmdContext? _parent;
public string Name { get; }
///
/// Creates a new instance of .
///
/// Parent context.
internal InputCmdContext(IInputCmdContext? parent, string name)
{
_parent = parent;
Name = name;
}
///
/// Creates a instance of with no parent.
///
internal InputCmdContext(string name)
{
Name = name;
}
///
public void AddFunction(BoundKeyFunction function)
{
_commands.Add(function);
}
///
public bool FunctionExists(BoundKeyFunction function)
{
return _commands.Contains(function);
}
///
public bool FunctionExistsHierarchy(BoundKeyFunction function)
{
if (_commands.Contains(function))
return true;
if (_parent != null)
return _parent.FunctionExistsHierarchy(function);
return false;
}
///
public void RemoveFunction(BoundKeyFunction function)
{
_commands.Remove(function);
}
///
public IEnumerator GetEnumerator()
{
return _commands.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}