aboutsummaryrefslogtreecommitdiff
path: root/src/test/InputTest.cpp
diff options
context:
space:
mode:
authormax-001 <maxsmits21@kpnmail.nl>2024-12-20 12:01:36 +0100
committermax-001 <maxsmits21@kpnmail.nl>2024-12-20 12:01:36 +0100
commit79d3a9f4311e6684b6df83a15ca7844f58c1959c (patch)
treeb2883e83f61cee9edf290a6a7228c7f0b1fbae8a /src/test/InputTest.cpp
parent9140b73e4af7aa925b53e4fb4e6aa7f4ea2e3385 (diff)
parent03aea832aa0bc2edba2cc5ab4d9f8eba42d355be (diff)
Merge remote-tracking branch 'origin/master' into max/game
Diffstat (limited to 'src/test/InputTest.cpp')
-rw-r--r--src/test/InputTest.cpp136
1 files changed, 97 insertions, 39 deletions
diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp
index d893276..0243bd8 100644
--- a/src/test/InputTest.cpp
+++ b/src/test/InputTest.cpp
@@ -1,7 +1,10 @@
-#include "system/RenderSystem.h"
#include <gtest/gtest.h>
+
+#include <crepe/manager/ResourceManager.h>
+#include <crepe/system/RenderSystem.h>
#define protected public
#define private public
+
#include "api/KeyCodes.h"
#include "manager/ComponentManager.h"
#include "manager/EventManager.h"
@@ -29,17 +32,19 @@ public:
SDLContext sdl_context{mediator};
InputSystem input_system{mediator};
+ ResourceManager resman{mediator};
RenderSystem render{mediator};
EventManager event_manager{mediator};
//GameObject camera;
+ vec2 offset = {100, 200};
protected:
void SetUp() override {
- GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1);
+ GameObject obj = mgr.new_object("camera", "camera", offset, 0, 1);
auto & camera
= obj.add_component<Camera>(ivec2{500, 500}, vec2{500, 500},
Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f});
- render.update();
+ render.frame_update();
//mediator.event_manager = event_manager;
//mediator.component_manager = mgr;
//event_manager.clear();
@@ -71,8 +76,7 @@ TEST_F(InputTest, MouseDown) {
EventHandler<MousePressEvent> on_mouse_down = [&](const MousePressEvent & event) {
mouse_triggered = true;
//middle of the screen = 0,0
- EXPECT_EQ(event.mouse_pos.x, 0);
- EXPECT_EQ(event.mouse_pos.y, 0);
+ EXPECT_EQ(event.mouse_pos, offset);
EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
return false;
};
@@ -87,7 +91,7 @@ TEST_F(InputTest, MouseDown) {
event.button.button = SDL_BUTTON_LEFT;
SDL_PushEvent(&event);
- input_system.update();
+ input_system.fixed_update();
event_manager.dispatch_events();
EXPECT_TRUE(mouse_triggered);
}
@@ -96,8 +100,7 @@ TEST_F(InputTest, MouseUp) {
bool function_triggered = false;
EventHandler<MouseReleaseEvent> on_mouse_release = [&](const MouseReleaseEvent & e) {
function_triggered = true;
- EXPECT_EQ(e.mouse_pos.x, 0);
- EXPECT_EQ(e.mouse_pos.y, 0);
+ EXPECT_EQ(e.mouse_pos, offset);
EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE);
return false;
};
@@ -111,7 +114,7 @@ TEST_F(InputTest, MouseUp) {
event.button.button = SDL_BUTTON_LEFT;
SDL_PushEvent(&event);
- input_system.update();
+ input_system.fixed_update();
event_manager.dispatch_events();
EXPECT_TRUE(function_triggered);
}
@@ -120,8 +123,7 @@ TEST_F(InputTest, MouseMove) {
bool function_triggered = false;
EventHandler<MouseMoveEvent> on_mouse_move = [&](const MouseMoveEvent & e) {
function_triggered = true;
- EXPECT_EQ(e.mouse_pos.x, 0);
- EXPECT_EQ(e.mouse_pos.y, 0);
+ EXPECT_EQ(e.mouse_pos, offset);
EXPECT_EQ(e.mouse_delta.x, 10);
EXPECT_EQ(e.mouse_delta.y, 10);
return false;
@@ -137,7 +139,7 @@ TEST_F(InputTest, MouseMove) {
event.motion.yrel = 10;
SDL_PushEvent(&event);
- input_system.update();
+ input_system.fixed_update();
event_manager.dispatch_events();
EXPECT_TRUE(function_triggered);
}
@@ -163,7 +165,7 @@ TEST_F(InputTest, KeyDown) {
test_event.key.repeat = 1; // Set repeat flag
SDL_PushEvent(&test_event);
- input_system.update(); // Process the event
+ input_system.fixed_update(); // Process the event
event_manager.dispatch_events(); // Dispatch events to handlers
EXPECT_TRUE(function_triggered); // Check if the handler was triggered
@@ -184,7 +186,7 @@ TEST_F(InputTest, KeyUp) {
event.key.keysym.scancode = SDL_SCANCODE_B;
SDL_PushEvent(&event);
- input_system.update();
+ input_system.fixed_update();
event_manager.dispatch_events();
EXPECT_TRUE(function_triggered);
}
@@ -194,14 +196,13 @@ TEST_F(InputTest, MouseClick) {
EventHandler<MouseClickEvent> on_mouse_click = [&](const MouseClickEvent & event) {
on_click_triggered = true;
EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE);
- EXPECT_EQ(event.mouse_pos.x, 0);
- EXPECT_EQ(event.mouse_pos.y, 0);
+ EXPECT_EQ(event.mouse_pos, offset);
return false;
};
event_manager.subscribe<MouseClickEvent>(on_mouse_click);
this->simulate_mouse_click(250, 250, SDL_BUTTON_LEFT);
- input_system.update();
+ input_system.fixed_update();
event_manager.dispatch_events();
EXPECT_TRUE(on_click_triggered);
}
@@ -209,43 +210,85 @@ TEST_F(InputTest, MouseClick) {
TEST_F(InputTest, testButtonClick) {
GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
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);
+ event_manager.subscribe<ButtonPressEvent>([&](const ButtonPressEvent & event) {
+ button_clicked = true;
+ EXPECT_EQ(event.metadata.game_object_id, button_obj.id);
+ return false;
+ });
+ auto & button = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0});
bool hover = false;
button.active = true;
this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT);
- input_system.update();
+ input_system.fixed_update();
event_manager.dispatch_events();
EXPECT_FALSE(button_clicked);
this->simulate_mouse_click(250, 250, SDL_BUTTON_LEFT);
- input_system.update();
+ input_system.fixed_update();
event_manager.dispatch_events();
EXPECT_TRUE(button_clicked);
}
-
-TEST_F(InputTest, testButtonHover) {
- GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+TEST_F(InputTest, buttonPositionCamera) {
+ GameObject button_obj = mgr.new_object("body", "person", vec2{50, 50}, 0, 1);
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);
- button.active = true;
+ event_manager.subscribe<ButtonPressEvent>([&](const ButtonPressEvent & event) {
+ button_clicked = true;
+ EXPECT_EQ(event.metadata.game_object_id, button_obj.id);
+ return false;
+ });
+ auto & button = button_obj.add_component<Button>(vec2{10, 10}, vec2{0, 0});
- // Mouse not on button
- SDL_Event event;
- SDL_zero(event);
- event.type = SDL_MOUSEMOTION;
- event.motion.x = 700;
- event.motion.y = 700;
- event.motion.xrel = 10;
- event.motion.yrel = 10;
- SDL_PushEvent(&event);
+ button.world_space = false;
+ bool hover = false;
+ button.active = true;
+ this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT);
+ input_system.fixed_update();
+ event_manager.dispatch_events();
+ EXPECT_FALSE(button_clicked);
- input_system.update();
+ this->simulate_mouse_click(300, 300, SDL_BUTTON_LEFT);
+ input_system.fixed_update();
event_manager.dispatch_events();
- EXPECT_FALSE(button.hover);
+ EXPECT_TRUE(button_clicked);
+}
+TEST_F(InputTest, buttonPositionWorld) {
+ GameObject button_obj = mgr.new_object("body", "person", vec2{50, 50}, 0, 1);
+ bool button_clicked = false;
+ event_manager.subscribe<ButtonPressEvent>([&](const ButtonPressEvent & event) {
+ button_clicked = true;
+ EXPECT_EQ(event.metadata.game_object_id, button_obj.id);
+ return false;
+ });
+ auto & button = button_obj.add_component<Button>(vec2{10, 10}, vec2{0, 0});
+ button.world_space = true;
+ bool hover = false;
+ button.active = true;
+ this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT);
+ input_system.fixed_update();
+ event_manager.dispatch_events();
+ EXPECT_FALSE(button_clicked);
+ this->simulate_mouse_click(300, 300, SDL_BUTTON_LEFT);
+ input_system.fixed_update();
+ event_manager.dispatch_events();
+ EXPECT_FALSE(button_clicked);
+}
+TEST_F(InputTest, testButtonHover) {
+ GameObject button_obj = mgr.new_object("body", "person", vec2{0, 0}, 0, 1);
+ bool button_hover = false;
+ event_manager.subscribe<ButtonEnterEvent>([&](const ButtonEnterEvent & event) {
+ button_hover = true;
+ EXPECT_EQ(event.metadata.game_object_id, button_obj.id);
+ return false;
+ });
+ event_manager.subscribe<ButtonExitEvent>([&](const ButtonExitEvent & event) {
+ button_hover = false;
+ EXPECT_EQ(event.metadata.game_object_id, button_obj.id);
+ return false;
+ });
+ auto & button = button_obj.add_component<Button>(vec2{100, 100}, vec2{0, 0});
+ button.active = true;
// Mouse on button
SDL_Event hover_event;
SDL_zero(hover_event);
@@ -256,7 +299,22 @@ TEST_F(InputTest, testButtonHover) {
hover_event.motion.yrel = 10;
SDL_PushEvent(&hover_event);
- input_system.update();
+ input_system.fixed_update();
event_manager.dispatch_events();
EXPECT_TRUE(button.hover);
+ EXPECT_TRUE(button_hover);
+ // Mouse not on button
+ SDL_Event event;
+ SDL_zero(event);
+ event.type = SDL_MOUSEMOTION;
+ event.motion.x = 500;
+ event.motion.y = 500;
+ event.motion.xrel = 10;
+ event.motion.yrel = 10;
+ SDL_PushEvent(&event);
+
+ input_system.fixed_update();
+ event_manager.dispatch_events();
+ EXPECT_FALSE(button.hover);
+ EXPECT_FALSE(button_hover);
}