/
PatchInspector.cpp
573 lines (441 loc) · 15.2 KB
/
PatchInspector.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#include "PatchInspector.h"
#include "i18n.h"
#include "itextstream.h"
#include "imainframe.h"
#include "iundo.h"
#include <wx/sizer.h>
#include <wx/panel.h>
#include <wx/choice.h>
#include <wx/stattext.h>
#include <wx/textctrl.h>
#include <wx/spinctrl.h>
#include "wxutil/Bitmap.h"
#include "registry/Widgets.h"
#include "selectionlib.h"
#include "wxutil/ControlButton.h"
#include "util/ScopedBoolLock.h"
#include <functional>
namespace ui
{
namespace
{
const char* const WINDOW_TITLE = N_("Patch Inspector");
const char* const LABEL_STEP = N_("Step:");
const std::string RKEY_ROOT = "user/ui/patch/patchInspector/";
const std::string RKEY_WINDOW_STATE = RKEY_ROOT + "window";
const std::string RKEY_X_STEP = RKEY_ROOT + "xCoordStep";
const std::string RKEY_Y_STEP = RKEY_ROOT + "yCoordStep";
const std::string RKEY_Z_STEP = RKEY_ROOT + "zCoordStep";
const std::string RKEY_S_STEP = RKEY_ROOT + "sCoordStep";
const std::string RKEY_T_STEP = RKEY_ROOT + "tCoordStep";
}
PatchInspector::PatchInspector() :
TransientWindow(_(WINDOW_TITLE), GlobalMainFrame().getWxTopLevelWindow(), true),
_rowCombo(nullptr),
_colCombo(nullptr),
_selectionInfo(GlobalSelectionSystem().getSelectionInfo()),
_patchRows(0),
_patchCols(0),
_updateActive(false),
_updateNeeded(false)
{
Bind(wxEVT_IDLE, &PatchInspector::onIdle, this);
// Create all the widgets and pack them into the window
populateWindow();
InitialiseWindowPosition(410, 480, RKEY_WINDOW_STATE);
}
std::shared_ptr<PatchInspector>& PatchInspector::InstancePtr()
{
static std::shared_ptr<PatchInspector> _instancePtr;
return _instancePtr;
}
void PatchInspector::onMainFrameShuttingDown()
{
rMessage() << "PatchInspector shutting down." << std::endl;
if (IsShownOnScreen())
{
Hide();
}
// Destroy the window
SendDestroyEvent();
InstancePtr().reset();
}
PatchInspector& PatchInspector::Instance()
{
auto& instancePtr = InstancePtr();
if (!instancePtr)
{
// Not yet instantiated, do it now
instancePtr.reset(new PatchInspector);
// Pre-destruction cleanup
GlobalMainFrame().signal_MainFrameShuttingDown().connect(
sigc::mem_fun(*instancePtr, &PatchInspector::onMainFrameShuttingDown)
);
}
return *instancePtr;
}
void PatchInspector::populateWindow()
{
SetSizer(new wxBoxSizer(wxVERTICAL));
GetSizer()->Add(loadNamedPanel(this, "PatchInspectorMainPanel"), 1, wxEXPAND);
makeLabelBold(this, "PatchInspectorVertexLabel");
makeLabelBold(this, "PatchInspectorCoordLabel");
makeLabelBold(this, "PatchInspectorTessLabel");
_rowCombo = findNamedObject<wxChoice>(this, "PatchInspectorControlRow");
_colCombo = findNamedObject<wxChoice>(this, "PatchInspectorControlColumn");
// Create the controls table
wxPanel* coordPanel = findNamedObject<wxPanel>(this, "PatchInspectorCoordPanel");
wxFlexGridSizer* table = new wxFlexGridSizer(5, 5, 6, 16);
table->AddGrowableCol(1);
coordPanel->GetSizer()->Add(table, 1, wxEXPAND);
_coords["x"] = createCoordRow("X:", coordPanel, table);
_coords["y"] = createCoordRow("Y:", coordPanel, table);
_coords["z"] = createCoordRow("Z:", coordPanel, table);
_coords["s"] = createCoordRow("S:", coordPanel, table);
_coords["t"] = createCoordRow("T:", coordPanel, table);
// Connect the step values to the according registry values
registry::bindWidget(_coords["x"].stepEntry, RKEY_X_STEP);
registry::bindWidget(_coords["y"].stepEntry, RKEY_Y_STEP);
registry::bindWidget(_coords["z"].stepEntry, RKEY_Z_STEP);
registry::bindWidget(_coords["s"].stepEntry, RKEY_S_STEP);
registry::bindWidget(_coords["t"].stepEntry, RKEY_T_STEP);
// Connect all the arrow buttons
for (CoordMap::iterator i = _coords.begin(); i != _coords.end(); ++i)
{
CoordRow& row = i->second;
// Pass a CoordRow ref to the callback, that's all it will need to update
row.smaller->Bind(wxEVT_BUTTON, std::bind(&PatchInspector::onClickSmaller, this, row));
row.larger->Bind(wxEVT_BUTTON, std::bind(&PatchInspector::onClickLarger, this, row));
}
// Tesselation checkbox
findNamedObject<wxCheckBox>(this, "PatchInspectorFixedSubdivisions")->Connect(
wxEVT_CHECKBOX, wxCommandEventHandler(PatchInspector::onFixedTessChange), NULL, this);
// Tesselation values
findNamedObject<wxSpinCtrl>(this, "PatchInspectorSubdivisionsX")->Connect(
wxEVT_SPINCTRL, wxSpinEventHandler(PatchInspector::onTessChange), NULL, this);
findNamedObject<wxSpinCtrl>(this, "PatchInspectorSubdivisionsY")->Connect(
wxEVT_SPINCTRL, wxSpinEventHandler(PatchInspector::onTessChange), NULL, this);
}
PatchInspector::CoordRow PatchInspector::createCoordRow(
const std::string& label, wxPanel* parent, wxSizer* sizer)
{
CoordRow coordRow;
// Create the coordinate label
wxStaticText* coordLabel = new wxStaticText(parent, wxID_ANY, label);
coordRow.value = new wxTextCtrl(parent, wxID_ANY);
coordRow.value->SetMinClientSize(wxSize(coordRow.value->GetCharWidth() * 7, -1));
coordRow.value->Connect(wxEVT_TEXT, wxCommandEventHandler(PatchInspector::onCoordChange), NULL, this);
// Coord control
wxBoxSizer* controlButtonBox = new wxBoxSizer(wxHORIZONTAL);
controlButtonBox->SetMinSize(30, 30);
coordRow.smaller = new wxutil::ControlButton(parent,
wxutil::GetLocalBitmap("arrow_left.png"));
coordRow.smaller->SetMinSize(wxSize(15, 24));
controlButtonBox->Add(coordRow.smaller, 0);
coordRow.larger = new wxutil::ControlButton(parent,
wxutil::GetLocalBitmap("arrow_right.png"));
coordRow.larger->SetMinSize(wxSize(15, 24));
controlButtonBox->Add(coordRow.larger, 0);
// Create the step label
wxStaticText* steplabel = new wxStaticText(parent, wxID_ANY, _(LABEL_STEP));
// Create the step entry field
coordRow.stepEntry = new wxTextCtrl(parent, wxID_ANY);
coordRow.stepEntry->SetMinClientSize(wxSize(coordRow.stepEntry->GetCharWidth() * 5, -1));
sizer->Add(coordLabel, 0, wxALIGN_CENTRE_VERTICAL);
sizer->Add(coordRow.value, 0, wxALIGN_CENTRE_VERTICAL);
sizer->Add(controlButtonBox, 0, wxALIGN_CENTRE_VERTICAL);
sizer->Add(steplabel, 0, wxALIGN_CENTRE_VERTICAL);
sizer->Add(coordRow.stepEntry, 0, wxALIGN_CENTRE_VERTICAL);
// Return the filled structure
return coordRow;
}
void PatchInspector::queueUpdate()
{
// Request an idle callback to perform the update when GTK is idle
_updateNeeded = true;
}
void PatchInspector::update()
{
util::ScopedBoolLock lock(_updateActive);
_updateNeeded = false;
auto patch = _patch.lock();
if (patch)
{
// Check if the matrix size has changed
if (patch->getPatch().getWidth() != _patchCols ||
patch->getPatch().getHeight() != _patchRows)
{
// Patch matrix got changed
_patchRows = patch->getPatch().getHeight();
_patchCols = patch->getPatch().getWidth();
clearVertexChooser();
repopulateVertexChooser();
}
// Load the data from the currently selected vertex
loadControlVertex();
}
}
void PatchInspector::loadControlVertex()
{
auto patch = _patch.lock();
if (patch)
{
int row = _rowCombo->GetSelection();
int col = _colCombo->GetSelection();
// Retrieve the controlvertex
const PatchControl& ctrl = patch->getPatch().ctrlAt(row, col);
_updateActive = true;
_coords["x"].value->SetValue(string::to_string(ctrl.vertex[0]));
_coords["y"].value->SetValue(string::to_string(ctrl.vertex[1]));
_coords["z"].value->SetValue(string::to_string(ctrl.vertex[2]));
_coords["s"].value->SetValue(string::to_string(ctrl.texcoord[0]));
_coords["t"].value->SetValue(string::to_string(ctrl.texcoord[1]));
_updateActive = false;
}
}
// Pre-hide callback
void PatchInspector::_preHide()
{
TransientWindow::_preHide();
// Clear the patch, we don't need to observe it while hidden
setPatch(IPatchNodePtr());
// A hidden PatchInspector doesn't need to listen for events
_undoHandler.disconnect();
_redoHandler.disconnect();
GlobalSelectionSystem().removeObserver(this);
}
// Pre-show callback
void PatchInspector::_preShow()
{
TransientWindow::_preShow();
_undoHandler.disconnect();
_redoHandler.disconnect();
// Register self to the SelSystem to get notified upon selection changes.
GlobalSelectionSystem().addObserver(this);
_undoHandler = GlobalUndoSystem().signal_postUndo().connect(
sigc::mem_fun(this, &PatchInspector::queueUpdate));
_redoHandler = GlobalUndoSystem().signal_postRedo().connect(
sigc::mem_fun(this, &PatchInspector::queueUpdate));
// Check for selection changes before showing the dialog again
rescanSelection();
}
void PatchInspector::selectionChanged(const scene::INodePtr& node, bool isComponent)
{
if (!isComponent)
{
rescanSelection();
}
}
void PatchInspector::clearVertexChooser()
{
_updateActive = true;
// Remove all the items from the combo boxes
_rowCombo->Clear();
_colCombo->Clear();
_updateActive = false;
}
void PatchInspector::setPatch(const IPatchNodePtr& newPatch)
{
// Detach if we had a previous patch
auto patch = _patch.lock();
if (patch)
{
patch->getPatch().detachObserver(this);
}
// Clear vertex chooser while _patchRows/_patchCols are still set
clearVertexChooser();
_patch = newPatch;
if (newPatch)
{
newPatch->getPatch().attachObserver(this);
_patchRows = newPatch->getPatch().getHeight();
_patchCols = newPatch->getPatch().getWidth();
// Now that rows/cols are known, build lists
repopulateVertexChooser();
}
else
{
_patchRows = 0;
_patchCols = 0;
}
}
void PatchInspector::rescanSelection()
{
// Check if there is one distinct patch selected
bool sensitive = (_selectionInfo.patchCount == 1);
findNamedObject<wxPanel>(this, "PatchInspectorVertexPanel")->Enable(sensitive);
findNamedObject<wxPanel>(this, "PatchInspectorCoordPanel")->Enable(sensitive);
// Tesselation is always sensitive when one or more patches are selected
findNamedObject<wxPanel>(this, "PatchInspectorTessPanel")->Enable(_selectionInfo.patchCount > 0);
// Clear the patch reference
setPatch(IPatchNodePtr());
if (_selectionInfo.patchCount > 0)
{
Subdivisions tess(UINT_MAX, UINT_MAX);
bool tessIsFixed = false;
bool continueSearching = true;
IPatchNodePtr patchNode;
// Try to find a pair of same tesselation values
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& node)
{
if (!continueSearching || !Node_isPatch(node)) return;
auto& patch = *Node_getIPatch(node);
if (!patchNode)
{
patchNode = std::dynamic_pointer_cast<IPatchNode>(node);
}
if (tess.x() == UINT_MAX)
{
// Not initialised yet, take these values for starters
tessIsFixed = patch.subdivisionsFixed();
tess = patch.getSubdivisions();
}
else
{
// We already have a pair of divisions, compare
Subdivisions otherTess = patch.getSubdivisions();
if (tessIsFixed != patch.subdivisionsFixed() || otherTess != tess)
{
// Our journey ends here, we cannot find a pair of tesselations
// for all selected patches or the same fixed/variable status
tessIsFixed = false;
continueSearching = false;
}
}
});
_updateActive = true;
// Load the "fixed tesselation" value
findNamedObject<wxCheckBox>(this, "PatchInspectorFixedSubdivisions")->SetValue(tessIsFixed);
wxSpinCtrl* fixedSubdivX = findNamedObject<wxSpinCtrl>(this, "PatchInspectorSubdivisionsX");
wxSpinCtrl* fixedSubdivY = findNamedObject<wxSpinCtrl>(this, "PatchInspectorSubdivisionsY");
fixedSubdivX->SetValue(tess[0]);
fixedSubdivY->SetValue(tess[1]);
fixedSubdivX->Enable(tessIsFixed);
fixedSubdivY->Enable(tessIsFixed);
findNamedObject<wxStaticText>(this, "PatchInspectorSubdivisionsXLabel")->Enable(tessIsFixed);
findNamedObject<wxStaticText>(this, "PatchInspectorSubdivisionsYLabel")->Enable(tessIsFixed);
if (_selectionInfo.patchCount == 1)
{
setPatch(patchNode);
}
_updateActive = false;
}
update();
}
void PatchInspector::repopulateVertexChooser()
{
_updateActive = true;
for (std::size_t i = 0; i < _patchRows; ++i)
{
_rowCombo->AppendString(string::to_string(i));
}
_rowCombo->Select(0);
for (std::size_t i = 0; i < _patchCols; ++i)
{
_colCombo->AppendString(string::to_string(i));
}
_colCombo->Select(0);
_updateActive = false;
}
void PatchInspector::emitCoords()
{
auto patch = _patch.lock();
if (!patch) return;
// Save the coords into the patch
UndoableCommand emitCoordsCmd("patchAdjustControlVertex");
patch->getPatch().undoSave();
int row = _rowCombo->GetSelection();
int col = _colCombo->GetSelection();
// Retrieve the controlvertex
PatchControl& ctrl = patch->getPatch().ctrlAt(row, col);
ctrl.vertex[0] = string::convert<float>(_coords["x"].value->GetValue().ToStdString());
ctrl.vertex[1] = string::convert<float>(_coords["y"].value->GetValue().ToStdString());
ctrl.vertex[2] = string::convert<float>(_coords["z"].value->GetValue().ToStdString());
ctrl.texcoord[0] = string::convert<float>(_coords["s"].value->GetValue().ToStdString());
ctrl.texcoord[1] = string::convert<float>(_coords["t"].value->GetValue().ToStdString());
patch->getPatch().controlPointsChanged();
GlobalMainFrame().updateAllWindows();
}
void PatchInspector::emitTesselation()
{
UndoableCommand setFixedTessCmd("patchSetFixedTesselation");
wxSpinCtrl* fixedSubdivX = findNamedObject<wxSpinCtrl>(this, "PatchInspectorSubdivisionsX");
wxSpinCtrl* fixedSubdivY = findNamedObject<wxSpinCtrl>(this, "PatchInspectorSubdivisionsY");
Subdivisions tess(
fixedSubdivX->GetValue(),
fixedSubdivY->GetValue()
);
bool fixed = findNamedObject<wxCheckBox>(this, "PatchInspectorFixedSubdivisions")->GetValue();
// Save the setting into the selected patch(es)
GlobalSelectionSystem().foreachPatch([&] (IPatch& patch)
{
patch.setFixedSubdivisions(fixed, tess);
});
fixedSubdivX->Enable(fixed);
fixedSubdivY->Enable(fixed);
findNamedObject<wxStaticText>(this, "PatchInspectorSubdivisionsXLabel")->Enable(fixed);
findNamedObject<wxStaticText>(this, "PatchInspectorSubdivisionsYLabel")->Enable(fixed);
GlobalMainFrame().updateAllWindows();
}
void PatchInspector::onCoordChange(wxCommandEvent& ev)
{
if (_updateActive) return;
emitCoords();
}
void PatchInspector::onTessChange(wxSpinEvent& ev)
{
if (_updateActive) return;
emitTesselation();
}
void PatchInspector::onFixedTessChange(wxCommandEvent& ev)
{
if (_updateActive) return;
emitTesselation();
}
void PatchInspector::onComboBoxChange()
{
if (_updateActive) return;
// Load the according patch row/col vertex
loadControlVertex();
}
void PatchInspector::onClickLarger(CoordRow& row)
{
// Get the current value and the step increment
float value = string::convert<float>(row.value->GetValue().ToStdString());
float step = string::convert<float>(row.stepEntry->GetValue().ToStdString());
// This triggers the onCoordChange callback method
row.value->SetValue(string::to_string(value + step));
}
void PatchInspector::onClickSmaller(CoordRow& row)
{
// Get the current value and the step increment
float value = string::convert<float>(row.value->GetValue().ToStdString());
float step = string::convert<float>(row.stepEntry->GetValue().ToStdString());
// This triggers the onCoordChange callback method
row.value->SetValue(string::to_string(value - step));
}
// static command target
void PatchInspector::toggle(const cmd::ArgumentList& args)
{
Instance().ToggleVisibility();
}
void PatchInspector::onPatchControlPointsChanged()
{
queueUpdate();
}
void PatchInspector::onPatchTextureChanged()
{
queueUpdate();
}
void PatchInspector::onPatchDestruction()
{
rescanSelection();
}
void PatchInspector::onIdle(wxIdleEvent& ev)
{
if (_updateNeeded)
{
update();
}
}
} // namespace ui