aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Template/Template_Main.cpp
blob: 9bebc2164d039f4f477c7530ee01492dbdcb895f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "Template.hpp"

#include "Model/GlobalStates.hpp"
#include "Model/Project.hpp"
#include "Utils/UUID.hpp"

#include <imgui.h>
#include <fstream>

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<void(SavedAsset)>& 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<Template::Kind>(asset.Payload);
}

Template* TemplateAssetList::CreateEmptyInstance(const SavedAsset& asset) const
{
	auto kind = static_cast<Template::Kind>(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<Template::Kind>(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<Template::Kind>(asset.Payload);
	ImGui::TextUnformatted(Template::FormatKind(kind));

	ImGui::TableNextColumn();
	// TODO
}