Files
OGG/Assets/Scripts/Game/Input/InputData.cs

385 lines
10 KiB
C#

using UnityEngine;
using System.Collections.Generic;
using UnityEngine.InputSystem;
public static class InputData
{
public const float kDoubleClickThreshold = 0.5f;
public static Vector2 mousePosition;
public static Vector2 mouseDelta;
public static float smoothScrollDelta;
public static float scrollDelta;
public static bool mouseOverUI
{
get; private set;
}
public static MouseButton leftMouseButton;
public static MouseButton middleMouseButton;
public static MouseButton rightMouseButton;
public static Button fireWeapon;
public static Button reload;
public static Button jump;
public static Axis horizontalMovement;
private static List<Button> allButtons = new List<Button>();
private static List<Axis> allAxis = new List<Axis>();
private static float storedScrollDelta;
private static InputConfig inputConfig = new InputConfig();
static InputData()
{
leftMouseButton = AddButton( new MouseButton( Defines.Input.kLeftMouseButton ) );
middleMouseButton = AddButton( new MouseButton( Defines.Input.kMiddleMouseButton ) );
rightMouseButton = AddButton( new MouseButton( Defines.Input.kRightMouseButton ) );
fireWeapon = AddButton( new MultiButton(
new Button[]
{
new MouseButton( Defines.Input.kLeftMouseButton),
new KeyboardButton( KeyCode.LeftControl ),
} ) );
reload = AddButton( new KeyboardButton( KeyCode.R ) );
jump = AddButton( new KeyboardButton( KeyCode.Space ) );
horizontalMovement = AddAxis( new MultiAxis(
new Axis[]
{
new ButtonAxis( new KeyboardButton( KeyCode.A ), new KeyboardButton( KeyCode.D ) )
} ) );
}
public static T AddButton<T>( T value ) where T : Button
{
allButtons.Add( value );
return value;
}
public static T AddAxis<T>( T value ) where T : Axis
{
allAxis.Add( value );
return value;
}
public class MouseButton : Button
{
public Vector2 downPosition;
public int mouseButton;
public MouseButton( int button )
{
mouseButton = button;
}
public override bool GetButton()
{
return Input.GetMouseButton( mouseButton );
}
protected override bool HeldCancelCheck()
{
return ( mousePosition - downPosition ).magnitude <= Defines.Input.kShortClickCancelMinMouseMovement;
}
public override void Update( float unscaledTime )
{
base.Update( unscaledTime );
if( pressed )
{
downPosition = mousePosition;
}
}
}
public class KeyboardButton : Button
{
private KeyCode key;
public KeyboardButton( KeyCode keycode )
{
key = keycode;
}
public override bool GetButton()
{
return Input.GetKey( key );
}
}
public class MultiButton : Button
{
private Button[] buttons;
public MultiButton( Button[] value )
{
buttons = value;
}
public override bool GetButton()
{
bool down = false;
for( int i = buttons.Length - 1; i >= 0; --i )
{
down |= buttons[i].GetButton();
}
return down;
}
}
public abstract class Button
{
/// <summary>
/// true when the mouse button is down.
/// </summary>
public bool down;
/// <summary>
/// true for the frame that the mouse button pressed (On Click).
/// </summary>
public bool pressed;
/// <summary>
/// true on the frame that the mouse button has been down
/// for X number of frames without being moved.
/// </summary>
public bool held;
/// <summary>
/// true on the frame the mouse button is released.
/// </summary>
public bool released;
/// <summary>
/// true if the mouse is clicked quickly and not moved much.
/// example: right clicking to cancel placement of item.
/// </summary>
public bool shortClick;
/// <summary>
/// The time that the button was presesd.
/// </summary>
public float downTime;
/// <summary>
/// True when the user has double clicked this button.
/// </summary>
public bool doubleClick;
/// <summary>
/// How long this button has been down for. Only valid when
/// down == true
/// </summary>
public float downLength;
private bool isHeld;
private float lastClickTime;
private Vector3 lastClickPosition;
public abstract bool GetButton();
/// <summary>
/// Overload this funtion in order to have
/// the mouse be able to cancel the held functionality
/// if the user moves the mouse.
/// </summary>
/// <returns>If the held or shortclick should be cancelled</returns>
protected virtual bool HeldCancelCheck()
{
return true;
}
public virtual void Update( float unscaledTime )
{
bool value = GetButton();
pressed = value && down == false;
released = value == false && down == true;
shortClick = false;
down = value;
bool isHeldValue = pressed == false && down && downTime + Defines.Input.kHeldClickDelay < unscaledTime && HeldCancelCheck();
held = isHeld == false && isHeldValue == true; // Only show this as true for a single frame.
isHeld = isHeldValue;
if( pressed )
{
downTime = unscaledTime;
downLength = 0.0f;
doubleClick = lastClickTime + kDoubleClickThreshold >= unscaledTime && Vector2.Distance( mousePosition, lastClickPosition ) < 5.0f;
}
if( released )
{
if( unscaledTime - downTime <= Defines.Input.kShortClickDelay
&& HeldCancelCheck() )
{
shortClick = true;
}
lastClickTime = unscaledTime;
doubleClick = false;
}
if( down )
{
downLength = unscaledTime - downTime;
lastClickPosition = mousePosition;
}
}
}
public abstract class Axis
{
public float value;
public abstract float GetAxisValue();
public virtual void Update( float unscaledTime)
{
value = GetAxisValue();
}
}
public class InputAxis : Axis
{
public string axisName;
public InputAxis( string name )
{
axisName = name;
}
public override float GetAxisValue()
{
return Input.GetAxis( axisName );
}
}
public class TriggerNormaliser : Axis
{
private Axis trigger;
bool invert = false;
public TriggerNormaliser( Axis value, bool doInvert )
{
trigger = value;
invert = doInvert;
}
public override float GetAxisValue()
{
float value = trigger.GetAxisValue();
if( invert == false )
{
value += 1.0f;
value /= 2.0f;
}
value *= invert ? -1.0f : 1.0f;
return value;
}
}
public class InvertAxis : Axis
{
private Axis targetAxis;
public InvertAxis( Axis target )
{
targetAxis = target;
}
public override float GetAxisValue()
{
return -1 * targetAxis.GetAxisValue();
}
}
public class ButtonAxis : Axis
{
private Button positiveButton;
private Button negativeButton;
public ButtonAxis( Button negativeButton, Button positiveButton )
{
this.positiveButton = positiveButton;
this.negativeButton = negativeButton;
}
public override float GetAxisValue()
{
return ( positiveButton.down ? 1.0f : 0.0f ) + ( negativeButton.down ? -1.0f : 0.0f );
}
public override void Update( float unscaledTime )
{
positiveButton.Update( unscaledTime );
negativeButton.Update( unscaledTime );
base.Update( unscaledTime );
}
}
public class InputActionAxis : Axis
{
private InputAction action;
public InputActionAxis( InputAction inputAction )
{
action = inputAction;
}
public override float GetAxisValue()
{
return action.ReadValue<float>();
}
}
public class MultiAxis : Axis
{
protected Axis[] axis;
public MultiAxis( Axis[] axis )
{
this.axis = axis;
}
public override float GetAxisValue()
{
float value = 0.0f;
for( int i = 0; i < axis.Length; i++ )
{
value += axis[i].GetAxisValue();
}
value = Mathf.Clamp( value, -1.0f, 1.0f );
return value;
}
}
public static void Update( float unscaledTime )
{
mouseDelta = mousePosition - (Vector2)Input.mousePosition;
mousePosition = Input.mousePosition;
//mouseOverUI = UIUtils.MouseIsOverUI();
foreach( Button i in allButtons )
{
i.Update( unscaledTime );
}
foreach( Axis i in allAxis )
{
i.Update( unscaledTime);
}
#if UNITY_STANDALONE_WIN
scrollDelta = Input.mouseScrollDelta.y;
#else
scrollDelta = ( Input.GetKey( KeyCode.LeftShift ) || Input.GetKey( KeyCode.RightShift ) ) ? Input.mouseScrollDelta.x : Input.mouseScrollDelta.y;
#endif
storedScrollDelta += scrollDelta;
float smoothAmount = Mathf.Lerp( 0.0f, storedScrollDelta, Time.unscaledDeltaTime * 5.0f );
storedScrollDelta -= smoothAmount;
smoothScrollDelta = smoothAmount;
}
}