/
MaterialPopulator.cpp
197 lines (158 loc) · 6.38 KB
/
MaterialPopulator.cpp
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
#include "MaterialPopulator.h"
#include "ifavourites.h"
#include "i18n.h"
#include "ishaders.h"
#include "string/string.h"
#include "shaderlib.h"
#include "wxutil/Bitmap.h"
#include "wxutil/dataview/TreeViewItemStyle.h"
namespace ui
{
namespace
{
const char* const OTHER_MATERIALS_FOLDER = N_("Other Materials");
constexpr const char* const FOLDER_ICON = "folder16.png";
constexpr const char* const TEXTURE_ICON = "icon_texture.png";
}
// Callback functor for processing shader names
struct ShaderNameCompareFunctor
{
bool operator()(const std::string& s1, const std::string& s2) const
{
return string_compare_nocase(s1.c_str(), s2.c_str()) < 0;
}
};
struct ShaderNameFunctor
{
// TreeStore to populate
wxutil::TreeModel& _store;
const MaterialTreeView::TreeColumns& _columns;
const std::set<std::string>& _favourites;
wxDataViewItem _root;
std::string _otherMaterialsPath;
// Maps of names to corresponding treemodel items, for both intermediate
// paths and explicitly presented paths
using NamedIterMap = std::map<std::string, wxDataViewItem, ShaderNameCompareFunctor>;
NamedIterMap _iters;
wxIcon _folderIcon;
wxIcon _textureIcon;
ShaderNameFunctor(wxutil::TreeModel& store, const MaterialTreeView::TreeColumns& columns, const std::set<std::string>& favourites) :
_store(store),
_columns(columns),
_favourites(favourites),
_root(_store.GetRoot()),
_otherMaterialsPath(_(OTHER_MATERIALS_FOLDER))
{
_folderIcon.CopyFromBitmap(wxutil::GetLocalBitmap(FOLDER_ICON));
_textureIcon.CopyFromBitmap(wxutil::GetLocalBitmap(TEXTURE_ICON));
}
// Recursive add function
wxDataViewItem& addRecursive(const std::string& path, bool isOtherMaterial)
{
// Look up candidate in the map and return it if found
auto it = _iters.find(path);
if (it != _iters.end())
{
return it->second;
}
/* Otherwise, split the path on its rightmost slash, call recursively on the
* first half in order to add the parent node, then add the second half as
* a child. Recursive bottom-out is when there is no slash (top-level node).
*/
// Find rightmost slash
std::size_t slashPos = path.rfind("/");
// Call recursively to get parent iter, leaving it at the toplevel if
// there is no slash
wxDataViewItem& parIter =
slashPos != std::string::npos ? addRecursive(path.substr(0, slashPos), isOtherMaterial) : _root;
// Append a node to the tree view for this child
wxutil::TreeModel::Row row = _store.AddItem(parIter);
std::string name = slashPos != std::string::npos ? path.substr(slashPos + 1) : path;
row[_columns.iconAndName] = wxVariant(wxDataViewIconText(name, _folderIcon));
row[_columns.leafName] = name;
row[_columns.fullName] = path;
row[_columns.isFolder] = true;
row[_columns.isOtherMaterialsFolder] = isOtherMaterial;
row[_columns.isFavourite] = false; // folders are not favourites
// Add a copy of the wxDataViewItem to our hashmap and return it
std::pair<NamedIterMap::iterator, bool> result = _iters.insert(
NamedIterMap::value_type(path, row.getItem()));
return result.first->second;
}
void visit(const std::string& name)
{
// Find rightmost slash
std::size_t slashPos = name.rfind("/");
wxDataViewItem parent;
if (string::istarts_with(name, GlobalTexturePrefix_get()))
{
// Regular texture, ensure parent folder
parent = slashPos != std::string::npos ? addRecursive(name.substr(0, slashPos), false) : _root;
}
else
{
// Put it under "other materials", ensure parent folder
parent = slashPos != std::string::npos ?
addRecursive(_otherMaterialsPath + "/" + name.substr(0, slashPos), true) :
addRecursive(_otherMaterialsPath, true);
}
// Insert the actual leaf
wxutil::TreeModel::Row row = _store.AddItem(parent);
std::string leafName = slashPos != std::string::npos ? name.substr(slashPos + 1) : name;
bool isFavourite = _favourites.count(name) > 0;
row[_columns.iconAndName] = wxVariant(wxDataViewIconText(leafName, _textureIcon));
row[_columns.leafName] = leafName;
row[_columns.fullName] = name;
row[_columns.isFolder] = false;
row[_columns.isOtherMaterialsFolder] = false;
row[_columns.isFavourite] = isFavourite;
// Formatting
row[_columns.iconAndName] = wxutil::TreeViewItemStyle::Declaration(isFavourite);
}
};
// Construct and initialise variables
MaterialPopulator::MaterialPopulator(const MaterialTreeView::TreeColumns& columns) :
ThreadedResourceTreePopulator(columns),
_columns(columns)
{
_favourites = GlobalFavouritesManager().getFavourites(decl::Type::Material);
}
MaterialPopulator::~MaterialPopulator()
{
// Stop the worker while the class is still intact
EnsureStopped();
}
void MaterialPopulator::PopulateModel(const wxutil::TreeModel::Ptr& model)
{
model->SetHasDefaultCompare(false);
ShaderNameFunctor functor(*model, _columns, _favourites);
GlobalMaterialManager().foreachShaderName([&](const std::string& name)
{
ThrowIfCancellationRequested();
functor.visit(name);
});
}
void MaterialPopulator::SortModel(const wxutil::TreeModel::Ptr& model)
{
// Sort the model while we're still in the worker thread
model->SortModelFoldersFirst(_columns.iconAndName, _columns.isFolder,
[&](const wxDataViewItem& a, const wxDataViewItem& b)
{
// Special folder comparison function
// A and B are both folders
wxVariant aIsOtherMaterialsFolder, bIsOtherMaterialsFolder;
model->GetValue(aIsOtherMaterialsFolder, a, _columns.isOtherMaterialsFolder.getColumnIndex());
model->GetValue(bIsOtherMaterialsFolder, b, _columns.isOtherMaterialsFolder.getColumnIndex());
// Special treatment for "Other Materials" folder, which always comes last
if (aIsOtherMaterialsFolder)
{
return +1;
}
if (bIsOtherMaterialsFolder)
{
return -1;
}
return 0; // no special folders, return equal to continue the regular sort algorithm
});
}
}