diff options
-rw-r--r-- | src/crepe/api/Button.h | 8 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 18 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 18 | ||||
-rw-r--r-- | src/test/InputTest.cpp | 6 |
4 files changed, 20 insertions, 30 deletions
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h index 0f446c9..d42527e 100644 --- a/src/crepe/api/Button.h +++ b/src/crepe/api/Button.h @@ -13,9 +13,9 @@ namespace crepe { * This component creates a clickable surface at the transform location with the specified width and height. * * The Button can be used in scripts by subscribing a EventHandler to the following events: - * - **ButtonPressEvent**: Triggered when the surface is clicked with the mouse. Contains GameObject metadata. - * - **MouseEnterEvent**: Triggered when the mouse enters the button area. Contains GameObject metadata. - * - **MouseExitEvent**: Triggered when the mouse leaves the button area. Contains GameObject metadata. + * - ButtonPressEvent + * - ButtonEnterEvent + * - ButtonExitEvent * \see EventManager * */ @@ -34,7 +34,7 @@ public: * * Since the button Event transfers the GameObject Metadata it will be the same for each button so only one button is allowed per GameObject * - * \return The maximum number of instances for this component + * \return 1 */ virtual int get_instances_max() const { return 1; } diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index e42eaeb..34aad06 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -10,10 +10,8 @@ using namespace crepe; void InputSystem::update() { ComponentManager & mgr = this->mediator.component_manager; - SDLContext & context = this->mediator.sdl_context; std::vector<EventData> event_list = context.get_events(); - RefVector<Button> buttons = mgr.get_components_by_type<Button>(); RefVector<Camera> cameras = mgr.get_components_by_type<Camera>(); OptionalRef<Camera> curr_cam_ref; @@ -160,12 +158,8 @@ void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_p for (Button & button : buttons) { if (!button.active) continue; - RefVector<Transform> transform_vec - = mgr.get_components_by_id<Transform>(button.game_object_id); - Transform & transform = transform_vec.front().get(); - RefVector<Metadata> metadata_vec - = mgr.get_components_by_id<Metadata>(button.game_object_id); - Metadata & metadata = metadata_vec.front().get(); + Metadata & metadata = mgr.get_components_by_id<Metadata>(button.game_object_id).front(); + Transform & transform = mgr.get_components_by_id<Transform>(button.game_object_id).front(); bool was_hovering = button.hover; if (this->is_mouse_inside_button(mouse_pos, button, transform)) { button.hover = true; @@ -188,12 +182,8 @@ void InputSystem::handle_click(const MouseButton & mouse_button, const vec2 & mo for (Button & button : buttons) { if (!button.active) continue; - RefVector<Metadata> metadata_vec - = mgr.get_components_by_id<Metadata>(button.game_object_id); - Metadata & metadata = metadata_vec.front().get(); - RefVector<Transform> transform_vec - = mgr.get_components_by_id<Transform>(button.game_object_id); - Transform & transform = transform_vec.front().get(); + Metadata & metadata = mgr.get_components_by_id<Metadata>(button.game_object_id).front(); + Transform & transform = mgr.get_components_by_id<Transform>(button.game_object_id).front(); if (this->is_mouse_inside_button(mouse_pos, button, transform)) { event_mgr.trigger_event<ButtonPressEvent>(metadata); diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 45c593a..e580d8e 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -19,31 +19,31 @@ class Transform; class ButtonPressEvent : public Event { public: //! Metadata of the button. - const Metadata & meta_data; + const Metadata & metadata; /** - * \param meta_data Metadata of the button pressed + * \param metadata Metadata of the button pressed */ - ButtonPressEvent(const Metadata & meta_data) : meta_data(meta_data){}; + ButtonPressEvent(const Metadata & metadata) : metadata(metadata){}; }; //! Event triggered when the mouse enters a button class ButtonEnterEvent : public Event { public: //! Metadata of the button. - const Metadata & meta_data; + const Metadata & metadata; /** - * \param meta_data Metadata of the button pressed + * \param metadata Metadata of the button pressed */ - ButtonEnterEvent(const Metadata & meta_data) : meta_data(meta_data){}; + ButtonEnterEvent(const Metadata & metadata) : metadata(metadata){}; }; //! Event triggered when the mouse leaves a button class ButtonExitEvent : public Event { public: //! Metadata of the button. - const Metadata & meta_data; + const Metadata & metadata; /** - * \param meta_data Metadata of the button pressed + * \param metadata Metadata of the button pressed */ - ButtonExitEvent(const Metadata & meta_data) : meta_data(meta_data){}; + ButtonExitEvent(const Metadata & metadata) : metadata(metadata){}; }; /** diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index 097667a..ce8ea44 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -215,7 +215,7 @@ TEST_F(InputTest, testButtonClick) { bool button_clicked = false; event_manager.subscribe<ButtonPressEvent>([&](const ButtonPressEvent & event) { button_clicked = true; - EXPECT_EQ(event.meta_data.game_object_id, button_obj.id); + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); return false; }); auto & button = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}); @@ -238,12 +238,12 @@ TEST_F(InputTest, testButtonHover) { bool button_hover = false; event_manager.subscribe<ButtonEnterEvent>([&](const ButtonEnterEvent & event) { button_hover = true; - EXPECT_EQ(event.meta_data.game_object_id, button_obj.id); + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); return false; }); event_manager.subscribe<ButtonExitEvent>([&](const ButtonExitEvent & event) { button_hover = false; - EXPECT_EQ(event.meta_data.game_object_id, button_obj.id); + EXPECT_EQ(event.metadata.game_object_id, button_obj.id); return false; }); auto & button = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}); |