diff --git a/include/gkit/core/input/input.hpp b/include/gkit/core/input/input.hpp index a92ede1..35f7fa5 100644 --- a/include/gkit/core/input/input.hpp +++ b/include/gkit/core/input/input.hpp @@ -13,32 +13,43 @@ 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; - 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; - - // 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 - 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 - 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_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 + *******************************/ + [[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 + *******************************/ + [[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; + + [[nodiscard]] auto get_mouse_move() -> input::MouseMove; + [[nodiscard]] auto get_mouse_wheel() -> input::MouseWheel; + + /******************************** + * gamepad button related + *******************************/ + [[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; 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 acb3541..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; } @@ -111,7 +125,7 @@ auto gkit::Input::is_action_pressed(std::string name) -> bool { } return gkit::input::Cache::instance().modifiers_pressed(chord.modifiers); - } + } return false; }, action.chord); diff --git a/test/core/input/test_mouse_input.cpp b/test/core/input/test_mouse_input.cpp index 3411f97..b472a54 100644 --- a/test/core/input/test_mouse_input.cpp +++ b/test/core/input/test_mouse_input.cpp @@ -1,4 +1,5 @@ #include "min_window_for_input_test.hpp" +#include #include #include #include @@ -57,5 +58,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