carotrauma/scripts/libs/input/InputAction.cs
Nick Guy c98d7739aa
All checks were successful
/ build (push) Successful in 1m32s
Add custom input mapping on top of Godot's InputMap
2024-06-02 17:10:15 +01:00

112 lines
No EOL
3 KiB
C#

using System;
using System.Collections.Generic;
using Godot;
namespace Carotrauma.scripts.libs.input;
public abstract class InputAction {
public string Name = Guid.NewGuid().ToString();
public InputEvent Event => getEvent();
protected abstract InputEvent getEvent();
public float GetStrength() {
return Input.GetActionStrength(Name);
}
public static implicit operator String(InputAction a) {
return a.Name;
}
public bool IsPressed() {
return Input.IsActionPressed(Name);
}
public bool IsJustPressed() {
return Input.IsActionJustPressed(Name);
}
public bool IsJustReleased() {
return Input.IsActionJustReleased(Name);
}
}
public class InputAction<T> : InputAction where T : new() {
protected override InputEvent getEvent() {
var evt = new T();
populateEvent(ref evt);
return evt as InputEvent;
}
protected virtual void populateEvent(ref T evt) {}
}
public class KeyInputAction : InputAction<InputEventKey> {
public Key Key;
protected override void populateEvent(ref InputEventKey evt) {
evt.Keycode = Key;
}
//
// public bool IsPressed() {
// LastPolledPressed = Input.IsKeyPressed(Key);
// return LastPolledPressed;
// }
//
// public bool IsJustPressed() {
// bool Pressed = Input.IsActionPressed(Key);
// bool JustPressed = Pressed && !LastPolledPressed;
// LastPolledPressed = Pressed;
// return JustPressed;
// }
//
// public bool IsJustReleased() {
// bool Pressed = Input.IsKeyPressed(Key);
// bool JustReleased = !Pressed && LastPolledPressed;
// LastPolledPressed = Pressed;
// return JustReleased;
// }
}
public class MouseButtonInputAction : InputAction<InputEventMouseButton> {
public MouseButton Button;
protected override void populateEvent(ref InputEventMouseButton evt) {
evt.ButtonIndex = Button;
}
}
public class AxisInputAction {
public AxisInputAction(string positiveActionName, string negativeActionName) {
PositiveActionName = positiveActionName;
NegativeActionName = negativeActionName;
}
public string PositiveActionName;
public string NegativeActionName;
public float GetStrength() {
return Input.GetAxis(NegativeActionName, PositiveActionName);
}
}
public class VectorInputAction {
private readonly string _positiveXName;
private readonly string _negativeXName;
private readonly string _positiveYName;
private readonly string _negativeYName;
public VectorInputAction(string positiveXName, string negativeXName, string positiveYName, string negativeYName) {
_positiveXName = positiveXName;
_negativeXName = negativeXName;
_positiveYName = positiveYName;
_negativeYName = negativeYName;
}
public Vector2 GetStrength() {
return Input.GetVector(_negativeXName, _positiveXName, _negativeYName, _positiveYName);
}
}