/
ShaderSelector.cpp
411 lines (333 loc) · 11.6 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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include "ShaderSelector.h"
#include "i18n.h"
#include "wxutil/dataview/TreeModel.h"
#include "wxutil/dataview/TreeView.h"
#include "wxutil/dataview/VFSTreePopulator.h"
#include "wxutil/menu/IconTextMenuItem.h"
#include "ui/materials/MaterialDefinitionView.h"
#include "texturelib.h"
#include "string/string.h"
#include "ishaders.h"
#include "iregistry.h"
#include <wx/dataview.h>
#include <wx/sizer.h>
#include "wxutil/Bitmap.h"
#include <GL/glew.h>
#include <vector>
#include <string>
#include <map>
#include "string/split.h"
#include "string/predicate.h"
#include "string/case_conv.h"
#include <functional>
namespace ui
{
/* CONSTANTS */
namespace
{
const char* const FOLDER_ICON = "folder16.png";
const char* const TEXTURE_ICON = "icon_texture.png";
constexpr const char* const SHOW_MATERIAL_DEF_TEXT = N_("Show Material Definition");
constexpr const char* const SHOW_MATERIAL_DEF_ICON = "icon_script.png";
}
// Constructor creates elements
ShaderSelector::ShaderSelector(wxWindow* parent, Client* client, const std::string& prefixes, bool isLightTexture) :
wxPanel(parent, wxID_ANY),
_treeView(NULL),
_treeStore(NULL),
_glWidget(NULL),
_client(client),
_isLightTexture(isLightTexture),
_infoStore(new wxutil::TreeModel(_infoStoreColumns, true)), // is a listmodel
_contextMenu(new wxutil::PopupMenu)
{
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();
// Construct the context menu
_contextMenu->addItem(
new wxutil::IconTextMenuItem(_(SHOW_MATERIAL_DEF_TEXT), SHOW_MATERIAL_DEF_ICON),
std::bind(&ShaderSelector::_onShowShaderDefinition, this),
[this]() { return !getSelection().empty(); }
);
}
void ShaderSelector::_onShowShaderDefinition()
{
std::string shaderName = getSelection();
// Construct a definition view and pass the shader name
auto view = new MaterialDefinitionView(shaderName);
view->ShowModal();
view->Destroy();
}
// Return the selection to the calling code
std::string ShaderSelector::getSelection()
{
wxDataViewItem item = _treeView->GetSelection();
if (!item.IsOk()) return "";
wxutil::TreeModel::Row row(item, *_treeView->GetModel());
return row[_shaderTreeColumns.shaderName];
}
// Set the selection in the treeview
void ShaderSelector::setSelection(const std::string& sel)
{
// If the selection string is empty, collapse the treeview and return with
// no selection
if (sel.empty())
{
_treeView->UnselectAll();
_treeView->Collapse(_treeStore->GetRoot());
return;
}
// Use the wxutil TreeModel algorithms to select the shader
wxDataViewItem found = _treeStore->FindString(
string::to_lower_copy(sel), _shaderTreeColumns.shaderName);
if (found.IsOk())
{
_treeView->Select(found);
_treeView->EnsureVisible(found);
}
}
namespace
{
// VFSPopulatorVisitor to fill in column data for the populator tree nodes
class DataInserter :
public wxutil::VFSTreePopulator::Visitor
{
private:
const ShaderSelector::ShaderTreeColumns& _columns;
wxIcon _folderIcon;
wxIcon _textureIcon;
public:
DataInserter(const ShaderSelector::ShaderTreeColumns& columns) :
_columns(columns)
{
_folderIcon.CopyFromBitmap(
wxutil::GetLocalBitmap(FOLDER_ICON));
_textureIcon.CopyFromBitmap(
wxutil::GetLocalBitmap(TEXTURE_ICON));
}
virtual ~DataInserter() {}
// Required visit function
void visit(wxutil::TreeModel& /* store */,
wxutil::TreeModel::Row& row,
const std::string& path,
bool isExplicit)
{
// Get the display name by stripping off everything before the last
// slash
std::string displayName = path.substr(path.rfind("/") + 1);
// Pathname is the model VFS name for a model, and blank for a folder
std::string fullPath = isExplicit ? path : "";
// Fill in the column values
row[_columns.iconAndName] = wxVariant(
wxDataViewIconText(displayName, isExplicit ? _textureIcon : _folderIcon));
row[_columns.shaderName] = fullPath;
}
};
}
// Create the Tree View
void ShaderSelector::createTreeView()
{
_treeStore = new wxutil::TreeModel(_shaderTreeColumns);
// Instantiate the helper class that populates the tree according to the paths
wxutil::VFSTreePopulator populator(_treeStore);
// Iterate over material names and pass matching shaders to the populator
GlobalMaterialManager().foreachShaderName(
[&](const std::string& shaderName)
{
for (const std::string& prefix : _prefixes)
{
if (!shaderName.empty() && string::istarts_with(shaderName, prefix + "/"))
{
populator.addPath(string::to_lower_copy(shaderName));
break; // don't consider any further prefixes
}
}
}
);
// Now visit the created iterators to load the actual data into the tree
DataInserter inserter(_shaderTreeColumns);
populator.forEachNode(inserter);
// Tree view
_treeView = wxutil::TreeView::CreateWithModel(this, _treeStore.get(), 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.iconAndName);
// Get selection and connect the changed callback
_treeView->Bind(wxEVT_DATAVIEW_SELECTION_CHANGED, &ShaderSelector::_onSelChange, this);
_treeView->Bind(wxEVT_DATAVIEW_ITEM_CONTEXT_MENU, &ShaderSelector::_onContextMenu, this);
GetSizer()->Add(_treeView, 1, wxEXPAND);
}
void ShaderSelector::_onContextMenu(wxDataViewEvent& ev)
{
_contextMenu->show(_treeView);
}
// 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