aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/InputSystem.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/system/InputSystem.cpp')
-rw-r--r--src/crepe/system/InputSystem.cpp94
1 files changed, 55 insertions, 39 deletions
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index d209282..be7eda6 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -1,8 +1,8 @@
#include "../api/Button.h"
+#include "../api/Config.h"
#include "../facade/SDLContext.h"
#include "../manager/ComponentManager.h"
#include "../manager/EventManager.h"
-#include "util/Log.h"
#include "InputSystem.h"
@@ -12,7 +12,7 @@ void InputSystem::fixed_update() {
ComponentManager & mgr = this->mediator.component_manager;
SDLContext & context = this->mediator.sdl_context;
std::vector<EventData> event_list = context.get_events();
- RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
+ const RefVector<Camera> cameras = mgr.get_components_by_type<Camera>();
OptionalRef<Camera> curr_cam_ref;
// Find the active camera
@@ -24,9 +24,8 @@ void InputSystem::fixed_update() {
if (!curr_cam_ref) return;
Camera & current_cam = curr_cam_ref;
- RefVector<Transform> transform_vec
- = mgr.get_components_by_id<Transform>(current_cam.game_object_id);
- Transform & cam_transform = transform_vec.front().get();
+ const Transform & cam_transform
+ = mgr.get_components_by_id<Transform>(current_cam.game_object_id).front();
vec2 camera_origin = cam_transform.position + current_cam.data.postion_offset
- (current_cam.viewport_size / 2);
@@ -45,12 +44,13 @@ void InputSystem::fixed_update() {
}
}
-void InputSystem::handle_mouse_event(const EventData & event, const vec2 & camera_origin,
- const Camera & current_cam) {
+void InputSystem::handle_mouse_event(
+ const 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;
+ adjusted_mouse.y = 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
@@ -83,7 +83,9 @@ void InputSystem::handle_mouse_event(const EventData & event, const vec2 & camer
.mouse_pos = adjusted_mouse,
.button = event.data.mouse_data.mouse_button,
});
- this->handle_click(event.data.mouse_data.mouse_button, adjusted_mouse);
+ this->handle_click(
+ event.data.mouse_data.mouse_button, adjusted_mouse, current_cam
+ );
}
break;
}
@@ -93,7 +95,7 @@ void InputSystem::handle_mouse_event(const EventData & event, const vec2 & camer
.mouse_pos = adjusted_mouse,
.mouse_delta = event.data.mouse_data.rel_mouse_move,
});
- this->handle_move(event, adjusted_mouse);
+ this->handle_move(event, adjusted_mouse, current_cam);
break;
case EventType::MOUSE_WHEEL:
@@ -115,7 +117,8 @@ void InputSystem::handle_non_mouse_event(const EventData & event) {
case EventType::KEY_DOWN:
event_mgr.queue_event<KeyPressEvent>(
- {.repeat = event.data.key_data.key_repeat, .key = event.data.key_data.key});
+ {.repeat = event.data.key_data.key_repeat, .key = event.data.key_data.key}
+ );
break;
case EventType::KEY_UP:
event_mgr.queue_event<KeyReleaseEvent>({.key = event.data.key_data.key});
@@ -128,11 +131,13 @@ void InputSystem::handle_non_mouse_event(const EventData & event) {
break;
case EventType::WINDOW_RESIZE:
event_mgr.queue_event<WindowResizeEvent>(
- WindowResizeEvent{.dimensions = event.data.window_data.resize_dimension});
+ WindowResizeEvent {.dimensions = event.data.window_data.resize_dimension}
+ );
break;
case EventType::WINDOW_MOVE:
event_mgr.queue_event<WindowMoveEvent>(
- {.delta_move = event.data.window_data.move_delta});
+ {.delta_move = event.data.window_data.move_delta}
+ );
break;
case EventType::WINDOW_MINIMIZE:
event_mgr.queue_event<WindowMinimizeEvent>({});
@@ -151,59 +156,70 @@ void InputSystem::handle_non_mouse_event(const EventData & event) {
}
}
-void InputSystem::handle_move(const EventData & event_data, const vec2 & mouse_pos) {
+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>();
+ const RefVector<Button> buttons = mgr.get_components_by_type<Button>();
for (Button & button : buttons) {
if (!button.active) continue;
- Metadata & metadata
- = mgr.get_components_by_id<Metadata>(button.game_object_id).front();
- Transform & transform
+
+ const Transform & transform
= mgr.get_components_by_id<Transform>(button.game_object_id).front();
+ const Transform & cam_transform
+ = mgr.get_components_by_id<Transform>(current_cam.game_object_id).front();
+ const 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)) {
+
+ if (this->is_mouse_inside_button(mouse_pos, button, transform, cam_transform)) {
button.hover = true;
if (!was_hovering) {
- event_mgr.trigger_event<ButtonEnterEvent>(metadata);
+ event_mgr.trigger_event<ButtonEnterEvent>(metadata, metadata.game_object_id);
}
} else {
button.hover = false;
if (was_hovering) {
- event_mgr.trigger_event<ButtonExitEvent>(metadata);
+ event_mgr.trigger_event<ButtonExitEvent>(metadata, metadata.game_object_id);
}
}
}
}
-void InputSystem::handle_click(const MouseButton & mouse_button, const vec2 & mouse_pos) {
+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>();
-
+ const RefVector<Button> buttons = mgr.get_components_by_type<Button>();
+ const Transform & cam_transform
+ = mgr.get_components_by_id<Transform>(current_cam.game_object_id).front();
for (Button & button : buttons) {
if (!button.active) continue;
- Metadata & metadata
+ const Metadata & metadata
= mgr.get_components_by_id<Metadata>(button.game_object_id).front();
- Transform & transform
+ const 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);
+ if (this->is_mouse_inside_button(mouse_pos, button, transform, cam_transform)) {
+ event_mgr.trigger_event<ButtonPressEvent>(metadata, metadata.game_object_id);
}
}
}
-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;
-
- int half_width = button.dimensions.x / 2;
- int half_height = button.dimensions.y / 2;
+bool InputSystem::is_mouse_inside_button(
+ const vec2 & mouse_pos, const Button & button, const Transform & transform,
+ const Transform & cam_transform
+) {
+ vec2 actual_pos = transform.position + button.offset;
+ if (!button.data.world_space) {
+ actual_pos += cam_transform.position;
+ }
+ vec2 half_dimensions = button.dimensions * transform.scale / 2;
- // Check if the mouse is within the button's boundaries
- 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;
+ return mouse_pos.x >= actual_pos.x - half_dimensions.x
+ && mouse_pos.x <= actual_pos.x + half_dimensions.x
+ && mouse_pos.y >= actual_pos.y - half_dimensions.y
+ && mouse_pos.y <= actual_pos.y + half_dimensions.y;
}