Gondwana Game Engine Development (master) - 2.7.0
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
GamepadStickState.cs
2
8public readonly struct GamepadStickState
9{
14 public float X { get; }
15
20 public float Y { get; }
21
25 public int RawX { get; }
26
30 public int RawY { get; }
31
51 float x,
52 float y,
53 int rawX = 0,
54 int rawY = 0)
55 {
56 X = x;
57 Y = y;
58 RawX = rawX;
59 RawY = rawY;
60 }
61
71 public static GamepadStickState FromRaw16(int rawX, int rawY)
72 {
73 const float range = 32767f; // signed range: -32768 to 32767
74 float normX = Math.Clamp(rawX / range, -1f, 1f);
75 float normY = Math.Clamp(rawY / range, -1f, 1f);
76
77 return new GamepadStickState(
78 normX,
79 normY,
80 rawX,
81 rawY);
82 }
83
93 public static GamepadStickState FromRawUnsigned16(ushort rawX, ushort rawY)
94 {
95 float normX = (rawX - 32768f) / 32767f;
96 float normY = (rawY - 32768f) / 32767f;
97
98 return new GamepadStickState(
99 normX,
100 normY,
101 rawX,
102 rawY);
103 }
104
108 public float Magnitude => MathF.Sqrt(X * X + Y * Y);
109
113 public float Angle => MathF.Atan2(Y, X);
114
120 public bool IsEngaged(float threshold = 0.15f) => Magnitude >= threshold;
121
127 public StickDirection Direction(float threshold = 0.15f)
128 {
129 if (!IsEngaged(threshold))
130 return StickDirection.None;
131
132 var dir = StickDirection.None;
133
134 if (Y >= threshold)
135 dir |= StickDirection.Up;
136
137 if (Y <= -threshold)
138 dir |= StickDirection.Down;
139
140 if (X <= -threshold)
141 dir |= StickDirection.Left;
142
143 if (X >= threshold)
144 dir |= StickDirection.Right;
145
146 return dir;
147 }
148
154 public GamepadStickState WithDeadzone(float threshold = 0.15f)
155 {
156 return IsEngaged(threshold)
157 ? this
158 : new GamepadStickState(
159 0,
160 0,
161 RawX,
162 RawY);
163 }
164
170 public override string ToString() => $"({X:0.00}, {Y:0.00})";
171}
StickDirection
Represents the directional state of an analog stick on a gamepad. This is a flags enumeration,...
static GamepadStickState FromRawUnsigned16(ushort rawX, ushort rawY)
Creates a GamepadStickState instance from raw 16-bit unsigned integer values.
float Angle
Returns the angle (in radians) relative to (1, 0).
float X
Gets the normalized horizontal position of the analog stick in the range [-1, 1], where -1 represents...
bool IsEngaged(float threshold=0.15f)
Returns true if the stick is pushed beyond the threshold.
GamepadStickState WithDeadzone(float threshold=0.15f)
Returns a new stick state with a deadzone applied.
int RawY
Raw vertical value before normalization (e.g., from XInput or SDL2)
static GamepadStickState FromRaw16(int rawX, int rawY)
Creates a GamepadStickState instance from raw 16-bit signed integer values.
float Magnitude
Returns the magnitude (0 to 1) of the stick position.
float Y
Gets the normalized vertical position of the analog stick in the range [-1, 1], where -1 represents f...
GamepadStickState(float x, float y, int rawX=0, int rawY=0)
Initializes a new instance of the GamepadStickState struct with the specified normalized and optional...
override string ToString()
Returns a string representation of the normalized stick position showing the X and Y values formatted...
StickDirection Direction(float threshold=0.15f)
Returns the primary stick direction(s) based on angle and threshold.
int RawX
Raw horizontal value before normalization (e.g., from XInput or SDL2)