mirror of
https://github.com/MadeOfJelly/MushMachine.git
synced 2025-06-19 19:26:36 +02:00
initial import, >900commits predate this
This commit is contained in:
33
framework/input/test/CMakeLists.txt
Normal file
33
framework/input/test/CMakeLists.txt
Normal file
@ -0,0 +1,33 @@
|
||||
add_executable(input_service_test
|
||||
start_test.cpp
|
||||
)
|
||||
|
||||
target_include_directories(input_service_test PRIVATE ".")
|
||||
|
||||
target_link_libraries(input_service_test
|
||||
input_service
|
||||
sdl_service
|
||||
gtest_main
|
||||
)
|
||||
|
||||
add_test(NAME input_service_test COMMAND input_service_test)
|
||||
|
||||
######################
|
||||
|
||||
add_executable(input_service_visualizer_test
|
||||
input_visualizer.cpp
|
||||
)
|
||||
|
||||
target_include_directories(input_service_visualizer_test PRIVATE ".")
|
||||
|
||||
target_link_libraries(input_service_visualizer_test
|
||||
input_service
|
||||
sdl_service
|
||||
imgui_service
|
||||
imgui_render_task
|
||||
imgui_tools
|
||||
gtest_main
|
||||
)
|
||||
|
||||
add_test(NAME input_service_visualizer_test COMMAND input_service_visualizer_test)
|
||||
|
162
framework/input/test/input_visualizer.cpp
Normal file
162
framework/input/test/input_visualizer.cpp
Normal file
@ -0,0 +1,162 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <mm/engine.hpp>
|
||||
|
||||
#include <mm/services/input_service.hpp>
|
||||
#include <mm/services/sdl_service.hpp>
|
||||
#include <mm/services/filesystem.hpp>
|
||||
#include <mm/services/opengl_renderer.hpp>
|
||||
#include <mm/services/imgui_s.hpp>
|
||||
|
||||
#include <mm/opengl/render_tasks/imgui.hpp>
|
||||
|
||||
#include <mm/imgui/fps_overlay.hpp>
|
||||
|
||||
#include <imgui/imgui.h>
|
||||
|
||||
|
||||
class InputVisualizer : public MM::Services::Service {
|
||||
private:
|
||||
MM::Input::PlayerID _player_id;
|
||||
MM::Engine::FunctionDataHandle _render_handle;
|
||||
MM::Services::SDLService::EventHandlerHandle _event_handle = nullptr;
|
||||
|
||||
public:
|
||||
bool enable(MM::Engine& engine) override {
|
||||
_player_id = UINT16_MAX;
|
||||
|
||||
auto* sdl_ss = engine.tryService<MM::Services::SDLService>();
|
||||
if (sdl_ss) {
|
||||
_event_handle = sdl_ss->addEventHandler([this, &engine](const SDL_Event& e) -> bool {
|
||||
return this->handleEvent(e, engine);
|
||||
});
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
for (int i = 0; i < SDL_NumJoysticks(); i++) {
|
||||
if (SDL_IsGameController(i)) {
|
||||
std::cerr << "got controller " << i << "\n";
|
||||
SDL_GameControllerOpen(i);
|
||||
}
|
||||
}
|
||||
|
||||
_render_handle = engine.addUpdate([this](MM::Engine& e){ this->renderImGui(e); });
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void disable(MM::Engine& engine) override {
|
||||
if (!_render_handle.expired()) {
|
||||
engine.removeUpdate(_render_handle);
|
||||
_render_handle.reset();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private:
|
||||
void renderImGui(MM::Engine& engine) {
|
||||
ImGui::Begin("InputVisualizer", NULL, ImGuiWindowFlags_AlwaysAutoResize);
|
||||
if (_player_id == UINT16_MAX) {
|
||||
ImGui::Text("press [SPACE](keyboard) or [A](controller)");
|
||||
} else {
|
||||
auto* input = engine.tryService<MM::Services::InputService>();
|
||||
ImGui::Text("InputAction active:");
|
||||
|
||||
ImGui::Value("SPELL_WEAPON", input->get(_player_id, MM::Services::InputService::SPELL_WEAPON));
|
||||
ImGui::Value("SPELL_1", input->get(_player_id, MM::Services::InputService::SPELL_1));
|
||||
ImGui::Value("SPELL_2", input->get(_player_id, MM::Services::InputService::SPELL_2));
|
||||
ImGui::Value("SPELL_3", input->get(_player_id, MM::Services::InputService::SPELL_3));
|
||||
ImGui::Value("SPELL_4", input->get(_player_id, MM::Services::InputService::SPELL_4));
|
||||
ImGui::Value("SPELL_5", input->get(_player_id, MM::Services::InputService::SPELL_5));
|
||||
ImGui::Value("SPELL_6", input->get(_player_id, MM::Services::InputService::SPELL_6));
|
||||
ImGui::Value("USE", input->get(_player_id, MM::Services::InputService::USE));
|
||||
|
||||
ImGui::Separator();
|
||||
|
||||
{
|
||||
ImDrawList* draw_list = ImGui::GetWindowDrawList();
|
||||
|
||||
static ImVec4 col = ImVec4(1.0f, 1.0f, 0.4f, 1.0f);
|
||||
const ImU32 col32 = ImColor(col);
|
||||
|
||||
const ImVec2 p = ImGui::GetCursorScreenPos();
|
||||
|
||||
float scaling = 50.f;
|
||||
float x = p.x + scaling;
|
||||
float y = p.y + scaling;
|
||||
float spacing = 8.0f;
|
||||
|
||||
draw_list->AddCircleFilled(ImVec2(x, y), scaling, ImColor(0.2f, 0.2f, 0.2f, 0.7f), 24);
|
||||
auto move_vec = input->getMoveVec(_player_id);
|
||||
draw_list->AddLine(ImVec2(x, y), ImVec2(x + move_vec.x * scaling, y + move_vec.y * scaling), col32, 3.f);
|
||||
|
||||
x += scaling*2 + spacing;
|
||||
|
||||
draw_list->AddCircleFilled(ImVec2(x, y), scaling, ImColor(0.2f, 0.2f, 0.2f, 0.7f), 24);
|
||||
auto view_vec = input->getViewVec(_player_id);
|
||||
draw_list->AddLine(ImVec2(x, y), ImVec2(x + view_vec.x * scaling, y + view_vec.y * scaling), col32, 3.f);
|
||||
|
||||
ImGui::Dummy(ImVec2(scaling*4 + spacing, scaling*2));
|
||||
}
|
||||
}
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
private:
|
||||
bool handleEvent(const SDL_Event& e, MM::Engine& engine) {
|
||||
auto* input = engine.tryService<MM::Services::InputService>();
|
||||
if (_player_id == UINT16_MAX) {
|
||||
if (e.type == SDL_KEYDOWN && e.key.keysym.sym == SDLK_SPACE) {
|
||||
_player_id = input->addPlayer(true, 0);
|
||||
return true;
|
||||
} else if (e.type == SDL_CONTROLLERDEVICEADDED) {
|
||||
SDL_GameControllerOpen(e.cdevice.which);
|
||||
std::cout << "added: " << e.cdevice.which << std::endl;
|
||||
return true;
|
||||
} else if (e.type == SDL_CONTROLLERBUTTONDOWN && e.cbutton.button == SDL_CONTROLLER_BUTTON_A) {
|
||||
_player_id = input->addPlayer(false, e.cbutton.which);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(input_service, input_visualizer) {
|
||||
MM::Engine engine;
|
||||
|
||||
auto& sdl_ss = engine.addService<MM::Services::SDLService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::SDLService>());
|
||||
|
||||
sdl_ss.createGLWindow("service_input_visualizer_test", 1280, 720);
|
||||
|
||||
engine.addService<InputVisualizer>();
|
||||
ASSERT_TRUE(engine.enableService<InputVisualizer>());
|
||||
|
||||
engine.addService<MM::Services::InputService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::InputService>());
|
||||
|
||||
engine.addService<MM::Services::ImGuiService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::ImGuiService>());
|
||||
|
||||
auto& rs = engine.addService<MM::Services::OpenGLRenderer>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::OpenGLRenderer>());
|
||||
|
||||
rs.addRenderTask<MM::OpenGL::RenderTasks::ImGuiRT>(engine);
|
||||
|
||||
{
|
||||
MM::ImGuiSimpleFPSOverlay fps_overlay;
|
||||
|
||||
engine.addUpdate([&](MM::Engine&) {
|
||||
fps_overlay.renderImGui();
|
||||
}
|
||||
);
|
||||
|
||||
engine.run();
|
||||
}
|
||||
|
||||
sdl_ss.destroyWindow();
|
||||
}
|
||||
|
221
framework/input/test/start_test.cpp
Normal file
221
framework/input/test/start_test.cpp
Normal file
@ -0,0 +1,221 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <mm/engine.hpp>
|
||||
|
||||
#include <mm/services/input_service.hpp>
|
||||
#include <mm/services/sdl_service.hpp>
|
||||
|
||||
TEST(input_service, add_en_dis) {
|
||||
MM::Engine engine;
|
||||
|
||||
// sdl dep
|
||||
{
|
||||
engine.addService<MM::Services::SDLService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::SDLService>());
|
||||
|
||||
auto* sdl_ss_ptr = engine.tryService<MM::Services::SDLService>();
|
||||
ASSERT_NE(sdl_ss_ptr, nullptr);
|
||||
}
|
||||
|
||||
engine.addService<MM::Services::InputService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::InputService>());
|
||||
|
||||
auto* input_service_ptr = engine.tryService<MM::Services::InputService>();
|
||||
ASSERT_NE(input_service_ptr, nullptr);
|
||||
|
||||
////
|
||||
|
||||
engine.disableService<MM::Services::InputService>();
|
||||
|
||||
engine.disableService<MM::Services::SDLService>();
|
||||
}
|
||||
|
||||
TEST(input_service, initial_error) {
|
||||
MM::Engine engine;
|
||||
|
||||
// sdl dep
|
||||
{
|
||||
engine.addService<MM::Services::SDLService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::SDLService>());
|
||||
|
||||
auto* sdl_ss_ptr = engine.tryService<MM::Services::SDLService>();
|
||||
ASSERT_NE(sdl_ss_ptr, nullptr);
|
||||
}
|
||||
|
||||
engine.addService<MM::Services::InputService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::InputService>());
|
||||
|
||||
auto* input_service_ptr = engine.tryService<MM::Services::InputService>();
|
||||
ASSERT_NE(input_service_ptr, nullptr);
|
||||
|
||||
auto& input = *input_service_ptr;
|
||||
|
||||
// actions (spells)
|
||||
{
|
||||
for (unsigned int action = 0; action < MM::Services::InputService::INPUT_ACTION_MAX; action++) {
|
||||
ASSERT_FALSE(input.get(0, (MM::Services::InputService::input_action_t)action));
|
||||
}
|
||||
|
||||
ASSERT_FALSE(input.get(2, MM::Services::InputService::SPELL_1));
|
||||
}
|
||||
|
||||
// move and view
|
||||
{
|
||||
{
|
||||
auto m = input.getMoveVec(0);
|
||||
ASSERT_EQ(m.x, 0.f);
|
||||
ASSERT_EQ(m.y, 0.f);
|
||||
}
|
||||
|
||||
{
|
||||
auto v = input.getViewVec(0);
|
||||
ASSERT_EQ(v.x, 0.f);
|
||||
ASSERT_EQ(v.y, 0.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(input_service, adding_players) {
|
||||
MM::Engine engine;
|
||||
|
||||
// sdl dep
|
||||
{
|
||||
engine.addService<MM::Services::SDLService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::SDLService>());
|
||||
|
||||
auto* sdl_ss_ptr = engine.tryService<MM::Services::SDLService>();
|
||||
ASSERT_NE(sdl_ss_ptr, nullptr);
|
||||
}
|
||||
|
||||
engine.addService<MM::Services::InputService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::InputService>());
|
||||
|
||||
auto* input_service_ptr = engine.tryService<MM::Services::InputService>();
|
||||
ASSERT_NE(input_service_ptr, nullptr);
|
||||
|
||||
auto& input = *input_service_ptr;
|
||||
|
||||
MM::Input::PlayerID p1_id = input.addPlayer(true, 0);
|
||||
ASSERT_NE(p1_id, UINT16_MAX);
|
||||
|
||||
{
|
||||
// TODO: check
|
||||
//ASSERT_EQ(input.addPlayer(true, 0), UINT16_MAX);
|
||||
}
|
||||
|
||||
// actions (spells)
|
||||
{
|
||||
for (unsigned int action = 0; action < MM::Services::InputService::INPUT_ACTION_MAX; action++) {
|
||||
ASSERT_FALSE(input.get(p1_id, (MM::Services::InputService::input_action_t)action));
|
||||
}
|
||||
}
|
||||
|
||||
// move and view
|
||||
{
|
||||
{
|
||||
auto m = input.getMoveVec(p1_id);
|
||||
ASSERT_EQ(m.x, 0.f);
|
||||
ASSERT_EQ(m.y, 0.f);
|
||||
}
|
||||
|
||||
{
|
||||
// never (0, 0)
|
||||
auto v = input.getViewVec(p1_id);
|
||||
ASSERT_EQ(v.x, 1.f);
|
||||
ASSERT_EQ(v.y, 0.f);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(input_service, basic_handling_keyboard) {
|
||||
MM::Engine engine;
|
||||
|
||||
// sdl dep
|
||||
{
|
||||
engine.addService<MM::Services::SDLService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::SDLService>());
|
||||
|
||||
auto* sdl_ss_ptr = engine.tryService<MM::Services::SDLService>();
|
||||
ASSERT_NE(sdl_ss_ptr, nullptr);
|
||||
}
|
||||
|
||||
engine.addService<MM::Services::InputService>();
|
||||
ASSERT_TRUE(engine.enableService<MM::Services::InputService>());
|
||||
|
||||
auto* input_service_ptr = engine.tryService<MM::Services::InputService>();
|
||||
ASSERT_NE(input_service_ptr, nullptr);
|
||||
|
||||
auto& input = *input_service_ptr;
|
||||
|
||||
MM::Input::PlayerID p_id = input.addPlayer(true, 0);
|
||||
ASSERT_NE(p_id, UINT16_MAX);
|
||||
|
||||
// actions (spells)
|
||||
{
|
||||
for (unsigned int action = 0; action < MM::Services::InputService::INPUT_ACTION_MAX; action++) {
|
||||
ASSERT_FALSE(input.get(p_id, (MM::Services::InputService::input_action_t)action));
|
||||
}
|
||||
|
||||
// forging sdl event spell 1
|
||||
{
|
||||
SDL_Event tmp_e;
|
||||
tmp_e.key.keysym.sym = SDLK_1;
|
||||
tmp_e.type = SDL_KEYDOWN;
|
||||
|
||||
ASSERT_TRUE(input.handleSDL_Event(tmp_e, engine));
|
||||
}
|
||||
|
||||
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_WEAPON));
|
||||
ASSERT_TRUE(input.get(p_id, MM::Services::InputService::SPELL_1));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_2));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_3));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_4));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_5));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_6));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::USE));
|
||||
|
||||
// forging sdl event spell weapon
|
||||
{
|
||||
SDL_Event tmp_e;
|
||||
tmp_e.key.keysym.sym = SDLK_SPACE;
|
||||
tmp_e.type = SDL_KEYDOWN;
|
||||
|
||||
ASSERT_TRUE(input.handleSDL_Event(tmp_e, engine));
|
||||
}
|
||||
|
||||
ASSERT_TRUE(input.get(p_id, MM::Services::InputService::SPELL_WEAPON));
|
||||
ASSERT_TRUE(input.get(p_id, MM::Services::InputService::SPELL_1));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_2));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_3));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_4));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_5));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_6));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::USE));
|
||||
|
||||
// forging sdl event spell 1 end
|
||||
{
|
||||
SDL_Event tmp_e;
|
||||
tmp_e.key.keysym.sym = SDLK_1;
|
||||
tmp_e.type = SDL_KEYUP;
|
||||
|
||||
ASSERT_TRUE(input.handleSDL_Event(tmp_e, engine));
|
||||
}
|
||||
|
||||
|
||||
ASSERT_TRUE(input.get(p_id, MM::Services::InputService::SPELL_WEAPON));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_1));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_2));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_3));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_4));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_5));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::SPELL_6));
|
||||
ASSERT_FALSE(input.get(p_id, MM::Services::InputService::USE));
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
}
|
||||
|
Reference in New Issue
Block a user