diff options
author | rtk0c <[email protected]> | 2022-06-03 22:58:28 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-06-03 22:58:28 -0700 |
commit | 8510a85f79f706b93982b4e398b187b5f77081dd (patch) | |
tree | 996c27f54f748d940f58454f7ef1e1570d1a31d5 /source/30-game/Ires.cpp | |
parent | bd07ae3f4e1bcdedc3e373460671ca9713a03de5 (diff) |
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.
Diffstat (limited to 'source/30-game/Ires.cpp')
-rw-r--r-- | source/30-game/Ires.cpp | 48 |
1 files changed, 16 insertions, 32 deletions
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 <Macros.hpp> +#include <Metadata.hpp> +#include <RapidJsonHelper.hpp> +#include <ScopeGuard.hpp> +#include <Utils.hpp> #include <imgui.h> #include <misc/cpp/imgui_stdlib.h> @@ -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> IresObject::Create(Kind kind) { switch (kind) { case KD_Texture: return std::make_unique<IresTexture>(); @@ -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> IresObject::ReadFull(IresLoadingContext& ctx, const std::unique_ptr<IresObject> 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<Kind>(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<std::vector<LoadCandidate*>> candidatesByKind; IresLoadTimeContext() { - candidatesByKind.resize(IresObject::KD_COUNT); + candidatesByKind.resize((int)IresObject::KD_COUNT); } std::vector<LoadCandidate*>& 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<IresObject::Kind>(i); auto& list = ctx.GetByKind(kind); for (auto cand : list) { @@ -302,7 +284,7 @@ void IresManager::DiscoverFiles(const fs::path& dir) { std::pair<IresObject*, bool> 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 <generated/Ires.gs.inl> |