aboutsummaryrefslogtreecommitdiff
path: root/core/src/Model/Assets.cpp
blob: fa295238d1c90a0ab863e8638ae7f8b3997c2b93 (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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "Assets.hpp"

#include "UI/UI.hpp"
#include "Utils/I18n.hpp"
#include "Utils/IO/DataStream.hpp"
#include "Utils/IO/StringIntegration.hpp"
#include "Utils/IO/UuidIntegration.hpp"

#include <IconsFontAwesome.h>
#include <imgui.h>
#include <imgui_stdlib.h>
#include <tsl/array_map.h>
#include <string>
#include <utility>

using namespace std::literals::string_view_literals;
namespace fs = std::filesystem;

template <class TSavedAsset, class TStream>
void OperateStreamForSavedAsset(TSavedAsset& cell, TStream& proxy)
{
	proxy.template ObjectAdapted<DataStreamAdapters::String>(cell.Name);
	proxy.template ObjectAdapted<DataStreamAdapters::Uuid>(cell.Uuid);
	proxy.Value(cell.Payload);
}

void SavedAsset::ReadFromDataStream(InputDataStream& stream)
{
	::OperateStreamForSavedAsset(*this, stream);
}

void SavedAsset::WriteToDataStream(OutputDataStream& stream) const
{
	::OperateStreamForSavedAsset(*this, stream);
}

Asset::Asset() = default;

class AssetList::Private
{
public:
	Project* ConnectedProject;
	tsl::array_map<char, SavedAsset> Assets;
	tsl::array_map<char, std::unique_ptr<Asset>> Cache;
	int CacheSizeLimit = 0;

	struct
	{
		std::string NewName;
		NameSelectionError NewNameError = NameSelectionError::Empty;

		void ShowErrors() const
		{
			switch (NewNameError) {
				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;
			}
		}

		bool HasErrors() const
		{
			return NewNameError != NameSelectionError::None;
		}

		void Validate(const AssetList& self)
		{
			if (NewName.empty()) {
				NewNameError = NameSelectionError::Empty;
				return;
			}

			if (self.FindByName(NewName)) {
				NewNameError = NameSelectionError::Duplicated;
				return;
			}

			NewNameError = NameSelectionError::None;
		}
	} PopupPrivateState;
};

AssetList::AssetList(Project& project)
	: mPrivate{ std::make_unique<Private>() }
{
	mPrivate->ConnectedProject = &project;
}

// Write an empty destructor here so std::unique_ptr's destructor can see AssetList::Private's implementation
AssetList::~AssetList()
{
}

Project& AssetList::GetConnectedProject() const
{
	return *mPrivate->ConnectedProject;
}

void AssetList::Reload()
{
	// TODO fix asset dicovery loading
	mPrivate->Assets.clear();
	mPrivate->Cache.clear();
	DiscoverFiles([this](SavedAsset asset) -> void {
		mPrivate->Assets.insert(asset.Name, std::move(asset));
	});
}

int AssetList::GetCount() const
{
	return mPrivate->Assets.size();
}

const tsl::array_map<char, SavedAsset>& AssetList::GetAssets() const
{
	return mPrivate->Assets;
}

const SavedAsset* AssetList::FindByName(std::string_view name) const
{
	auto iter = mPrivate->Assets.find(name);
	if (iter != mPrivate->Assets.end()) {
		return &iter.value();
	} else {
		return nullptr;
	}
}

const SavedAsset& AssetList::Create(SavedAsset asset)
{
	auto [iter, DISCARD] = mPrivate->Assets.insert(asset.Name, SavedAsset{});
	auto& savedAsset = iter.value();

	savedAsset = std::move(asset);
	if (savedAsset.Uuid.is_nil()) {
		savedAsset.Uuid = uuids::uuid_random_generator{}();
	}

	SaveInstance(savedAsset, nullptr);

	return savedAsset;
}

std::unique_ptr<Asset> AssetList::CreateAndLoad(SavedAsset assetIn)
{
	auto& savedAsset = Create(std::move(assetIn));
	auto asset = std::unique_ptr<Asset>(CreateInstance(savedAsset));
	return asset;
}

std::unique_ptr<Asset> AssetList::Load(std::string_view name) const
{
	if (auto savedAsset = FindByName(name)) {
		auto asset = Load(*savedAsset);
		return asset;
	} else {
		return nullptr;
	}
}

std::unique_ptr<Asset> AssetList::Load(const SavedAsset& asset) const
{
	return std::unique_ptr<Asset>(LoadInstance(asset));
}

const SavedAsset* AssetList::Rename(std::string_view oldName, std::string_view newName)
{
	auto iter = mPrivate->Assets.find(oldName);
	if (iter == mPrivate->Assets.end()) return nullptr;

	auto info = std::move(iter.value());
	info.Name = newName;

	RenameInstanceOnDisk(info, oldName);

	mPrivate->Assets.erase(iter);
	auto [newIter, DISCARD] = mPrivate->Assets.insert(newName, std::move(info));

	return &newIter.value();
}

bool AssetList::Remove(std::string_view name)
{
	auto iter = mPrivate->Assets.find(name);
	if (iter == mPrivate->Assets.end()) {
		return false;
	}
	auto& asset = iter.value();

	fs::remove(RetrievePathFromAsset(asset));
	mPrivate->Assets.erase(iter);

	return true;
}

int AssetList::GetCacheSizeLimit() const
{
	return mPrivate->CacheSizeLimit;
}

void AssetList::SetCacheSizeLimit(int limit)
{
	mPrivate->CacheSizeLimit = limit;
}

void AssetList::DisplayIconsList(ListState& state)
{
	// TODO
}

void AssetList::DisplayDetailsList(ListState& state)
{
	// Note: stub function remained here in case any state processing needs to be done before issuing to implementers
	DisplayDetailsTable(state);
}

void AssetList::DisplayControls(ListState& state)
{
	auto& ps = mPrivate->PopupPrivateState;
	bool openedDummy = true;

	if (ImGui::Button(ICON_FA_PLUS " " I18N_TEXT("Add", L10N_ADD))) {
		ImGui::OpenPopup(I18N_TEXT("Add asset wizard", L10N_ADD_ASSET_DIALOG_TITLE));
	}
	if (ImGui::BeginPopupModal(I18N_TEXT("Add asset wizard", L10N_ADD_ASSET_DIALOG_TITLE), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) {
		DisplayAssetCreator(state);
		ImGui::EndPopup();
	}

	ImGui::SameLine();
	if (ImGui::Button(ICON_FA_I_CURSOR " " I18N_TEXT("Rename", L10N_RENAME), state.SelectedAsset == nullptr)) {
		ImGui::OpenPopup(I18N_TEXT("Rename asset wizard", L10N_RENAME_ASSET_DIALOG_TITLE));
	}
	if (ImGui::BeginPopupModal(I18N_TEXT("Rename asset wizard", L10N_RENAME_ASSET_DIALOG_TITLE), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) {
		if (ImGui::InputText(I18N_TEXT("Name", L10N_NAME), &ps.NewName)) {
			ps.Validate(*this);
		}

		if (ImGui::Button(I18N_TEXT("Confirm", L10N_CONFIRM), ps.HasErrors())) {
			ImGui::CloseCurrentPopup();

			auto movedAsset = Rename(state.SelectedAsset->Name, ps.NewName);
			// Update the selected pointer to the new location (we mutated the map, the pointer may be invalid now)
			state.SelectedAsset = movedAsset;

			ps = {};
		}
		ImGui::SameLine();
		if (ImGui::Button(I18N_TEXT("Cancel", L10N_CANCEL))) {
			ImGui::CloseCurrentPopup();
		}

		ps.ShowErrors();

		ImGui::EndPopup();
	}

	ImGui::SameLine();
	if (ImGui::Button(ICON_FA_TRASH " " I18N_TEXT("Delete", L10N_DELETE), state.SelectedAsset == nullptr)) {
		ImGui::OpenPopup(I18N_TEXT("Delete asset", L10N_DELETE_ASSET_DIALOG_TITLE));
	}
	if (ImGui::BeginPopupModal(I18N_TEXT("Delete asset", L10N_DELETE_ASSET_DIALOG_TITLE), &openedDummy, ImGuiWindowFlags_AlwaysAutoResize)) {
		if (ImGui::Button(I18N_TEXT("Confirm", L10N_CONFIRM))) {
			ImGui::CloseCurrentPopup();

			auto& assetName = state.SelectedAsset->Name;
			Remove(assetName);

			state.SelectedAsset = nullptr;
		}
		ImGui::SameLine();
		if (ImGui::Button(I18N_TEXT("Cancel", L10N_CANCEL))) {
			ImGui::CloseCurrentPopup();
		}
		ImGui::EndPopup();
	}
}

void AssetList::DiscoverFilesByExtension(const std::function<void(SavedAsset)>& callback, const fs::path& containerDir, std::string_view extension) const
{
	for (auto entry : fs::directory_iterator(containerDir)) {
		if (!entry.is_regular_file()) continue;

		// If the caller provided an extension to match against, and it doesn't equal to current file extension, skip
		if (!extension.empty() &&
			entry.path().extension() != extension)
		{
			continue;
		}

		callback(SavedAsset{
			.Name = RetrieveNameFromFile(entry.path()),
			.Uuid = RetrieveUuidFromFile(entry.path()),
			// TODO load payload
		});
	}
}

void AssetList::DiscoverFilesByHeader(const std::function<void(SavedAsset)>& callback, const fs::path& containerDir, const std::function<bool(std::istream&)>& validater) const
{
	// TODO
}