add mouse cursor locking

This commit is contained in:
Tuomas Katajisto 2026-10-17 08:58:45 +03:00
parent 9911229282
commit 73cc16f1d4
5 changed files with 42 additions and 6 deletions

View File

@ -4,6 +4,28 @@
#load "sokol.jai"; #load "sokol.jai";
#scope_file
shouldLockMouseCursor : bool = false;
#scope_export;
set_should_lock_cursor :: (shouldLock: bool) {
shouldLockMouseCursor = shouldLock;
}
is_cursor_locked :: () -> bool {
return shouldLockMouseCursor;
}
unlock_cursor :: () {
plat_set_cursor_lock(false);
}
// We use this so menu and editor is unlocked and game is locked if the game sets the should lock.
lock_cursor_if_should :: () {
if shouldLockMouseCursor then plat_set_cursor_lock(true);
}
Event_Type :: enum u32 { // If we set this to u8, our packing will stop matching C's. Event_Type :: enum u32 { // If we set this to u8, our packing will stop matching C's.
UNINITIALIZED :: 0; UNINITIALIZED :: 0;
KEYBOARD :: 1; KEYBOARD :: 1;

View File

@ -7,6 +7,10 @@ input_mouse_y : float;
set_custom_cursor_handling :: (is_custom: bool) { set_custom_cursor_handling :: (is_custom: bool) {
} }
plat_set_cursor_lock :: (locked: bool) {
sapp_lock_mouse(locked);
}
get_key_code :: (key: sapp_keycode) -> Key_Code { get_key_code :: (key: sapp_keycode) -> Key_Code {
using Key_Code; using Key_Code;
// non-ascii ordered as in ./module.jai/Key_Code // non-ascii ordered as in ./module.jai/Key_Code

View File

@ -109,6 +109,11 @@ tick_editor_ui :: () {
in_editor_view = !in_editor_view; in_editor_view = !in_editor_view;
g_mixer.paused = in_editor_view; g_mixer.paused = in_editor_view;
clear_particles(); clear_particles();
if in_editor_view {
unlock_cursor();
} else {
lock_cursor_if_should();
}
} }
#if !FLAG_RELEASE_BUILD && OS != .WASM { #if !FLAG_RELEASE_BUILD && OS != .WASM {

View File

@ -167,7 +167,7 @@ init_after_core :: () {
// We want to do this last. // We want to do this last.
game_init(); game_init();
// #if UNCAPPED_FRAMES then sapp_glx_swapinterval(0); lock_cursor_if_should();
} }

View File

@ -220,10 +220,15 @@ navigate_to :: (page: Settings_Page) {
g_settings.cursor = 0; g_settings.cursor = 0;
} }
close_settings_menu :: () {
save_settings();
lock_cursor_if_should();
g_settings.open = false;
}
go_back :: () { go_back :: () {
if g_settings.page == .MAIN { if g_settings.page == .MAIN {
save_settings(); close_settings_menu();
g_settings.open = false;
} else { } else {
navigate_to(page_parent()); navigate_to(page_parent());
} }
@ -234,10 +239,9 @@ handle_enter :: (index: s32 = -1) {
if g_settings.page == { if g_settings.page == {
case .MAIN; case .MAIN;
game_items := cast(s32) g_settings_config.main_items.count; game_items := cast(s32) g_settings_config.main_items.count;
if i == 0 { save_settings(); g_settings.open = false; } if i == 0 then close_settings_menu();
else if i <= game_items { else if i <= game_items {
save_settings(); close_settings_menu();
g_settings.open = false;
g_settings_config.main_items[i - 1].on_select(); g_settings_config.main_items[i - 1].on_select();
} else { } else {
i -= game_items; i -= game_items;
@ -306,6 +310,7 @@ tick_settings_menu :: () {
if is_action_start(Editor_Action.TOGGLE_SETTINGS) { if is_action_start(Editor_Action.TOGGLE_SETTINGS) {
if !g_settings.open { if !g_settings.open {
g_settings.open = true; g_settings.open = true;
unlock_cursor();
navigate_to(.MAIN); navigate_to(.MAIN);
generate_blobs(); generate_blobs();
} else { } else {