Skip to content

Commit

Permalink
Add a new option to hide the mouse's cursor when the mouse isn't movi…
Browse files Browse the repository at this point in the history
…ng for 2 seconds.
  • Loading branch information
Arignir committed Aug 1, 2024
1 parent f46b41f commit 9975f1e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
6 changes: 6 additions & 0 deletions include/app/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,9 @@ struct settings {

// Pause when the game resets
bool pause_when_game_resets;

// Hide the cursor after a few seconds of inactivity
bool hide_cursor_when_mouse_inactive;
} misc;
};

Expand Down Expand Up @@ -354,6 +357,9 @@ struct app {
/* Temporary value used to measure the FPS. */
uint32_t ticks_last_frame;

/* Temporary value used to measure the time since the last mouse movement (in ms) */
float time_elapsed_since_last_mouse_motion_ms;

/*
** The size of the `game` window.
** Usually the size of the window minus the menubar's height (if it is visible).
Expand Down
10 changes: 8 additions & 2 deletions source/app/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ app_config_load(
if (mjson_get_bool(data, data_len, "$.misc.pause_when_game_resets", &b)) {
app->settings.misc.pause_when_game_resets = b;
}

if (mjson_get_bool(data, data_len, "$.misc.hide_cursor_when_mouse_inactive", &b)) {
app->settings.misc.hide_cursor_when_mouse_inactive = b;
}
}

// Binds
Expand Down Expand Up @@ -285,7 +289,8 @@ app_config_save(
// Misc
"misc": {
"pause_when_window_inactive": %B,
"pause_when_game_resets": %B
"pause_when_game_resets": %B,
"hide_cursor_when_mouse_inactive": %B
}
}),
app->settings.emulation.bios_path,
Expand All @@ -310,7 +315,8 @@ app_config_save(
(int)app->settings.audio.mute,
app->settings.audio.level,
(int)app->settings.misc.pause_when_window_inactive,
(int)app->settings.misc.pause_when_game_resets
(int)app->settings.misc.pause_when_game_resets,
(int)app->settings.misc.hide_cursor_when_mouse_inactive
);

if (!data) {
Expand Down
20 changes: 19 additions & 1 deletion source/app/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ app_settings_default(
settings->video.vsync = false;
settings->video.display_size = 3;
settings->video.aspect_ratio = ASPECT_RATIO_RESIZE;
settings->video.texture_filter = TEXTURE_FILTER_NEAREST;
settings->audio.mute = false;
settings->audio.level = 1.0f;
settings->video.texture_filter = TEXTURE_FILTER_NEAREST;
settings->misc.hide_cursor_when_mouse_inactive = true;
}

int
Expand Down Expand Up @@ -256,6 +257,23 @@ main(
}
}

// Hide the cursor if the game is running and the mouse is inactive for a while
if (app.settings.misc.hide_cursor_when_mouse_inactive && app.emulation.is_started && app.emulation.is_running) {
int show_cursor;

show_cursor = SDL_DISABLE;
if (app.ui.time_elapsed_since_last_mouse_motion_ms <= 2000.0) { // 2s
app.ui.time_elapsed_since_last_mouse_motion_ms += elapsed_ms;
show_cursor = SDL_ENABLE;
}

if (SDL_ShowCursor(SDL_QUERY) != show_cursor) {
SDL_ShowCursor(show_cursor);
}
} else {
app.ui.time_elapsed_since_last_mouse_motion_ms = 0;
}

// Flush the quick save cache
if (app.file.flush_qsaves_cache) {
size_t i;
Expand Down
7 changes: 5 additions & 2 deletions source/app/sdl/event.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ app_sdl_handle_events(
bool state_a;
bool state_b;

/* Disable the joysticks if the settings are visible */
/* Disable the joysticks if the settings are open */
if (app->ui.settings.open) {
break;
}
Expand All @@ -229,7 +229,10 @@ app_sdl_handle_events(
break;
}
case SDL_MOUSEMOTION: {

// Handle the "hide cursor on mouse inactivity" option by
// resetting the "time_elapsed_since_last_mouse_motion_ms" counter.
// The main loop will then update the cursor's visibility if needed.
app->ui.time_elapsed_since_last_mouse_motion_ms = 0.f;
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions source/app/windows/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,17 @@ app_win_settings_misc(
igCheckbox("##PauseWhenGameResets", &app->settings.misc.pause_when_game_resets);
#endif

// Hide the cursor when the mouse is inactive
igTableNextRow(ImGuiTableRowFlags_None, 0.f);
igTableNextColumn();
igTextWrapped("Hide cursor when the mouse is inactive");

igTableNextColumn();
if (igCheckbox("##HideCursorWhenMouseInactive", &app->settings.misc.hide_cursor_when_mouse_inactive)) {
app->ui.time_elapsed_since_last_mouse_motion_ms = 0;
SDL_ShowCursor(SDL_ENABLE);
}

igEndTable();
}
}
Expand Down

0 comments on commit 9975f1e

Please sign in to comment.