/
SelectionSetManager.cpp
238 lines (188 loc) · 5.95 KB
/
SelectionSetManager.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
#include "SelectionSetManager.h"
#include "itextstream.h"
#include "i18n.h"
#include "iselection.h"
#include "idialogmanager.h"
#include "imapinfofile.h"
#include "iuimanager.h"
#include "ieventmanager.h"
#include "imainframe.h"
#include "modulesystem/StaticModule.h"
#include "SelectionSetToolmenu.h"
#include "SelectionSetInfoFileModule.h"
#include <wx/toolbar.h>
#include <wx/frame.h>
#include <wx/artprov.h>
#include <wx/stattext.h>
#include <functional>
namespace selection
{
namespace
{
// Tool items created by the ToolBarManager carry ID >= 100
const int CLEAR_TOOL_ID = 1;
}
SelectionSetManager::SelectionSetManager() :
_toolMenu(NULL),
_clearAllButton(NULL)
{}
const std::string& SelectionSetManager::getName() const
{
static std::string _name("SelectionSetManager");
return _name;
}
const StringSet& SelectionSetManager::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_SELECTIONSYSTEM);
_dependencies.insert(MODULE_EVENTMANAGER);
_dependencies.insert(MODULE_COMMANDSYSTEM);
_dependencies.insert(MODULE_RADIANT);
_dependencies.insert(MODULE_MAP);
_dependencies.insert(MODULE_MAPINFOFILEMANAGER);
}
return _dependencies;
}
void SelectionSetManager::initialiseModule(const ApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
// Register for the startup event
GlobalRadiant().signal_radiantStarted().connect(
sigc::mem_fun(this, &SelectionSetManager::onRadiantStartup)
);
GlobalCommandSystem().addCommand("DeleteAllSelectionSets",
std::bind(&SelectionSetManager::deleteAllSelectionSetsCmd, this, std::placeholders::_1));
GlobalEventManager().addCommand("DeleteAllSelectionSets", "DeleteAllSelectionSets");
GlobalMapModule().signal_mapEvent().connect(
sigc::mem_fun(*this, &SelectionSetManager::onMapEvent)
);
GlobalMapInfoFileManager().registerInfoFileModule(
std::make_shared<SelectionSetInfoFileModule>()
);
}
void SelectionSetManager::shutdownModule()
{
_sigSelectionSetsChanged.clear();
_selectionSets.clear();
}
void SelectionSetManager::onMapEvent(IMap::MapEvent ev)
{
if (ev == IMap::MapUnloaded)
{
deleteAllSelectionSets();
}
}
void SelectionSetManager::onRadiantStartup()
{
// Get the horizontal toolbar and add a custom widget
wxToolBar* toolbar = GlobalMainFrame().getToolbar(IMainFrame::TOOLBAR_HORIZONTAL);
// Insert a separator at the end of the toolbar
toolbar->AddSeparator();
wxStaticText* label = new wxStaticText(toolbar, wxID_ANY, _("Selection Set: "));
toolbar->AddControl(label);
// Construct a new tool menu object
_toolMenu = new SelectionSetToolmenu(toolbar);
toolbar->AddControl(_toolMenu);
_clearAllButton = toolbar->AddTool(CLEAR_TOOL_ID, "",
wxArtProvider::GetBitmap(GlobalUIManager().ArtIdPrefix() + "delete.png"), _("Clear Selection Sets"));
_clearAllButton->GetToolBar()->EnableTool(_clearAllButton->GetId(), !_selectionSets.empty());
toolbar->Connect(_clearAllButton->GetId(), wxEVT_TOOL,
wxCommandEventHandler(SelectionSetManager::onDeleteAllSetsClicked), NULL, this);
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();
_toolMenu->Hide();
_toolMenu->Show();
#endif
}
sigc::signal<void> SelectionSetManager::signal_selectionSetsChanged() const
{
return _sigSelectionSetsChanged;
}
void SelectionSetManager::foreachSelectionSet(const VisitorFunc& functor)
{
for (SelectionSets::const_iterator i = _selectionSets.begin(); i != _selectionSets.end(); )
{
functor((i++)->second);
}
}
void SelectionSetManager::foreachSelectionSet(Visitor& visitor)
{
foreachSelectionSet([&] (const ISelectionSetPtr& set)
{
visitor.visit(set);
});
}
ISelectionSetPtr SelectionSetManager::createSelectionSet(const std::string& name)
{
SelectionSets::iterator i = _selectionSets.find(name);
if (i == _selectionSets.end())
{
// Create new set
std::pair<SelectionSets::iterator, bool> result = _selectionSets.insert(
SelectionSets::value_type(name, SelectionSetPtr(new SelectionSet(name))));
i = result.first;
_sigSelectionSetsChanged();
if (_clearAllButton)
{
_clearAllButton->GetToolBar()->EnableTool(_clearAllButton->GetId(), !_selectionSets.empty());
}
}
return i->second;
}
void SelectionSetManager::deleteSelectionSet(const std::string& name)
{
SelectionSets::iterator i = _selectionSets.find(name);
if (i != _selectionSets.end())
{
_selectionSets.erase(i);
_sigSelectionSetsChanged();
if (_clearAllButton)
{
_clearAllButton->GetToolBar()->EnableTool(_clearAllButton->GetId(), !_selectionSets.empty());
}
}
}
void SelectionSetManager::deleteAllSelectionSets()
{
_selectionSets.clear();
_sigSelectionSetsChanged();
if (_clearAllButton)
{
_clearAllButton->GetToolBar()->EnableTool(_clearAllButton->GetId(), false);
}
}
void SelectionSetManager::deleteAllSelectionSetsCmd(const cmd::ArgumentList& args)
{
deleteAllSelectionSets();
}
ISelectionSetPtr SelectionSetManager::findSelectionSet(const std::string& name)
{
SelectionSets::iterator i = _selectionSets.find(name);
return (i != _selectionSets.end()) ? i->second : ISelectionSetPtr();
}
void SelectionSetManager::onDeleteAllSetsClicked(wxCommandEvent& ev)
{
if (ev.GetId() != _clearAllButton->GetId())
{
ev.Skip();
return; // not our business
}
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);
ui::IDialog::Result result = dialog->run();
if (result == ui::IDialog::RESULT_YES)
{
deleteAllSelectionSets();
}
}
// Define the static SelectionSetManager module
module::StaticModule<SelectionSetManager> selectionSetManager;
} // namespace