From df1eb9f47b8e1dc40a2c75d16bf560de22caad48 Mon Sep 17 00:00:00 2001 From: roby2014 Date: Tue, 10 Oct 2023 17:09:06 +0100 Subject: [PATCH] feat(tools): add toggleable debug camera plugin --- engine/CMakeLists.txt | 1 + .../engine/tools/debug_camera/plugin.hpp | 18 ++++++ .../engine/tools/debug_camera/plugin.cpp | 63 +++++++++++++++++++ tools/tesseratos/src/main.cpp | 2 + 4 files changed, 84 insertions(+) create mode 100644 engine/include/cubos/engine/tools/debug_camera/plugin.hpp create mode 100644 engine/src/cubos/engine/tools/debug_camera/plugin.cpp diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index ae90b8b17..605fdae4e 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -28,6 +28,7 @@ set(CUBOS_ENGINE_SOURCE "src/cubos/engine/tools/world_inspector/plugin.cpp" "src/cubos/engine/tools/entity_inspector/plugin.cpp" "src/cubos/engine/tools/scene_editor/plugin.cpp" + "src/cubos/engine/tools/debug_camera/plugin.cpp" "src/cubos/engine/transform/plugin.cpp" "src/cubos/engine/transform/local_to_world.cpp" diff --git a/engine/include/cubos/engine/tools/debug_camera/plugin.hpp b/engine/include/cubos/engine/tools/debug_camera/plugin.hpp new file mode 100644 index 000000000..96e551fd5 --- /dev/null +++ b/engine/include/cubos/engine/tools/debug_camera/plugin.hpp @@ -0,0 +1,18 @@ +/// @dir +/// @brief @ref debug-camera-plugin plugin directory. + +/// @file +/// @brief Plugin entry point. +/// @ingroup debug-camera-plugin + +#pragma once + +#include + +namespace cubos::engine::tools +{ + /// @brief Plugin entry function. + /// @param cubos @b CUBOS. main class + /// @ingroup debug-camera-plugin + void debugCameraPlugin(Cubos& cubos); +} // namespace cubos::engine::tools diff --git a/engine/src/cubos/engine/tools/debug_camera/plugin.cpp b/engine/src/cubos/engine/tools/debug_camera/plugin.cpp new file mode 100644 index 000000000..73a9c99d6 --- /dev/null +++ b/engine/src/cubos/engine/tools/debug_camera/plugin.cpp @@ -0,0 +1,63 @@ +#include + +#include +#include +#include +#include +#include +#include + +using cubos::core::ecs::Commands; +using cubos::core::ecs::Entity; +using cubos::core::ecs::Read; +using cubos::core::ecs::Write; + +using namespace cubos::engine; + +struct DebugCameraInfo +{ + Entity copy[4]; // copy old camera entities to re-apply them in the next camera change + Entity ent; // debug camera entity +}; + +static void createDebugCameraSystem(Commands commands, Write debugCamera) +{ + debugCamera->ent = commands.create().add(Camera{}).add(Position{{}}).entity(); +} + +static void changeToDebugCameraSystem(Write camera, Write debugCamera) +{ + ImGui::Begin("Debug Camera"); + + ImGui::Text("Current camera: %s", debugCamera->ent == camera->entities[0] ? "Debug" : "Game"); + ImGui::Text("Key to change: F12"); + + if (ImGui::Button("Change camera") || ImGui::IsKeyPressed(ImGuiKey_F12)) + { + CUBOS_DEBUG("Changed camera ..."); + if (debugCamera->ent == camera->entities[0]) + { + std::copy(std::begin(debugCamera->copy), std::end(debugCamera->copy), camera->entities); + } + else + { + std::copy(std::begin(camera->entities), std::end(camera->entities), debugCamera->copy); + camera->entities[0] = debugCamera->ent; + camera->entities[1] = Entity(); + camera->entities[2] = Entity(); + camera->entities[3] = Entity(); + } + } + + ImGui::End(); +} + +void cubos::engine::tools::debugCameraPlugin(Cubos& cubos) +{ + cubos.addPlugin(imguiPlugin); + + cubos.startupSystem(createDebugCameraSystem); + cubos.system(changeToDebugCameraSystem).tagged("cubos.imgui"); + + cubos.addResource(); +} diff --git a/tools/tesseratos/src/main.cpp b/tools/tesseratos/src/main.cpp index b1fd95c70..d25306f8f 100644 --- a/tools/tesseratos/src/main.cpp +++ b/tools/tesseratos/src/main.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -35,6 +36,7 @@ int main(int argc, char** argv) cubos.addPlugin(tools::entityInspectorPlugin); cubos.addPlugin(tools::worldInspectorPlugin); cubos.addPlugin(tools::assetExplorerPlugin); + cubos.addPlugin(tools::debugCameraPlugin); cubos.startupSystem(mockCamera).tagged("setup"); cubos.startupSystem(mockSettings).tagged("setup");