diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-13 09:19:55 +0100 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-12-13 09:19:55 +0100 |
commit | a03f90fa9bcbd5a0c5b5d1f2e50df183c7c3640c (patch) | |
tree | 2970cf9a1457cdd1f020e6edac868250777a8dcd /src/crepe/system | |
parent | 4b816c460d3b2fb81123ac7ce6113216fc7c9b17 (diff) |
make format
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/InputSystem.cpp | 57 | ||||
-rw-r--r-- | src/crepe/system/InputSystem.h | 14 |
2 files changed, 34 insertions, 37 deletions
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp index bb454af..8de700b 100644 --- a/src/crepe/system/InputSystem.cpp +++ b/src/crepe/system/InputSystem.cpp @@ -10,7 +10,7 @@ using namespace crepe; void InputSystem::update() { ComponentManager & mgr = this->mediator.component_manager; - + SDLContext & context = this->mediator.sdl_context; std::vector<SDLContext::EventData> event_list = context.get_events(); RefVector<Button> buttons = mgr.get_components_by_type<Button>(); @@ -31,31 +31,31 @@ void InputSystem::update() { Transform & cam_transform = transform_vec.front().get(); vec2 camera_origin = cam_transform.position + current_cam.data.postion_offset - - (current_cam.viewport_size / 2); + - (current_cam.viewport_size / 2); for (const SDLContext::EventData & event : event_list) { // Only calculate mouse coordinates for relevant events - if(this->is_mouse_event(event.event_type)) { - this->handle_mouse_event(event,camera_origin,current_cam); - + if (this->is_mouse_event(event.event_type)) { + this->handle_mouse_event(event, camera_origin, current_cam); + } else { this->handle_non_mouse_event(event); - } } } -void InputSystem::handle_mouse_event(const SDLContext::EventData &event, const vec2 &camera_origin, - const Camera ¤t_cam){ +void InputSystem::handle_mouse_event(const SDLContext::EventData & event, + const vec2 & camera_origin, const Camera & current_cam) { EventManager & event_mgr = this->mediator.event_manager; vec2 adjusted_mouse; adjusted_mouse.x = event.data.mouse_data.mouse_position.x + camera_origin.x; adjusted_mouse.x = event.data.mouse_data.mouse_position.y + camera_origin.y; // Check if the mouse is within the viewport if ((adjusted_mouse.x < camera_origin.x - || adjusted_mouse.x > camera_origin.x + current_cam.viewport_size.x - || adjusted_mouse.y < camera_origin.y - || adjusted_mouse.y > camera_origin.y + current_cam.viewport_size.y)) return; + || adjusted_mouse.x > camera_origin.x + current_cam.viewport_size.x + || adjusted_mouse.y < camera_origin.y + || adjusted_mouse.y > camera_origin.y + current_cam.viewport_size.y)) + return; // Handle mouse-specific events switch (event.event_type) { @@ -105,14 +105,14 @@ void InputSystem::handle_mouse_event(const SDLContext::EventData &event, const v default: break; - } + } } -void InputSystem::handle_non_mouse_event(const SDLContext::EventData &event){ +void InputSystem::handle_non_mouse_event(const SDLContext::EventData & event) { EventManager & event_mgr = this->mediator.event_manager; switch (event.event_type) { case SDLContext::EventType::KEYDOWN: - + event_mgr.queue_event<KeyPressEvent>( {.repeat = event.data.key_data.key_repeat, .key = event.data.key_data.key}); break; @@ -150,7 +150,7 @@ void InputSystem::handle_non_mouse_event(const SDLContext::EventData &event){ } } -bool InputSystem::is_mouse_event(SDLContext::EventType event_type){ +bool InputSystem::is_mouse_event(SDLContext::EventType event_type) { return (event_type == SDLContext::EventType::MOUSEDOWN || event_type == SDLContext::EventType::MOUSEUP || event_type == SDLContext::EventType::MOUSEMOVE @@ -158,29 +158,28 @@ bool InputSystem::is_mouse_event(SDLContext::EventType event_type){ } void InputSystem::handle_move(const SDLContext::EventData & event_data, - const vec2& mouse_pos) { + const vec2 & mouse_pos) { ComponentManager & mgr = this->mediator.component_manager; RefVector<Button> buttons = mgr.get_components_by_type<Button>(); for (Button & button : buttons) { - if(!button.active) continue; + if (!button.active) continue; RefVector<Transform> transform_vec = mgr.get_components_by_id<Transform>(button.game_object_id); Transform & transform(transform_vec.front().get()); bool was_hovering = button.hover; - if (this->is_mouse_inside_button(mouse_pos, button, - transform)) { + if (this->is_mouse_inside_button(mouse_pos, button, transform)) { button.hover = true; - if(!button.on_mouse_enter) continue; + if (!button.on_mouse_enter) continue; if (!was_hovering) { button.on_mouse_enter(); } } else { button.hover = false; // Trigger the on_exit callback if the hover state just changed to false - if(!button.on_mouse_exit) continue; + if (!button.on_mouse_exit) continue; if (was_hovering) { button.on_mouse_exit(); } @@ -188,28 +187,27 @@ void InputSystem::handle_move(const SDLContext::EventData & event_data, } } -void InputSystem::handle_click(const MouseButton & mouse_button, const vec2& mouse_pos) { +void InputSystem::handle_click(const MouseButton & mouse_button, const vec2 & mouse_pos) { ComponentManager & mgr = this->mediator.component_manager; RefVector<Button> buttons = mgr.get_components_by_type<Button>(); for (Button & button : buttons) { - if(!button.active) continue; + 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(); - - if (this->is_mouse_inside_button(mouse_pos, button, - transform)) { - + + if (this->is_mouse_inside_button(mouse_pos, button, transform)) { + button.on_click(); } } } -bool InputSystem::is_mouse_inside_button(const vec2& mouse_pos, - const Button & button, const Transform & transform) { +bool InputSystem::is_mouse_inside_button(const vec2 & mouse_pos, const Button & button, + const Transform & transform) { int actual_x = transform.position.x + button.offset.x; int actual_y = transform.position.y + button.offset.y; @@ -220,4 +218,3 @@ bool InputSystem::is_mouse_inside_button(const vec2& mouse_pos, return mouse_pos.x >= actual_x - half_width && mouse_pos.x <= actual_x + half_width && mouse_pos.y >= actual_y - half_height && mouse_pos.y <= actual_y + half_height; } - diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h index 7b8f510..63dfae5 100644 --- a/src/crepe/system/InputSystem.h +++ b/src/crepe/system/InputSystem.h @@ -52,8 +52,8 @@ private: * This method processes mouse events, adjusts the mouse position to world coordinates, * and triggers the appropriate mouse-specific event handling logic. */ - void handle_mouse_event(const SDLContext::EventData &event, const vec2 &camera_origin, - const Camera ¤t_cam); + void handle_mouse_event(const SDLContext::EventData & event, const vec2 & camera_origin, + const Camera & current_cam); /** * \brief Handles non-mouse-related events. * \param event The event data for the non-mouse event. @@ -61,7 +61,7 @@ private: * This method processes events that do not involve the mouse, such as keyboard events, * window events, and shutdown events, and triggers the corresponding event actions. */ - void handle_non_mouse_event(const SDLContext::EventData &event); + void handle_non_mouse_event(const SDLContext::EventData & event); /** * \brief Handles the mouse click event. * \param mouse_button The mouse button involved in the click. @@ -70,7 +70,7 @@ private: * * This method processes the mouse click event and triggers the corresponding button action. */ - void handle_click(const MouseButton & mouse_button,const vec2& mouse_pos); + void handle_click(const MouseButton & mouse_button, const vec2 & mouse_pos); /** * \brief Handles the mouse movement event. @@ -80,7 +80,7 @@ private: * * This method processes the mouse movement event and updates the button hover state. */ - void handle_move(const SDLContext::EventData & event_data, const vec2& mouse_pos); + void handle_move(const SDLContext::EventData & event_data, const vec2 & mouse_pos); /** * \brief Checks if the mouse position is inside the bounds of the button. @@ -90,8 +90,8 @@ private: * \param transform The transform component of the button. * \return True if the mouse is inside the button, false otherwise. */ - bool is_mouse_inside_button(const vec2& mouse_pos, - const Button & button, const Transform & transform); + bool is_mouse_inside_button(const vec2 & mouse_pos, const Button & button, + const Transform & transform); /** * \brief Handles the button press event, calling the on_click callback if necessary. |