/
LightInspector.cpp
582 lines (483 loc) · 18.1 KB
/
LightInspector.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
574
575
576
577
578
579
580
581
582
#include "LightInspector.h"
#include "i18n.h"
#include "ientity.h"
#include "ieclass.h"
#include "igame.h"
#include "ishaders.h"
#include "iradiant.h"
#include "imainframe.h"
#include "iselection.h"
#include "iundo.h"
#include <wx/tglbtn.h>
#include <wx/clrpicker.h>
#include <wx/checkbox.h>
#include <wx/artprov.h>
#include <wx/stattext.h>
#include <wx/radiobut.h>
#include "ui/common/ShaderChooser.h" // for static displayLightInfo() function
#include "util/ScopedBoolLock.h"
#include "gamelib.h"
namespace ui
{
/* CONSTANTS */
namespace
{
const char* LIGHTINSPECTOR_TITLE = N_("Light properties");
const std::string RKEY_WINDOW_STATE = "user/ui/lightInspector/window";
const char* LIGHT_PREFIX_XPATH = "/light/texture//prefix";
/** greebo: Loads the prefixes from the registry and creates a
* comma-separated list string
*/
inline std::string getPrefixList() {
std::string prefixes;
// Get the list of light texture prefixes from the registry
xml::NodeList prefList = GlobalGameManager().currentGame()->getLocalXPath(LIGHT_PREFIX_XPATH);
// Copy the Node contents into the prefix vector
for (xml::NodeList::iterator i = prefList.begin();
i != prefList.end();
++i)
{
prefixes += (prefixes.empty()) ? "" : ",";
prefixes += i->getContent();
}
return prefixes;
}
}
// Private constructor sets up dialog
LightInspector::LightInspector() :
wxutil::TransientWindow(_(LIGHTINSPECTOR_TITLE), GlobalMainFrame().getWxTopLevelWindow(), true),
_isProjected(false),
_texSelector(nullptr),
_updateActive(false),
_supportsAiSee(game::current::getValue<bool>("/light/supportsAiSeeSpawnarg", false))
{
// Load XRC panel and access widgets
wxPanel* contents = loadNamedPanel(this, "LightInspectorMainPanel");
_brightnessSlider = findNamedObject<wxSlider>(this, "BrightnessSlider");
setupLightShapeOptions();
setupOptionsPanel();
setupTextureWidgets();
makeLabelBold(this, "LightInspectorVolumeLabel");
makeLabelBold(this, "LightInspectorColourLabel");
makeLabelBold(this, "LightInspectorOptionsLabel");
SetMinSize(contents->GetEffectiveMinSize());
InitialiseWindowPosition(600, 360, RKEY_WINDOW_STATE);
}
LightInspectorPtr& LightInspector::InstancePtr()
{
static LightInspectorPtr _instancePtr;
return _instancePtr;
}
void LightInspector::onMainFrameShuttingDown()
{
if (IsShownOnScreen())
{
Hide();
}
// Destroy the window
SendDestroyEvent();
InstancePtr().reset();
}
void LightInspector::shaderSelectionChanged(
const std::string& shader,
wxutil::TreeModel& listStore)
{
// Get the shader, and its image map if possible
MaterialPtr ishader = _texSelector->getSelectedShader();
// Pass the call to the static member of ShaderSelector
ShaderSelector::displayLightShaderInfo(ishader, listStore);
// greebo: Do not write to the entities if this call resulted from an update()
if (_updateActive) return;
std::string commandStr("setLightTexture: ");
commandStr += _texSelector->getSelection();
UndoableCommand command(commandStr);
// Write the texture key
setKeyValueAllLights("texture", _texSelector->getSelection());
}
// Set up the point/projected light options
void LightInspector::setupLightShapeOptions()
{
// wxRadioButton does not emit a signal when it becomes UN-checked, so we
// must connect to both buttons.
wxRadioButton* omni = findNamedObject<wxRadioButton>(this, "omniRbtn");
omni->SetValue(true);
omni->Connect(wxEVT_RADIOBUTTON,
wxCommandEventHandler(LightInspector::_onProjToggle),
NULL, this);
wxRadioButton* proj = findNamedObject<wxRadioButton>(this, "projectedRbtn");
proj->Connect(wxEVT_RADIOBUTTON,
wxCommandEventHandler(LightInspector::_onProjToggle), NULL,
this);
// Start/end checkbox
wxControl* startEnd = findNamedObject<wxCheckBox>(this, "LightInspectorStartEnd");
startEnd->Connect(
wxEVT_CHECKBOX, wxCommandEventHandler(LightInspector::_onOptionsToggle),
NULL, this
);
startEnd->Enable(false);
}
// Connect the options checkboxes
void LightInspector::setupOptionsPanel()
{
// Colour and brightness
findNamedObject<wxColourPickerCtrl>(this, "LightInspectorColour")->Bind(
wxEVT_COLOURPICKER_CHANGED, &LightInspector::_onColourChange, this
);
_brightnessSlider->Bind(
wxEVT_SLIDER, [=](wxCommandEvent&) { adjustBrightness(); }
);
findNamedObject<wxCheckBox>(this, "LightInspectorParallel")->Bind(wxEVT_CHECKBOX, &LightInspector::_onOptionsToggle, this);
findNamedObject<wxCheckBox>(this, "LightInspectorNoShadows")->Bind(wxEVT_CHECKBOX, &LightInspector::_onOptionsToggle, this);
findNamedObject<wxCheckBox>(this, "LightInspectorSkipSpecular")->Bind(wxEVT_CHECKBOX, &LightInspector::_onOptionsToggle, this);
findNamedObject<wxCheckBox>(this, "LightInspectorSkipDiffuse")->Bind(wxEVT_CHECKBOX, &LightInspector::_onOptionsToggle, this);
if (_supportsAiSee)
{
findNamedObject<wxCheckBox>(this, "LightInspectorAiSee")->Show();
findNamedObject<wxCheckBox>(this, "LightInspectorAiSee")->Bind(wxEVT_CHECKBOX, &LightInspector::_onOptionsToggle, this);
}
else
{
findNamedObject<wxCheckBox>(this, "LightInspectorAiSee")->Hide();
}
}
// Create the texture widgets
void LightInspector::setupTextureWidgets()
{
wxPanel* parent = findNamedObject<wxPanel>(this, "LightInspectorChooserPanel");
_texSelector = new ShaderSelector(parent, this, getPrefixList(), true);
parent->GetSizer()->Add(_texSelector, 1, wxEXPAND);
}
// Update dialog from map
void LightInspector::update()
{
// Clear the list of light entities
_lightEntities.clear();
// Find all selected objects which are lights, and add them to our list of
// light entities
class LightEntityFinder
: public SelectionSystem::Visitor
{
// List of light entities to add to
EntityList& _entityList;
public:
// Constructor initialises entity list
LightEntityFinder(EntityList& list)
: _entityList(list)
{ }
// Required visit method
void visit(const scene::INodePtr& node) const
{
Entity* ent = Node_getEntity(node);
if (ent && ent->getEntityClass()->isLight())
{
// Add light to list
_entityList.push_back(ent);
}
}
};
LightEntityFinder lightFinder(_lightEntities);
// Find the selected lights
GlobalSelectionSystem().foreachSelected(lightFinder);
wxPanel* panel = findNamedObject<wxPanel>(this, "LightInspectorMainPanel");
if (!_lightEntities.empty())
{
// Update the dialog with the correct values from the first entity
getValuesFromEntity();
// Enable the dialog
panel->Enable(true);
}
else
{
// Nothing found, disable the dialog
panel->Enable(false);
}
}
// Pre-hide callback
void LightInspector::_preHide()
{
TransientWindow::_preHide();
// Remove as observer, an invisible inspector doesn't need to receive events
_selectionChanged.disconnect();
_undoHandler.disconnect();
_redoHandler.disconnect();
}
// Pre-show callback
void LightInspector::_preShow()
{
TransientWindow::_preShow();
_selectionChanged.disconnect();
_undoHandler.disconnect();
_redoHandler.disconnect();
// Register self as observer to receive events
_undoHandler = GlobalUndoSystem().signal_postUndo().connect(
sigc::mem_fun(this, &LightInspector::update));
_redoHandler = GlobalUndoSystem().signal_postRedo().connect(
sigc::mem_fun(this, &LightInspector::update));
// Register self to the SelSystem to get notified upon selection changes.
_selectionChanged = GlobalSelectionSystem().signal_selectionChanged().connect(
[this](const ISelectable&) { update(); });
// Update the widgets before showing
update();
}
// Static method to toggle the dialog
void LightInspector::toggleInspector(const cmd::ArgumentList& args)
{
// Toggle the instance
Instance().ToggleVisibility();
}
LightInspector& LightInspector::Instance()
{
LightInspectorPtr& instancePtr = InstancePtr();
if (instancePtr == NULL)
{
// Not yet instantiated, do it now
instancePtr.reset(new LightInspector);
// Pre-destruction cleanup
GlobalMainFrame().signal_MainFrameShuttingDown().connect(
sigc::mem_fun(*instancePtr, &LightInspector::onMainFrameShuttingDown)
);
}
return *instancePtr;
}
// CALLBACKS
void LightInspector::updateLightShapeWidgets()
{
// Set radio button values. wxWidgets only allows setting a radio button,
// not clearing it.
wxRadioButton* omni = findNamedObject<wxRadioButton>(this, "omniRbtn");
wxRadioButton* proj = findNamedObject<wxRadioButton>(this, "projectedRbtn");
if (_isProjected)
proj->SetValue(true);
else
omni->SetValue(true);
// Enable start/end checkbox if the light is projected
findNamedObject<wxCheckBox>(this, "LightInspectorStartEnd")->Enable(_isProjected);
}
void LightInspector::_onProjToggle(wxCommandEvent& ev)
{
if (_updateActive) return; // avoid callback loops
// Set the projected flag
_isProjected = findNamedObject<wxRadioButton>(this, "projectedRbtn")->GetValue();
// Set button state based on the value of the flag
findNamedObject<wxCheckBox>(this, "LightInspectorStartEnd")->Enable(_isProjected);
writeToAllEntities();
}
namespace
{
// Return highest RGB component from a given colour
float highestComponent(const Vector3& colour)
{
float highest = colour.x();
if (colour.y() > highest) highest = colour.y();
if (colour.z() > highest) highest = colour.z();
return highest;
}
// Get colour of entity as float vector [0.0 - 1.0]
Vector3 entityColour(const Entity& entity)
{
// If the light has no colour key, use a default of white rather than
// the Vector3 default of black (0, 0, 0).
std::string colString = entity.getKeyValue("_color");
if (colString.empty())
{
colString = "1.0 1.0 1.0";
}
return string::convert<Vector3>(colString);
}
inline void setEntityValueIfDifferent(Entity* entity,
const std::string& key,
const std::string& value)
{
// Only set the values if the entity carries different ones
// to avoid triggering lots of undo system state savings
if (entity->getKeyValue(key) != value)
{
entity->setKeyValue(key, value);
}
}
// Set colour on entity
void setEntityColour(Entity* entity, const Vector3& col)
{
setEntityValueIfDifferent(
entity, "_color",
fmt::format("{:.3f} {:.3f} {:.3f}", col.x(), col.y(), col.z())
);
}
}
void LightInspector::updateColourWidgets(const Entity& entity)
{
Vector3 colour = entityColour(entity);
colour *= 255;
// Set colour chooser button
findNamedObject<wxColourPickerCtrl>(this, "LightInspectorColour")->
SetColour(wxColour(colour[0], colour[1], colour[2]));
// Set brightness slider based on the brightest channel
float highest = highestComponent(colour);
_brightnessSlider->SetValue(highest * 100.f / 255.f);
}
// Get keyvals from entity and insert into text entries
void LightInspector::getValuesFromEntity()
{
// Disable unwanted callbacks
util::ScopedBoolLock updateLock(_updateActive);
// Read values from the first entity in the list of lights (we have to pick
// one, so might as well use the first).
assert(!_lightEntities.empty());
Entity* entity = _lightEntities[0];
// Populate the value map with defaults
_valueMap["light_radius"] = "320 320 320";
_valueMap["light_center"] = "0 0 0";
_valueMap["light_target"] = "0 0 -256";
_valueMap["light_right"] = "128 0 0";
_valueMap["light_up"] = "0 128 0";
_valueMap["light_start"] = "0 0 -64";
_valueMap["light_end"] = "0 0 -256";
// Now load values from entity, overwriting the defaults if the value is
// set
for (auto& pair : _valueMap)
{
// Overwrite the map value if the key exists on the entity
std::string val = entity->getKeyValue(pair.first);
if (!val.empty())
{
pair.second = val;
}
}
updateColourWidgets(*entity);
// Set the texture selection from the "texture" key
_texSelector->setSelection(entity->getKeyValue("texture"));
// Determine whether this is a projected light, and set the toggles
// appropriately
_isProjected = (!entity->getKeyValue("light_target").empty() &&
!entity->getKeyValue("light_right").empty() &&
!entity->getKeyValue("light_up").empty());
updateLightShapeWidgets();
wxCheckBox* useStartEnd = findNamedObject<wxCheckBox>(this, "LightInspectorStartEnd");
// If this entity has light_start and light_end keys, set the checkbox
if (!entity->getKeyValue("light_start").empty()
&& !entity->getKeyValue("light_end").empty())
{
useStartEnd->SetValue(true);
}
else
{
useStartEnd->SetValue(false);
}
// Set the options checkboxes
findNamedObject<wxCheckBox>(this, "LightInspectorParallel")->SetValue(entity->getKeyValue("parallel") == "1");
findNamedObject<wxCheckBox>(this, "LightInspectorSkipSpecular")->SetValue(entity->getKeyValue("nospecular") == "1");
findNamedObject<wxCheckBox>(this, "LightInspectorSkipDiffuse")->SetValue(entity->getKeyValue("nodiffuse") == "1");
findNamedObject<wxCheckBox>(this, "LightInspectorNoShadows")->SetValue(entity->getKeyValue("noshadows") == "1");
if (_supportsAiSee)
{
findNamedObject<wxCheckBox>(this, "LightInspectorAiSee")->SetValue(entity->getKeyValue("ai_see") == "1");
}
}
void LightInspector::adjustBrightness() const
{
for (auto light: _lightEntities)
{
// Get existing colour
Vector3 colour = entityColour(*light);
// The slider represents the absolute brightness of the highest
// component (which means that 100% sets that component to 1.0, and it
// is hopefully not possible to overbrighten and lose colour
// information).
float origHighest = highestComponent(colour);
float newHighest = std::max(_brightnessSlider->GetValue(), 1) / 100.f;
Vector3 newColour;
if (origHighest > 0.0f)
{
float ratio = newHighest / origHighest;
newColour = colour * ratio;
}
else
{
// No point in trying to brighten black, just set a grey value based
// on the brightness value
newColour = Vector3(newHighest, newHighest, newHighest);
}
setEntityColour(light, newColour);
}
// Update camera immediately to provide user feedback
GlobalCameraManager().getActiveView().queueDraw();
}
// Write to all entities
void LightInspector::writeToAllEntities()
{
UndoableCommand command("setLightProperties");
for (auto entity : _lightEntities)
{
setValuesOnEntity(entity);
}
}
// Set a given key value on all light entities
void LightInspector::setKeyValueAllLights(const std::string& key,
const std::string& value)
{
for (auto entity : _lightEntities)
{
entity->setKeyValue(key, value);
}
}
// Set the keyvalues on the entity from the dialog widgets
void LightInspector::setValuesOnEntity(Entity* entity)
{
// Set the "_color" keyvalue
wxColour col = findNamedObject<wxColourPickerCtrl>(this, "LightInspectorColour")->GetColour();
Vector3 colFloat(col.Red() / 255.0f, col.Green() / 255.0f,
col.Blue() / 255.0f);
setEntityColour(entity, colFloat);
// Write out all vectors to the entity
for (const auto& pair : _valueMap)
{
// Only set the values if the entity carries different ones
// to avoid triggering lots of undo system state savings
setEntityValueIfDifferent(entity, pair.first, pair.second);
}
// Remove vector keys that should not exist, depending on the lightvolume
// options
if (_isProjected)
{
// Clear start/end vectors if checkbox is disabled
if (!findNamedObject<wxCheckBox>(this, "LightInspectorStartEnd")->GetValue())
{
setEntityValueIfDifferent(entity, "light_start", "");
setEntityValueIfDifferent(entity, "light_end", "");
}
// Blank out pointlight values
setEntityValueIfDifferent(entity, "light_radius", "");
setEntityValueIfDifferent(entity, "light_center", "");
}
else
{
// Blank out projected light values
setEntityValueIfDifferent(entity, "light_target", "");
setEntityValueIfDifferent(entity, "light_right", "");
setEntityValueIfDifferent(entity, "light_up", "");
setEntityValueIfDifferent(entity, "light_start", "");
setEntityValueIfDifferent(entity, "light_end", "");
}
// Write the texture key
setEntityValueIfDifferent(entity, "texture", _texSelector->getSelection());
// Write the options
setEntityValueIfDifferent(entity, "parallel", findNamedObject<wxCheckBox>(this, "LightInspectorParallel")->GetValue() ? "1" : "0");
setEntityValueIfDifferent(entity, "nospecular", findNamedObject<wxCheckBox>(this, "LightInspectorSkipSpecular")->GetValue() ? "1" : "0");
setEntityValueIfDifferent(entity, "nodiffuse", findNamedObject<wxCheckBox>(this, "LightInspectorSkipDiffuse")->GetValue() ? "1" : "0");
setEntityValueIfDifferent(entity, "noshadows", findNamedObject<wxCheckBox>(this, "LightInspectorNoShadows")->GetValue() ? "1" : "0");
if (_supportsAiSee)
{
setEntityValueIfDifferent(entity, "ai_see", findNamedObject<wxCheckBox>(this, "LightInspectorAiSee")->GetValue() ? "1" : "0");
}
}
void LightInspector::_onOptionsToggle(wxCommandEvent& ev)
{
if (_updateActive) return; // avoid callback loops
writeToAllEntities();
}
void LightInspector::_onColourChange(wxColourPickerEvent& ev)
{
if (_updateActive) return; // avoid callback loops
writeToAllEntities();
}
} // namespace ui