diff options
author | rtk0c <[email protected]> | 2022-04-18 17:54:29 -0700 |
---|---|---|
committer | rtk0c <[email protected]> | 2022-04-18 17:54:29 -0700 |
commit | 7af9992ca81c699bc1cf05eb691e284bf424f050 (patch) | |
tree | 0f9bf191270791d80a28d7df1f1ffd5223158124 /source/Ires.cpp | |
parent | 5424a1d5434e3ddd911a504719918c2df027e2fd (diff) |
Changeset: 9 Implement IresSpritesheet
Diffstat (limited to 'source/Ires.cpp')
-rw-r--r-- | source/Ires.cpp | 103 |
1 files changed, 86 insertions, 17 deletions
diff --git a/source/Ires.cpp b/source/Ires.cpp index f700ed6..255c221 100644 --- a/source/Ires.cpp +++ b/source/Ires.cpp @@ -1,12 +1,15 @@ #include "Ires.hpp" #include "AppConfig.hpp" +#include "EditorUtils.hpp" #include "RapidJsonHelper.hpp" #include "ScopeGuard.hpp" #include "Sprite.hpp" #include "Texture.hpp" #include "Utils.hpp" +#include <imgui.h> +#include <misc/cpp/imgui_stdlib.h> #include <rapidjson/document.h> #include <rapidjson/filereadstream.h> #include <rapidjson/filewritestream.h> @@ -22,18 +25,18 @@ IresObject::IresObject(Kind kind) std::string_view IresObject::ToString(Kind kind) { switch (kind) { - case KD_Texture: return "Texture"sv; - case KD_SpriteFiles: return "SpriteFiles"sv; - case KD_Spritesheet: return "Spritesheet"sv; + case KD_Texture: return BRUSSEL_TAG_PREFIX_Ires "Texture"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 == "Texture"sv) return KD_Texture; - if (name == "SpriteFiles"sv) return KD_SpriteFiles; - if (name == "Spritesheet"sv) return KD_Spritesheet; + if (name == BRUSSEL_TAG_PREFIX_Ires "Texture"sv) return KD_Texture; + 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; } @@ -51,14 +54,29 @@ bool IresObject::IsAnnoymous() const { return mName.empty(); } -rapidjson::Value IresObject::WriteFull(const IresObject& ires, rapidjson::Document& root) { - rapidjson::Value rvIres; - ires.Write(rvIres, root); +void IresObject::SetName(std::string name) { + if (mMan) { + mMan->Rename(this, std::move(name)); + } else { + mName = std::move(name); + } +} - rapidjson::Value result; - result.AddMember("Type", rapidjson::StringRef(ToString(ires.GetKind())), root.GetAllocator()); - result.AddMember("Value", rvIres, root.GetAllocator()); - return result; +void IresObject::ShowEditor(EditorInstance& editor) { + bool isAnnoymous = mName.empty(); + if (isAnnoymous) { + ImGui::Text("<Annoymous Ires at %p>", (void*)this); + } else { + ImGui::Text("%s", mName.c_str()); + } +} + +void IresObject::WriteFull(IresObject* ires, rapidjson::Value& value, rapidjson::Document& root) { + rapidjson::Value rvIres(rapidjson::kObjectType); + ires->Write(rvIres, root); + + value.AddMember("Type", rapidjson::StringRef(ToString(ires->GetKind())), root.GetAllocator()); + value.AddMember("Value", rvIres, root.GetAllocator()); } std::unique_ptr<IresObject> IresObject::ReadFull(const rapidjson::Value& value) { @@ -68,13 +86,20 @@ std::unique_ptr<IresObject> IresObject::ReadFull(const rapidjson::Value& value) auto ires = Create(kind); if (!ires) return nullptr; - auto rvValue = rapidjson::GetProperty(value, "Value"sv); - if (!rvValue) return nullptr; - ires->Read(*rvValue); + if (!ReadPartial(ires.get(), value)) { + return nullptr; + } return ires; } +bool IresObject::ReadPartial(IresObject* ires, const rapidjson::Value& value) { + auto rvValue = rapidjson::GetProperty(value, "Value"sv); + if (!rvValue) return false; + ires->Read(*rvValue); + return true; +} + void IresManager::DiscoverFilesDesignatedLocation() { auto path = AppConfig::assetDirPath / "Ires"; DiscoverFiles(path); @@ -121,7 +146,9 @@ void IresManager::DiscoverFiles(const fs::path& dir) { idenView = std::string_view(idenView.data(), idenView.size() - 1); } #endif - mObjects.try_emplace(std::move(iden), ires.release()); + ires->mName = std::move(iden); + std::string_view key(ires->mName); + mObjects.try_emplace(key, ires.release()); } } @@ -166,6 +193,48 @@ bool IresManager::Rename(IresObject* ires, std::string newName) { return true; } +IresObject* IresManager::Load(const fs::path& path) { + // TODO + return nullptr; +} + +static fs::path GetDesignatedPath(IresObject* ires) { + return AppConfig::assetDirPath / "Ires" / fs::path(ires->GetName()).replace_extension(".json"); +} + +void IresManager::Reload(IresObject* ires) { + auto file = Utils::OpenCstdioFile(GetDesignatedPath(ires), Utils::Read); + if (!file) return; + DEFER { fclose(file); }; + + char readerBuffer[65536]; + rapidjson::FileReadStream stream(file, readerBuffer, sizeof(readerBuffer)); + + rapidjson::Document root; + root.ParseStream(stream); + + IresObject::ReadPartial(ires, root); +} + +void IresManager::Save(IresObject* ires) { + Save(ires, GetDesignatedPath(ires)); +} + +void IresManager::Save(IresObject* ires, const fs::path& filePath) { + rapidjson::Document root(rapidjson::kObjectType); + + IresObject::WriteFull(ires, root, root); + + auto file = Utils::OpenCstdioFile(filePath, Utils::WriteTruncate); + if (!file) return; + DEFER { fclose(file); }; + + char writerBuffer[65536]; + rapidjson::FileWriteStream stream(file, writerBuffer, sizeof(writerBuffer)); + rapidjson::Writer<rapidjson::FileWriteStream> writer(stream); + root.Accept(writer); +} + IresObject* IresManager::FindIres(std::string_view path) { auto iter = mObjects.find(path); if (iter != mObjects.end()) { |