Skip to content

Commit c30d479

Browse files
authored
Merge pull request #74 from photex/photex/relative_mouse
Include the mouse delta as part of the input state
2 parents ad5b8a0 + 4b21877 commit c30d479

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

Framework/Input/Input.cs

+2
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@ internal static unsafe void OnFosterEvent(in Platform.FosterEvent ev)
203203
var pixels = new Point2(App.WidthInPixels, App.HeightInPixels);
204204
nextState.Mouse.Position.X = (ev.Mouse.X / size.X) * pixels.X;
205205
nextState.Mouse.Position.Y = (ev.Mouse.Y / size.Y) * pixels.Y;
206+
nextState.Mouse.Delta.X = ev.Mouse.deltaX;
207+
nextState.Mouse.Delta.Y = ev.Mouse.deltaY;
206208
break;
207209
}
208210
case Platform.FosterEventType.MouseWheel:

Framework/Input/Mouse.cs

+6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public class Mouse
2121
/// </summary>
2222
public Vector2 Position;
2323

24+
/// <summary>
25+
/// Delta to the previous mouse position, in Pixel Coordinates.
26+
/// </summary>
27+
public Vector2 Delta;
28+
2429
public float X
2530
{
2631
get => Position.X;
@@ -70,6 +75,7 @@ internal void Copy(Mouse other)
7075
Array.Copy(other.timestamp, 0, timestamp, 0, MaxButtons);
7176

7277
Position = other.Position;
78+
Delta = other.Delta;
7379
wheelValue = other.wheelValue;
7480
}
7581

Framework/Platform.cs

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ public struct MouseEvent
6060
public FosterEventType EventType;
6161
public float X;
6262
public float Y;
63+
public float deltaX;
64+
public float deltaY;
6365
public int Button;
6466
public byte ButtonPressed;
6567
}

Platform/include/foster_platform.h

+2
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ typedef union FosterEvent
477477
int eventType;
478478
float x;
479479
float y;
480+
float deltaX;
481+
float deltaY;
480482
int button;
481483
FosterBool buttonPressed;
482484
} mouse;

Platform/src/foster_platform.c

+7-1
Original file line numberDiff line numberDiff line change
@@ -171,11 +171,17 @@ FosterBool FosterPollEvents(FosterEvent* output)
171171
// TODO: should this just change to a getter?
172172
if (!fstate.polledMouseMovement)
173173
{
174+
output->eventType = FOSTER_EVENT_TYPE_MOUSE_MOVE;
175+
174176
int mouseX, mouseY;
175177
SDL_GetMouseState(&mouseX, &mouseY);
176-
output->eventType = FOSTER_EVENT_TYPE_MOUSE_MOVE;
177178
output->mouse.x = (float)mouseX;
178179
output->mouse.y = (float)mouseY;
180+
181+
SDL_GetRelativeMouseState(&mouseX, &mouseY);
182+
output->mouse.deltaX = (float)mouseX;
183+
output->mouse.deltaY = (float)mouseY;
184+
179185
fstate.polledMouseMovement = 1;
180186
return 1;
181187
}

0 commit comments

Comments
 (0)