From 4b21877a4498419942bee1a193df45296a4b51a3 Mon Sep 17 00:00:00 2001 From: Chip Collier Date: Thu, 15 Feb 2024 12:18:03 +0100 Subject: [PATCH] Include the mouse delta as part of the input state --- Framework/Input/Input.cs | 2 ++ Framework/Input/Mouse.cs | 6 ++++++ Framework/Platform.cs | 2 ++ Platform/include/foster_platform.h | 2 ++ Platform/src/foster_platform.c | 8 +++++++- 5 files changed, 19 insertions(+), 1 deletion(-) diff --git a/Framework/Input/Input.cs b/Framework/Input/Input.cs index 201c83a..454f6f3 100644 --- a/Framework/Input/Input.cs +++ b/Framework/Input/Input.cs @@ -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: diff --git a/Framework/Input/Mouse.cs b/Framework/Input/Mouse.cs index 66dd8e1..8054fa9 100644 --- a/Framework/Input/Mouse.cs +++ b/Framework/Input/Mouse.cs @@ -21,6 +21,11 @@ public class Mouse /// public Vector2 Position; + /// + /// Delta to the previous mouse position, in Pixel Coordinates. + /// + public Vector2 Delta; + public float X { get => Position.X; @@ -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; } diff --git a/Framework/Platform.cs b/Framework/Platform.cs index 2a86594..4f6b7e5 100644 --- a/Framework/Platform.cs +++ b/Framework/Platform.cs @@ -67,6 +67,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; } diff --git a/Platform/include/foster_platform.h b/Platform/include/foster_platform.h index 313af09..78aaf48 100644 --- a/Platform/include/foster_platform.h +++ b/Platform/include/foster_platform.h @@ -477,6 +477,8 @@ typedef union FosterEvent int eventType; float x; float y; + float deltaX; + float deltaY; int button; FosterBool buttonPressed; } mouse; diff --git a/Platform/src/foster_platform.c b/Platform/src/foster_platform.c index 92cc833..3b6b1e2 100644 --- a/Platform/src/foster_platform.c +++ b/Platform/src/foster_platform.c @@ -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; }