From e3af067972c10f0edb431bf50be3d4ebd0f1be3e Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Thu, 2 Apr 2026 19:18:39 +0300 Subject: [PATCH] Fixed keybinds --- src/InputManager.cpp | 14 ++------------ src/InputManager.hpp | 4 ---- src/MainProgram.cpp | 18 ++++++++++++++++++ src/MainProgram.hpp | 4 ++++ src/Toolbar.cpp | 41 +++++------------------------------------ src/Toolbar.hpp | 2 -- 6 files changed, 29 insertions(+), 54 deletions(-) diff --git a/src/InputManager.cpp b/src/InputManager.cpp index 6681afc..cfeff04 100644 --- a/src/InputManager.cpp +++ b/src/InputManager.cpp @@ -847,14 +847,12 @@ void InputManager::backend_key_down_update(const SDL_KeyboardEvent& e) { if(kPress != SDLK_LSHIFT && kPress != SDLK_RSHIFT && kPress != SDLK_LCTRL && kPress != SDLK_RCTRL && kPress != SDLK_LGUI && kPress != SDLK_RGUI && kPress != SDLK_LALT && kPress != SDLK_RALT) { - lastPressedKeybind = {make_generic_key_mod(kMod), kPress}; + if(main.input_keybind_callback({make_generic_key_mod(kMod), kPress})) + return; } // e.key != SDLK_LMETA && e.key != SDLK_RMETA && - if(stopKeyInput) - return; - std::shared_ptr textBox = text.textBoxes.empty() ? nullptr : text.textBoxes.front().textBox; std::shared_ptr cursor = text.textBoxes.empty() ? nullptr : text.textBoxes.front().cursor; std::optional modMap = text.textBoxes.empty() ? std::nullopt : shared_ptr_to_opt(text.textBoxes.front().modMap); @@ -1168,10 +1166,6 @@ void InputManager::backend_key_up_update(const SDL_KeyboardEvent& e) { } } -void InputManager::stop_key_input() { - stopKeyInput = 2; -} - void InputManager::Mouse::set_pos(const Vector2f& newPos) { pos = newPos; } @@ -1195,10 +1189,6 @@ void InputManager::update() { void InputManager::frame_reset(const Vector2i& windowSize) { cursorIcon = SystemCursorType::DEFAULT; - lastPressedKeybind = std::nullopt; - if(stopKeyInput) - stopKeyInput--; - toggleFullscreen = false; hideCursor = false; } diff --git a/src/InputManager.hpp b/src/InputManager.hpp index 16a249a..d707463 100644 --- a/src/InputManager.hpp +++ b/src/InputManager.hpp @@ -187,9 +187,6 @@ struct InputManager { SDL_Keymod CTRL_MOD = SDL_KMOD_CTRL; #endif - int stopKeyInput = 0; - std::optional lastPressedKeybind; - enum KeyCodeEnum : unsigned { // Assignable KEY_CAMERA_ROTATE_CLOCKWISE = 0, @@ -263,7 +260,6 @@ struct InputManager { typedef unsigned KeyCode; bool ctrl_or_meta_held(); - void stop_key_input(); uint32_t make_generic_key_mod(SDL_Keymod m); void set_key_up(const SDL_KeyboardEvent& e, KeyCode kCode); void set_key_down(const SDL_KeyboardEvent& e, KeyCode kCode); diff --git a/src/MainProgram.cpp b/src/MainProgram.cpp index 4c447c8..575d083 100755 --- a/src/MainProgram.cpp +++ b/src/MainProgram.cpp @@ -317,6 +317,24 @@ sk_sp MainProgram::create_native_surface(Vector2i resolution, bool is return nullptr; } +bool MainProgram::input_keybind_callback(const Vector2ui32& newKey) { + if(keybindWaiting.has_value()) { + unsigned v = keybindWaiting.value(); + + input.keyAssignments.erase(newKey); + auto f = std::find_if(input.keyAssignments.begin(), input.keyAssignments.end(), [&](auto& p) { + return p.second == v; + }); + if(f != input.keyAssignments.end()) + input.keyAssignments.erase(f); + input.keyAssignments.emplace(newKey, v); + keybindWaiting = std::nullopt; + g.gui.set_to_layout(); + return true; + } + return false; +} + void MainProgram::input_drop_file_callback(const InputManager::DropCallbackArgs& drop) { if(std::filesystem::is_regular_file(drop.data)) { std::filesystem::path droppedFilePath(drop.data); diff --git a/src/MainProgram.hpp b/src/MainProgram.hpp index cca1e05..aa5fe47 100644 --- a/src/MainProgram.hpp +++ b/src/MainProgram.hpp @@ -3,6 +3,7 @@ #include #include #include +#include "Helpers/Hashes.hpp" #include "InputManager.hpp" #include "FontData.hpp" #include "TimePoint.hpp" @@ -119,6 +120,9 @@ class MainProgram { GlobalConfig conf; + std::optional keybindWaiting; + bool input_keybind_callback(const Vector2ui32& newKey); + void input_drop_file_callback(const InputManager::DropCallbackArgs& drop); void input_drop_text_callback(const InputManager::DropCallbackArgs& drop); void input_key_callback(const InputManager::KeyCallbackArgs& key); diff --git a/src/Toolbar.cpp b/src/Toolbar.cpp index db96131..8f80247 100644 --- a/src/Toolbar.cpp +++ b/src/Toolbar.cpp @@ -290,22 +290,6 @@ void Toolbar::layout_run() { colorLeftJustDisabled = false; colorRightJustEnabled = nullptr; colorRightJustDisabled = false; - - if(!main.world->clientStillConnecting) { - //if(io.hoverObstructed && (io.mouse.leftClick || io.mouse.rightClick)) - // rightClickPopupLocation = std::nullopt; - - //if(rightClickPopupLocation.has_value()) { - // if(!main.world->drawProg.right_click_popup_gui(rightClickPopupLocation.value())) - // rightClickPopupLocation = std::nullopt; - //} - - //if(io.hoverObstructed && io.mouse.rightClick) // This runs specifically if the paint popup has the cursor - // rightClickPopupLocation = std::nullopt; - } - - if(!optionsMenuOpen || generalSettingsOptions != GSETTINGS_KEYBINDS) - keybindWaiting = std::nullopt; } bool Toolbar::app_close_requested() { @@ -1711,6 +1695,7 @@ void Toolbar::general_settings_inner_gui() { main.g.load_theme(main.configPath, main.conf.themeCurrentlyLoaded); themeData.selectedThemeIndex = std::nullopt; generalSettingsOptions = opt; + main.keybindWaiting = std::nullopt; } }); }; @@ -1878,23 +1863,6 @@ void Toolbar::general_settings_inner_gui() { break; } case GSETTINGS_KEYBINDS: { - if(keybindWaiting.has_value()) { - main.input.stop_key_input(); - if(main.input.lastPressedKeybind) { - unsigned v = keybindWaiting.value(); - - Vector2ui32 newKey = main.input.lastPressedKeybind.value(); - main.input.keyAssignments.erase(newKey); - auto f = std::find_if(main.input.keyAssignments.begin(), main.input.keyAssignments.end(), [&](auto& p) { - return p.second == v; - }); - if(f != main.input.keyAssignments.end()) - main.input.keyAssignments.erase(f); - main.input.keyAssignments.emplace(newKey, v); - keybindWaiting = std::nullopt; - } - } - general_scroll_area("keybind entries", [&] { for(unsigned i = 0; i < InputManager::KEY_ASSIGNABLE_COUNT; i++) { gui.new_id(i, [&] { @@ -1913,10 +1881,10 @@ void Toolbar::general_settings_inner_gui() { }); std::string assignedKeystrokeStr = f != main.input.keyAssignments.end() ? main.input.key_assignment_to_str(f->first) : ""; text_button(gui, "keybind button", assignedKeystrokeStr, { - .isSelected = keybindWaiting.has_value() && keybindWaiting.value() == i, + .isSelected = main.keybindWaiting.has_value() && main.keybindWaiting.value() == i, .wide = true, - .onClick = [&] { - keybindWaiting = i; + .onClick = [&, i] { + main.keybindWaiting = i; } }); } @@ -1953,6 +1921,7 @@ void Toolbar::general_settings_inner_gui() { main.save_config(); main.g.load_theme(main.configPath, main.conf.themeCurrentlyLoaded); themeData.selectedThemeIndex = std::nullopt; + main.keybindWaiting = std::nullopt; optionsMenuOpen = false; }); } diff --git a/src/Toolbar.hpp b/src/Toolbar.hpp index 66ef7b2..ac8419d 100644 --- a/src/Toolbar.hpp +++ b/src/Toolbar.hpp @@ -212,8 +212,6 @@ class Toolbar { }; static NativeFilePicker nativeFilePicker; - std::optional keybindWaiting; - std::unique_ptr build_paragraph_from_chat_message(const ChatMessage& message, float alpha); void load_icons_at(const std::filesystem::path& pathToLoad);