-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathButton.cpp
63 lines (52 loc) · 1.49 KB
/
Button.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
#include "Button.hpp"
#include <SDL.h>
#include <list>
#include <map>
#include <stdexcept>
namespace kit {
struct ButtonInternal : public Button {
std::string name;
Button::Config config;
};
struct ButtonList : std::list< ButtonInternal > {
std::multimap< int, ButtonInternal * > by_scancode;
std::map< std::string, ButtonInternal * > by_name;
static ButtonList &instance() {
static ButtonList list;
return list;
}
};
Button const *Button::create(std::string const &name, Config const &config) {
static ButtonList &list = ButtonList::instance();
if (list.by_name.count(name)) {
throw std::runtime_error("Two buttons with the name '" + name + "' created.");
}
list.emplace_back();
list.back().name = name;
list.back().config = config;
list.by_name.insert(std::make_pair(name, &list.back()));
list.by_scancode.insert(std::make_pair(config.scancode, &list.back()));
return &list.back();
}
void Button::clear_events() {
static ButtonList &list = ButtonList::instance();
for (auto &b : list) {
b.downs = 0;
b.ups = 0;
}
}
void Button::handle_event(SDL_Event const &evt) {
static ButtonList &list = ButtonList::instance();
if ((evt.type == SDL_KEYDOWN || evt.type == SDL_KEYUP) && evt.key.repeat == 0) {
auto r = list.by_scancode.equal_range(evt.key.keysym.scancode);
for (auto bi = r.first; bi != r.second; ++bi) {
if (evt.type == SDL_KEYDOWN) {
bi->second->downs += 1;
} else {
bi->second->ups += 1;
}
bi->second->pressed = (evt.key.state == SDL_PRESSED);
}
}
}
}