From 39066900773e7857faf2d02e7ed51b71d219d97d Mon Sep 17 00:00:00 2001 From: godosa Date: Tue, 6 Oct 2026 23:39:36 +0200 Subject: godosa-engine: initial public history --- src/Godosa.Core/Input/GamepadState.cs | 58 +++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/Godosa.Core/Input/GamepadState.cs (limited to 'src/Godosa.Core/Input/GamepadState.cs') 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, +} + +/// 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. +public readonly record struct GamepadState(Vector2 LeftStick, Vector2 RightStick, float LeftTrigger, float RightTrigger, PadButtons Buttons) +{ + public bool Held(PadButtons button) => (Buttons & button) != 0; +} + +/// Input-script pad events (PadA PadB PadX PadY PadBack PadStart PadUp PadDown +/// PadLeft PadRight PadLB PadRB PadLT PadRT, case-insensitive): triggers go full on/off. +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 }; + } +} -- cgit