using System;
using System.Collections.Generic;
using UnityEngine;
namespace Framework.Input {
/// Action that combines any number of input devices
internal class Action :
IAction,
ObservableUniqueCollection.IObserver,
ObservableUniqueCollection.IObserver {
#region interface IBindingObserver
/// Interface through which the action notifies its observers of changes
public interface IBindingObserver {
/// Called when all bindings are about to be removed from the action
/// Action from which all bindings are being removed
void RemovingAllBindings(Action action);
/// Called when a key or joystick button has been bound
/// Action to which the key or button has been bound
/// Key or button that has been bound
void KeyBindingAdded(Action action, KeyCode keyCode);
/// Called when a key or joystick button binding has been removed
/// Action from which the key or button has been removed
/// Key or button that is no longer bound
void KeyBindingRemoved(Action action, KeyCode keyCode);
/// Called when a joystick axis has been bound
/// Action to which the joystick axis has been bound
/// Joystick axis that has been bound
void axisAdded(Action action, JoystickAxis axis);
/// Called when a joystick axis binding has been removed
/// Action from which the joystick axis has been removed
/// Joystick axis that is no longer bound
void axisRemoved(Action action, JoystickAxis axis);
}
#endregion // interface IBindingObserver
/// Initializes a new action
/// Name that uniquely identifies the action
/// Describes what the action is used for
public Action(string name, string description) {
this.name = name;
this.description = description;
this.observers = new List();
this.keyBindings = new ObservableUniqueCollection();
this.keyBindings.AddObserver(this);
this.axisBindings = new ObservableUniqueCollection();
this.axisBindings.AddObserver(this);
}
/// Adds a binding observer to the action
/// Observer that will get notified of binding changes
public void AddObserver(IBindingObserver observer) {
this.observers.Add(observer);
}
/// Removes a binding observer from the action
/// Observer that will no longer get notified
public void RemoveObserver(IBindingObserver observer) {
this.observers.Remove(observer);
}
/// Unique name of the action
public string Name {
get { return this.name; }
}
/// Describes what purpose the action serves
///
/// Can be displayed to the user in the key binding screen, for example,
/// to explain what the action does.
///
public string Description {
get { return this.description; }
}
/// Keys and buttons that have been bound to this action
///
/// Simply adding a key or button to this collection will bind it.
///
public ICollection BoundKeys {
get { return this.keyBindings; }
}
/// Joystick axes that have been bound to this action
///
/// Simply adding a joystick axis to this collection will bind it.
///
public ICollection BoundJoystickAxes {
get { return this.axisBindings; }
}
/// Whether the action is currently active
///
/// If joystick axes are bound to this action, it becomes active when the axis
/// goes over 67% and inactive when the axis goes under 33%. This prevents
/// flickering of the input between active/inactive if the axis has noise.
///
public bool IsActive {
get { return this.isActive; }
set { this.isActive = value; }
}
/// Whether the action has become active since it was last checked
///
/// This goes high as soon as the action becomes active and is only reset
/// in the next update phase after it has been queried at least once.
///
public bool WasTriggered {
get {
this.WasChecked = true;
return this.wasTriggered;
}
set { this.wasTriggered = value; }
}
/// Analogue state of the action
///
/// If keys or buttons are bound to the action, these will immediately result
/// in a value of 1.0. Analogue axes will result in v
///
public float State {
get { return this.state; }
set { this.state = value; }
}
/// Removes all bound keys, buttons and axes from the action
public void RemoveAllBindings() {
for(int index = 0; index < this.observers.Count; ++index) {
this.observers[index].RemovingAllBindings(this);
}
this.keyBindings.ClearWithoutNotification();
this.axisBindings.ClearWithoutNotification();
}
/// Whether the action is no longer part of the input mapper
public bool IsAbandoned {
get { return (this.observers.Count == 0); }
}
/// Called when a key or button is added to the bound keys
/// Key or button that has been bound to this action
void ObservableUniqueCollection.IObserver.ItemAdded(KeyCode key) {
for(int index = 0; index < this.observers.Count; ++index) {
this.observers[index].KeyBindingAdded(this, key);
}
}
/// Called when a key or button is removed from the bound keys
/// Key or button that has been unbound from this action
void ObservableUniqueCollection.IObserver.ItemRemoved(KeyCode key) {
for(int index = 0; index < this.observers.Count; ++index) {
this.observers[index].KeyBindingRemoved(this, key);
}
}
/// Called when a joystick axis is added to the bound axes
/// Joystick axis that has been bound to this action
void ObservableUniqueCollection.IObserver.ItemAdded(JoystickAxis axis) {
for(int index = 0; index < this.observers.Count; ++index) {
this.observers[index].axisAdded(this, axis);
}
}
/// Called when a joystick axis is removed from the bound axes
/// Joystick axis that has been unbound from this action
void ObservableUniqueCollection.IObserver.ItemRemoved(JoystickAxis axis) {
for(int index = 0; index < this.observers.Count; ++index) {
this.observers[index].axisRemoved(this, axis);
}
}
///
/// Whether the action's triggered state has been checked this update cycle
///
public bool WasChecked;
/// Unique name of the action
private string name;
/// Describes what purpose the action serves
private string description;
/// Whether the action is currently active (pressed/moved)
private bool isActive;
/// Whether the action has been active since it was last checked
private bool wasTriggered;
/// Current analogue state of the action
private float state;
/// Keys that have been bound to the action
private ObservableUniqueCollection keyBindings;
/// Joystick axes that have been bound to the action
private ObservableUniqueCollection axisBindings;
/// Observers that receive notifications to binding changes
private IList observers;
}
} // namespace Framework.Input