using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
namespace Framework.Input {
/// Stores the input actions set up for a game
internal class ActionCollection : Collection, IActionCollection {
/// Called before all actions are removed from the collection
public event Action ActionsCleared;
/// Called after a single action has been removed from the collection
public event Action ActionRemoved;
/// Accesses an action by its name
/// Name of the action that will be accessed
/// The action with the specified name
public IAction this[string actionName] {
get {
int index = IndexOf(actionName);
if(index == -1) {
throw new ArgumentException(
"An action with the specified name does not exist", "actionName"
);
} else {
return Items[index];
}
}
}
/// Tries to look up an action by its name
/// Name of the action that will be looked up
/// Receives the action, if found
/// True if the action was found, false otherwise
public bool TryGet(string actionName, out IAction action) {
int index = IndexOf(actionName);
if(index == -1) {
action = null;
return false;
} else {
action = Items[index];
return true;
}
}
/// Removes an action by its name
/// Name of the action that will be removed
/// True if an action with the specified name was found and removed
public bool Remove(string actionName) {
int index = IndexOf(actionName);
if(index == -1) {
return false;
} else {
base.RemoveAt(index);
return true;
}
}
/// Checks whether the collection contains the specified action
/// Action for which the collection will be checked
/// True if the collection contains an action with the specified name
public bool Contains(string actionName) {
return (IndexOf(actionName) != -1);
}
/// Adds an action to the collection from the input mapper
/// Action that will be added
///
/// This circumvents the tampering check that prevents the user from re-adding
/// removed actions or some custom IAction implementation into the collection.
///
public void InternalAdd(Action action) {
Items.Add(action);
}
/// Called when the user tries to replace an action with another
/// Index of the action that should be overwritten
/// Action that should replace the existing one
protected override void SetItem(int index, IAction action) {
throw new InvalidOperationException(
"You cannot replace actions in this collection"
);
}
/// Called when the user tries to insert an action into the collection
/// Index at which the action should be inserted
/// Action that should be inserted
protected override void InsertItem(int index, IAction action) {
throw new InvalidOperationException(
"You cannot insert your own actions into this collection"
);
}
/// Called when the user has removed all actions from the collection
protected override void ClearItems() {
base.ClearItems();
if(this.ActionsCleared != null) {
this.ActionsCleared(this);
}
}
/// Called when the user has removed a single action from the collection
/// Index of the item that wil be removed
protected override void RemoveItem(int index) {
if(this.ActionRemoved != null) {
this.ActionRemoved(this, Items[index]);
}
base.RemoveItem(index);
}
/// Returns the index of the specified action
/// Name of the action whose index will be determined
/// The index of the specified action or -1 if not found
protected int IndexOf(string actionName) {
bool ignoreCase = true;
IList items = Items;
for(int index = 0; index < items.Count; ++index) {
if(string.Compare(items[index].Name, actionName, ignoreCase) == 0) {
return index;
}
}
return -1;
}
}
} // namespace Framework.Input