/
LayerControlDialog.cpp
373 lines (292 loc) · 9.46 KB
/
LayerControlDialog.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
#include "LayerControlDialog.h"
#include "i18n.h"
#include "itextstream.h"
#include "ieventmanager.h"
#include "ilayer.h"
#include "imainframe.h"
#include "iselection.h"
#include "registry/registry.h"
#include <wx/button.h>
#include <wx/bmpbuttn.h>
#include <wx/tglbtn.h>
#include <wx/sizer.h>
#include <wx/panel.h>
#include <wx/artprov.h>
#include <functional>
#include "wxutil/ScrollWindow.h"
#include "wxutil/Button.h"
#include "layers/LayerManager.h"
#include "layers/LayerUsageBreakdown.h"
namespace ui
{
namespace
{
const std::string RKEY_ROOT = "user/ui/layers/controlDialog/";
const std::string RKEY_WINDOW_STATE = RKEY_ROOT + "window";
}
LayerControlDialog::LayerControlDialog() :
TransientWindow(_("Layers"), GlobalMainFrame().getWxTopLevelWindow(), true),
_dialogPanel(nullptr),
_controlContainer(nullptr),
_showAllLayers(nullptr),
_hideAllLayers(nullptr),
_rescanSelectionOnIdle(false)
{
populateWindow();
Bind(wxEVT_IDLE, [&](wxIdleEvent&) { onIdle(); });
InitialiseWindowPosition(230, 400, RKEY_WINDOW_STATE);
SetMinClientSize(wxSize(230, 200));
}
void LayerControlDialog::populateWindow()
{
auto dialogPanel = new wxutil::ScrollWindow(this, wxID_ANY);
dialogPanel->SetShouldScrollToChildOnFocus(false);
dialogPanel->SetScrollRate(0, 15);
_dialogPanel = dialogPanel;
_dialogPanel->SetSizer(new wxBoxSizer(wxVERTICAL));
_controlContainer = new wxFlexGridSizer(1, 4, 3, 3);
_controlContainer->AddGrowableCol(2);
_dialogPanel->GetSizer()->Add(_controlContainer, 1, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 12);
// Add the option buttons ("Create Layer", etc.) to the window
createButtons();
_dialogPanel->FitInside(); // ask the sizer about the needed size
}
void LayerControlDialog::createButtons()
{
// Show all / hide all buttons
wxBoxSizer* hideShowBox = new wxBoxSizer(wxHORIZONTAL);
_showAllLayers = new wxButton(_dialogPanel, wxID_ANY, _("Show all"));
_hideAllLayers = new wxButton(_dialogPanel, wxID_ANY, _("Hide all"));
_showAllLayers->Bind(wxEVT_BUTTON, &LayerControlDialog::onShowAllLayers, this);
_hideAllLayers->Bind(wxEVT_BUTTON, &LayerControlDialog::onHideAllLayers, this);
// Create layer button
wxButton* createButton = new wxButton(_dialogPanel, wxID_ANY, _("New"));
createButton->SetBitmap(wxArtProvider::GetBitmap(wxART_PLUS));
wxutil::button::connectToCommand(createButton, "CreateNewLayer");
hideShowBox->Add(_showAllLayers, 1, wxEXPAND | wxTOP, 6);
hideShowBox->Add(_hideAllLayers, 1, wxEXPAND | wxLEFT | wxTOP, 6);
_dialogPanel->GetSizer()->Add(createButton, 0, wxEXPAND | wxTOP | wxLEFT | wxRIGHT, 12);
_dialogPanel->GetSizer()->Add(hideShowBox, 0, wxEXPAND | wxBOTTOM | wxLEFT | wxRIGHT, 12);
}
void LayerControlDialog::clearControls()
{
// Delete all wxWidgets objects first
_controlContainer->Clear(true);
// Remove all previously allocated layercontrols
_layerControls.clear();
}
void LayerControlDialog::refresh()
{
clearControls();
if (!GlobalMapModule().getRoot())
{
return; // no map present, don't add any layer controls
}
std::map<std::string, LayerControlPtr> sortedControls;
// Traverse the layers
GlobalMapModule().getRoot()->getLayerManager().foreachLayer([&](int layerID, const std::string& layerName)
{
// Create a new layercontrol for each visited layer
// Store the object in a sorted container
sortedControls[layerName] = std::make_shared<LayerControl>(_dialogPanel, layerID);
});
// Assign all controls to the target vector, alphabetically sorted
for (std::pair<std::string, LayerControlPtr> pair : sortedControls)
{
_layerControls.push_back(pair.second);
}
_controlContainer->SetRows(static_cast<int>(_layerControls.size()));
for (const LayerControlPtr& control : _layerControls)
{
_controlContainer->Add(control->getToggle(), 0);
_controlContainer->Add(control->getStatusWidget(), 0, wxEXPAND | wxTOP | wxBOTTOM, 1);
_controlContainer->Add(control->getLabelButton(), 0, wxEXPAND);
_controlContainer->Add(control->getButtons(), 0, wxEXPAND);
if (control == _layerControls.front())
{
// Prevent setting the focus on the buttons at the bottom which lets the scrollbar
// of the window jump around (#4089), set the focus on the first button.
control->getLabelButton()->SetFocus();
}
}
_controlContainer->Layout();
_dialogPanel->FitInside(); // ask the sizer about the needed size
update();
}
void LayerControlDialog::update()
{
if (!GlobalMapModule().getRoot())
{
return;
}
auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager();
// Broadcast the update() call
for (const LayerControlPtr& control : _layerControls)
{
control->update();
}
// Update usage next time round
_rescanSelectionOnIdle = true;
// Update the show/hide all button sensitiveness
std::size_t numVisible = 0;
std::size_t numHidden = 0;
layerSystem.foreachLayer([&](int layerID, const std::string& layerName)
{
if (layerSystem.layerIsVisible(layerID))
{
numVisible++;
}
else
{
numHidden++;
}
});
_showAllLayers->Enable(numHidden > 0);
_hideAllLayers->Enable(numVisible > 0);
}
void LayerControlDialog::updateLayerUsage()
{
_rescanSelectionOnIdle = false;
// Scan the selection and get the histogram
scene::LayerUsageBreakdown breakDown = scene::LayerUsageBreakdown::CreateFromSelection();
for (const LayerControlPtr& control : _layerControls)
{
assert(static_cast<int>(breakDown.size()) > control->getLayerId());
control->updateUsageStatusWidget(breakDown[control->getLayerId()]);
}
}
void LayerControlDialog::onIdle()
{
if (!_rescanSelectionOnIdle) return;
updateLayerUsage();
}
void LayerControlDialog::toggle(const cmd::ArgumentList& args)
{
Instance().ToggleVisibility();
}
void LayerControlDialog::onRadiantStartup()
{
// Lookup the stored window information in the registry
if (GlobalRegistry().getAttribute(RKEY_WINDOW_STATE, "visible") == "1")
{
// Show dialog
Instance().Show();
}
}
void LayerControlDialog::onRadiantShutdown()
{
rMessage() << "LayerControlDialog shutting down." << std::endl;
// Write the visibility status to the registry
GlobalRegistry().setAttribute(RKEY_WINDOW_STATE, "visible", IsShownOnScreen() ? "1" : "0");
// Hide the window and save its state
if (IsShownOnScreen())
{
Hide();
}
// Destroy the window (after it has been disconnected from the Eventmanager)
SendDestroyEvent();
// Final step: clear the instance pointer
InstancePtr().reset();
}
LayerControlDialogPtr& LayerControlDialog::InstancePtr()
{
static LayerControlDialogPtr _instancePtr;
return _instancePtr;
}
LayerControlDialog& LayerControlDialog::Instance()
{
LayerControlDialogPtr& instancePtr = InstancePtr();
if (!instancePtr)
{
// Not yet instantiated, do it now
instancePtr.reset(new LayerControlDialog);
// Register this instance with GlobalRadiant() at once
GlobalRadiant().signal_radiantShutdown().connect(
sigc::mem_fun(*instancePtr, &LayerControlDialog::onRadiantShutdown));
}
return *instancePtr;
}
// TransientWindow callbacks
void LayerControlDialog::_preShow()
{
TransientWindow::_preShow();
_selectionChangedSignal = GlobalSelectionSystem().signal_selectionChanged().connect([this](const ISelectable&)
{
_rescanSelectionOnIdle = true;
});
connectToMapRoot();
_mapEventSignal = GlobalMapModule().signal_mapEvent().connect(
sigc::mem_fun(this, &LayerControlDialog::onMapEvent)
);
// Re-populate the dialog
refresh();
}
void LayerControlDialog::_postHide()
{
disconnectFromMapRoot();
_mapEventSignal.disconnect();
_selectionChangedSignal.disconnect();
_rescanSelectionOnIdle = false;
}
void LayerControlDialog::connectToMapRoot()
{
// Always disconnect first
disconnectFromMapRoot();
if (GlobalMapModule().getRoot())
{
auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager();
// Layer creation/addition/removal triggers a refresh
_layersChangedSignal = layerSystem.signal_layersChanged().connect(
sigc::mem_fun(this, &LayerControlDialog::refresh));
// Visibility change doesn't repopulate the dialog
_layerVisibilityChangedSignal = layerSystem.signal_layerVisibilityChanged().connect(
sigc::mem_fun(this, &LayerControlDialog::update));
// Node membership triggers a selection rescan
_nodeLayerMembershipChangedSignal = layerSystem.signal_nodeMembershipChanged().connect([this]()
{
_rescanSelectionOnIdle = true;
});
}
}
void LayerControlDialog::disconnectFromMapRoot()
{
_nodeLayerMembershipChangedSignal.disconnect();
_layersChangedSignal.disconnect();
_layerVisibilityChangedSignal.disconnect();
}
void LayerControlDialog::onShowAllLayers(wxCommandEvent& ev)
{
if (!GlobalMapModule().getRoot())
{
rError() << "Can't show layers, no map loaded." << std::endl;
return;
}
auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager();
layerSystem.foreachLayer([&](int layerID, const std::string& layerName)
{
layerSystem.setLayerVisibility(layerID, true);
});
}
void LayerControlDialog::onHideAllLayers(wxCommandEvent& ev)
{
auto& layerSystem = GlobalMapModule().getRoot()->getLayerManager();
layerSystem.foreachLayer([&](int layerID, const std::string& layerName)
{
layerSystem.setLayerVisibility(layerID, false);
});
}
void LayerControlDialog::onMapEvent(IMap::MapEvent ev)
{
if (ev == IMap::MapLoaded)
{
// Rebuild the dialog once a map is loaded
connectToMapRoot();
refresh();
}
else if (ev == IMap::MapUnloading)
{
disconnectFromMapRoot();
clearControls();
}
}
} // namespace ui