/
ShaderSelector.cpp
372 lines (301 loc) · 10.8 KB
/
ShaderSelector.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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include "ShaderSelector.h"
#include "i18n.h"
#include "wxutil/dataview/TreeView.h"
#include "wxutil/dataview/VFSTreePopulator.h"
#include "wxutil/DeclarationSourceView.h"
#include "texturelib.h"
#include "ishaders.h"
#include <wx/dataview.h>
#include <wx/sizer.h>
#include "wxutil/Bitmap.h"
#include <GL/glew.h>
#include <vector>
#include <string>
#include "string/split.h"
#include "string/predicate.h"
#include <functional>
#include "ifavourites.h"
#include "wxutil/dataview/ThreadedDeclarationTreePopulator.h"
#include "wxutil/dataview/TreeViewItemStyle.h"
namespace ui
{
/* CONSTANTS */
namespace
{
const char* const FOLDER_ICON = "folder16.png";
const char* const TEXTURE_ICON = "icon_texture.png";
}
/**
* Visitor class to retrieve material names and add them to folders.
*/
class ThreadedMaterialLoader final :
public wxutil::ThreadedDeclarationTreePopulator
{
private:
const wxutil::DeclarationTreeView::Columns& _columns;
const ShaderSelector::PrefixList& _prefixes;
wxIcon _folderIcon;
wxIcon _textureIcon;
public:
ThreadedMaterialLoader(const wxutil::DeclarationTreeView::Columns& columns, const ShaderSelector::PrefixList& prefixes) :
ThreadedDeclarationTreePopulator(decl::Type::Material, columns),
_columns(columns),
_prefixes(prefixes)
{
_textureIcon.CopyFromBitmap(wxutil::GetLocalBitmap(TEXTURE_ICON));
_folderIcon.CopyFromBitmap(wxutil::GetLocalBitmap(FOLDER_ICON));
}
~ThreadedMaterialLoader()
{
EnsureStopped();
}
protected:
void PopulateModel(const wxutil::TreeModel::Ptr& model) override
{
wxutil::VFSTreePopulator populator(model);
GlobalMaterialManager().foreachShaderName([&](const std::string& materialName)
{
for (const std::string& prefix : _prefixes)
{
if (string::istarts_with(materialName, prefix + "/"))
{
populator.addPath(materialName, [&](wxutil::TreeModel::Row& row,
const std::string& path, const std::string& leafName, bool isFolder)
{
StoreMaterialValues(row, path, leafName, isFolder);
});
break; // don't consider any further prefixes
}
}
});
}
void SortModel(const wxutil::TreeModel::Ptr& model) override
{
model->SortModelFoldersFirst(_columns.leafName, _columns.isFolder);
}
private:
void StoreMaterialValues(wxutil::TreeModel::Row& row, const std::string& materialName, const std::string& leafName, bool isFolder)
{
bool isFavourite = IsFavourite(materialName);
row[_columns.iconAndName] = wxVariant(wxDataViewIconText(leafName, !isFolder ? _textureIcon : _folderIcon));
row[_columns.iconAndName] = wxutil::TreeViewItemStyle::Declaration(isFavourite);
row[_columns.fullName] = materialName;
row[_columns.leafName] = leafName;
row[_columns.declName] = materialName;
row[_columns.isFolder] = isFolder;
row[_columns.isFavourite] = isFavourite;
row.SendItemAdded();
}
};
// Constructor creates elements
ShaderSelector::ShaderSelector(wxWindow* parent, Client* client, const std::string& prefixes, bool isLightTexture) :
wxPanel(parent, wxID_ANY),
_treeView(nullptr),
_glWidget(nullptr),
_client(client),
_isLightTexture(isLightTexture),
_infoStore(new wxutil::TreeModel(_infoStoreColumns, true)) // is a listmodel
{
SetSizer(new wxBoxSizer(wxVERTICAL));
// Split the given comma-separated list into the vector
string::split(_prefixes, prefixes, ",");
// Pack in TreeView and info panel
createTreeView();
createPreview();
}
void ShaderSelector::_onShowShaderDefinition()
{
// Construct a definition view and pass the material name
auto view = new wxutil::DeclarationSourceView(this);
view->setDeclaration(decl::Type::Material, getSelection());
view->ShowModal();
view->Destroy();
}
// Return the selection to the calling code
std::string ShaderSelector::getSelection()
{
return _treeView->GetSelectedDeclName();
}
// Set the selection in the treeview
void ShaderSelector::setSelection(const std::string& sel)
{
_treeView->SetSelectedDeclName(sel);
}
void ShaderSelector::createTreeView()
{
_treeView = new wxutil::DeclarationTreeView(this, decl::Type::Material,
_shaderTreeColumns, wxDV_NO_HEADER | wxDV_SINGLE);
// Single visible column, containing the directory/shader name and the icon
_treeView->AppendIconTextColumn(_("Value"), _shaderTreeColumns.iconAndName.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE, wxALIGN_NOT, wxDATAVIEW_COL_SORTABLE);
// Use the TreeModel's full string search function
_treeView->AddSearchColumn(_shaderTreeColumns.leafName);
// Get selection and connect the changed callback
_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &ShaderSelector::_onSelChange, this);
GetSizer()->Add(_treeView, 1, wxEXPAND);
_treeView->Populate(std::make_shared<ThreadedMaterialLoader>(_shaderTreeColumns, _prefixes));
}
// Create the preview panel (GL widget and info table)
void ShaderSelector::createPreview()
{
// HBox contains the preview GL widget along with a texture attributes pane.
wxBoxSizer* sizer = new wxBoxSizer(wxHORIZONTAL);
// Cast the GLWidget object to GtkWidget
_glWidget = new wxutil::GLWidget(this, std::bind(&ShaderSelector::onPreviewRender, this), "ShaderSelector");
_glWidget->SetMinClientSize(wxSize(128, 128));
// Attributes table
wxDataViewCtrl* tree = new wxDataViewCtrl(this, wxID_ANY,
wxDefaultPosition, wxDefaultSize, wxDV_NO_HEADER | wxDV_SINGLE);
tree->AssociateModel(_infoStore.get());
tree->AppendTextColumn(_("Attribute"), _infoStoreColumns.attribute.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE);
tree->AppendTextColumn(_("Value"), _infoStoreColumns.value.getColumnIndex(),
wxDATAVIEW_CELL_INERT, wxCOL_WIDTH_AUTOSIZE);
sizer->Add(_glWidget, 0, wxEXPAND);
sizer->Add(tree, 1, wxEXPAND);
GetSizer()->Add(sizer, 0, wxEXPAND | wxTOP, 3);
}
// Get the selected shader
MaterialPtr ShaderSelector::getSelectedShader() {
return GlobalMaterialManager().getMaterial(getSelection());
}
// Update the attributes table
void ShaderSelector::updateInfoTable()
{
_infoStore->Clear();
// Get the selected texture name. If nothing is selected, we just leave the
// infotable empty.
std::string selName = getSelection();
// Notify the client of the change to give it a chance to update the infostore
if (_client != NULL && !selName.empty())
{
_client->shaderSelectionChanged(selName, *_infoStore);
}
}
// Callback to redraw the GL widget
bool ShaderSelector::onPreviewRender()
{
// Get the viewport size from the GL widget
wxSize req = _glWidget->GetClientSize();
if (req.GetWidth() == 0 || req.GetHeight() == 0) return false;
glViewport(0, 0, req.GetWidth(), req.GetHeight());
// Initialise
glClearColor(0.3f, 0.3f, 0.3f, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(0, req.GetWidth(), 0, req.GetHeight(), -100, 100);
glEnable (GL_TEXTURE_2D);
// Get the selected texture, and set up OpenGL to render it on
// the quad.
MaterialPtr shader = getSelectedShader();
bool drawQuad = false;
TexturePtr tex;
// Check what part of the shader we should display in the preview
if (_isLightTexture) {
// This is a light, take the first layer texture
const IShaderLayer* first = shader->firstLayer();
if (first != NULL) {
tex = shader->firstLayer()->getTexture();
glBindTexture (GL_TEXTURE_2D, tex->getGLTexNum());
drawQuad = true;
}
}
else {
// This is an "ordinary" texture, take the editor image
tex = shader->getEditorImage();
if (tex != NULL) {
glBindTexture (GL_TEXTURE_2D, tex->getGLTexNum());
drawQuad = true;
}
}
if (drawQuad)
{
// Calculate the correct aspect ratio for preview.
float aspect = float(tex->getWidth()) / float(tex->getHeight());
float hfWidth, hfHeight;
if (aspect > 1.0) {
hfWidth = 0.5*req.GetWidth();
hfHeight = 0.5*req.GetHeight() / aspect;
}
else {
hfHeight = 0.5*req.GetWidth();
hfWidth = 0.5*req.GetHeight() * aspect;
}
// Draw a quad to put the texture on
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL);
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
glTexCoord2i(0, 1);
glVertex2f(0.5*req.GetWidth() - hfWidth, 0.5*req.GetHeight() - hfHeight);
glTexCoord2i(1, 1);
glVertex2f(0.5*req.GetWidth() + hfWidth, 0.5*req.GetHeight() - hfHeight);
glTexCoord2i(1, 0);
glVertex2f(0.5*req.GetWidth() + hfWidth, 0.5*req.GetHeight() + hfHeight);
glTexCoord2i(0, 0);
glVertex2f(0.5*req.GetWidth() - hfWidth, 0.5*req.GetHeight() + hfHeight);
glEnd();
}
return true;
}
namespace
{
// Helper function
void addInfoItem(wxutil::TreeModel& listStore, const std::string& attr, const std::string& value,
int attrCol, int valueCol)
{
wxDataViewItemAttr bold;
bold.SetBold(true);
wxDataViewItem item = listStore.AddItem().getItem();
listStore.SetValue(attr, item, attrCol);
listStore.SetAttr(item, attrCol, bold);
listStore.SetValue(value, item, valueCol);
listStore.ItemAdded(listStore.GetRoot(), item);
}
}
void ShaderSelector::displayShaderInfo(const MaterialPtr& shader,
wxutil::TreeModel& listStore,
int attrCol, int valueCol)
{
// Update the infostore in the ShaderSelector
addInfoItem(listStore, _("Shader"), shader->getName(), attrCol, valueCol);
addInfoItem(listStore, _("Defined in"), shader->getShaderFileName(), attrCol, valueCol);
addInfoItem(listStore, _("Description"), shader->getDescription(), attrCol, valueCol);
}
void ShaderSelector::displayLightShaderInfo(const MaterialPtr& shader,
wxutil::TreeModel& listStore,
int attrCol, int valueCol)
{
const IShaderLayer* first = shader->firstLayer();
std::string texName = _("None");
if (first != NULL)
{
TexturePtr tex = shader->firstLayer()->getTexture();
texName = tex->getName();
}
addInfoItem(listStore, _("Image map"), texName, attrCol, valueCol);
addInfoItem(listStore, _("Defined in"), shader->getShaderFileName(), attrCol, valueCol);
// Light types, from the Material
std::string lightType;
if (shader->isAmbientLight())
lightType.append("ambient ");
if (shader->isBlendLight())
lightType.append("blend ");
if (shader->isFogLight())
lightType.append("fog");
if (lightType.size() == 0)
lightType.append("-");
addInfoItem(listStore, _("Light flags"), lightType, attrCol, valueCol);
std::string descr = shader->getDescription();
addInfoItem(listStore, _("Description"), descr.empty() ? "-" : descr, attrCol, valueCol);
}
// Callback for selection changed
void ShaderSelector::_onSelChange(wxDataViewEvent& ev)
{
updateInfoTable();
_glWidget->Refresh();
}
} // namespace ui