-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventmanager.cpp
More file actions
84 lines (69 loc) · 2.08 KB
/
Copy patheventmanager.cpp
File metadata and controls
84 lines (69 loc) · 2.08 KB
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
#include "eventmanager.h"
#include<iostream>
/** Converts strings from UPPERCASE or MiXedCasE to lowercase. */
// This is a regular old C-style function not a method.
string tolower(string &s)
{
string ns;
for (auto c : s) {
ns += tolower(c);
}
return ns;
}
EventManager::EventManager()
{
running = true;
}
EventManager &EventManager::getInstance()
{
// static inside functions creates an instance of this variable for all the calls of this function, and initializes only once.
static EventManager instance;
return instance;
}
void EventManager::listen(string event_name, EventListener *listener)
{
registeredEvents[tolower(event_name)].push_back(listener);
}
void EventManager::trigger(string event_name, void *args)
{
for (auto listener : registeredEvents[tolower(event_name)]) {
listener->run(args);
}
}
bool EventManager::is_running()
{
return running;
}
void EventManager::stop()
{
running = false;
}
void EventManager::check_events()
{
string buffer;
vector<string> words;
cout << "> "; // print prompt
getline(cin, buffer, '\n'); // read a line from cin to "buffer"
buffer = tolower(buffer);
string::size_type pos = 0, last_pos = 0;
// Break "buffer" up by spaces
bool finished = false;
while (!finished) {
pos = buffer.find_first_of(' ', last_pos); // find and remember first space.
if (pos == string::npos ) { // if we found the last word,
words.push_back(buffer.substr(last_pos)); // add it to vector "words"
finished = true; // and finish searching.
} else { // otherwise add to vector and move on to next word.
words.push_back(buffer.substr(last_pos, pos - last_pos));
last_pos = pos + 1;
}
}
trigger("input", &words);
}
void EventManager::event_loop()
{
EventManager &eventManager = EventManager::getInstance();
while (eventManager.is_running()) {
eventManager.check_events();
}
}