diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-25 11:17:20 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-11-25 11:17:20 +0100 |
commit | 596358ffea72aec48b389609349f717e76396ae2 (patch) | |
tree | 4c023d4797f18c5b240765b5fef07fc793f52f42 /src | |
parent | 327154fd428e0798eea544d9f073f8e1293aa158 (diff) |
button test working
Diffstat (limited to 'src')
-rw-r--r-- | src/crepe/api/Button.h | 1 | ||||
-rw-r--r-- | src/crepe/facade/SDLContext.cpp | 2 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 31 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 2 | ||||
-rw-r--r-- | src/test/inputTest.cpp | 75 |
5 files changed, 92 insertions, 19 deletions
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 709854a..f533452 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -11,6 +11,7 @@ public: bool interactable = true; bool is_toggle = false; bool is_pressed = false; + bool hover = false; std::function<void()> on_click; public: virtual int get_instances_max() const { return 1; } diff --git a/src/crepe/facade/SDLContext.cpp b/src/crepe/facade/SDLContext.cpp index f87cc61..6371a51 100644 --- a/src/crepe/facade/SDLContext.cpp +++ b/src/crepe/facade/SDLContext.cpp @@ -361,6 +361,7 @@ std::vector<SDLContext::EventData> SDLContext::get_events(){ event_list.push_back(EventData{ .event_type = SDLContext::Event::MOUSEMOVE, .mouse_position = {event.button.x,event.button.y}, + .rel_mouse_move = {event.motion.yrel,event.motion.xrel} }); } break; @@ -371,7 +372,6 @@ std::vector<SDLContext::EventData> SDLContext::get_events(){ .event_type = SDLContext::Event::MOUSEWHEEL, .mouse_position = {event.motion.x,event.motion.y}, .wheel_delta = event.wheel.y, - .rel_mouse_move = {event.motion.yrel,event.motion.xrel}, }); } break; diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index 4560411..a3e28e4 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -56,10 +56,6 @@ void InputSystem::update() { if (last_mouse_button == event.mouse_button && std::abs(delta_x) <= click_tolerance && std::abs(delta_y) <= click_tolerance) { - std::cout << "Click registered at (" << event.mouse_position.first - << ", " << event.mouse_position.second << ") with button " - << static_cast<int>(event.mouse_button) << std::endl; - event_mgr.queue_event<MouseClickEvent>(MouseClickEvent{ .mouse_x = event.mouse_position.first, .mouse_y = event.mouse_position.second, @@ -67,8 +63,6 @@ void InputSystem::update() { }); this->handle_click(mouse_release_event); - } else { - std::cout << "Mouse release did not register as a click." << std::endl; } break; @@ -101,6 +95,25 @@ void InputSystem::update() { } } +void InputSystem::handle_move(const MouseMoveEvent){ + ComponentManager &mgr = this->component_manager; + + // Get the buttons and transforms + std::vector<std::reference_wrapper<Button>> buttons = mgr.get_components_by_type<Button>(); + std::vector<std::reference_wrapper<Transform>> transforms = mgr.get_components_by_type<Transform>(); + + for (Button &button : buttons) { + Transform* transform = find_transform_for_button(button, transforms); + if (!transform) continue; + + if (button.interactable && is_mouse_inside_button(event, button, *transform)) { + button.hover = true; + }else{ + button.hover = false; + } + } +} + void InputSystem::handle_click(const MouseReleaseEvent event) { ComponentManager &mgr = this->component_manager; @@ -134,11 +147,11 @@ bool InputSystem::is_mouse_inside_button(const MouseReleaseEvent &event, const B void InputSystem::handle_button_press(Button &button, const MouseReleaseEvent &event) { if (button.is_toggle) { - if (!button.is_pressed) { - button.on_click(); + if (!button.is_pressed && button.on_click) { + button.on_click(); } button.is_pressed = !button.is_pressed; - } else { + } else if(button.on_click) { button.on_click(); } } diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index db6b374..5f71687 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -21,7 +21,7 @@ private: MouseButton last_mouse_button = MouseButton::NONE; const int click_tolerance = 5; void handle_click(const MouseReleaseEvent); - + void handle_move(const MouseMoveEvent); Transform* find_transform_for_button(Button &button, std::vector<std::reference_wrapper<Transform>> &transforms); bool is_mouse_inside_button(const MouseReleaseEvent &event, const Button &button, const Transform &transform); diff --git a/src/test/inputTest.cpp b/src/test/inputTest.cpp index 2fc5ad9..0ca415a 100644 --- a/src/test/inputTest.cpp +++ b/src/test/inputTest.cpp @@ -96,6 +96,32 @@ TEST_F(InputTest, MouseUp) { EXPECT_TRUE(function_triggered); } +TEST_F(InputTest, MouseMove) { + bool function_triggered = false; + EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent& e) { + // Handle the mouse click event here + function_triggered = true; + EXPECT_EQ(e.mouse_x, 10); + EXPECT_EQ(e.mouse_y, 10); + EXPECT_EQ(e.rel_x, 10); + EXPECT_EQ(e.rel_y, 10); + return false; + }; + event_manager.subscribe<MouseMoveEvent>(on_mouse_move); + + SDL_Event event; + SDL_zero(event); + event.type = SDL_MOUSEMOTION; + event.motion.x = 10; + event.motion.y = 10; + event.motion.xrel = 10; + event.motion.yrel = 10; + SDL_PushEvent(&event); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(function_triggered); +} + TEST_F(InputTest, KeyDown) { bool function_triggered = false; @@ -159,13 +185,11 @@ TEST_F(InputTest, MouseClick) { EXPECT_TRUE(on_click_triggered); } -TEST_F(InputTest, testButton) { +TEST_F(InputTest, testButtonClick) { GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); - //auto test_button = std::make_unique<Button>(); - // Button test_button = new Button(obj.id); - - auto button = obj.add_component<Button>(); + auto& button = obj.add_component<Button>(); bool button_clicked = false; + bool hover = false; button.active = true; button.interactable = true; button.width = 100; @@ -173,11 +197,46 @@ TEST_F(InputTest, testButton) { std::function<void()> on_click = [&]() { button_clicked = true; }; - button.on_click = on_click; + button.on_click = on_click; + button.is_pressed = false; + button.is_toggle = false; + this->simulate_mouse_click(101,101, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); + this->simulate_mouse_click(10,10, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_TRUE(button_clicked); +} + +TEST_F(InputTest, testButtonHover) { + GameObject obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1); + auto& button = obj.add_component<Button>(); + bool button_clicked = false; + bool hover = false; + button.active = true; + button.interactable = true; + button.width = 100; + button.height = 100; button.is_pressed = false; button.is_toggle = false; - this->simulate_mouse_click(10,10, SDL_BUTTON_LEFT); + + SDL_Event event; + SDL_zero(event); + event.type = SDL_MOUSEMOTION; + event.motion.x = 10; + event.motion.y = 10; + event.motion.xrel = 10; + event.motion.yrel = 10; + SDL_PushEvent(&event); + + this->simulate_mouse_click(101,101, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); + this->simulate_mouse_click(10,10, SDL_BUTTON_LEFT); input_system.update(); event_manager.dispatch_events(); - //EXPECT_TRUE(button_clicked); + EXPECT_TRUE(button_clicked); } |