using UnityEngine; using System.Collections; namespace MoreMountains.Tools { /// /// Input helpers /// public class MMInput : MonoBehaviour { /// /// All possible states for a button. Can be used in a state machine. /// public enum ButtonStates { Off, ButtonDown, ButtonPressed, ButtonUp } public enum AxisTypes { Positive, Negative } /// /// Takes an axis and returns a ButtonState depending on whether the axis is pressed or not (useful for xbox triggers for example), and when you need to use an axis/trigger as a binary thing /// /// The axis as button. /// Axis name. /// Threshold value below which the button is off or released. /// Current state of the axis. public static ButtonStates ProcessAxisAsButton (string axisName, float threshold, ButtonStates currentState, AxisTypes AxisType = AxisTypes.Positive) { float axisValue = Input.GetAxis (axisName); ButtonStates returnState; bool comparison = (AxisType == AxisTypes.Positive) ? (axisValue < threshold) : (axisValue > threshold); if (comparison) { if (currentState == ButtonStates.ButtonPressed) { returnState = ButtonStates.ButtonUp; } else { returnState = ButtonStates.Off; } } else { if (currentState == ButtonStates.Off) { returnState = ButtonStates.ButtonDown; } else { returnState = ButtonStates.ButtonPressed; } } return returnState; } /// /// IM button, short for InputManager button /// public class IMButton { public MMStateMachine State {get;protected set;} public string ButtonID; public delegate void ButtonDownMethodDelegate(); public delegate void ButtonPressedMethodDelegate(); public delegate void ButtonUpMethodDelegate(); public ButtonDownMethodDelegate ButtonDownMethod; public ButtonPressedMethodDelegate ButtonPressedMethod; public ButtonUpMethodDelegate ButtonUpMethod; public float TimeSinceLastButtonDown { get { return Time.unscaledTime - _lastButtonDownAt; } } public float TimeSinceLastButtonUp { get { return Time.unscaledTime - _lastButtonUpAt; } } protected float _lastButtonDownAt; protected float _lastButtonUpAt; public IMButton(string playerID, string buttonID, ButtonDownMethodDelegate btnDown = null, ButtonPressedMethodDelegate btnPressed = null, ButtonUpMethodDelegate btnUp = null) { ButtonID = playerID + "_" + buttonID; ButtonDownMethod = btnDown; ButtonUpMethod = btnUp; ButtonPressedMethod = btnPressed; State = new MMStateMachine (null, false); State.ChangeState (MMInput.ButtonStates.Off); } public virtual void TriggerButtonDown() { _lastButtonDownAt = Time.unscaledTime; if (ButtonDownMethod == null) { State.ChangeState(MMInput.ButtonStates.ButtonDown); } else { ButtonDownMethod(); } } public virtual void TriggerButtonPressed() { if (ButtonPressedMethod == null) { State.ChangeState(MMInput.ButtonStates.ButtonPressed); } else { ButtonPressedMethod(); } } public virtual void TriggerButtonUp() { _lastButtonUpAt = Time.unscaledTime; if (ButtonUpMethod == null) { State.ChangeState(MMInput.ButtonStates.ButtonUp); } else { ButtonUpMethod(); } } } } }