/
ImageFilePopulator.h
150 lines (125 loc) · 4.42 KB
/
ImageFilePopulator.h
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
#pragma once
#include "ifilesystem.h"
#include "gamelib.h"
#include "string/case_conv.h"
#include "string/predicate.h"
#include "string/trim.h"
#include "os/path.h"
#include "wxutil/dataview/ThreadedResourceTreePopulator.h"
#include "wxutil/dataview/ResourceTreeView.h"
#include "wxutil/dataview/VFSTreePopulator.h"
#include "wxutil/Bitmap.h"
namespace ui
{
namespace
{
const char* const TEXTURE_ICON = "icon_texture.png";
const char* const FOLDER_ICON = "folder16.png";
}
class ImageFileFunctor :
public wxutil::VFSTreePopulator
{
private:
const wxutil::ResourceTreeView::Columns& _columns;
wxIcon _fileIcon;
wxIcon _folderIcon;
std::set<std::string> _cubemapSuffixes;
public:
// Constructor
ImageFileFunctor(const wxutil::TreeModel::Ptr& treeStore,
const wxutil::ResourceTreeView::Columns& columns) :
VFSTreePopulator(treeStore),
_columns(columns),
_cubemapSuffixes({ "_nx", "_ny", "_nz", "_px", "_py", "_pz",
"_forward", "_back", "_left", "_right", "_up", "_down" })
{
_fileIcon.CopyFromBitmap(wxutil::GetLocalBitmap(TEXTURE_ICON));
_folderIcon.CopyFromBitmap(wxutil::GetLocalBitmap(FOLDER_ICON));
}
void addFile(const vfs::FileInfo& fileInfo)
{
const std::string ddsPrefix = "dds/";
std::string fullPath = fileInfo.fullPath();
// Sort the shader into the tree and set the values
addPath(fullPath, [&](wxutil::TreeModel::Row& row, const std::string& path,
const std::string& leafName, bool isFolder)
{
// The map expressions don't need any file extensions
auto imageFilePath = os::removeExtension(path);
// Cut off the dds prefix, it won't be valid when set in a material
if (string::istarts_with(path, ddsPrefix))
{
imageFilePath = imageFilePath.substr(ddsPrefix.length());
}
// For cubemaps, cut off the suffixes
if (string::istarts_with(imageFilePath, "env/"))
{
auto underscorePos = imageFilePath.find_last_of('_');
if (underscorePos != std::string::npos)
{
auto suffix = imageFilePath.substr(underscorePos);
string::to_lower(suffix);
if (_cubemapSuffixes.count(suffix) != 0)
{
imageFilePath = imageFilePath.substr(0, imageFilePath.length() - suffix.length());
}
}
}
row[_columns.iconAndName] = wxVariant(wxDataViewIconText(leafName, isFolder ? _folderIcon : _fileIcon));
row[_columns.leafName] = leafName;
row[_columns.fullName] = imageFilePath;
row[_columns.isFolder] = isFolder;
row[_columns.isFavourite] = false;
row.SendItemAdded();
});
}
};
class ImageFilePopulator final :
public wxutil::ThreadedResourceTreePopulator
{
private:
const wxutil::ResourceTreeView::Columns& _columns;
const char* const GKEY_IMAGE_TYPES = "/filetypes/texture//extension";
std::set<std::string> _extensions;
public:
// Construct and initialise variables
ImageFilePopulator(const wxutil::ResourceTreeView::Columns& columns) :
ThreadedResourceTreePopulator(columns),
_columns(columns)
{
auto texTypes = game::current::getNodes(GKEY_IMAGE_TYPES);
for (const auto& node : texTypes)
{
// Get the file extension, store it as lowercase
std::string extension = node.getContent();
_extensions.emplace(string::to_lower_copy(extension));
}
}
~ImageFilePopulator()
{
EnsureStopped();
}
protected:
void PopulateModel(const wxutil::TreeModel::Ptr& model) override
{
ImageFileFunctor functor(model, _columns);
GlobalFileSystem().forEachFile(
"", "*",
[&](const vfs::FileInfo& fileInfo)
{
ThrowIfCancellationRequested();
if (_extensions.count(string::to_lower_copy(os::getExtension(fileInfo.name))) == 0)
{
return;
}
functor.addFile(fileInfo);
},
99
);
}
void SortModel(const wxutil::TreeModel::Ptr& model) override
{
model->SortModelFoldersFirst(_columns.iconAndName, _columns.isFolder);
}
};
}