Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions include/gkit/core/input/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, input::Action> action_map;
Expand Down
1 change: 1 addition & 0 deletions include/gkit/core/input/mouse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions src/core/input/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<MouseButton>(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<MouseButton>(e.button.button);
frame.mouse_button_cache.pressed_buttons.erase(button);
frame.mouse_cache.pressed_buttons.erase(button);
});


Expand All @@ -48,15 +48,15 @@ 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;
});


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;
});
Expand Down
3 changes: 1 addition & 2 deletions src/core/input/cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ namespace gkit::input {
private:
struct KeyCache_t {
std::unordered_set<Key> pressed_keys;
/* std::uint32_t pressed_modifiers; */
};

struct MouseCache_t {
Expand All @@ -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 = {};
};

Expand Down
34 changes: 24 additions & 10 deletions src/core/input/input.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "gkit/core/input/input.hpp"
#include "core/input/cache.hpp"
#include "gkit/core/input/mouse.hpp"
#include <SDL3/SDL.h>
#include <type_traits>
#include <variant>
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions test/core/input/test_mouse_input.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "min_window_for_input_test.hpp"
#include <cstdio>
#include <gkit/core/input/input.hpp>
#include <misc/sdl_event_dispatcher.hpp>
#include <cstdint>
Expand Down Expand Up @@ -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);
}
}
}
Loading