From 23728b72b3795e48885e34fcea019f8bf17a706e Mon Sep 17 00:00:00 2001 From: Cora <17833654450@163.com> Date: Sun, 26 Apr 2026 18:25:50 +0800 Subject: [PATCH 1/4] fix comment style --- include/gkit/core/input/input.hpp | 16 ++++++++++++---- src/core/input/input.cpp | 2 +- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/gkit/core/input/input.hpp b/include/gkit/core/input/input.hpp index a92ede1..b358502 100644 --- a/include/gkit/core/input/input.hpp +++ b/include/gkit/core/input/input.hpp @@ -13,7 +13,9 @@ namespace gkit { virtual ~Input() = default; public: - // action related + /******************************** + * action related + *******************************/ auto register_action(const input::Action& action) -> void; auto unregister_action(const std::string& name) -> void; @@ -22,19 +24,25 @@ namespace gkit { auto is_action_just_pressed(std::string name) -> bool; auto is_action_just_released(std::string name) -> bool; - // key related + /******************************** + * key related + *******************************/ auto is_key_pressed(gkit::input::Key key) -> bool; auto is_key_released(gkit::input::Key key) -> bool; auto is_key_just_pressed(gkit::input::Key key) -> bool; auto is_key_just_released(gkit::input::Key key) -> bool; - // mouse button related + /******************************** + * mouse button related + *******************************/ auto is_mouse_button_pressed(input::MouseButton button) -> bool; auto is_mouse_button_released(input::MouseButton button) -> bool; auto is_mouse_button_just_pressed(input::MouseButton button) -> bool; auto is_mouse_button_just_released(input::MouseButton button) -> bool; - // gamepad button related + /******************************** + * gamepad button related + *******************************/ auto is_gamepad_button_pressed(int button) -> bool; auto is_gamepad_button_released(int button) -> bool; auto is_gamepad_button_just_pressed(int button) -> bool; diff --git a/src/core/input/input.cpp b/src/core/input/input.cpp index acb3541..610b138 100644 --- a/src/core/input/input.cpp +++ b/src/core/input/input.cpp @@ -111,7 +111,7 @@ auto gkit::Input::is_action_pressed(std::string name) -> bool { } return gkit::input::Cache::instance().modifiers_pressed(chord.modifiers); - } + } return false; }, action.chord); From e1ecd8300ceca492f0d5ea115169efffd781061b Mon Sep 17 00:00:00 2001 From: Cora <17833654450@163.com> Date: Sun, 26 Apr 2026 19:20:38 +0800 Subject: [PATCH 2/4] nodiscard attitube for input method --- include/gkit/core/input/input.hpp | 32 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/include/gkit/core/input/input.hpp b/include/gkit/core/input/input.hpp index b358502..673f467 100644 --- a/include/gkit/core/input/input.hpp +++ b/include/gkit/core/input/input.hpp @@ -19,34 +19,34 @@ namespace gkit { auto register_action(const input::Action& action) -> void; auto unregister_action(const std::string& name) -> void; - auto is_action_pressed(std::string name) -> bool; - auto is_action_released(std::string name) -> bool; - auto is_action_just_pressed(std::string name) -> bool; - auto is_action_just_released(std::string name) -> bool; + [[nodiscard]] auto is_action_pressed(std::string name) -> bool; + [[nodiscard]] auto is_action_released(std::string name) -> bool; + [[nodiscard]] auto is_action_just_pressed(std::string name) -> bool; + [[nodiscard]] auto is_action_just_released(std::string name) -> bool; /******************************** * key related *******************************/ - auto is_key_pressed(gkit::input::Key key) -> bool; - auto is_key_released(gkit::input::Key key) -> bool; - auto is_key_just_pressed(gkit::input::Key key) -> bool; - auto is_key_just_released(gkit::input::Key key) -> bool; + [[nodiscard]] auto is_key_pressed(gkit::input::Key key) -> bool; + [[nodiscard]] auto is_key_released(gkit::input::Key key) -> bool; + [[nodiscard]] auto is_key_just_pressed(gkit::input::Key key) -> bool; + [[nodiscard]] auto is_key_just_released(gkit::input::Key key) -> bool; /******************************** * mouse button related *******************************/ - auto is_mouse_button_pressed(input::MouseButton button) -> bool; - auto is_mouse_button_released(input::MouseButton button) -> bool; - auto is_mouse_button_just_pressed(input::MouseButton button) -> bool; - auto is_mouse_button_just_released(input::MouseButton button) -> bool; + [[nodiscard]] auto is_mouse_button_pressed(input::MouseButton button) -> bool; + [[nodiscard]] auto is_mouse_button_released(input::MouseButton button) -> bool; + [[nodiscard]] auto is_mouse_button_just_pressed(input::MouseButton button) -> bool; + [[nodiscard]] auto is_mouse_button_just_released(input::MouseButton button) -> bool; /******************************** * gamepad button related *******************************/ - auto is_gamepad_button_pressed(int button) -> bool; - auto is_gamepad_button_released(int button) -> bool; - auto is_gamepad_button_just_pressed(int button) -> bool; - auto is_gamepad_button_just_released(int button) -> bool; + [[nodiscard]] auto is_gamepad_button_pressed(int button) -> bool; + [[nodiscard]] auto is_gamepad_button_released(int button) -> bool; + [[nodiscard]] auto is_gamepad_button_just_pressed(int button) -> bool; + [[nodiscard]] auto is_gamepad_button_just_released(int button) -> bool; private: std::unordered_map action_map; From 94858be845fd18069dfcfd0522b3457f0991f6cb Mon Sep 17 00:00:00 2001 From: Cora <17833654450@163.com> Date: Sun, 26 Apr 2026 21:31:08 +0800 Subject: [PATCH 3/4] - add mouse wheel and move implment and test code - rename the property `mouse_button_cache` to `mouse_cache` --- include/gkit/core/input/input.hpp | 3 +++ include/gkit/core/input/mouse.hpp | 1 + src/core/input/cache.cpp | 8 +++---- src/core/input/cache.hpp | 3 +-- src/core/input/input.cpp | 32 ++++++++++++++++++++-------- test/core/input/test_mouse_input.cpp | 15 +++++++++++++ 6 files changed, 47 insertions(+), 15 deletions(-) diff --git a/include/gkit/core/input/input.hpp b/include/gkit/core/input/input.hpp index 673f467..35f7fa5 100644 --- a/include/gkit/core/input/input.hpp +++ b/include/gkit/core/input/input.hpp @@ -40,6 +40,9 @@ namespace gkit { [[nodiscard]] auto is_mouse_button_just_pressed(input::MouseButton button) -> bool; [[nodiscard]] auto is_mouse_button_just_released(input::MouseButton button) -> bool; + [[nodiscard]] auto get_mouse_move() -> input::MouseMove; + [[nodiscard]] auto get_mouse_wheel() -> input::MouseWheel; + /******************************** * gamepad button related *******************************/ diff --git a/include/gkit/core/input/mouse.hpp b/include/gkit/core/input/mouse.hpp index 720258d..88dd750 100644 --- a/include/gkit/core/input/mouse.hpp +++ b/include/gkit/core/input/mouse.hpp @@ -21,5 +21,6 @@ namespace gkit::input { uint32_t modifiers; }; // struct MouseChord + using MouseMove = gkit::math::Vector2; using MouseWheel = gkit::math::Vector2; } // namespace gkit::input diff --git a/src/core/input/cache.cpp b/src/core/input/cache.cpp index 6ca3054..4e03d8e 100644 --- a/src/core/input/cache.cpp +++ b/src/core/input/cache.cpp @@ -33,13 +33,13 @@ gkit::input::Cache::Cache() { event_dispatcher.register_event_handler(SDL_EVENT_MOUSE_BUTTON_DOWN, [this](const SDL_Event& e) { auto& frame = this->current_cache; auto button = static_cast(e.button.button); - frame.mouse_button_cache.pressed_buttons.insert(button); + frame.mouse_cache.pressed_buttons.insert(button); }); event_dispatcher.register_event_handler(SDL_EVENT_MOUSE_BUTTON_UP, [this](const SDL_Event& e) { auto& frame = this->current_cache; auto button = static_cast(e.button.button); - frame.mouse_button_cache.pressed_buttons.erase(button); + frame.mouse_cache.pressed_buttons.erase(button); }); @@ -48,7 +48,7 @@ gkit::input::Cache::Cache() { ************************************/ event_dispatcher.register_event_handler(SDL_EVENT_MOUSE_MOTION, [this](const SDL_Event& e) { auto& frame = this->current_cache; - auto [x, y] = frame.mouse_button_cache.offset.properties(); + auto [x, y] = frame.mouse_cache.offset.properties(); x = e.motion.xrel; y = e.motion.yrel; }); @@ -56,7 +56,7 @@ gkit::input::Cache::Cache() { event_dispatcher.register_event_handler(SDL_EVENT_MOUSE_WHEEL, [this](const SDL_Event& e) { auto& frame = this->current_cache; - auto [x, y] = frame.mouse_button_cache.wheel.properties(); + auto [x, y] = frame.mouse_cache.wheel.properties(); x = e.wheel.x; y = e.wheel.y; }); diff --git a/src/core/input/cache.hpp b/src/core/input/cache.hpp index 4ca6cdd..9b79671 100644 --- a/src/core/input/cache.hpp +++ b/src/core/input/cache.hpp @@ -44,7 +44,6 @@ namespace gkit::input { private: struct KeyCache_t { std::unordered_set pressed_keys; - /* std::uint32_t pressed_modifiers; */ }; struct MouseCache_t { @@ -59,7 +58,7 @@ namespace gkit::input { struct CacheData { KeyCache_t key_cache = {}; - MouseCache_t mouse_button_cache = {}; + MouseCache_t mouse_cache = {}; GamepadCache_t gamepad_button_cache = {}; }; diff --git a/src/core/input/input.cpp b/src/core/input/input.cpp index 610b138..65f0733 100644 --- a/src/core/input/input.cpp +++ b/src/core/input/input.cpp @@ -1,5 +1,6 @@ #include "gkit/core/input/input.hpp" #include "core/input/cache.hpp" +#include "gkit/core/input/mouse.hpp" #include #include #include @@ -44,34 +45,47 @@ auto gkit::Input::is_key_just_pressed(gkit::input::Key key) -> bool { } +auto gkit::Input::is_key_just_released(gkit::input::Key key) -> bool { + auto& cache = gkit::input::Cache::instance(); + return !cache.current_cache.key_cache.pressed_keys.contains(key) && + cache.previous_cache.key_cache.pressed_keys.contains(key); +} + + auto gkit::Input::is_mouse_button_pressed(input::MouseButton button) -> bool { - return gkit::input::Cache::instance().current_cache.mouse_button_cache.pressed_buttons.contains(button); + return gkit::input::Cache::instance().current_cache.mouse_cache.pressed_buttons.contains(button); } auto gkit::Input::is_mouse_button_released(input::MouseButton button) -> bool { - return !gkit::input::Cache::instance().current_cache.mouse_button_cache.pressed_buttons.contains(button); + return !gkit::input::Cache::instance().current_cache.mouse_cache.pressed_buttons.contains(button); } auto gkit::Input::is_mouse_button_just_pressed(input::MouseButton button) -> bool { auto& cache = gkit::input::Cache::instance(); - return cache.current_cache.mouse_button_cache.pressed_buttons.contains(button) && - !cache.previous_cache.mouse_button_cache.pressed_buttons.contains(button); + return cache.current_cache.mouse_cache.pressed_buttons.contains(button) && + !cache.previous_cache.mouse_cache.pressed_buttons.contains(button); } auto gkit::Input::is_mouse_button_just_released(input::MouseButton button) -> bool { auto& cache = gkit::input::Cache::instance(); - return !cache.current_cache.mouse_button_cache.pressed_buttons.contains(button) && - cache.previous_cache.mouse_button_cache.pressed_buttons.contains(button); + return !cache.current_cache.mouse_cache.pressed_buttons.contains(button) && + cache.previous_cache.mouse_cache.pressed_buttons.contains(button); } -auto gkit::Input::is_key_just_released(gkit::input::Key key) -> bool { +auto gkit::Input::get_mouse_move() -> input::MouseMove { auto& cache = gkit::input::Cache::instance(); - return !cache.current_cache.key_cache.pressed_keys.contains(key) && - cache.previous_cache.key_cache.pressed_keys.contains(key); + return cache.current_cache.mouse_cache.offset; +} + + + +auto gkit::Input::get_mouse_wheel() -> input::MouseWheel { + auto& cache = gkit::input::Cache::instance(); + return cache.current_cache.mouse_cache.wheel; } diff --git a/test/core/input/test_mouse_input.cpp b/test/core/input/test_mouse_input.cpp index 3411f97..a8aa5d6 100644 --- a/test/core/input/test_mouse_input.cpp +++ b/test/core/input/test_mouse_input.cpp @@ -1,4 +1,6 @@ +#include "gkit/math/vector2.hpp" #include "min_window_for_input_test.hpp" +#include #include #include #include @@ -57,5 +59,18 @@ auto main() -> int { std::cout << "Key Q is just pressed, exiting..." << std::endl; break; } + + auto mouse_move = input.get_mouse_move(); + auto mouse_wheel = input.get_mouse_wheel(); + + if (mouse_move != gkit::math::Vector2::zero()) { + auto [x, y] = mouse_move.properties(); + std::printf("Mouse move x = %f, y = %f\n", x, y); + } + + if (mouse_wheel != gkit::math::Vector2::zero()) { + auto [x, y] = mouse_wheel.properties(); + std::printf("Mouse wheel x = %f, y = %f\n", x, y); + } } } \ No newline at end of file From 510a52e895924638f0a420cccd365a3b38b8f8af Mon Sep 17 00:00:00 2001 From: Cora <17833654450@163.com> Date: Sun, 26 Apr 2026 21:32:07 +0800 Subject: [PATCH 4/4] remove unnesseary header file --- test/core/input/test_mouse_input.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/test/core/input/test_mouse_input.cpp b/test/core/input/test_mouse_input.cpp index a8aa5d6..b472a54 100644 --- a/test/core/input/test_mouse_input.cpp +++ b/test/core/input/test_mouse_input.cpp @@ -1,4 +1,3 @@ -#include "gkit/math/vector2.hpp" #include "min_window_for_input_test.hpp" #include #include