diff --git a/src/dusk/imgui/ImGuiConsole.cpp b/src/dusk/imgui/ImGuiConsole.cpp index f15377abd1..5248bc9d36 100644 --- a/src/dusk/imgui/ImGuiConsole.cpp +++ b/src/dusk/imgui/ImGuiConsole.cpp @@ -12,7 +12,6 @@ #include "ImGuiConsole.hpp" #include "ImGuiEngine.hpp" #include "JSystem/JUtility/JUTGamePad.h" -#include "SDL3/SDL_mouse.h" #include "dusk/action_bindings.h" #include "dusk/audio/DuskAudioSystem.h" #include "dusk/config.hpp" @@ -372,20 +371,6 @@ namespace dusk { m_menuTools.ShowActorSpawner(); } - // Hide mouse cursor if the F1 menu is not open and the cursor is idle for 3 seconds. - if (!dusk::getSettings().game.enableMouseAim) - { - ImGuiIO& io = ImGui::GetIO(); - if (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f) { - mouseHideTimer = 0.0f; - ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NoMouseCursorChange; // Imgui will re-show cursor. - } else if (mouseHideTimer <= 3.0f) { - mouseHideTimer += ImGui::GetIO().DeltaTime; - } else { - ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange; - SDL_HideCursor(); - } - } } void ImGuiConsole::PostDraw() { diff --git a/src/dusk/imgui/ImGuiConsole.hpp b/src/dusk/imgui/ImGuiConsole.hpp index bc9f253156..d97f1b00e6 100644 --- a/src/dusk/imgui/ImGuiConsole.hpp +++ b/src/dusk/imgui/ImGuiConsole.hpp @@ -26,7 +26,6 @@ public: static bool CheckMenuViewToggle(ImGuiKey key, bool& active); private: - float mouseHideTimer = 0.0f; bool m_isHidden = true; bool m_isLaunchInitialized = false; diff --git a/src/dusk/mouse.cpp b/src/dusk/mouse.cpp index 2f4b8e4e29..1a9ce18fc0 100644 --- a/src/dusk/mouse.cpp +++ b/src/dusk/mouse.cpp @@ -5,17 +5,20 @@ #include "d/d_com_inf_game.h" #include +#include #include #include namespace dusk::mouse { namespace { constexpr float kMousePixelToRad = 0.0025f; +constexpr int kIdleHideFrames = 99; // Approx. 3 seconds with 33ms ticks float s_aim_yaw_rad = 0.0f; float s_aim_pitch_rad = 0.0f; float s_camera_yaw_rad = 0.0f; float s_camera_pitch_rad = 0.0f; +int s_idle_frames = 0; void reset_deltas() { s_aim_yaw_rad = s_aim_pitch_rad = 0.0f; @@ -118,11 +121,48 @@ void accumulateDeltas(float mx_rel, float my_rel, bool camera_active, bool aim_a s_camera_yaw_rad = s_camera_pitch_rad = 0.0f; } } + +void set_cursor_visible(bool visible) { + if (visible) { + ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NoMouseCursorChange; + SDL_ShowCursor(); + } else { + ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange; + SDL_HideCursor(); + } +} + +void update_cursor_visibility(SDL_Window* window, bool captured) { + if (window == nullptr || !isWindowFocused(window)) { + return; + } + + if (captured) { + s_idle_frames = 0; + set_cursor_visible(false); + return; + } + + const ImGuiIO& io = ImGui::GetIO(); + if (io.MouseDelta.x != 0.0f || io.MouseDelta.y != 0.0f) { + s_idle_frames = 0; + set_cursor_visible(true); + return; + } + + if (s_idle_frames < kIdleHideFrames) { + ++s_idle_frames; + set_cursor_visible(true); + } else { + set_cursor_visible(false); + } +} } // namespace void read() { SDL_Window* window = aurora::window::get_sdl_window(); const bool capture_active = syncCaptureState(window, shouldCaptureMouse(window)); + update_cursor_visibility(window, capture_active); if (!capture_active) { return; @@ -171,7 +211,8 @@ void onFocusLost() { if (window != nullptr) { SDL_SetWindowRelativeMouseMode(window, false); } - SDL_ShowCursor(); + s_idle_frames = 0; + set_cursor_visible(true); reset_deltas(); } diff --git a/src/dusk/ui/document.cpp b/src/dusk/ui/document.cpp index cb70578e30..a7bcc3f9ed 100644 --- a/src/dusk/ui/document.cpp +++ b/src/dusk/ui/document.cpp @@ -5,7 +5,6 @@ #include "Z2AudioLib/Z2SeMgr.h" #include "m_Do/m_Do_audio.h" -#include namespace dusk::ui { namespace { @@ -107,7 +106,6 @@ bool Document::visible() const { bool Document::handle_nav_command(Rml::Event& event, NavCommand cmd) { if (cmd == NavCommand::Menu) { - toggle_cursor_if_gyro(!visible()); mDoAud_seStartMenu(visible() ? kSoundMenuClose : kSoundMenuOpen); toggle(); return true; @@ -115,17 +113,4 @@ bool Document::handle_nav_command(Rml::Event& event, NavCommand cmd) { return false; } -void Document::toggle_cursor_if_gyro(bool cursor_enabled) { - if (dusk::getSettings().game.enableMouseAim) - { - if (cursor_enabled) { - ImGui::GetIO().ConfigFlags &= ~ImGuiConfigFlags_NoMouseCursorChange; - SDL_ShowCursor(); - } else { - ImGui::GetIO().ConfigFlags |= ImGuiConfigFlags_NoMouseCursorChange; - SDL_HideCursor(); - } - } -} - } // namespace dusk::ui diff --git a/src/dusk/ui/document.hpp b/src/dusk/ui/document.hpp index eed489beb3..d0f4cae841 100644 --- a/src/dusk/ui/document.hpp +++ b/src/dusk/ui/document.hpp @@ -43,8 +43,6 @@ public: bool pending_close() const { return mPendingClose; } bool closed() const { return mClosed; } - void toggle_cursor_if_gyro(bool); - protected: virtual bool handle_nav_command(Rml::Event& event, NavCommand cmd); diff --git a/src/dusk/ui/menu_bar.cpp b/src/dusk/ui/menu_bar.cpp index 1be5c7abb0..068913d592 100644 --- a/src/dusk/ui/menu_bar.cpp +++ b/src/dusk/ui/menu_bar.cpp @@ -45,7 +45,6 @@ MenuBar::MenuBar() : Document(kDocumentSource), mRoot(mDocument->GetElementById( mTabBar = std::make_unique(mRoot, TabBar::Props{ .onClose = [this] { - toggle_cursor_if_gyro(false); mDoAud_seStartMenu(kSoundMenuClose); hide(false); }, @@ -219,7 +218,6 @@ bool MenuBar::handle_nav_command(Rml::Event& event, NavCommand cmd) { return true; } if (cmd == NavCommand::Cancel && visible()) { - toggle_cursor_if_gyro(false); mDoAud_seStartMenu(kSoundMenuClose); hide(false); return true; diff --git a/src/dusk/ui/prelaunch.cpp b/src/dusk/ui/prelaunch.cpp index ef6b6e5709..44b73b75ef 100644 --- a/src/dusk/ui/prelaunch.cpp +++ b/src/dusk/ui/prelaunch.cpp @@ -699,8 +699,6 @@ Prelaunch::Prelaunch() : Document(kDocumentSource), mRoot(mDocument->GetElementB return; } - toggle_cursor_if_gyro(false); - mDoAud_seStartMenu(kSoundPlay); show_menu_notification();