Gondwana Game Engine Development (master) - 2.6.1
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
XInputGamepadAdapter.cs
3
5
10{
11 private readonly int _controllerIndex;
12 private readonly HashSet<string> _pressedButtons = new();
13
17 public string GamepadId { get; }
18
22 public IReadOnlyCollection<string> PressedButtons => _pressedButtons;
23
27 public GamepadStickState? LeftStick { get; private set; }
28
32 public GamepadStickState? RightStick { get; private set; }
33
37 public float LeftTrigger { get; private set; }
38
42 public float RightTrigger { get; private set; }
43
48 public XInputGamepadAdapter(int controllerIndex = 0)
49 {
50 _controllerIndex = controllerIndex;
51 GamepadId = $"XInput_{controllerIndex}";
52 }
53
58 internal void Poll()
59 {
60 _pressedButtons.Clear();
61
62 if (GetState(_controllerIndex, out var state) != 0)
63 {
64 ClearState();
65 return;
66 }
67
68 ApplyState(state.Gamepad);
69 }
70
74 internal void ClearState()
75 {
76 _pressedButtons.Clear();
77 LeftStick = null;
78 RightStick = null;
79 LeftTrigger = 0f;
80 RightTrigger = 0f;
81 }
82
87 internal void ApplyState(XINPUT_GAMEPAD gamepad)
88 {
89 _pressedButtons.Clear();
90
91 LeftStick = GamepadStickState.FromRaw16(gamepad.sThumbLX, gamepad.sThumbLY);
92 RightStick = GamepadStickState.FromRaw16(gamepad.sThumbRX, gamepad.sThumbRY);
93 LeftTrigger = gamepad.bLeftTrigger / 255f;
94 RightTrigger = gamepad.bRightTrigger / 255f;
95
96 var buttons = (XInputButtons)gamepad.wButtons;
97 foreach (XInputButtons button in Enum.GetValues<XInputButtons>())
98 {
99 if ((buttons & button) != 0)
100 _pressedButtons.Add(button.ToString());
101 }
102
103 if (LeftTrigger > 0.5f)
104 _pressedButtons.Add("LeftTrigger");
105
106 if (RightTrigger > 0.5f)
107 _pressedButtons.Add("RightTrigger");
108 }
109}
GamepadStickState? LeftStick
Gets the current state of the left analog stick.
float RightTrigger
Gets the current pressure value of the right trigger (0.0 to 1.0).
XInputGamepadAdapter(int controllerIndex=0)
Initializes a new instance of the XInputGamepadAdapter class for the specified controller index.
float LeftTrigger
Gets the current pressure value of the left trigger (0.0 to 1.0).
GamepadStickState? RightStick
Gets the current state of the right analog stick.
IReadOnlyCollection< string > PressedButtons
Gets the collection of currently pressed button names.
string GamepadId
Gets the unique identifier for this gamepad.
Defines an adapter interface for accessing gamepad input state and information.
Represents the state of an analog stick on a gamepad, including both normalized floating-point values...
static GamepadStickState FromRaw16(int rawX, int rawY)
Creates a GamepadStickState instance from raw 16-bit signed integer values.