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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#include "Template.hpp"
#include <Cplt/Model/GlobalStates.hpp>
#include <Cplt/Model/Project.hpp>
#include <Cplt/UI/UI.hpp>
#include <Cplt/Utils/I18n.hpp>
#include <Cplt/Utils/IO/Archive.hpp>
#include <Cplt/Utils/UUID.hpp>
#include <imgui.h>
#include <imgui_stdlib.h>
#include <algorithm>
#include <cstdint>
#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 dir = GetConnectedProject().GetTemplatesDirectory();
DiscoverFilesByExtension(callback, dir, ".cplt-template"sv);
}
std::string TemplateAssetList::RetrieveNameFromFile(const fs::path& file) const {
auto res = DataArchive::LoadFile(file);
if (!res) return "";
auto& stream = res.value();
SavedAsset assetInfo;
stream.ReadObject(assetInfo);
return assetInfo.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 fileName = uuids::to_string(asset.Uuid);
return GetConnectedProject().GetTemplatePath(fileName);
}
bool TemplateAssetList::SaveInstance(const SavedAsset& assetInfo, const Asset* asset) const {
auto path = RetrievePathFromAsset(assetInfo);
auto res = DataArchive::SaveFile(path);
if (!res) return false;
auto& stream = res.value();
stream.WriteObject(assetInfo);
// This cast is fine: calls to this class will always be wrapped in TypedAssetList<T>, which will ensure `asset` points to some Template
if (auto tmpl = static_cast<const Template*>(asset)) { // NOLINT(cppcoreguidelines-pro-type-static-cast-downcast)
stream.WriteObject(*tmpl);
}
return true;
}
static std::unique_ptr<Template> LoadTemplateFromFile(const fs::path& path) {
auto res = DataArchive::LoadFile(path);
if (!res) return nullptr;
auto& stream = res.value();
SavedAsset assetInfo;
stream.ReadObject(assetInfo);
auto kind = static_cast<Template::Kind>(assetInfo.Payload);
auto tmpl = Template::CreateByKind(kind);
stream.ReadObject(*tmpl);
return tmpl;
}
Template* TemplateAssetList::LoadInstance(const SavedAsset& assetInfo) const {
return ::LoadTemplateFromFile(RetrievePathFromAsset(assetInfo)).release();
}
Template* TemplateAssetList::CreateInstance(const SavedAsset& assetInfo) const {
auto kind = static_cast<Template::Kind>(assetInfo.Payload);
return Template::CreateByKind(kind).release();
}
bool TemplateAssetList::RenameInstanceOnDisk(const SavedAsset& assetInfo, std::string_view oldName) const {
// Get asset path, which is only dependent on UUID
auto path = RetrievePathFromAsset(assetInfo);
auto tmpl = ::LoadTemplateFromFile(path);
if (!tmpl) return false;
// Rewrite the asset with the updated name (note the given assetInfo already has the update name)
SaveInstance(assetInfo, tmpl.get());
return true;
}
void TemplateAssetList::DisplayAssetCreator(ListState& state) {
auto ValidateNewName = [&]() -> void {
if (mACNewName.empty()) {
mACNewNameError = NameSelectionError::Empty;
return;
}
if (FindByName(mACNewName)) {
mACNewNameError = NameSelectionError::Duplicated;
return;
}
mACNewNameError = NameSelectionError::None;
};
auto ShowNewNameErrors = [&]() -> void {
switch (mACNewNameError) {
case NameSelectionError::None: break;
case NameSelectionError::Duplicated:
ImGui::ErrorMessage(I18N_TEXT("Duplicate name", L10N_DUPLICATE_NAME_ERROR));
break;
case NameSelectionError::Empty:
ImGui::ErrorMessage(I18N_TEXT("Name cannot be empty", L10N_EMPTY_NAME_ERROR));
break;
}
};
auto ShowNewKindErrors = [&]() -> void {
if (mACNewKind == Template::InvalidKind) {
ImGui::ErrorMessage(I18N_TEXT("Invalid template type", L10N_TEMPLATE_INVALID_TYPE_ERROR));
}
};
auto IsInputValid = [&]() -> bool {
return mACNewNameError == NameSelectionError::None &&
mACNewKind != Template::InvalidKind;
};
auto ResetState = [&]() -> void {
mACNewName.clear();
mACNewKind = Template::InvalidKind;
ValidateNewName();
};
if (ImGui::InputText(I18N_TEXT("Name", L10N_NAME), &mACNewName)) {
ValidateNewName();
}
if (ImGui::BeginCombo(I18N_TEXT("Type", L10N_TYPE), Template::FormatKind(mACNewKind))) {
for (int i = 0; i < Template::KindCount; ++i) {
auto kind = static_cast<Template::Kind>(i);
if (ImGui::Selectable(Template::FormatKind(kind), mACNewKind == kind)) {
mACNewKind = kind;
}
}
ImGui::EndCombo();
}
ShowNewNameErrors();
ShowNewKindErrors();
if (ImGui::Button(I18N_TEXT("OK", L10N_CONFIRM), !IsInputValid())) {
ImGui::CloseCurrentPopup();
Create(SavedAsset{
.Name = mACNewName,
.Payload = static_cast<uint64_t>(mACNewKind),
});
ResetState();
}
ImGui::SameLine();
if (ImGui::Button(I18N_TEXT("Cancel", L10N_CANCEL))) {
ImGui::CloseCurrentPopup();
}
}
void TemplateAssetList::DisplayDetailsTable(ListState& state) const {
ImGui::BeginTable("AssetDetailsTable", 2, ImGuiTableFlags_Borders);
ImGui::TableSetupColumn(I18N_TEXT("Name", L10N_NAME));
ImGui::TableSetupColumn(I18N_TEXT("Type", L10N_TYPE));
ImGui::TableHeadersRow();
for (auto& asset : this->GetAssets()) {
ImGui::TableNextRow();
ImGui::TableNextColumn();
if (ImGui::Selectable(asset.Name.c_str(), state.SelectedAsset == &asset, ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_DontClosePopups)) {
state.SelectedAsset = &asset;
}
ImGui::TableNextColumn();
auto kind = static_cast<Template::Kind>(asset.Payload);
ImGui::TextUnformatted(Template::FormatKind(kind));
}
ImGui::EndTable();
}
|