aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/crepe/api/Button.cpp3
-rw-r--r--src/crepe/api/Button.h12
-rw-r--r--src/crepe/api/Config.h4
-rw-r--r--src/crepe/system/InputSystem.cpp77
-rw-r--r--src/crepe/system/InputSystem.h10
-rw-r--r--src/test/InputTest.cpp4
6 files changed, 44 insertions, 66 deletions
diff --git a/src/crepe/api/Button.cpp b/src/crepe/api/Button.cpp
index 76f74f0..305922c 100644
--- a/src/crepe/api/Button.cpp
+++ b/src/crepe/api/Button.cpp
@@ -3,9 +3,8 @@
namespace crepe {
Button::Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::function<void()> & on_click, bool is_toggle)
+ const std::function<void()> & on_click)
: UIObject(id, dimensions, offset),
- is_toggle(is_toggle),
on_click(on_click) {}
} // namespace crepe
diff --git a/src/crepe/api/Button.h b/src/crepe/api/Button.h
index 61b18d7..08f5dec 100644
--- a/src/crepe/api/Button.h
+++ b/src/crepe/api/Button.h
@@ -15,19 +15,11 @@ public:
* \param id The unique ID of the game object associated with this button.
* \param dimensions The width and height of the UIObject
* \param offset The offset relative this GameObjects Transform
- * \param is_toggle Optional flag to indicate if the button is a toggle button. Defaults to false.
* \param on_click callback function that will be invoked when the button is clicked.
*/
Button(game_object_id_t id, const vec2 & dimensions, const vec2 & offset,
- const std::function<void()> & on_click, bool is_toggle = false);
+ const std::function<void()> & on_click);
- /**
- * \brief Indicates if the button is a toggle button (can be pressed and released).
- *
- * A toggle button allows for a pressed/released state, whereas a regular button
- * typically only has an on-click state.
- */
- bool is_toggle = false;
// TODO: create separate toggle button class
/**
* \brief The callback function to be executed when the button is clicked.
@@ -56,8 +48,6 @@ public:
private:
//! friend relation for is_pressed and hover variables
friend class InputSystem;
- //! Indicates whether the toggle button is pressed
- bool is_pressed = false;
//! Indicates whether the mouse is currently hovering over the button
bool hover = false;
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index d2be3f3..324e639 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -89,8 +89,8 @@ public:
//! Configuration for click tolerance.
struct {
//! The maximum number of pixels the mouse can move between MouseDown and MouseUp events to be considered a click.
- int tolerance = 5;
- } click_tolerance;
+ int click_tolerance = 5;
+ } input;
};
} // namespace crepe
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index f5d8536..e76b1ec 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -2,7 +2,6 @@
#include "../api/Button.h"
#include "../manager/ComponentManager.h"
#include "../manager/EventManager.h"
-
#include "InputSystem.h"
using namespace crepe;
@@ -41,15 +40,15 @@ void InputSystem::update() {
|| event.event_type == SDLContext::EventType::MOUSEMOVE
|| event.event_type == SDLContext::EventType::MOUSEWHEEL) {
- int adjusted_mouse_x = event.mouse_data.mouse_position.x + camera_origin.x;
- int adjusted_mouse_y = event.mouse_data.mouse_position.y + camera_origin.y;
-
+ ivec2 adjusted_mouse;
+ adjusted_mouse.x = event.mouse_data.mouse_position.x + camera_origin.x;
+ adjusted_mouse.y = event.mouse_data.mouse_position.y + camera_origin.y;
// Check if the mouse is within the viewport
bool mouse_in_viewport
- = !(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);
+ = !(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);
if (!mouse_in_viewport) continue;
@@ -57,45 +56,44 @@ void InputSystem::update() {
switch (event.event_type) {
case SDLContext::EventType::MOUSEDOWN:
event_mgr.queue_event<MousePressEvent>({
- .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y},
+ .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
.button = event.mouse_data.mouse_button,
});
- this->last_mouse_down_position = {adjusted_mouse_x, adjusted_mouse_y};
+ this->last_mouse_down_position = {adjusted_mouse.x, adjusted_mouse.y};
this->last_mouse_button = event.mouse_data.mouse_button;
break;
case SDLContext::EventType::MOUSEUP: {
event_mgr.queue_event<MouseReleaseEvent>({
- .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y},
+ .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
.button = event.mouse_data.mouse_button,
});
- int delta_x = adjusted_mouse_x - this->last_mouse_down_position.x;
- int delta_y = adjusted_mouse_y - this->last_mouse_down_position.y;
+ int delta_x = adjusted_mouse.x - this->last_mouse_down_position.x;
+ int delta_y = adjusted_mouse.y - this->last_mouse_down_position.y;
if (this->last_mouse_button == event.mouse_data.mouse_button
&& std::abs(delta_x) <= click_tolerance
&& std::abs(delta_y) <= click_tolerance) {
event_mgr.queue_event<MouseClickEvent>({
- .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y},
+ .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
.button = event.mouse_data.mouse_button,
});
- this->handle_click(event.mouse_data.mouse_button, adjusted_mouse_x,
- adjusted_mouse_y);
+ this->handle_click(event.mouse_data.mouse_button, adjusted_mouse);
}
break;
}
case SDLContext::EventType::MOUSEMOVE:
event_mgr.queue_event<MouseMoveEvent>({
- .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y},
+ .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
.mouse_delta = event.mouse_data.rel_mouse_move,
});
- this->handle_move(event, adjusted_mouse_x, adjusted_mouse_y);
+ this->handle_move(event, adjusted_mouse);
break;
case SDLContext::EventType::MOUSEWHEEL:
event_mgr.queue_event<MouseScrollEvent>({
- .mouse_pos = {adjusted_mouse_x, adjusted_mouse_y},
+ .mouse_pos = {adjusted_mouse.x, adjusted_mouse.y},
.scroll_direction = event.mouse_data.scroll_direction,
.scroll_delta = event.mouse_data.scroll_delta,
});
@@ -148,54 +146,57 @@ void InputSystem::update() {
}
void InputSystem::handle_move(const SDLContext::EventData & event_data,
- const int adjusted_mouse_x, const int adjusted_mouse_y) {
+ const ivec2& 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;
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 (button.active
- && this->is_mouse_inside_button(adjusted_mouse_x, adjusted_mouse_y, button,
+ if (this->is_mouse_inside_button(mouse_pos, button,
transform)) {
button.hover = true;
- if (!was_hovering && button.on_mouse_enter) {
+ 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 (was_hovering && button.on_mouse_exit) {
+ if(!button.on_mouse_exit) continue;
+ if (was_hovering) {
button.on_mouse_exit();
}
}
}
}
-void InputSystem::handle_click(const MouseButton & mouse_button, const int adjusted_mouse_x,
- const int adjusted_mouse_y) {
+void InputSystem::handle_click(const MouseButton & mouse_button, const ivec2& 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.on_click) continue;
RefVector<Transform> transform_vec
= mgr.get_components_by_id<Transform>(button.game_object_id);
Transform & transform = transform_vec.front().get();
-
- if (button.active
- && this->is_mouse_inside_button(adjusted_mouse_x, adjusted_mouse_y, button,
+
+ if (this->is_mouse_inside_button(mouse_pos, button,
transform)) {
- this->handle_button_press(button);
+
+ button.on_click();
}
}
}
-bool InputSystem::is_mouse_inside_button(const int mouse_x, const int mouse_y,
+bool InputSystem::is_mouse_inside_button(const ivec2& 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;
@@ -204,17 +205,7 @@ bool InputSystem::is_mouse_inside_button(const int mouse_x, const int mouse_y,
int half_height = button.dimensions.y / 2;
// Check if the mouse is within the button's boundaries
- return mouse_x >= actual_x - half_width && mouse_x <= actual_x + half_width
- && mouse_y >= actual_y - half_height && mouse_y <= actual_y + half_height;
+ 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;
}
-void InputSystem::handle_button_press(Button & button) {
- if (button.is_toggle) {
- if (!button.is_pressed && button.on_click) {
- button.on_click();
- }
- button.is_pressed = !button.is_pressed;
- } else if (button.on_click) {
- button.on_click();
- }
-}
diff --git a/src/crepe/system/InputSystem.h b/src/crepe/system/InputSystem.h
index 5da8f32..3703635 100644
--- a/src/crepe/system/InputSystem.h
+++ b/src/crepe/system/InputSystem.h
@@ -39,7 +39,7 @@ private:
MouseButton last_mouse_button = MouseButton::NONE;
//! The maximum allowable distance between mouse down and mouse up to register as a click. This can be changed using the Config.
- int click_tolerance = Config::get_instance().click_tolerance.tolerance;
+ int click_tolerance = Config::get_instance().input.click_tolerance;
/**
* \brief Handles the mouse click event.
@@ -49,8 +49,7 @@ private:
*
* This method processes the mouse click event and triggers the corresponding button action.
*/
- void handle_click(const MouseButton & mouse_button, const int world_mouse_x,
- const int world_mouse_y);
+ void handle_click(const MouseButton & mouse_button,const ivec2& mouse_pos);
/**
* \brief Handles the mouse movement event.
@@ -60,8 +59,7 @@ private:
*
* This method processes the mouse movement event and updates the button hover state.
*/
- void handle_move(const SDLContext::EventData & event_data, const int world_mouse_x,
- const int world_mouse_y);
+ void handle_move(const SDLContext::EventData & event_data, const ivec2& mouse_pos);
/**
* \brief Checks if the mouse position is inside the bounds of the button.
@@ -71,7 +69,7 @@ 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 int world_mouse_x, const int world_mouse_y,
+ bool is_mouse_inside_button(const ivec2& mouse_pos,
const Button & button, const Transform & transform);
/**
diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp
index 94549a0..a7058b2 100644
--- a/src/test/InputTest.cpp
+++ b/src/test/InputTest.cpp
@@ -227,7 +227,7 @@ TEST_F(InputTest, testButtonClick) {
bool button_clicked = false;
std::function<void()> on_click = [&]() { button_clicked = true; };
auto & button
- = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false);
+ = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click);
bool hover = false;
button.active = true;
@@ -251,7 +251,7 @@ TEST_F(InputTest, testButtonHover) {
bool button_clicked = false;
std::function<void()> on_click = [&]() { button_clicked = true; };
auto & button
- = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click, false);
+ = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0}, on_click);
button.active = true;
// Mouse not on button