diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Button.cpp | 6 | ||||
-rw-r--r-- | src/crepe/api/Button.h | 2 | ||||
-rw-r--r-- | src/crepe/api/Event.h | 2 | ||||
-rw-r--r-- | src/crepe/api/UiObject.cpp | 5 | ||||
-rw-r--r-- | src/crepe/api/UiObject.h | 3 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 25 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 6 | ||||
-rw-r--r-- | src/example/button.cpp | 3 | ||||
-rw-r--r-- | src/test/InputTest.cpp | 10 |
9 files changed, 30 insertions, 32 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp index f179f60..b104329 100644 --- a/src/crepe/api/Button.cpp +++ b/src/crepe/api/Button.cpp @@ -2,9 +2,9 @@ namespace crepe { -Button::Button(game_object_id_t id, vec2 dimensions,vec2 offset, std::function<void()> on_click, - bool is_toggle) - : UIObject(id, dimensions,offset), +Button::Button(game_object_id_t id, vec2 dimensions, vec2 offset, + std::function<void()> on_click, bool is_toggle) + : UIObject(id, dimensions, offset), is_toggle(is_toggle), on_click(on_click) {} diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 043cc78..5c803ba 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -21,7 +21,7 @@ public: * \param is_toggle Optional flag to indicate if the button is a toggle button. Defaults to false. * \param on_click callback function that will be invoked when the button is clicked. */ - Button(game_object_id_t id, vec2 dimensions,vec2 offset, std::function<void()> on_click, + Button(game_object_id_t id, vec2 dimensions, vec2 offset, std::function<void()> on_click, bool is_toggle = false); /** diff --git a/src/crepe/api/Event.h b/src/crepe/api/Event.h index 8083d7c..6298118 100644 --- a/src/crepe/api/Event.h +++ b/src/crepe/api/Event.h @@ -94,8 +94,6 @@ public: // Movement since last event in y int delta_y = 0; - - }; /** diff --git a/src/crepe/api/UiObject.cpp b/src/crepe/api/UiObject.cpp index 0262d31..2b287b3 100644 --- a/src/crepe/api/UiObject.cpp +++ b/src/crepe/api/UiObject.cpp @@ -2,6 +2,7 @@ using namespace crepe; -UIObject::UIObject(game_object_id_t id, vec2 dimensions,vec2 offset) +UIObject::UIObject(game_object_id_t id, vec2 dimensions, vec2 offset) : Component(id), - dimensions(dimensions),offset(offset){} + dimensions(dimensions), + offset(offset) {} diff --git a/src/crepe/api/UiObject.h b/src/crepe/api/UiObject.h index 1130f99..0634f97 100644 --- a/src/crepe/api/UiObject.h +++ b/src/crepe/api/UiObject.h @@ -16,11 +16,10 @@ public: * \param dimensions width and height of the UIObject * \param offset Offset relative to the GameObject Transform */ - UIObject(game_object_id_t id, vec2 dimensions,vec2 offset); + UIObject(game_object_id_t id, vec2 dimensions, vec2 offset); //! Width and height of the UIObject vec2 dimensions; vec2 offset; - }; } // namespace crepe diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index bbf2547..21ec24b 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -152,7 +152,7 @@ void InputSystem::handle_click(const MouseButton & mouse_button, const int world for (Button & button : buttons) { RefVector<Transform> transform_vec = mgr.get_components_by_id<Transform>(button.game_object_id); - Transform& transform(transform_vec.front().get()); + Transform & transform(transform_vec.front().get()); if (button.active && this->is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) { @@ -162,20 +162,17 @@ void InputSystem::handle_click(const MouseButton & mouse_button, const int world } bool InputSystem::is_mouse_inside_button(const int mouse_x, const int mouse_y, - const Button &button, const Transform &transform) { - int actual_x = transform.position.x + button.offset.x; - int actual_y = transform.position.y + button.offset.y; - - int half_width = button.dimensions.x / 2; - int half_height = button.dimensions.y / 2; - - // Check if the mouse is within the button's boundaries - return mouse_x >= actual_x - half_width - && mouse_x <= actual_x + half_width - && mouse_y >= actual_y - half_height - && mouse_y <= actual_y + half_height; -} + const Button & button, const Transform & transform) { + int actual_x = transform.position.x + button.offset.x; + int actual_y = transform.position.y + button.offset.y; + int half_width = button.dimensions.x / 2; + int half_height = button.dimensions.y / 2; + + // Check if the mouse is within the button's boundaries + return mouse_x >= actual_x - half_width && mouse_x <= actual_x + half_width + && mouse_y >= actual_y - half_height && mouse_y <= actual_y + half_height; +} void InputSystem::handle_button_press(Button & button) { //checks if the button is a toggle button diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 557ba47..eafeb91 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -31,15 +31,15 @@ public: private: //! Stores the last position of the mouse when the button was pressed. - ivec2 last_mouse_down_position{std::numeric_limits<int>::max(), std::numeric_limits<int>::max()}; + ivec2 last_mouse_down_position{std::numeric_limits<int>::max(), + std::numeric_limits<int>::max()}; //! Stores the last mouse button pressed. MouseButton last_mouse_button = MouseButton::NONE; - + //! The maximum allowable distance between mouse down and mouse up to register as a click. const int click_tolerance = 5; - /** * \brief Handles the mouse click event. * \param mouse_button The mouse button involved in the click. diff --git a/src/example/button.cpp b/src/example/button.cpp index 7efd889..00bdc28 100644 --- a/src/example/button.cpp +++ b/src/example/button.cpp @@ -36,7 +36,8 @@ int main(int argc, char * argv[]) { std::function<void()> on_click = [&]() { std::cout << "button clicked" << std::endl; }; std::function<void()> on_enter = [&]() { std::cout << "enter" << std::endl; }; std::function<void()> on_exit = [&]() { std::cout << "exit" << std::endl; }; - auto & button = button_obj.add_component<Button>(vec2{100,100},vec2{0,0}, on_click, false); + auto & button + = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false); button.on_mouse_enter = on_enter; button.on_mouse_exit = on_exit; button.is_toggle = true; diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index f7d1059..f9b2fe7 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -119,8 +119,8 @@ TEST_F(InputTest, MouseMove) { function_triggered = true; EXPECT_EQ(e.mouse_x, 0); EXPECT_EQ(e.mouse_y, 0); - EXPECT_EQ(e.delta_x , 10); - EXPECT_EQ(e.delta_y , 10); + EXPECT_EQ(e.delta_x, 10); + EXPECT_EQ(e.delta_y, 10); return false; }; event_manager.subscribe<MouseMoveEvent>(on_mouse_move); @@ -223,7 +223,8 @@ TEST_F(InputTest, testButtonClick) { GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); bool button_clicked = false; std::function<void()> on_click = [&]() { button_clicked = true; }; - auto & button = button_obj.add_component<Button>(vec2{100,100},vec2{0,0}, on_click, false); + auto & button + = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false); bool hover = false; button.active = true; @@ -249,7 +250,8 @@ TEST_F(InputTest, testButtonHover) { GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); bool button_clicked = false; std::function<void()> on_click = [&]() { button_clicked = true; }; - auto & button = button_obj.add_component<Button>(vec2{100,100},vec2{0,0}, on_click, false); + auto & button + = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false); button.active = true; button.is_pressed = false; button.is_toggle = false; |