diff options
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 26 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 33 | ||||
-rw-r--r-- | src/crepe/system/ScriptSystem.cpp | 5 |
3 files changed, 48 insertions, 16 deletions
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index fee664c..cd6091d 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; @@ -156,7 +154,7 @@ void InputSystem::handle_non_mouse_event(const EventData & event) { void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_pos, const Camera & current_cam) { 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) { @@ -166,20 +164,19 @@ void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_p = mgr.get_components_by_id<Transform>(button.game_object_id).front(); Transform & cam_transform = mgr.get_components_by_id<Transform>(current_cam.game_object_id).front(); + Metadata & metadata + = mgr.get_components_by_id<Metadata>(button.game_object_id).front(); bool was_hovering = button.hover; if (this->is_mouse_inside_button(mouse_pos, button, transform, cam_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); } } } @@ -188,19 +185,18 @@ void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_p void InputSystem::handle_click(const MouseButton & mouse_button, const vec2 & mouse_pos, const Camera & current_cam) { ComponentManager & mgr = this->mediator.component_manager; - + EventManager & event_mgr = this->mediator.event_manager; RefVector<Button> buttons = mgr.get_components_by_type<Button>(); Transform & cam_transform = mgr.get_components_by_id<Transform>(current_cam.game_object_id).front(); for (Button & button : buttons) { if (!button.active) continue; - if (!button.on_click) continue; - 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, cam_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 9ab76f6..fbcf3f6 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -3,6 +3,8 @@ #include "../api/Config.h" #include "../facade/EventData.h" +#include "../api/Event.h" +#include "../api/Metadata.h" #include "../types.h" #include "../util/OptionalRef.h" @@ -13,6 +15,37 @@ namespace crepe { class Camera; class Button; class Transform; +//! Event triggered when a button is clicked +class ButtonPressEvent : public Event { +public: + //! Metadata of the button. + const Metadata & metadata; + /** + * \param metadata Metadata of the button pressed + */ + 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 & metadata; + /** + * \param metadata Metadata of the button pressed + */ + 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 & metadata; + /** + * \param metadata Metadata of the button pressed + */ + ButtonExitEvent(const Metadata & metadata) : metadata(metadata){}; +}; + /** * \brief Handles the processing of input events created by SDLContext * diff --git a/src/crepe/system/ScriptSystem.cpp b/src/crepe/system/ScriptSystem.cpp index d6b2ca1..0605c7a 100644 --- a/src/crepe/system/ScriptSystem.cpp +++ b/src/crepe/system/ScriptSystem.cpp @@ -11,6 +11,7 @@ void ScriptSystem::update() { dbg_trace(); ComponentManager & mgr = this->mediator.component_manager; + LoopTimerManager & timer = this->mediator.loop_timer; RefVector<BehaviorScript> behavior_scripts = mgr.get_components_by_type<BehaviorScript>(); for (BehaviorScript & behavior_script : behavior_scripts) { @@ -23,6 +24,8 @@ void ScriptSystem::update() { script->init(); script->initialized = true; } - script->update(); + + duration_t delta_time = timer.get_scaled_fixed_delta_time(); + script->update(delta_time); } } |