-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathkit.cpp
More file actions
140 lines (125 loc) · 4.41 KB
/
Copy pathkit.cpp
File metadata and controls
140 lines (125 loc) · 4.41 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
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
#include "kit.hpp"
#include <iostream>
namespace kit {
std::vector< std::string > args;
State state;
Display display;
Config config;
std::unordered_map< PointerID, Pointer > pointers;
}
namespace {
std::shared_ptr< kit::Mode > current_mode = nullptr;
std::shared_ptr< kit::Mode > pending_mode = nullptr;
}
void kit::set_mode(std::shared_ptr< Mode > const &new_mode) {
pending_mode = new_mode;
}
std::shared_ptr< kit::Mode > const &kit::get_mode() {
return current_mode;
}
void kit::commit_mode() {
if (current_mode == pending_mode) return;
std::unordered_map< PointerID, Pointer > old_pointers = pointers;
for (auto p : old_pointers) {
dispatch_pointer_action(p.first, PointerLeave, p.second);
}
assert(pointers.empty());
current_mode = pending_mode;
for (auto p : old_pointers) {
dispatch_pointer_action(p.first, PointerEnter, p.second);
}
}
void kit::dispatch_pointer_action(PointerID pointer, PointerAction action, Pointer const &new_state) {
if (action == PointerEnter) {
//code shouldn't dispatch_pointer_action if pointer exists already:
if (pointers.count(pointer)) {
std::cerr << "WARNING: got an Enter action for an already-present pointer." << std::endl;
return;
}
if (current_mode) {
current_mode->pointer_action(pointer, action, new_state, new_state);
}
pointers.insert(std::make_pair(pointer, new_state));
return;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - -
} else if (action == PointerLeave) {
auto p = pointers.find(pointer);
if (p == pointers.end()) {
std::cerr << "WARNING: got a Leave action for a pointer that isn't present." << std::endl;
return;
}
Pointer const &old_state = p->second;
if (new_state.at != old_state.at) {
std::cerr << "WARNING: pointer moved during Leave action." << std::endl;
}
if (new_state.buttons != old_state.buttons) {
std::cerr << "WARNING: pointer buttons changed during Leave action." << std::endl;
}
if (current_mode) {
current_mode->pointer_action(pointer, action, old_state, new_state);
}
pointers.erase(p);
//- - - - - - - - - - - - - - - - - - - - - - - - - - - -
} else if (action == PointerDown) {
auto p = pointers.find(pointer);
if (p == pointers.end()) {
std::cerr << "WARNING: got a Down action for a pointer that isn't present." << std::endl;
return;
}
Pointer const &old_state = p->second;
if (new_state.at != old_state.at) {
std::cerr << "WARNING: pointer moved during Down action." << std::endl;
}
if (new_state.buttons == old_state.buttons) {
std::cerr << "WARNING: no button change during Down action." << std::endl;
}
if ((old_state.buttons ^ (new_state.buttons & old_state.buttons)) != 0) {
std::cerr << "WARNING: buttons lifted during Down action." << std::endl;
}
if (current_mode) {
current_mode->pointer_action(pointer, action, old_state, new_state);
}
p->second = new_state;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - -
} else if (action == PointerUp) {
auto p = pointers.find(pointer);
if (p == pointers.end()) {
std::cerr << "WARNING: got a Up action for a pointer that isn't present." << std::endl;
return;
}
Pointer const &old_state = p->second;
if (new_state.at != old_state.at) {
std::cerr << "WARNING: pointer moved during Up action." << std::endl;
}
if (new_state.buttons == old_state.buttons) {
std::cerr << "WARNING: no button change during Up action." << std::endl;
}
if ((new_state.buttons ^ (new_state.buttons & old_state.buttons)) != 0) {
std::cerr << "WARNING: buttons pressed during Up action." << std::endl;
}
if (current_mode) {
current_mode->pointer_action(pointer, action, old_state, new_state);
}
p->second = new_state;
//- - - - - - - - - - - - - - - - - - - - - - - - - - - -
} else if (action == PointerMove) {
auto p = pointers.find(pointer);
if (p == pointers.end()) {
std::cerr << "WARNING: got a Move action for a pointer that isn't present." << std::endl;
return;
}
Pointer const &old_state = p->second;
if (new_state.at == old_state.at && new_state.pressure == old_state.pressure) {
//std::cerr << "WARNING: pointer didn't move during Move action." << std::endl;
}
if (new_state.buttons != old_state.buttons) {
std::cerr << "WARNING: button change during Move action." << std::endl;
}
if (current_mode) {
current_mode->pointer_action(pointer, action, old_state, new_state);
}
p->second = new_state;
} else {
assert(0 && "Invalid PointerAction");
}
}