using System; using UnityEngine; namespace Framework.Input.States { /// Gives an input action a default joystick axis [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = true)] public class DefaultJoystickAxisAttribute : Attribute { /// Initializes a new default joystick axis provider /// /// Joystick axis that will be bound to both the negative and positive side /// of the action by default /// public DefaultJoystickAxisAttribute(string defaultAxis) { if(!string.IsNullOrEmpty(defaultAxis)) { int lastCharacterIndex = defaultAxis.Length - 1; char lastCharacter = defaultAxis[lastCharacterIndex]; if(lastCharacter == '-') { this.defaultPositiveAxis = new JoystickAxis( defaultAxis.Substring(0, lastCharacterIndex), true ); } else if(lastCharacter == '+') { this.defaultPositiveAxis = new JoystickAxis( defaultAxis.Substring(0, lastCharacterIndex), false ); } else { this.defaultNegativeAxis = new JoystickAxis(defaultAxis, true); this.defaultPositiveAxis = new JoystickAxis(defaultAxis, false); } } } /// Initializes a new default joystick axis provider /// /// Joystick axis that will be bound to the negative side of the action by default /// /// /// Joystick axis that will be bound to the positive side of the action by default /// public DefaultJoystickAxisAttribute(string defaultNegativeAxis, string defaultPositiveAxis) { if(!string.IsNullOrEmpty(defaultNegativeAxis)) { this.defaultNegativeAxis = joystickAxisFromAxisName(defaultNegativeAxis); } if(!string.IsNullOrEmpty(defaultPositiveAxis)) { this.defaultPositiveAxis = joystickAxisFromAxisName(defaultPositiveAxis); } } /// /// Default joystick axis that will be bound to the negative side of the axis /// public JoystickAxis DefaultNegativeAxis { get { return this.defaultNegativeAxis; } } /// /// Default joystick axis that will be bound to the negative side of the axis /// public JoystickAxis DefaultPositiveAxis { get { return this.defaultPositiveAxis; } } /// Converts a joystick axis name with postfix into a joystick axis /// Name of the joystick axis with postfix /// The matching joystick axis structure private static JoystickAxis joystickAxisFromAxisName(string axisName) { if(string.IsNullOrEmpty(axisName)) { return new JoystickAxis(); } else { int lastCharacterIndex = axisName.Length - 1; char lastCharacter = axisName[lastCharacterIndex]; if(lastCharacter == '-') { return new JoystickAxis(axisName.Substring(0, lastCharacter), true); } else if(lastCharacter == '+') { return new JoystickAxis(axisName.Substring(0, lastCharacter), false); } else { Debug.LogWarning( "Joystick axis '" + axisName + "' does not specify whether to use " + "the negative or the positive side of the axis. Binding not performed." ); return new JoystickAxis(); } } } /// /// Default joystick axis that will be bound to the negative side of the axis /// private JoystickAxis defaultNegativeAxis; /// /// Default joystick axis that will be bound to the positive side of the axis /// private JoystickAxis defaultPositiveAxis; } } // namespace Framework.Input.States