From 8510a85f79f706b93982b4e398b187b5f77081dd Mon Sep 17 00:00:00 2001 From: rtk0c Date: Fri, 3 Jun 2022 22:58:28 -0700 Subject: Changeset: 61 [BUGGED] Move object kind enums to use generated ToString and FromStrong The old mechanism rely on a specific prefix to Ires and GameObject string representations, but the generator currently leaves the enum value. Therefore all editor (e.g. drag & drop) and IO (e.g. ires loading) mechanisms are broken. --- source/30-game/Ires.cpp | 48 ++++++++++++++++-------------------------------- 1 file changed, 16 insertions(+), 32 deletions(-) (limited to 'source/30-game/Ires.cpp') diff --git a/source/30-game/Ires.cpp b/source/30-game/Ires.cpp index 10a6867..bfa4cdf 100644 --- a/source/30-game/Ires.cpp +++ b/source/30-game/Ires.cpp @@ -3,14 +3,16 @@ #include "AppConfig.hpp" #include "EditorCore.hpp" #include "EditorUtils.hpp" -#include "Macros.hpp" #include "Material.hpp" -#include "RapidJsonHelper.hpp" -#include "ScopeGuard.hpp" #include "Shader.hpp" #include "Sprite.hpp" #include "Texture.hpp" -#include "Utils.hpp" + +#include +#include +#include +#include +#include #include #include @@ -29,27 +31,6 @@ IresObject::IresObject(Kind kind) : mKind{ kind } { } -std::string_view IresObject::ToString(Kind kind) { - switch (kind) { - case KD_Texture: return BRUSSEL_TAG_PREFIX_Ires "Texture"sv; - case KD_Shader: return BRUSSEL_TAG_PREFIX_Ires "Shader"sv; - case KD_Material: return BRUSSEL_TAG_PREFIX_Ires "Material"sv; - case KD_SpriteFiles: return BRUSSEL_TAG_PREFIX_Ires "SpriteFiles"sv; - case KD_Spritesheet: return BRUSSEL_TAG_PREFIX_Ires "Spritesheet"sv; - case KD_COUNT: break; - } - return std::string_view(); -} - -IresObject::Kind IresObject::FromString(std::string_view name) { - if (name == BRUSSEL_TAG_PREFIX_Ires "Texture"sv) return KD_Texture; - if (name == BRUSSEL_TAG_PREFIX_Ires "Shader"sv) return KD_Shader; - if (name == BRUSSEL_TAG_PREFIX_Ires "Material"sv) return KD_Material; - if (name == BRUSSEL_TAG_PREFIX_Ires "SpriteFiles"sv) return KD_SpriteFiles; - if (name == BRUSSEL_TAG_PREFIX_Ires "Spritesheet"sv) return KD_Spritesheet; - return KD_COUNT; -} - std::unique_ptr IresObject::Create(Kind kind) { switch (kind) { case KD_Texture: return std::make_unique(); @@ -128,7 +109,7 @@ void IresObject::ShowReference(IEditor& editor) { } void IresObject::ShowEditor(IEditor& editor) { - ImGui::Text("%s", ToString(mKind).data()); + ImGui::Text("%.*s", PRINTF_STRING_VIEW(Metadata::EnumToString(mKind))); bool isAnnoymous = mName.empty(); if (isAnnoymous) { @@ -148,7 +129,7 @@ void IresObject::WriteFull(IresWritingContext& ctx, IresObject* ires, rapidjson: rapidjson::Value rvIres(rapidjson::kObjectType); ires->Write(ctx, rvIres, root); - value.AddMember("Type", rapidjson::StringRef(ToString(ires->GetKind())), root.GetAllocator()); + value.AddMember("Type", rapidjson::StringRef(Metadata::EnumToString(ires->GetKind())), root.GetAllocator()); value.AddMember("Uid", ires->mUid.Write(root), root.GetAllocator()); value.AddMember("Value", rvIres, root.GetAllocator()); } @@ -169,8 +150,9 @@ std::unique_ptr IresObject::ReadFull(IresLoadingContext& ctx, const std::unique_ptr IresObject::ReadBasic(const rapidjson::Value& value) { auto rvType = rapidjson::GetProperty(value, rapidjson::kStringType, "Type"sv); if (!rvType) return nullptr; - auto kind = FromString(rapidjson::AsStringView(*rvType)); - auto ires = Create(kind); + auto kind = Metadata::EnumFromString(rapidjson::AsStringView(*rvType)); + assert(kind.has_value()); + auto ires = Create(kind.value()); if (!ires) return nullptr; auto rvUid = rapidjson::GetProperty(value, rapidjson::kArrayType, "Uid"sv); @@ -212,7 +194,7 @@ void IresManager::DiscoverFiles(const fs::path& dir) { std::vector> candidatesByKind; IresLoadTimeContext() { - candidatesByKind.resize(IresObject::KD_COUNT); + candidatesByKind.resize((int)IresObject::KD_COUNT); } std::vector& GetByKind(IresObject::Kind kind) { @@ -282,7 +264,7 @@ void IresManager::DiscoverFiles(const fs::path& dir) { // Load Ires in order by type, there are dependencies between them // TODO full arbitary dependency between Ires - for (int i = 0; i < IresObject::KD_COUNT; ++i) { + for (int i = 0; i < (int)IresObject::KD_COUNT; ++i) { auto kind = static_cast(i); auto& list = ctx.GetByKind(kind); for (auto cand : list) { @@ -302,7 +284,7 @@ void IresManager::DiscoverFiles(const fs::path& dir) { std::pair IresManager::Add(IresObject* ires) { auto& name = ires->mName; if (name.empty()) { - name = Utils::MakeRandomNumberedName(IresObject::ToString(ires->GetKind()).data()); + name = Utils::MakeRandomNumberedName(Metadata::EnumToString(ires->GetKind()).data()); } auto& uid = ires->mUid; @@ -423,3 +405,5 @@ IresObject* IresManager::FindIres(const Uid& uid) const { return nullptr; } } + +#include -- cgit v1.2.3-70-g09d2