/
AuiLayout.cpp
325 lines (257 loc) · 9.74 KB
/
AuiLayout.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
#include "AuiLayout.h"
#include "i18n.h"
#include "ui/imenumanager.h"
#include "ui/igroupdialog.h"
#include "ui/imainframe.h"
#include "ui/iuserinterface.h"
#include <wx/sizer.h>
#include <wx/aui/auibook.h>
#include "camera/CameraWndManager.h"
#include "ui/texturebrowser/TextureBrowser.h"
#include "wxutil/Bitmap.h"
#include "xyview/GlobalXYWnd.h"
namespace ui
{
namespace
{
const std::string RKEY_ROOT = "user/ui/mainFrame/aui/";
const std::string RKEY_AUI_PERSPECTIVE = RKEY_ROOT + "perspective";
const std::string RKEY_AUI_PANES = RKEY_ROOT + "panes";
const std::string RKEY_AUI_LAYOUT_VERSION = RKEY_ROOT + "layoutVersion";
const std::string PANE_NODE_NAME = "pane";
const std::string PANE_NAME_ATTRIBUTE = "paneName";
const std::string CONTROL_NAME_ATTRIBUTE = "controlName";
constexpr int AuiLayoutVersion = 1;
// Minimum size of docked panels
const wxSize MIN_SIZE(128, 128);
// Return a pane info with default options
wxAuiPaneInfo DEFAULT_PANE_INFO(const std::string& caption,
const wxSize& minSize)
{
return wxAuiPaneInfo().Caption(caption).CloseButton(false).MaximizeButton()
.BestSize(minSize).MinSize(minSize).DestroyOnClose(true);
}
void setupFloatingPane(wxAuiPaneInfo& pane)
{
pane.Float().CloseButton(true).MinSize(300, 450);
}
}
AuiLayout::AuiLayout() :
_auiMgr(nullptr, wxAUI_MGR_ALLOW_FLOATING | wxAUI_MGR_VENETIAN_BLINDS_HINT | wxAUI_MGR_LIVE_RESIZE),
_propertyNotebook(nullptr)
{
_auiMgr.Bind(wxEVT_AUI_PANE_CLOSE, &AuiLayout::onPaneClose, this);
}
bool AuiLayout::paneNameExists(const std::string& name) const
{
for (const auto& info : _panes)
{
if (info.paneName == name)
{
return true;
}
}
return false;
}
std::string AuiLayout::generateUniquePaneName(const std::string& controlName)
{
auto paneName = controlName;
auto index = 1;
while (paneNameExists(paneName))
{
paneName += fmt::format("{0}{1}", controlName, ++index);
}
return paneName;
}
void AuiLayout::addPane(const std::string& controlName, wxWindow* window, const wxAuiPaneInfo& info)
{
// Give the pane a unique name so we can restore perspectives
addPane(controlName, generateUniquePaneName(controlName), window, info);
}
void AuiLayout::addPane(const std::string& controlName, const std::string& paneName, wxWindow* window, const wxAuiPaneInfo& info)
{
wxAuiPaneInfo paneInfo = info;
paneInfo.Name(paneName);
// Add and store the pane
_auiMgr.AddPane(window, paneInfo);
// Remember this pane
_panes.push_back({ paneName, controlName, window });
}
void AuiLayout::convertPaneToPropertyTab(const std::string& paneName)
{
for (auto i = _panes.begin(); i != _panes.end(); ++i)
{
if (i->paneName != paneName) continue;
// Close the pane if it's present
if (auto paneInfo = _auiMgr.GetPane(i->paneName); paneInfo.IsOk())
{
_auiMgr.ClosePane(paneInfo);
}
_propertyNotebook->addControl(i->controlName);
_panes.erase(i);
break;
}
}
void AuiLayout::onPaneClose(wxAuiManagerEvent& ev)
{
// This is a desperate work around to let undocked property windows
// return to the property notebook when they're closed
// I failed finding any other way to have floating windows dragged into
// the notebook, or adding a custom pane button - I'm open to ideas
auto closedPane = ev.GetPane();
for (auto i = _panes.begin(); i != _panes.end(); ++i)
{
if (i->paneName != closedPane->name) continue;
_propertyNotebook->addControl(i->controlName);
_panes.erase(i);
break;
}
}
std::string AuiLayout::getName()
{
return AUI_LAYOUT_NAME;
}
void AuiLayout::saveStateToRegistry()
{
registry::setValue(RKEY_AUI_LAYOUT_VERSION, AuiLayoutVersion);
// Save the pane perspective
registry::setValue(RKEY_AUI_PERSPECTIVE, _auiMgr.SavePerspective().ToStdString());
// Save tracked panes, we need to create all named panes before we can load the perspective
GlobalRegistry().deleteXPath(RKEY_AUI_PANES);
auto panesKey = GlobalRegistry().createKey(RKEY_AUI_PANES);
for (const auto& pane : _panes)
{
auto paneNode = panesKey.createChild(PANE_NODE_NAME);
paneNode.setAttributeValue(CONTROL_NAME_ATTRIBUTE, pane.controlName);
paneNode.setAttributeValue(PANE_NAME_ATTRIBUTE, pane.paneName);
}
// Save property notebook stae
_propertyNotebook->saveState(RKEY_ROOT);
}
void AuiLayout::activate()
{
auto topLevelParent = GlobalMainFrame().getWxTopLevelWindow();
// AUI manager can't manage a Sizer, we need to create an actual wxWindow
// container
auto managedArea = new wxWindow(topLevelParent, wxID_ANY);
_auiMgr.SetManagedWindow(managedArea);
GlobalMainFrame().getWxMainContainer()->Add(managedArea, 1, wxEXPAND);
_propertyNotebook = new PropertyNotebook(managedArea, *this);
auto orthoViewControl = GlobalUserInterface().findControl(UserControl::OrthoView);
auto cameraControl = GlobalUserInterface().findControl(UserControl::Camera);
assert(cameraControl);
assert(orthoViewControl);
// Add the camera and notebook to the left, as with the Embedded layout, and
// the 2D view on the right
wxSize size = topLevelParent->GetSize();
size.Scale(0.5, 1.0);
addPane(cameraControl->getControlName(), cameraControl->createWidget(managedArea),
DEFAULT_PANE_INFO(cameraControl->getDisplayName(), size).Left().Position(0));
addPane("PropertiesPanel", _propertyNotebook,
DEFAULT_PANE_INFO(_("Properties"), size).Left().Position(1));
addPane(orthoViewControl->getControlName(), orthoViewControl->createWidget(managedArea),
DEFAULT_PANE_INFO(orthoViewControl->getDisplayName(), size).CenterPane());
_auiMgr.Update();
// Hide the camera toggle option for non-floating views
GlobalMenuManager().setVisibility("main/view/cameraview", false);
// Hide the console/texture browser toggles for non-floating/non-split views
GlobalMenuManager().setVisibility("main/view/textureBrowser", false);
}
void AuiLayout::deactivate()
{
// Delete all active views
GlobalXYWndManager().destroyViews();
// Get a reference to the managed window, it might be cleared by UnInit()
auto managedWindow = _auiMgr.GetManagedWindow();
// Unregister the AuiMgr from the event handlers of the managed window
// otherwise we run into crashes during shutdown (#5586)
_auiMgr.UnInit();
managedWindow->Destroy();
}
void AuiLayout::createPane(const std::string& controlName, const std::string& paneName,
const std::function<void(wxAuiPaneInfo&)>& setupPane)
{
auto control = GlobalUserInterface().findControl(controlName);
if (!control)
{
rError() << "Cannot find named control: " << controlName << std::endl;
return;
}
auto managedWindow = _auiMgr.GetManagedWindow();
auto pane = DEFAULT_PANE_INFO(control->getDisplayName(), MIN_SIZE);
pane.name = paneName;
// Run client code to setup the pane properties
setupPane(pane);
if (!control->getIcon().empty())
{
pane.Icon(wxutil::GetLocalBitmap(control->getIcon()));
}
auto widget = control->createWidget(managedWindow);
_auiMgr.AddPane(widget, pane);
_panes.push_back({ paneName, controlName, widget });
}
void AuiLayout::createFloatingControl(const std::string& controlName)
{
createPane(controlName, generateUniquePaneName(controlName), setupFloatingPane);
_auiMgr.Update();
}
void AuiLayout::addControl(const std::string& controlName, const IMainFrame::ControlSettings& defaultSettings)
{
_defaultControlSettings[controlName] = defaultSettings;
if (defaultSettings.visible)
{
switch (defaultSettings.location)
{
case IMainFrame::Location::PropertyPanel:
_propertyNotebook->addControl(controlName);
break;
case IMainFrame::Location::FloatingWindow:
createFloatingControl(controlName);
break;
}
}
}
void AuiLayout::restoreStateFromRegistry()
{
// Check the saved version
if (registry::getValue<int>(RKEY_AUI_LAYOUT_VERSION) != AuiLayoutVersion)
{
rMessage() << "No compatible AUI layout state information found in registry" << std::endl;
return;
}
// Restore all missing panes, this has to be done before the perspective is restored
for (const auto& node : GlobalRegistry().findXPath(RKEY_AUI_PANES + "//*"))
{
if (node.getName() != PANE_NODE_NAME) continue;
auto controlName = node.getAttributeValue(CONTROL_NAME_ATTRIBUTE);
auto paneName = node.getAttributeValue(PANE_NAME_ATTRIBUTE);
if (paneNameExists(paneName)) continue; // this one already exists
createPane(controlName, paneName, setupFloatingPane);
}
// Restore the property notebook state
_propertyNotebook->restoreState(RKEY_ROOT);
// Nasty hack to get the panes sized properly. Since BestSize() is
// completely ignored (at least on Linux), we have to add the panes with a
// large *minimum* size and then reset this size after the initial addition.
for (const auto& info : _panes)
{
_auiMgr.GetPane(info.control).MinSize(MIN_SIZE);
}
_auiMgr.Update();
// If we have a stored perspective, load it
auto storedPersp = registry::getValue<std::string>(RKEY_AUI_PERSPECTIVE);
if (!storedPersp.empty())
{
_auiMgr.LoadPerspective(storedPersp);
}
// Restore all floating XY views
GlobalXYWnd().restoreState();
}
void AuiLayout::toggleFullscreenCameraView()
{
}
std::shared_ptr<AuiLayout> AuiLayout::CreateInstance()
{
return std::make_shared<AuiLayout>();
}
} // namespace ui