aboutsummaryrefslogtreecommitdiffziptar.gz
path: root/src/Godosa.Core/Input/GamepadState.cs
diff options
context:
space:
mode:
authorgodosa <godosa@godosa.eu>2026-10-06 23:39:36 +0200
committergodosa <godosa@godosa.eu>2026-10-06 23:39:36 +0200
commit39066900773e7857faf2d02e7ed51b71d219d97d (patch)
treeffab8e4ddd971626776c3b1f4ce1da2c186c2187 /src/Godosa.Core/Input/GamepadState.cs
downloadgodosa-engine-39066900773e7857faf2d02e7ed51b71d219d97d.tar.gz
godosa-engine-39066900773e7857faf2d02e7ed51b71d219d97d.zip
godosa-engine: initial public history
Diffstat (limited to 'src/Godosa.Core/Input/GamepadState.cs')
-rw-r--r--src/Godosa.Core/Input/GamepadState.cs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/Godosa.Core/Input/GamepadState.cs b/src/Godosa.Core/Input/GamepadState.cs
new file mode 100644
index 0000000..5f1d5a5
--- /dev/null
+++ b/src/Godosa.Core/Input/GamepadState.cs
@@ -0,0 +1,58 @@
+using System.Numerics;
+
+namespace Godosa.Core.Input;
+
+[Flags]
+public enum PadButtons
+{
+ None = 0,
+ A = 1 << 0,
+ B = 1 << 1,
+ X = 1 << 2,
+ Y = 1 << 3,
+ Back = 1 << 4,
+ Start = 1 << 5,
+ LeftShoulder = 1 << 6,
+ RightShoulder = 1 << 7,
+ DpadUp = 1 << 8,
+ DpadDown = 1 << 9,
+ DpadLeft = 1 << 10,
+ DpadRight = 1 << 11,
+}
+
+/// <summary>Raw held pad state, no game meaning: sticks −1…1 (SDL convention, stick up =
+/// −Y), triggers 0…1. Each game maps it to its own input frame.</summary>
+public readonly record struct GamepadState(Vector2 LeftStick, Vector2 RightStick, float LeftTrigger, float RightTrigger, PadButtons Buttons)
+{
+ public bool Held(PadButtons button) => (Buttons & button) != 0;
+}
+
+/// <summary>Input-script pad events (<c>PadA PadB PadX PadY PadBack PadStart PadUp PadDown
+/// PadLeft PadRight PadLB PadRB PadLT PadRT</c>, case-insensitive): triggers go full on/off.</summary>
+public static class GamepadScript
+{
+ public static GamepadState? Apply(GamepadState state, string name, bool down)
+ {
+ string key = name.ToLowerInvariant();
+ if (key == "padlt") return state with { LeftTrigger = down ? 1f : 0f };
+ if (key == "padrt") return state with { RightTrigger = down ? 1f : 0f };
+ PadButtons button = key switch
+ {
+ "pada" => PadButtons.A,
+ "padb" => PadButtons.B,
+ "padx" => PadButtons.X,
+ "pady" => PadButtons.Y,
+ "padback" => PadButtons.Back,
+ "padstart" => PadButtons.Start,
+ "padup" => PadButtons.DpadUp,
+ "paddown" => PadButtons.DpadDown,
+ "padleft" => PadButtons.DpadLeft,
+ "padright" => PadButtons.DpadRight,
+ "padlb" => PadButtons.LeftShoulder,
+ "padrb" => PadButtons.RightShoulder,
+ _ => PadButtons.None,
+ };
+ if (button == PadButtons.None) return null;
+ return state with { Buttons = down ? state.Buttons | button : state.Buttons & ~button };
+ }
+}