diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/InputTest.cpp | 64 |
1 files changed, 55 insertions, 9 deletions
diff --git a/src/test/InputTest.cpp b/src/test/InputTest.cpp index f5ebaf0..86afad9 100644 --- a/src/test/InputTest.cpp +++ b/src/test/InputTest.cpp @@ -36,10 +36,12 @@ public: RenderSystem render{mediator}; EventManager event_manager{mediator}; //GameObject camera; + int offset_x = 100; + int offset_y = 200; protected: void SetUp() override { - GameObject obj = mgr.new_object("camera", "camera", vec2{0, 0}, 0, 1); + GameObject obj = mgr.new_object("camera", "camera", vec2{offset_x, offset_y}, 0, 1); auto & camera = obj.add_component<Camera>(ivec2{500, 500}, vec2{500, 500}, Camera::Data{.bg_color = Color::WHITE, .zoom = 1.0f}); @@ -75,8 +77,8 @@ 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.x, offset_x); + EXPECT_EQ(event.mouse_pos.y, offset_y); EXPECT_EQ(event.button, MouseButton::LEFT_MOUSE); return false; }; @@ -100,8 +102,8 @@ 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.x, offset_x); + EXPECT_EQ(e.mouse_pos.y, offset_y); EXPECT_EQ(e.button, MouseButton::LEFT_MOUSE); return false; }; @@ -124,8 +126,8 @@ 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.x, offset_x); + EXPECT_EQ(e.mouse_pos.y, offset_y); EXPECT_EQ(e.mouse_delta.x, 10); EXPECT_EQ(e.mouse_delta.y, 10); return false; @@ -198,8 +200,8 @@ 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.x, offset_x); + EXPECT_EQ(event.mouse_pos.y, offset_y); return false; }; event_manager.subscribe<MouseClickEvent>(on_mouse_click); @@ -232,7 +234,51 @@ TEST_F(InputTest, testButtonClick) { event_manager.dispatch_events(); EXPECT_TRUE(button_clicked); } +TEST_F(InputTest, buttonPositionCamera) { + 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 = false; + bool hover = false; + button.active = true; + this->simulate_mouse_click(999, 999, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); + + this->simulate_mouse_click(300, 300, SDL_BUTTON_LEFT); + input_system.update(); + event_manager.dispatch_events(); + 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.update(); + event_manager.dispatch_events(); + EXPECT_FALSE(button_clicked); + this->simulate_mouse_click(300, 300, SDL_BUTTON_LEFT); + input_system.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; |