/
SelectionSetToolmenu.cpp
269 lines (207 loc) · 6.35 KB
/
SelectionSetToolmenu.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
#include "SelectionSetToolmenu.h"
#include "i18n.h"
#include "iselection.h"
#include "ieventmanager.h"
#include "iuimanager.h"
#include "imap.h"
#include "iradiant.h"
#include "imainframe.h"
#include "itextstream.h"
#include "idialogmanager.h"
#include <wx/combobox.h>
#include <wx/toolbar.h>
#include <wx/sizer.h>
#include <wx/stattext.h>
#include <wx/artprov.h>
namespace ui
{
namespace
{
const char* const ENTRY_TOOLTIP = N_("Enter a name and hit ENTER to save a set.\n\n"
"Select an item from the dropdown list to restore the selection.\n\n"
"Hold SHIFT when opening the dropdown list and selecting the item to de-select the set.");
// Tool items created by the ToolBarManager carry ID >= 100
const int CLEAR_TOOL_ID = 1;
}
std::unique_ptr<SelectionSetToolmenu> SelectionSetToolmenu::_instance;
SelectionSetToolmenu::SelectionSetToolmenu() :
_dropdownToolId(wxID_NONE)
{
// Get the horizontal toolbar and add a custom widget
wxToolBar* toolbar = GlobalMainFrame().getToolbar(IMainFrame::Toolbar::TOP);
// Insert a separator at the end of the toolbar
toolbar->AddSeparator();
wxStaticText* label = new wxStaticText(toolbar, wxID_ANY, _("Selection Set: "));
toolbar->AddControl(label);
_dropdown = new wxComboBox(toolbar, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0, NULL, wxTE_PROCESS_ENTER);
// Add tooltip
_dropdown->SetHelpText(_(ENTRY_TOOLTIP));
// Connect the signals
_dropdown->Bind(wxEVT_TEXT_ENTER, &SelectionSetToolmenu::onEntryActivated, this);
_dropdown->Bind(wxEVT_COMBOBOX, &SelectionSetToolmenu::onSelectionChanged, this);
auto dropdownTool = toolbar->AddControl(_dropdown);
_dropdownToolId = dropdownTool->GetId();
_clearAllButton = toolbar->AddTool(CLEAR_TOOL_ID, "",
wxArtProvider::GetBitmap(GlobalUIManager().ArtIdPrefix() + "delete.png"), _("Clear Selection Sets"));
toolbar->Bind(wxEVT_TOOL, &SelectionSetToolmenu::onDeleteAllSetsClicked, this, _clearAllButton->GetId());
toolbar->Realize();
#ifdef __WXOSX__
// Weird workaround to stop an empty area from being drawn
// where the label and combobox are supposed to be
label->Hide();
label->Show();
_dropdown->Hide();
_dropdown->Show();
#endif
_mapEventHandler = GlobalMapModule().signal_mapEvent().connect(
sigc::mem_fun(*this, &SelectionSetToolmenu::onMapEvent)
);
GlobalMainFrame().signal_MainFrameShuttingDown().connect(
sigc::mem_fun(*this, &SelectionSetToolmenu::onMainFrameShuttingDown)
);
connectToMapRoot();
update();
}
SelectionSetToolmenu::~SelectionSetToolmenu()
{}
void SelectionSetToolmenu::Init()
{
GlobalMainFrame().signal_MainFrameConstructed().connect([&]()
{
// Construct a new tool menu object on startup
_instance.reset(new SelectionSetToolmenu);
});
}
void SelectionSetToolmenu::onMainFrameShuttingDown()
{
if (_dropdownToolId != wxID_NONE)
{
auto toolbar = static_cast<wxToolBar*>(_dropdown->GetParent());
toolbar->DeleteTool(_dropdownToolId);
}
_dropdown = nullptr;
_mapEventHandler.disconnect();
_instance.reset();
}
void SelectionSetToolmenu::onMapEvent(IMap::MapEvent ev)
{
if (ev == IMap::MapLoaded)
{
// Rebuild the dialog once a map is loaded
connectToMapRoot();
update();
}
else if (ev == IMap::MapUnloading)
{
disconnectFromMapRoot();
update();
}
}
void SelectionSetToolmenu::connectToMapRoot()
{
// Always disconnect first
disconnectFromMapRoot();
if (GlobalMapModule().getRoot())
{
auto& setManager = GlobalMapModule().getRoot()->getSelectionSetManager();
_setsChangedSignal = setManager.signal_selectionSetsChanged().connect(
sigc::mem_fun(this, &SelectionSetToolmenu::update));
}
}
void SelectionSetToolmenu::disconnectFromMapRoot()
{
_setsChangedSignal.disconnect();
}
void SelectionSetToolmenu::update()
{
_dropdown->Clear();
auto root = GlobalMapModule().getRoot();
if (!root)
{
_clearAllButton->GetToolBar()->EnableTool(_clearAllButton->GetId(), false);
return;
}
bool hasSelectionSets = false;
root->getSelectionSetManager().foreachSelectionSet([&] (const selection::ISelectionSetPtr& set)
{
_dropdown->Append(set->getName());
hasSelectionSets = true;
});
_clearAllButton->GetToolBar()->EnableTool(_clearAllButton->GetId(), hasSelectionSets);
}
void SelectionSetToolmenu::onEntryActivated(wxCommandEvent& ev)
{
auto root = GlobalMapModule().getRoot();
if (!root)
{
rError() << "No map loaded, cannot create selection sets" << std::endl;
return;
}
// Create new selection set if possible
std::string name = _dropdown->GetValue().ToStdString();
if (name.empty()) return;
// don't create empty sets
if (GlobalSelectionSystem().countSelected() == 0)
{
ui::IDialogPtr dialog = GlobalDialogManager().createMessageBox(
_("Cannot create selection set"),
_("Cannot create a selection set, there is nothing selected in the current scene."),
ui::IDialog::MESSAGE_CONFIRM);
dialog->run();
return;
}
auto set = root->getSelectionSetManager().createSelectionSet(name);
assert(set);
set->assignFromCurrentScene();
// Clear the entry again
_dropdown->SetValue("");
// Unset our focus
wxGetTopLevelParent(_dropdown)->SetFocus();
}
void SelectionSetToolmenu::onSelectionChanged(wxCommandEvent& ev)
{
auto root = GlobalMapModule().getRoot();
if (!root)
{
rError() << "No map loaded, cannot select or deselect sets" << std::endl;
return;
}
std::string name = _dropdown->GetStringSelection().ToStdString();
if (name.empty()) return;
auto set = root->getSelectionSetManager().findSelectionSet(name);
if (!set) return;
// The user can choose to DESELECT the set nodes when holding down shift
if (wxGetKeyState(WXK_SHIFT))
{
set->deselect();
}
else
{
set->select();
}
_dropdown->SetValue("");
// Unset our focus
wxGetTopLevelParent(_dropdown)->SetFocus();
}
void SelectionSetToolmenu::onDeleteAllSetsClicked(wxCommandEvent& ev)
{
if (ev.GetId() != _clearAllButton->GetId())
{
ev.Skip();
return; // not our business
}
if (!GlobalMapModule().getRoot())
{
rError() << "No map loaded, can't delete any sets" << std::endl;
return;
}
ui::IDialogPtr dialog = GlobalDialogManager().createMessageBox(
_("Delete all selection sets?"),
_("This will delete all set definitions. The actual map objects will not be affected by this step.\n\nContinue with that operation?"),
ui::IDialog::MESSAGE_ASK);
if (dialog->run() == ui::IDialog::RESULT_YES)
{
GlobalMapModule().getRoot()->getSelectionSetManager().deleteAllSelectionSets();
}
}
} // namespace