From 4a79a6b804ec2f4077c4e51eae11ed4e6b8aaf2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Fonseca?= Date: Tue, 26 Sep 2023 12:22:37 +0100 Subject: [PATCH] chore: move serialization utils to tool plugins --- core/CMakeLists.txt | 1 - engine/CMakeLists.txt | 1 + .../engine/tools/utils}/serialization.hpp | 27 ++-- .../engine/tools/entity_inspector/plugin.cpp | 6 +- .../tools/settings_inspector/plugin.cpp | 5 +- .../engine/tools/utils}/serialization.cpp | 133 +++++++++--------- 6 files changed, 86 insertions(+), 87 deletions(-) rename {core/include/cubos/core/ui => engine/include/cubos/engine/tools/utils}/serialization.hpp (80%) rename {core/src/cubos/core/ui => engine/src/cubos/engine/tools/utils}/serialization.cpp (79%) diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 498574dfb..d200ab7d0 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -72,7 +72,6 @@ set(CUBOS_CORE_SOURCE "src/cubos/core/al/oal_audio_device.hpp" "src/cubos/core/ui/imgui.cpp" - "src/cubos/core/ui/serialization.cpp" "src/cubos/core/ecs/entity_manager.cpp" "src/cubos/core/ecs/component_manager.cpp" diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 0f69b3702..e8df796a3 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -20,6 +20,7 @@ set(CUBOS_ENGINE_SOURCE "src/cubos/engine/imgui/plugin.cpp" + "src/cubos/engine/tools/utils/serialization.cpp" "src/cubos/engine/tools/asset_explorer/plugin.cpp" "src/cubos/engine/tools/settings_inspector/plugin.cpp" "src/cubos/engine/tools/entity_selector/plugin.cpp" diff --git a/core/include/cubos/core/ui/serialization.hpp b/engine/include/cubos/engine/tools/utils/serialization.hpp similarity index 80% rename from core/include/cubos/core/ui/serialization.hpp rename to engine/include/cubos/engine/tools/utils/serialization.hpp index da99eb707..349ff841b 100644 --- a/core/include/cubos/core/ui/serialization.hpp +++ b/engine/include/cubos/engine/tools/utils/serialization.hpp @@ -1,12 +1,17 @@ +/// @dir +/// @brief @ref utils-tool-plugin plugin directory. + /// @file /// @brief Functions for showing and editing serializable objects in the UI. -/// @ingroup core-ui +/// @ingroup utils-tool-plugin #pragma once #include -namespace cubos::core::ui +using Package = cubos::core::data::Package; + +namespace cubos::engine::tools { /// @brief Shows a packaged object's properties in the UI. Should be called inside a /// `ImGui::BeginTable(2)` and `ImGui::EndTable()` block. @@ -16,8 +21,8 @@ namespace cubos::core::ui /// /// @param pkg Packaged object to show. /// @param name Name of the object. - /// @ingroup core-ui - void showPackage(const data::Package& pkg, const std::string& name); + /// @ingroup utils-tool-plugin + void showPackage(const Package& pkg, const std::string& name); /// @brief Shows a packaged object's properties in the UI, allowing the user to edit the /// object. Should be called inside a `ImGui::BeginTable(3)` and `ImGui::EndTable()` block. @@ -29,8 +34,8 @@ namespace cubos::core::ui /// @param pkg Packaged object to edit. /// @param name Name of the object. /// @return True if the object was modified, false otherwise. - /// @ingroup core-ui - bool editPackage(data::Package& pkg, const std::string& name); + /// @ingroup utils-tool-plugin + bool editPackage(Package& pkg, const std::string& name); /// @brief Shows a serializable object's properties in the UI. Should be called inside a /// `ImGui::BeginTable(2)` and `ImGui::EndTable()` block. @@ -40,11 +45,11 @@ namespace cubos::core::ui /// @tparam T Type of the serializable object. /// @param object Object to show. /// @param name Name of the object. - /// @ingroup core-ui + /// @ingroup utils-tool-plugin template inline void show(const T& object, const std::string& name) { - auto pkg = data::Package::from(object); + auto pkg = Package::from(object); showPackage(pkg, name); } @@ -58,11 +63,11 @@ namespace cubos::core::ui /// @param object Object to edit. /// @param name Name of the object. /// @return True if the object was edited, false otherwise. - /// @ingroup core-ui + /// @ingroup utils-tool-plugin template inline bool edit(T& object, const std::string& name) { - auto pkg = data::Package::from(object); + auto pkg = Package::from(object); if (editPackage(pkg, name)) { pkg.into(object); @@ -71,4 +76,4 @@ namespace cubos::core::ui return false; } -} // namespace cubos::core::ui +} // namespace cubos::engine::tools diff --git a/engine/src/cubos/engine/tools/entity_inspector/plugin.cpp b/engine/src/cubos/engine/tools/entity_inspector/plugin.cpp index 5e94c77f9..26ce06c0e 100644 --- a/engine/src/cubos/engine/tools/entity_inspector/plugin.cpp +++ b/engine/src/cubos/engine/tools/entity_inspector/plugin.cpp @@ -1,17 +1,15 @@ #include -#include - #include #include #include +#include using cubos::core::data::Context; using cubos::core::data::SerializationMap; using cubos::core::ecs::Entity; using cubos::core::ecs::World; using cubos::core::ecs::Write; -using cubos::core::ui::editPackage; using namespace cubos::engine; static void inspectEntity(Write world) @@ -26,7 +24,7 @@ static void inspectEntity(Write world) if (!selection.isNull() && world->isAlive(selection)) { auto pkg = world->pack(selection); - if (editPackage(pkg, std::to_string(selection.index))) + if (tools::editPackage(pkg, std::to_string(selection.index))) { world->unpack(selection, pkg); } diff --git a/engine/src/cubos/engine/tools/settings_inspector/plugin.cpp b/engine/src/cubos/engine/tools/settings_inspector/plugin.cpp index e46d85edd..885f0aae6 100644 --- a/engine/src/cubos/engine/tools/settings_inspector/plugin.cpp +++ b/engine/src/cubos/engine/tools/settings_inspector/plugin.cpp @@ -1,10 +1,9 @@ #include -#include - #include #include #include +#include using cubos::core::ecs::Write; @@ -25,7 +24,7 @@ static void inspector(Write settings) ImGui::BeginTable("split", 2, ImGuiTableFlags_BordersOuter | ImGuiTableFlags_Resizable); for (auto& setting : map) { - cubos::core::ui::edit(setting.second, setting.first); + tools::edit(setting.second, setting.first); } ImGui::EndTable(); } diff --git a/core/src/cubos/core/ui/serialization.cpp b/engine/src/cubos/engine/tools/utils/serialization.cpp similarity index 79% rename from core/src/cubos/core/ui/serialization.cpp rename to engine/src/cubos/engine/tools/utils/serialization.cpp index 407f658c3..fb113be28 100644 --- a/core/src/cubos/core/ui/serialization.cpp +++ b/engine/src/cubos/engine/tools/utils/serialization.cpp @@ -2,44 +2,41 @@ #include -#include +#include -using namespace cubos::core; -using namespace cubos::core::ui; - -using Type = data::Package::Type; +using namespace cubos::engine; // Converts a scalar (must not be an Object, Array or Dictionary) value to a // string. -static std::string valueToString(const data::Package& pkg) +static std::string valueToString(const Package& pkg) { switch (pkg.type()) { - case Type::None: + case Package::Type::None: return "None"; - case Type::I8: + case Package::Type::I8: return std::to_string(pkg.get()); - case Type::I16: + case Package::Type::I16: return std::to_string(pkg.get()); - case Type::I32: + case Package::Type::I32: return std::to_string(pkg.get()); - case Type::I64: + case Package::Type::I64: return std::to_string(pkg.get()); - case Type::U8: + case Package::Type::U8: return std::to_string(pkg.get()); - case Type::U16: + case Package::Type::U16: return std::to_string(pkg.get()); - case Type::U32: + case Package::Type::U32: return std::to_string(pkg.get()); - case Type::U64: + case Package::Type::U64: return std::to_string(pkg.get()); - case Type::F32: + case Package::Type::F32: return std::to_string(pkg.get()); - case Type::F64: + case Package::Type::F64: return std::to_string(pkg.get()); - case Type::Bool: + case Package::Type::Bool: return pkg.get() ? "true" : "false"; - case Type::String: + case Package::Type::String: return "\"" + pkg.get() + "\""; default: CUBOS_CRITICAL("Expected scalar value, got object/array/dictionary"); @@ -47,9 +44,9 @@ static std::string valueToString(const data::Package& pkg) } } -static void showInternal(const data::Package& pkg, const std::string& name); +static void showInternal(const Package& pkg, const std::string& name); -static void showScalar(const data::Package& pkg, const std::string& name) +static void showScalar(const Package& pkg, const std::string& name) { ImGui::TableSetColumnIndex(0); ImGui::AlignTextToFramePadding(); @@ -59,43 +56,43 @@ static void showScalar(const data::Package& pkg, const std::string& name) switch (pkg.type()) { - case Type::None: + case Package::Type::None: ImGui::Text("None"); break; - case Type::I8: + case Package::Type::I8: ImGui::Text("%d", pkg.get()); break; - case Type::I16: + case Package::Type::I16: ImGui::Text("%d", pkg.get()); break; - case Type::I32: + case Package::Type::I32: ImGui::Text("%d", pkg.get()); break; - case Type::I64: + case Package::Type::I64: ImGui::Text("%lld", static_cast(pkg.get())); break; - case Type::U8: + case Package::Type::U8: ImGui::Text("%u", pkg.get()); break; - case Type::U16: + case Package::Type::U16: ImGui::Text("%u", pkg.get()); break; - case Type::U32: + case Package::Type::U32: ImGui::Text("%u", pkg.get()); break; - case Type::U64: + case Package::Type::U64: ImGui::Text("%llu", static_cast(pkg.get())); break; - case Type::F32: + case Package::Type::F32: ImGui::Text("%f", pkg.get()); break; - case Type::F64: + case Package::Type::F64: ImGui::Text("%f", pkg.get()); break; - case Type::Bool: + case Package::Type::Bool: ImGui::Text("%s", pkg.get() ? "true" : "false"); break; - case Type::String: + case Package::Type::String: ImGui::Text("\"%s\"", pkg.get().c_str()); break; default: @@ -103,18 +100,18 @@ static void showScalar(const data::Package& pkg, const std::string& name) } } -static void showStructured(const data::Package& pkg, const std::string& name) +static void showStructured(const Package& pkg, const std::string& name) { const char* elements; switch (pkg.type()) { - case Type::Object: + case Package::Type::Object: elements = "fields"; break; - case Type::Array: + case Package::Type::Array: elements = "elements"; break; - case Type::Dictionary: + case Package::Type::Dictionary: elements = "entries"; break; default: @@ -134,13 +131,13 @@ static void showStructured(const data::Package& pkg, const std::string& name) ImGui::PushID(static_cast(i)); switch (pkg.type()) { - case Type::Object: + case Package::Type::Object: showInternal(pkg.fields()[i].second, pkg.fields()[i].first); break; - case Type::Array: + case Package::Type::Array: showInternal(pkg.elements()[i], std::to_string(i)); break; - case Type::Dictionary: + case Package::Type::Dictionary: showInternal(pkg.dictionary()[i].second, valueToString(pkg.dictionary()[i].first)); break; default: @@ -153,27 +150,27 @@ static void showStructured(const data::Package& pkg, const std::string& name) } } -static void showInternal(const data::Package& pkg, const std::string& name) +static void showInternal(const Package& pkg, const std::string& name) { pkg.isStructured() ? showStructured(pkg, name) : showScalar(pkg, name); } -void cubos::core::ui::showPackage(const data::Package& pkg, const std::string& name) +void cubos::engine::tools::showPackage(const Package& pkg, const std::string& name) { ImGui::TableNextRow(); showInternal(pkg, name); } -static bool editInternal(data::Package& pkg, const std::string& name); +static bool editInternal(Package& pkg, const std::string& name); -static bool pickScalar(data::Package& pkg, const std::string& name) +static bool pickScalar(Package& pkg, const std::string& name) { switch (pkg.type()) { - case Type::None: + case Package::Type::None: ImGui::Text("None"); return true; - case Type::I8: { + case Package::Type::I8: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_S8, &value)) { @@ -182,7 +179,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::I16: { + case Package::Type::I16: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_S16, &value)) { @@ -191,7 +188,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::I32: { + case Package::Type::I32: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_S32, &value)) { @@ -200,7 +197,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::I64: { + case Package::Type::I64: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_S64, &value)) { @@ -209,7 +206,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::U8: { + case Package::Type::U8: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_U8, &value)) { @@ -218,7 +215,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::U16: { + case Package::Type::U16: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_U16, &value)) { @@ -227,7 +224,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::U32: { + case Package::Type::U32: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_U32, &value)) { @@ -236,7 +233,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::U64: { + case Package::Type::U64: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_U64, &value)) { @@ -245,7 +242,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::F32: { + case Package::Type::F32: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_Float, &value)) { @@ -254,7 +251,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::F64: { + case Package::Type::F64: { auto value = pkg.get(); if (ImGui::InputScalar(name.c_str(), ImGuiDataType_Double, &value)) { @@ -263,7 +260,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::Bool: { + case Package::Type::Bool: { auto value = pkg.get(); if (ImGui::Checkbox(name.c_str(), &value)) { @@ -272,7 +269,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) } break; } - case Type::String: { + case Package::Type::String: { char buf[1024]; memset(buf, 0, sizeof(buf)); strncpy(buf, pkg.get().c_str(), sizeof(buf) - 1); @@ -290,7 +287,7 @@ static bool pickScalar(data::Package& pkg, const std::string& name) return false; } -static bool editScalar(data::Package& pkg, const std::string& name) +static bool editScalar(Package& pkg, const std::string& name) { ImGui::TableSetColumnIndex(0); ImGui::AlignTextToFramePadding(); @@ -303,20 +300,20 @@ static bool editScalar(data::Package& pkg, const std::string& name) return ret; } -static bool editStructured(data::Package& pkg, const std::string& name) +static bool editStructured(Package& pkg, const std::string& name) { bool changed = false; const char* elements; switch (pkg.type()) { - case Type::Object: + case Package::Type::Object: elements = "fields"; break; - case Type::Array: + case Package::Type::Array: elements = "elements"; break; - case Type::Dictionary: + case Package::Type::Dictionary: elements = "entries"; break; default: @@ -341,10 +338,10 @@ static bool editStructured(data::Package& pkg, const std::string& name) switch (pkg.type()) { - case Type::Object: + case Package::Type::Object: changed |= editInternal(pkg.fields()[i].second, pkg.fields()[i].first); break; - case Type::Array: + case Package::Type::Array: ImGui::TableSetColumnIndex(2); shouldErase = ImGui::Button("-"); ImGui::SameLine(); @@ -362,7 +359,7 @@ static bool editStructured(data::Package& pkg, const std::string& name) changed = true; } break; - case Type::Dictionary: + case Package::Type::Dictionary: ImGui::TableSetColumnIndex(2); shouldErase = ImGui::Button("-"); ImGui::SameLine(); @@ -408,12 +405,12 @@ static bool editStructured(data::Package& pkg, const std::string& name) return changed; } -static bool editInternal(data::Package& pkg, const std::string& name) +static bool editInternal(Package& pkg, const std::string& name) { return pkg.isStructured() ? editStructured(pkg, name) : editScalar(pkg, name); } -bool cubos::core::ui::editPackage(data::Package& pkg, const std::string& name) +bool cubos::engine::tools::editPackage(Package& pkg, const std::string& name) { ImGui::TableNextRow(); return editInternal(pkg, name);