From 175d888611db73ed6d2a6922877183dd7bc4e977 Mon Sep 17 00:00:00 2001 From: Reece Mackie <20544390+Rover656@users.noreply.github.com> Date: Thu, 30 Apr 2020 15:41:39 +0100 Subject: [PATCH] Implemented mouse locking of a sort. --- projects/VS2017.UWP/raylib.App.UWP/App.cpp | 25 ++++++++++++++++++---- projects/VS2017.UWP/raylib.App.UWP/App.h | 3 ++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/projects/VS2017.UWP/raylib.App.UWP/App.cpp b/projects/VS2017.UWP/raylib.App.UWP/App.cpp index b01487fc7..813dda39b 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/App.cpp +++ b/projects/VS2017.UWP/raylib.App.UWP/App.cpp @@ -91,6 +91,7 @@ void App::SetWindow(Windows::UI::Core::CoreWindow^ window) void App::Load(Platform::String ^entryPoint) {} // Ignored for this example +static bool mouseLocked = false; void App::Run() { // Set up our UWP implementation @@ -124,13 +125,13 @@ void App::Run() UWPSetMouseLockFunc([]() { CoreWindow::GetForCurrentThread()->PointerCursor = nullptr; - // TODO: + mouseLocked = true; }); UWPSetMouseUnlockFunc([]() { CoreWindow::GetForCurrentThread()->PointerCursor = ref new CoreCursor(CoreCursorType::Arrow, 0); - // TODO: + mouseLocked = false; }); UWPSetMouseSetPosFunc([](int x, int y) @@ -170,8 +171,9 @@ void App::Run() { if (mWindowVisible) { - ProcessGamepads(); + PreProcessInputs(); GameLoop(); + PostProcessInputs(); CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessAllIfPresent); } else CoreWindow::GetForCurrentThread()->Dispatcher->ProcessEvents(CoreProcessEventsOption::ProcessOneAndAllPending); } @@ -247,7 +249,7 @@ struct GamepadBinding static GamepadBinding gGamepadBindings[MAX_GAMEPADS]; -void App::ProcessGamepads() +void App::PreProcessInputs() { // Here, we will see if we have bound gamepads. If we do we check they are still present. If they aren't present we free the binding. // if anyone does not have a binding but there is a gamepad available, we will bind it to the first player who is missing a controller. @@ -351,6 +353,21 @@ void App::ProcessGamepads() } } +void App::PostProcessInputs() +{ + /* + * So here's the deal. UWP doesn't officially have mouse locking, so we're doing it ourselves here. + * If anyone has any better ideas on how to implement this feel free! + * This is done after the game loop so getting mouse delta etc. still works. + */ + if (mouseLocked) + { + auto w = GetScreenWidth(); + auto h = GetScreenHeight(); + SetMousePosition(w / 2, h / 2); + } +} + // Events void App::OnActivated(Windows::ApplicationModel::Core::CoreApplicationView^ applicationView, Windows::ApplicationModel::Activation::IActivatedEventArgs^ args) { diff --git a/projects/VS2017.UWP/raylib.App.UWP/App.h b/projects/VS2017.UWP/raylib.App.UWP/App.h index a3b7577f4..6cbeadac7 100644 --- a/projects/VS2017.UWP/raylib.App.UWP/App.h +++ b/projects/VS2017.UWP/raylib.App.UWP/App.h @@ -22,7 +22,8 @@ namespace raylibUWP bool mSuspended = false; void GameLoop(); - void ProcessGamepads(); + void PreProcessInputs(); + void PostProcessInputs(); // Helpers int GetRaylibKey(Windows::System::VirtualKey kVey);