Skip to content

Commit

Permalink
Merge pull request #74 from photex/photex/relative_mouse
Browse files Browse the repository at this point in the history
Include the mouse delta as part of the input state
  • Loading branch information
NoelFB authored Mar 25, 2024
2 parents ad5b8a0 + 4b21877 commit c30d479
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Framework/Input/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,8 @@ internal static unsafe void OnFosterEvent(in Platform.FosterEvent ev)
var pixels = new Point2(App.WidthInPixels, App.HeightInPixels);
nextState.Mouse.Position.X = (ev.Mouse.X / size.X) * pixels.X;
nextState.Mouse.Position.Y = (ev.Mouse.Y / size.Y) * pixels.Y;
nextState.Mouse.Delta.X = ev.Mouse.deltaX;
nextState.Mouse.Delta.Y = ev.Mouse.deltaY;
break;
}
case Platform.FosterEventType.MouseWheel:
Expand Down
6 changes: 6 additions & 0 deletions Framework/Input/Mouse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class Mouse
/// </summary>
public Vector2 Position;

/// <summary>
/// Delta to the previous mouse position, in Pixel Coordinates.
/// </summary>
public Vector2 Delta;

public float X
{
get => Position.X;
Expand Down Expand Up @@ -70,6 +75,7 @@ internal void Copy(Mouse other)
Array.Copy(other.timestamp, 0, timestamp, 0, MaxButtons);

Position = other.Position;
Delta = other.Delta;
wheelValue = other.wheelValue;
}

Expand Down
2 changes: 2 additions & 0 deletions Framework/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ public struct MouseEvent
public FosterEventType EventType;
public float X;
public float Y;
public float deltaX;
public float deltaY;
public int Button;
public byte ButtonPressed;
}
Expand Down
2 changes: 2 additions & 0 deletions Platform/include/foster_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ typedef union FosterEvent
int eventType;
float x;
float y;
float deltaX;
float deltaY;
int button;
FosterBool buttonPressed;
} mouse;
Expand Down
8 changes: 7 additions & 1 deletion Platform/src/foster_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,17 @@ FosterBool FosterPollEvents(FosterEvent* output)
// TODO: should this just change to a getter?
if (!fstate.polledMouseMovement)
{
output->eventType = FOSTER_EVENT_TYPE_MOUSE_MOVE;

int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
output->eventType = FOSTER_EVENT_TYPE_MOUSE_MOVE;
output->mouse.x = (float)mouseX;
output->mouse.y = (float)mouseY;

SDL_GetRelativeMouseState(&mouseX, &mouseY);
output->mouse.deltaX = (float)mouseX;
output->mouse.deltaY = (float)mouseY;

fstate.polledMouseMovement = 1;
return 1;
}
Expand Down

0 comments on commit c30d479

Please sign in to comment.