/
EventManager.cpp
557 lines (443 loc) · 14.7 KB
/
EventManager.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
#include "EventManager.h"
#include "imodule.h"
#include "iradiant.h"
#include "itextstream.h"
#include "icommandsystem.h"
#include "iselection.h"
#include <iostream>
#include <typeinfo>
#include <wx/wxprec.h>
#include <wx/toolbar.h>
#include "registry/registry.h"
#include "xmlutil/Node.h"
#include "wxutil/Modifier.h"
#include "Statement.h"
#include "Toggle.h"
#include "WidgetToggle.h"
#include "RegistryToggle.h"
#include "KeyEvent.h"
#include "SaveEventVisitor.h"
#include "MouseToolManager.h"
#include "debugging/debugging.h"
#include <iostream>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/lexical_cast.hpp>
namespace ui
{
// RegisterableModule implementation
const std::string& EventManager::getName() const
{
static std::string _name(MODULE_EVENTMANAGER);
return _name;
}
const StringSet& EventManager::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_XMLREGISTRY);
}
return _dependencies;
}
void EventManager::initialiseModule(const ApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
// Deactivate the empty event, so it's safe to return it as NullEvent
_emptyEvent->setEnabled(false);
_shortcutFilter.reset(new GlobalKeyEventFilter(*this));
rMessage() << getName() << " successfully initialised." << std::endl;
}
void EventManager::shutdownModule()
{
rMessage() << getName() << "::shutdownModule called" << std::endl;
_shortcutFilter.reset();
saveEventListToRegistry();
_accelerators.clear();
_events.clear();
}
// Constructor
EventManager::EventManager() :
_emptyEvent(new Event()),
_emptyAccelerator(0, 0, _emptyEvent)
{}
IAccelerator& EventManager::addAccelerator(const std::string& key, const std::string& modifierStr)
{
unsigned int keyVal = Accelerator::getKeyCodeFromName(key);
unsigned int modifierFlags = wxutil::Modifier::GetStateFromModifierString(modifierStr);
Accelerator accel(keyVal, modifierFlags, _emptyEvent);
// Add a new Accelerator to the list
_accelerators.push_back(accel);
// return the reference to the last accelerator in the list
AcceleratorList::reverse_iterator i = _accelerators.rbegin();
return (*i);
}
IAccelerator& EventManager::addAccelerator(wxKeyEvent& ev)
{
int keyCode = ev.GetKeyCode();
unsigned int modifierFlags = wxutil::Modifier::GetStateForKeyEvent(ev);
// Create a new accelerator with the given arguments
Accelerator accel(keyCode, modifierFlags, _emptyEvent);
// Add a new Accelerator to the list
_accelerators.push_back(accel);
// return the reference to the last accelerator in the list
return *_accelerators.rbegin();
}
IEventPtr EventManager::findEvent(const std::string& name) {
// Try to lookup the command
EventMap::iterator i = _events.find(name);
if (i != _events.end()) {
// Return the pointer to the command
return i->second;
}
else {
// Nothing found, return the NullEvent
return _emptyEvent;
}
}
IEventPtr EventManager::findEvent(wxKeyEvent& ev)
{
// Retrieve the accelerators for this eventkey
AcceleratorList accelList = findAccelerator(ev);
// Did we find any matching accelerators? If yes, take the first found accelerator
return !accelList.empty() ? accelList.begin()->getEvent() : _emptyEvent;
}
std::string EventManager::getEventName(const IEventPtr& event)
{
// Try to lookup the given eventptr
for (EventMap::iterator i = _events.begin(); i != _events.end(); i++) {
if (i->second == event) {
return i->first;
}
}
return "";
}
std::string EventManager::getAcceleratorStr(const IEventPtr& event, bool forMenu)
{
std::string returnValue = "";
IAccelerator& accelerator = findAccelerator(event);
return static_cast<Accelerator&>(accelerator).getAcceleratorString(forMenu);
}
// Checks if the eventName is already registered and writes to rMessage, if so
bool EventManager::alreadyRegistered(const std::string& eventName) {
// Try to find the command and see if it's already registered
IEventPtr foundEvent = findEvent(eventName);
if (foundEvent->empty()) {
return false;
}
else {
rWarning() << "EventManager: Event " << eventName
<< " already registered!" << std::endl;
return true;
}
}
// Add the given command to the internal list
IEventPtr EventManager::addCommand(const std::string& name, const std::string& statement, bool reactOnKeyUp) {
if (!alreadyRegistered(name)) {
// Add the command to the list
_events[name] = IEventPtr(new Statement(statement, reactOnKeyUp));
// Return the pointer to the newly created event
return _events[name];
}
return _emptyEvent;
}
IEventPtr EventManager::addKeyEvent(const std::string& name, const KeyStateChangeCallback& keyStateChangeCallback)
{
if (!alreadyRegistered(name))
{
// Add the new keyevent to the list (implicitly cast onto Event&)
_events[name] = IEventPtr(new KeyEvent(keyStateChangeCallback));
// Return the pointer to the newly created event
return _events[name];
}
return _emptyEvent;
}
IEventPtr EventManager::addWidgetToggle(const std::string& name) {
if (!alreadyRegistered(name)) {
// Add the command to the list (implicitly cast the pointer on Event&)
_events[name] = IEventPtr(new WidgetToggle());
// Return the pointer to the newly created event
return _events[name];
}
return _emptyEvent;
}
IEventPtr EventManager::addRegistryToggle(const std::string& name, const std::string& registryKey)
{
if (!alreadyRegistered(name)) {
// Add the command to the list (implicitly cast the pointer on Event&)
_events[name] = IEventPtr(new RegistryToggle(registryKey));
// Return the pointer to the newly created event
return _events[name];
}
return _emptyEvent;
}
IEventPtr EventManager::addToggle(const std::string& name, const ToggleCallback& onToggled)
{
if (!alreadyRegistered(name)) {
// Add the command to the list (implicitly cast the pointer on Event&)
_events[name] = IEventPtr(new Toggle(onToggled));
// Return the pointer to the newly created event
return _events[name];
}
return _emptyEvent;
}
void EventManager::setToggled(const std::string& name, const bool toggled)
{
// Check could be placed here by boost::shared_ptr's dynamic_pointer_cast
if (!findEvent(name)->setToggled(toggled)) {
rWarning() << "EventManager: Event " << name
<< " is not a Toggle." << std::endl;
}
}
// Connects the given accelerator to the given command (identified by the string)
void EventManager::connectAccelerator(IAccelerator& accelerator, const std::string& command)
{
IEventPtr event = findEvent(command);
if (!event->empty())
{
// Command found, connect it to the accelerator by passing its pointer
event->connectAccelerator(accelerator);
static_cast<Accelerator&>(accelerator).setEvent(event);
}
else
{
// Command NOT found
rWarning() << "EventManager: Unable to connect command: " << command << std::endl;
}
}
void EventManager::disconnectAccelerator(const std::string& command)
{
IEventPtr event = findEvent(command);
if (!event->empty())
{
// Cycle through the accelerators and check for matches
for (Accelerator& accel : _accelerators)
{
if (accel.match(event))
{
// Connect the accelerator to the empty event (disable the accelerator)
event->disconnectAccelerators();
accel.setEvent(_emptyEvent);
accel.setKey(0);
accel.setModifiers(0);
}
}
}
else
{
// Command NOT found
rWarning() << "EventManager: Unable to disconnect command: " << command << std::endl;
}
}
void EventManager::disableEvent(const std::string& eventName) {
findEvent(eventName)->setEnabled(false);
}
void EventManager::enableEvent(const std::string& eventName) {
findEvent(eventName)->setEnabled(true);
}
void EventManager::removeEvent(const std::string& eventName) {
// Try to lookup the command
EventMap::iterator i = _events.find(eventName);
if (i != _events.end()) {
// Remove all accelerators beforehand
disconnectAccelerator(eventName);
// Remove the event from the list
_events.erase(i);
}
}
void EventManager::disconnectToolbar(wxToolBar* toolbar)
{
std::for_each(_events.begin(), _events.end(), [&] (EventMap::value_type& pair)
{
for (std::size_t tool = 0; tool < toolbar->GetToolsCount(); tool++)
{
pair.second->disconnectToolItem(const_cast<wxToolBarToolBase*>(toolbar->GetToolByPos(tool)));
}
});
}
// Loads the default shortcuts from the registry
void EventManager::loadAccelerators()
{
// Register all custom statements as events too to make them shortcut-bindable
// before going ahead
GlobalCommandSystem().foreachStatement([&] (const std::string& statementName)
{
addCommand(statementName, statementName, false);
}, true); // custom statements only
xml::NodeList shortcutSets = GlobalRegistry().findXPath("user/ui/input//shortcuts");
// If we have two sets of shortcuts, delete the default ones
if (shortcutSets.size() > 1) {
GlobalRegistry().deleteXPath("user/ui/input//shortcuts[@name='default']");
}
// Find all accelerators
xml::NodeList shortcutList = GlobalRegistry().findXPath("user/ui/input/shortcuts//shortcut");
if (!shortcutList.empty())
{
rMessage() << "EventManager: Shortcuts found in Registry: " << shortcutList.size() << std::endl;
for (std::size_t i = 0; i < shortcutList.size(); i++)
{
const std::string key = shortcutList[i].getAttributeValue("key");
const std::string cmd = shortcutList[i].getAttributeValue("command");
// Try to lookup the command
IEventPtr event = findEvent(cmd);
// Check for a non-empty key string
if (!key.empty())
{
// Check for valid command definitions were found
if (!event->empty())
{
// Get the modifier string (e.g. "SHIFT+ALT")
const std::string modifierStr = shortcutList[i].getAttributeValue("modifiers");
if (!duplicateAccelerator(key, modifierStr, event))
{
// Create the accelerator object
IAccelerator& accelerator = addAccelerator(key, modifierStr);
// Connect the newly created accelerator to the command
event->connectAccelerator(accelerator);
static_cast<Accelerator&>(accelerator).setEvent(event);
}
}
else
{
rWarning() << "EventManager: Cannot load shortcut definition (command invalid): "
<< cmd << std::endl;
}
}
}
}
else
{
// No accelerator definitions found!
rWarning() << "EventManager: No shortcut definitions found..." << std::endl;
}
}
void EventManager::foreachEvent(IEventVisitor& eventVisitor)
{
// Cycle through the event and pass them to the visitor class
for (const EventMap::value_type& pair : _events)
{
eventVisitor.visit(pair.first, pair.second);
}
}
// Tries to locate an accelerator, that is connected to the given command
IAccelerator& EventManager::findAccelerator(const IEventPtr& event)
{
// Cycle through the accelerators and check for matches
for (AcceleratorList::iterator i = _accelerators.begin(); i != _accelerators.end(); ++i)
{
if (i->match(event))
{
// Return the reference to the found accelerator
return (*i);
}
}
// Return an empty accelerator if nothing is found
return _emptyAccelerator;
}
void EventManager::saveEventListToRegistry()
{
const std::string rootKey = "user/ui/input";
// The visitor class to save each event definition into the registry
// Note: the SaveEventVisitor automatically wipes all the existing shortcuts from the registry
SaveEventVisitor visitor(rootKey, this);
foreachEvent(visitor);
}
AcceleratorList EventManager::findAccelerator(
const std::string& key, const std::string& modifierStr)
{
unsigned int keyVal = Accelerator::getKeyCodeFromName(key);
unsigned int modifierFlags = wxutil::Modifier::GetStateFromModifierString(modifierStr);
return findAccelerator(keyVal, modifierFlags);
}
bool EventManager::duplicateAccelerator(const std::string& key,
const std::string& modifiers,
const IEventPtr& event)
{
AcceleratorList accelList = findAccelerator(key, modifiers);
for (AcceleratorList::iterator i = accelList.begin(); i != accelList.end(); ++i)
{
// If one of the accelerators in the list matches the event, return true
if (i->match(event))
{
return true;
}
}
return false;
}
AcceleratorList EventManager::findAccelerator(unsigned int keyVal,
const unsigned int modifierFlags)
{
AcceleratorList returnList;
// Cycle through the accelerators and check for matches
for (AcceleratorList::iterator i = _accelerators.begin(); i != _accelerators.end(); ++i)
{
if (i->match(keyVal, modifierFlags))
{
// Add the pointer to the found accelerators
returnList.push_back((*i));
}
}
return returnList;
}
AcceleratorList EventManager::findAccelerator(wxKeyEvent& ev)
{
int keyval = ev.GetKeyCode(); // is always uppercase
return findAccelerator(keyval, wxutil::Modifier::GetStateForKeyEvent(ev));
}
bool EventManager::isModifier(wxKeyEvent& ev)
{
int keyCode = ev.GetKeyCode();
return (keyCode == WXK_SHIFT || keyCode == WXK_CONTROL ||
keyCode == WXK_ALT || keyCode == WXK_WINDOWS_LEFT ||
keyCode == WXK_WINDOWS_MENU || keyCode == WXK_WINDOWS_RIGHT);
}
namespace
{
std::string getKeyString(wxKeyEvent& ev)
{
int keycode = ev.GetKeyCode();
std::string virtualKeyCodeName = Accelerator::getNameFromKeyCode(keycode);
if (!virtualKeyCodeName.empty())
{
return virtualKeyCodeName;
}
if (keycode > 0 && keycode < 32)
{
return wxString::Format("Ctrl-%c", (unsigned char)('A' + keycode - 1)).ToStdString();
}
if (keycode >= 32 && keycode < 128)
{
return wxString::Format("%c", (unsigned char)keycode).ToStdString();
}
return "unknown";
}
} // namespace
std::string EventManager::getEventStr(wxKeyEvent& ev)
{
std::string returnValue("");
// Don't react on modifiers only (no actual key like A, 2, U, etc.)
if (isModifier(ev))
{
return returnValue;
}
// Convert the key event to modifier flags
const unsigned int modifierFlags = wxutil::Modifier::GetStateForKeyEvent(ev);
// Construct the complete string
returnValue += wxutil::Modifier::GetModifierStringForMenu(modifierFlags);
returnValue += (returnValue != "") ? "-" : "";
returnValue += getKeyString(ev);
return returnValue;
}
}
extern "C" void DARKRADIANT_DLLEXPORT RegisterModule(IModuleRegistry& registry)
{
registry.registerModule(std::make_shared<ui::EventManager>());
registry.registerModule(std::make_shared<ui::MouseToolManager>());
// Initialise the streams using the given application context
module::initialiseStreams(registry.getApplicationContext());
// Remember the reference to the ModuleRegistry
module::RegistryReference::Instance().setRegistry(registry);
// Set up the assertion handler
GlobalErrorHandler() = registry.getApplicationContext().getErrorHandlingFunction();
}