aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system
diff options
context:
space:
mode:
authorWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-17 20:12:30 +0100
committerWBoerenkamps <wrj.boerenkamps@student.avans.nl>2024-12-17 20:12:30 +0100
commiteb9c76082d7d0ee0c3e55cb8eadfa5f8978bd8f2 (patch)
tree97b0a6c7ae7e0feaba856a1e3e3836fa91a4dc0b /src/crepe/system
parentb421eec1073c1fb4b99d46cc36c5c9cbd8d3c4a7 (diff)
button improvements
Diffstat (limited to 'src/crepe/system')
-rw-r--r--src/crepe/system/InputSystem.cpp26
-rw-r--r--src/crepe/system/InputSystem.h22
2 files changed, 35 insertions, 13 deletions
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index fca540f..5d1537d 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -155,28 +155,27 @@ void InputSystem::handle_non_mouse_event(const EventData & event) {
void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_pos) {
ComponentManager & mgr = this->mediator.component_manager;
-
+ EventManager& event_mgr = this->mediator.event_manager;
RefVector<Button> buttons = mgr.get_components_by_type<Button>();
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());
-
+ 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();
bool was_hovering = button.hover;
if (this->is_mouse_inside_button(mouse_pos, button, transform)) {
button.hover = true;
- if (!button.on_mouse_enter) continue;
if (!was_hovering) {
- button.on_mouse_enter();
+ event_mgr.trigger_event<ButtonEnterEvent>(metadata);
}
} else {
button.hover = false;
- // Trigger the on_exit callback if the hover state just changed to false
- if (!button.on_mouse_exit) continue;
if (was_hovering) {
- button.on_mouse_exit();
+ event_mgr.trigger_event<ButtonExitEvent>(metadata);
}
}
}
@@ -184,19 +183,20 @@ void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_p
void InputSystem::handle_click(const MouseButton & mouse_button, const vec2 & mouse_pos) {
ComponentManager & mgr = this->mediator.component_manager;
-
+ EventManager& event_mgr = this->mediator.event_manager;
RefVector<Button> buttons = mgr.get_components_by_type<Button>();
-
+
for (Button & button : buttons) {
if (!button.active) continue;
- if (!button.on_click) 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();
if (this->is_mouse_inside_button(mouse_pos, button, transform)) {
-
- button.on_click();
+ event_mgr.trigger_event<ButtonPressEvent>(metadata);
}
}
}
diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h
index eefd9fe..9cad178 100644
--- a/src/crepe/system/InputSystem.h
+++ b/src/crepe/system/InputSystem.h
@@ -5,6 +5,9 @@
#include "../types.h"
#include "../util/OptionalRef.h"
+#include "../api/Metadata.h"
+#include "../api/Event.h"
+
#include "System.h"
@@ -14,6 +17,25 @@ class Camera;
class Button;
class Transform;
/**
+ * \brief Event triggered during a collision between objects.
+ */
+class ButtonPressEvent : public Event {
+public:
+ const Metadata& meta_data;
+ ButtonPressEvent(const Metadata& meta_data) : meta_data(meta_data){};;
+};
+class ButtonEnterEvent : public Event {
+public:
+ const Metadata& meta_data;
+ ButtonEnterEvent(const Metadata& meta_data) : meta_data(meta_data){};
+};
+class ButtonExitEvent : public Event {
+public:
+ const Metadata& meta_data;
+ ButtonExitEvent(const Metadata& meta_data) : meta_data(meta_data){};
+};
+
+/**
* \brief Handles the processing of input events created by SDLContext
*
* This system processes events such as mouse clicks, mouse movement, and keyboard