aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/crepe/system/InputSystem.cpp58
-rw-r--r--src/example/button.cpp28
2 files changed, 46 insertions, 40 deletions
diff --git a/src/crepe/system/InputSystem.cpp b/src/crepe/system/InputSystem.cpp
index e746ab0..f590649 100644
--- a/src/crepe/system/InputSystem.cpp
+++ b/src/crepe/system/InputSystem.cpp
@@ -6,7 +6,6 @@
#include <iostream>
using namespace crepe;
-
void InputSystem::update() {
ComponentManager & mgr = this->component_manager;
EventManager & event_mgr = EventManager::get_instance();
@@ -21,18 +20,24 @@ void InputSystem::update() {
break;
}
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();
- int camera_origin_x = cam_transform.position.x + current_cam.offset.x - (current_cam.viewport_size.x / 2);
- int camera_origin_y = cam_transform.position.y + current_cam.offset.y - (current_cam.viewport_size.y / 2);
-
+ 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();
+ int camera_origin_x
+ = cam_transform.position.x + current_cam.offset.x - (current_cam.viewport_size.x / 2);
+ int camera_origin_y
+ = cam_transform.position.y + current_cam.offset.y - (current_cam.viewport_size.y / 2);
+
for (const SDLContext::EventData & event : event_list) {
int world_mouse_x = event.mouse_position.first + camera_origin_x;
int world_mouse_y = event.mouse_position.second + camera_origin_y;
// check if the mouse is within the viewport
- bool mouse_in_viewport = !(world_mouse_x < camera_origin_x || world_mouse_x > camera_origin_x + current_cam.viewport_size.x ||
- world_mouse_y < camera_origin_y || world_mouse_y > camera_origin_y + current_cam.viewport_size.y);
+ bool mouse_in_viewport
+ = !(world_mouse_x < camera_origin_x
+ || world_mouse_x > camera_origin_x + current_cam.viewport_size.x
+ || world_mouse_y < camera_origin_y
+ || world_mouse_y > camera_origin_y + current_cam.viewport_size.y);
switch (event.event_type) {
case SDLContext::EventType::KEYDOWN:
@@ -47,7 +52,7 @@ void InputSystem::update() {
});
break;
case SDLContext::EventType::MOUSEDOWN:
- if(!mouse_in_viewport){
+ if (!mouse_in_viewport) {
break;
}
event_mgr.queue_event<MousePressEvent>(MousePressEvent{
@@ -59,7 +64,7 @@ void InputSystem::update() {
last_mouse_button = event.mouse_button;
break;
case SDLContext::EventType::MOUSEUP: {
- if(!mouse_in_viewport){
+ if (!mouse_in_viewport) {
break;
}
event_mgr.queue_event<MouseReleaseEvent>(MouseReleaseEvent{
@@ -84,7 +89,7 @@ void InputSystem::update() {
}
} break;
case SDLContext::EventType::MOUSEMOVE:
- if(!mouse_in_viewport){
+ if (!mouse_in_viewport) {
break;
}
event_mgr.queue_event<MouseMoveEvent>(MouseMoveEvent{
@@ -110,7 +115,8 @@ void InputSystem::update() {
}
}
}
-void InputSystem::handle_move(const SDLContext::EventData & event_data, const int& world_mouse_x, const int& world_mouse_y) {
+void InputSystem::handle_move(const SDLContext::EventData & event_data,
+ const int & world_mouse_x, const int & world_mouse_y) {
ComponentManager & mgr = this->component_manager;
RefVector<Button> buttons = mgr.get_components_by_type<Button>();
@@ -122,7 +128,8 @@ void InputSystem::handle_move(const SDLContext::EventData & event_data, const in
if (!transform) continue;
bool was_hovering = button.hover;
- if (button.active && is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) {
+ if (button.active
+ && is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) {
button.hover = true;
if (!was_hovering && button.on_enter) {
button.on_enter();
@@ -137,7 +144,8 @@ void InputSystem::handle_move(const SDLContext::EventData & event_data, const in
}
}
-void InputSystem::handle_click(const MouseButton& mouse_button, const int& world_mouse_x, const int& world_mouse_y) {
+void InputSystem::handle_click(const MouseButton & mouse_button, const int & world_mouse_x,
+ const int & world_mouse_y) {
ComponentManager & mgr = this->component_manager;
RefVector<Button> buttons = mgr.get_components_by_type<Button>();
@@ -147,22 +155,22 @@ void InputSystem::handle_click(const MouseButton& mouse_button, const int& world
= mgr.get_components_by_id<Transform>(button.game_object_id);
OptionalRef<Transform> transform(transform_vec.front().get());
- if (button.active && is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) {
+ if (button.active
+ && is_mouse_inside_button(world_mouse_x, world_mouse_y, button, transform)) {
handle_button_press(button);
}
}
}
-bool InputSystem::is_mouse_inside_button(
- const int& mouse_x, const int& mouse_y,
- const Button & button, const Transform & transform) {
- int half_width = button.width / 2;
- int half_height = button.height / 2;
+bool InputSystem::is_mouse_inside_button(const int & mouse_x, const int & mouse_y,
+ const Button & button, const Transform & transform) {
+ int half_width = button.width / 2;
+ int half_height = button.height / 2;
- return mouse_x >= transform.position.x - half_width
- && mouse_x <= transform.position.x + half_width
- && mouse_y >= transform.position.y - half_height
- && mouse_y <= transform.position.y + half_height;
+ return mouse_x >= transform.position.x - half_width
+ && mouse_x <= transform.position.x + half_width
+ && mouse_y >= transform.position.y - half_height
+ && mouse_y <= transform.position.y + half_height;
}
void InputSystem::handle_button_press(Button & button) {
diff --git a/src/example/button.cpp b/src/example/button.cpp
index be236ec..96e6571 100644
--- a/src/example/button.cpp
+++ b/src/example/button.cpp
@@ -1,20 +1,20 @@
-#include <crepe/api/Camera.h>
-#include <crepe/ComponentManager.h>
+#include <SDL2/SDL_timer.h>
+#include <chrono>
#include <crepe/Component.h>
+#include <crepe/ComponentManager.h>
+#include <crepe/api/Animator.h>
+#include <crepe/api/Button.h>
+#include <crepe/api/Camera.h>
#include <crepe/api/Color.h>
+#include <crepe/api/EventManager.h>
#include <crepe/api/GameObject.h>
#include <crepe/api/Sprite.h>
+#include <crepe/api/Texture.h>
#include <crepe/api/Transform.h>
-#include <crepe/system/RenderSystem.h>
-#include <crepe/api/EventManager.h>
-#include <crepe/system/InputSystem.h>
#include <crepe/system/AnimatorSystem.h>
-#include <crepe/api/Button.h>
-#include <crepe/api/Animator.h>
-#include <crepe/api/Texture.h>
-#include <SDL2/SDL_timer.h>
+#include <crepe/system/InputSystem.h>
+#include <crepe/system/RenderSystem.h>
#include <crepe/types.h>
-#include <chrono>
#include <iostream>
using namespace crepe;
using namespace std;
@@ -22,7 +22,7 @@ using namespace std;
int main(int argc, char * argv[]) {
ComponentManager mgr;
RenderSystem sys{mgr};
- EventManager& event_mgr = EventManager::get_instance();
+ EventManager & event_mgr = EventManager::get_instance();
InputSystem input_sys{mgr};
AnimatorSystem asys{mgr};
GameObject camera_obj = mgr.new_object("", "", vec2{0, 0}, 0, 1);
@@ -32,10 +32,8 @@ int main(int argc, char * argv[]) {
auto s2 = Texture("asset/texture/test_ap43.png");
bool button_clicked = false;
auto & sprite2 = button_obj.add_component<Sprite>(
- s2, Color::GREEN, Sprite::FlipSettings{false, false}, 2, 1, 100);
- std::function<void()> on_click = [&]() {
- std::cout << "button clicked" << std::endl;
- };
+ s2, Color::GREEN, Sprite::FlipSettings{false, false}, 2, 1, 100);
+ std::function<void()> on_click = [&]() { std::cout << "button clicked" << std::endl; };
auto & button = button_obj.add_component<Button>(100, 100, on_click, false);
button.active = true;
auto start = std::chrono::steady_clock::now();