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
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
50 public GamepadStickState(float x, float y, int rawX = 0, int rawY = 0)
51 {
52 X = x;
53 Y = y;
54 RawX = rawX;
55 RawY = rawY;
56 }
57
67 public static GamepadStickState FromRaw16(int rawX, int rawY)
68 {
69 const float range = 32767f; // signed range: -32768 to 32767
70 float normX = Math.Clamp(rawX / range, -1f, 1f);
71 float normY = Math.Clamp(rawY / range, -1f, 1f);
72 return new GamepadStickState(normX, normY, rawX, rawY);
73 }
74
84 public static GamepadStickState FromRawUnsigned16(ushort rawX, ushort rawY)
85 {
86 float normX = (rawX - 32768f) / 32767f;
87 float normY = (rawY - 32768f) / 32767f;
88 return new GamepadStickState(normX, normY, rawX, rawY);
89 }
90
94 public float Magnitude => MathF.Sqrt(X * X + Y * Y);
95
99 public float Angle => MathF.Atan2(Y, X);
100
106 public bool IsEngaged(float threshold = 0.15f) => Magnitude >= threshold;
107
113 public StickDirection Direction(float threshold = 0.15f)
114 {
115 if (!IsEngaged(threshold)) return StickDirection.None;
116
117 var dir = StickDirection.None;
118 if (Y >= threshold) dir |= StickDirection.Up;
119 if (Y <= -threshold) dir |= StickDirection.Down;
120 if (X <= -threshold) dir |= StickDirection.Left;
121 if (X >= threshold) dir |= StickDirection.Right;
122
123 return dir;
124 }
125
131 public GamepadStickState WithDeadzone(float threshold = 0.15f)
132 {
133 return IsEngaged(threshold) ? this : new GamepadStickState(0, 0, RawX, RawY);
134 }
135
141 public override string ToString() => $"({X:0.00}, {Y:0.00})";
142}
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)