From e2f3ae66f83a8f19f479bf697f48a0929125b1e3 Mon Sep 17 00:00:00 2001 From: Muzychenko Andrey <33288308+k4zmu2a@users.noreply.github.com> Date: Tue, 11 Jul 2023 15:33:09 +0300 Subject: [PATCH] Added hide mouse cursor option. Issue #181. --- SpaceCadetPinball/options.cpp | 1 + SpaceCadetPinball/options.h | 1 + SpaceCadetPinball/winmain.cpp | 43 +++++++++++++++++++++++++---------- SpaceCadetPinball/winmain.h | 1 + 4 files changed, 34 insertions(+), 12 deletions(-) diff --git a/SpaceCadetPinball/options.cpp b/SpaceCadetPinball/options.cpp index 60778a9..e597e07 100644 --- a/SpaceCadetPinball/options.cpp +++ b/SpaceCadetPinball/options.cpp @@ -151,6 +151,7 @@ optionsStruct options::Options {"Debug Overlay AABB", true}, {"FontFileName", ""}, {"Language", translations::GetCurrentLanguage()->ShortName}, + {"Hide Cursor", false}, }; void options::InitPrimary() diff --git a/SpaceCadetPinball/options.h b/SpaceCadetPinball/options.h index 98db7f5..f9ebfab 100644 --- a/SpaceCadetPinball/options.h +++ b/SpaceCadetPinball/options.h @@ -288,4 +288,5 @@ struct optionsStruct BoolOption DebugOverlayAabb; StringOption FontFileName; StringOption Language; + BoolOption HideCursor; }; diff --git a/SpaceCadetPinball/winmain.cpp b/SpaceCadetPinball/winmain.cpp index a08e719..7791ad5 100644 --- a/SpaceCadetPinball/winmain.cpp +++ b/SpaceCadetPinball/winmain.cpp @@ -49,6 +49,7 @@ winmain::DurationMs winmain::TargetFrameTime; optionsStruct& winmain::Options = options::Options; winmain::DurationMs winmain::SpinThreshold = DurationMs(0.005); WelfordState winmain::SleepState{}; +int winmain::CursorIdleCounter = 0; int winmain::WinMain(LPCSTR lpCmdLine) { @@ -353,6 +354,8 @@ void winmain::MainLoop() if (UpdateToFrameCounter >= UpdateToFrameRatio) { + if (Options.HideCursor && CursorIdleCounter <= 0) + ImGui::SetMouseCursor(ImGuiMouseCursor_None); ImGui_ImplSDL2_NewFrame(); ImGui_Render_NewFrame(); ImGui::NewFrame(); @@ -420,6 +423,8 @@ void winmain::MainLoop() frameDuration = std::min(DurationMs(frameEnd - frameStart), 2 * TargetFrameTime); frameStart = frameEnd; UpdateToFrameCounter++; + + CursorIdleCounter = std::max(CursorIdleCounter - static_cast(frameDuration.count()), 0); } } @@ -581,10 +586,6 @@ void winmain::RenderUi() if (ImGui::BeginMenu("Graphics")) { - if (ImGui::MenuItem("Change Font")) - { - font_selection::ShowDialog(); - } if (ImGui::MenuItem(pb::get_rc_string(Msg::Menu1_WindowUniformScale), nullptr, Options.UniformScaling)) { options::toggle(Menu1::WindowUniformScale); @@ -640,6 +641,16 @@ void winmain::RenderUi() { UpdateFrameRate(); } + ImGui::Separator(); + + if (ImGui::MenuItem("Hide Cursor", nullptr, Options.HideCursor)) + { + Options.HideCursor ^= true; + } + if (ImGui::MenuItem("Change Font...")) + { + font_selection::ShowDialog(); + } ImGui::EndMenu(); } @@ -832,6 +843,21 @@ int winmain::event_handler(const SDL_Event* event) if (!options::WaitingForInput() || !inputDown) ImGui_ImplSDL2_ProcessEvent(event); + bool mouseEvent; + switch (event->type) + { + case SDL_MOUSEMOTION: + case SDL_MOUSEBUTTONDOWN: + case SDL_MOUSEBUTTONUP: + case SDL_MOUSEWHEEL: + CursorIdleCounter = 1000; + mouseEvent = true; + break; + default: + mouseEvent = false; + break; + } + if (ImIO->WantCaptureMouse && !options::WaitingForInput()) { if (mouse_down) @@ -839,15 +865,8 @@ int winmain::event_handler(const SDL_Event* event) mouse_down = 0; SDL_SetWindowGrab(MainWindow, SDL_FALSE); } - switch (event->type) - { - case SDL_MOUSEMOTION: - case SDL_MOUSEBUTTONDOWN: - case SDL_MOUSEBUTTONUP: - case SDL_MOUSEWHEEL: + if (mouseEvent) return 1; - default: ; - } } if (ImIO->WantCaptureKeyboard && !options::WaitingForInput()) { diff --git a/SpaceCadetPinball/winmain.h b/SpaceCadetPinball/winmain.h index a58f8ff..173dae5 100644 --- a/SpaceCadetPinball/winmain.h +++ b/SpaceCadetPinball/winmain.h @@ -106,6 +106,7 @@ private: static unsigned PrevSdlErrorCount; static unsigned gfrOffset; static float gfrWindow; + static int CursorIdleCounter; static void RenderUi(); static void RenderFrameTimeDialog();