mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-06-09 03:59:34 -04:00
Refactor cursor visibility handling
This commit is contained in:
@@ -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() {
|
||||
|
||||
@@ -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;
|
||||
|
||||
+42
-1
@@ -5,17 +5,20 @@
|
||||
#include "d/d_com_inf_game.h"
|
||||
|
||||
#include <aurora/lib/window.hpp>
|
||||
#include <imgui.h>
|
||||
#include <SDL3/SDL_mouse.h>
|
||||
#include <SDL3/SDL_video.h>
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
|
||||
#include "Z2AudioLib/Z2SeMgr.h"
|
||||
#include "m_Do/m_Do_audio.h"
|
||||
#include <imgui.h>
|
||||
|
||||
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
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -45,7 +45,6 @@ MenuBar::MenuBar() : Document(kDocumentSource), mRoot(mDocument->GetElementById(
|
||||
mTabBar = std::make_unique<TabBar>(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;
|
||||
|
||||
@@ -699,8 +699,6 @@ Prelaunch::Prelaunch() : Document(kDocumentSource), mRoot(mDocument->GetElementB
|
||||
return;
|
||||
}
|
||||
|
||||
toggle_cursor_if_gyro(false);
|
||||
|
||||
mDoAud_seStartMenu(kSoundPlay);
|
||||
show_menu_notification();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user