#include "Template.hpp" #include "Model/GlobalStates.hpp" #include "Model/Project.hpp" #include #include #include using namespace std::literals::string_view_literals; namespace fs = std::filesystem; Template::Template(Kind kind) : mKind{ kind } { } Template::Kind Template::GetKind() const { return mKind; } void TemplateAssetList::DiscoverFiles(const std::function& callback) const { auto& gs = GlobalStates::GetInstance(); DiscoverFilesByExtension(callback, gs.GetCurrentProject()->GetTemplatesDirectory(), ".cplt-template"sv); } std::string TemplateAssetList::RetrieveNameFromFile(const fs::path& file) const { std::ifstream ifs(file); if (!ifs) return ""; std::string name; ifs >> name; return name; } uuids::uuid TemplateAssetList::RetrieveUuidFromFile(const fs::path& file) const { return uuids::uuid::from_string(file.stem().string()); } fs::path TemplateAssetList::RetrievePathFromAsset(const SavedAsset& asset) const { auto uuid = uuids::uuid_random_generator{}(); auto fileName = uuids::to_string(uuid); fileName.append(".cplt-template"); auto& gs = GlobalStates::GetInstance(); return gs.GetCurrentProject()->GetTemplatePath(fileName); } void TemplateAssetList::SaveEmptyInstance(const SavedAsset& asset) const { auto path = RetrievePathFromAsset(asset); std::ofstream ofs(path); if (!ofs) return; ofs << asset.Name; ofs << static_cast(asset.Payload); } Template* TemplateAssetList::CreateEmptyInstance(const SavedAsset& asset) const { auto kind = static_cast(asset.Payload); return Template::CreateByKind(kind).release(); } Template* TemplateAssetList::LoadImpl(const SavedAsset& asset) const { std::ifstream ifs(RetrievePathFromAsset(asset)); if (!ifs) return nullptr; uint32_t iKind; ifs >> iKind; auto kind = static_cast(iKind); auto tmpl = Template::CreateByKind(kind); auto res = tmpl->ReadFrom(ifs); if (res != Template::RR_Success) { return nullptr; } return tmpl.release(); } void TemplateAssetList::SetupDetailsTable(const char* tableId) const { ImGui::BeginTable(tableId, 3); ImGui::TableSetupColumn("Name"); ImGui::TableSetupColumn("Type"); ImGui::TableSetupColumn("Modified time"); ImGui::TableHeadersRow(); } void TemplateAssetList::DrawBigIcon(const SavedAsset& asset) const { // TODO } void TemplateAssetList::DrawDetailsTableRow(const SavedAsset& asset) const { ImGui::TableNextColumn(); ImGui::TextUnformatted(asset.Name.c_str()); ImGui::TableNextColumn(); auto kind = static_cast(asset.Payload); ImGui::TextUnformatted(Template::FormatKind(kind)); ImGui::TableNextColumn(); // TODO }