-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwitchControl.cpp
More file actions
145 lines (129 loc) · 5.98 KB
/
SwitchControl.cpp
File metadata and controls
145 lines (129 loc) · 5.98 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
141
142
143
144
145
#include <utility>
// TODO utility needed?
#include "SwitchControl.h"
int SwitchControl::s_currentY = 5;
int SwitchControl::s_currentID = 0;
SwitchControl::SwitchControl(
const std::vector<std::string>& states,
const std::string& keyBackward,
const std::string& keyForward,
std::function<void(int, const std::vector<int>&)> callee)
:
m_currentID(s_currentID++),
m_currentY(s_currentY + 2 * c_textHeight),
m_states(states),
m_keyBackward(keyBackward),
m_keyForward(keyForward),
m_currentState(0),
m_callee(std::move(callee)) {
s_currentY += 2 * c_textHeight + (states.size() + 1) * c_textHeight;
}
void SwitchControl::addToVisualizer(pcl::visualization::PCLVisualizer &viewer, int viewportID) {
int numStates = (int) m_states.size();
int maxY = (numStates + 1) * c_textHeight + m_currentY;
std::string idStr = "switch " + std::to_string(m_currentID) + " ";
std::string id2Str = "switch2 " + std::to_string(m_currentID) + " ";
std::vector<std::string> propertyNames;
if (m_keyForward == "Right") {
maxY = 65;
// add 4-switch keys
viewer.addText(
"[ Up | Down | Left | Right ]",
200, maxY,
1, 0, 0,
id2Str + "keys",
viewportID);
propertyNames = std::vector<std::string>{"age", "weight", "gender"};
int i = 0;
for (auto& state : propertyNames) {
viewer.addText(
(i == m_selectedProp ? ">" : " ") + state + ": " + std::to_string(m_props[i]),
200, maxY - (i + 1) * c_textHeight,
0, 1, 0,
id2Str + "option " + std::to_string(i),
viewportID);
i++;
}
} else {
// add header text
viewer.addText(
"[ " + (m_keyBackward.empty() ? "" : (m_keyBackward + " | ")) + m_keyForward + " ]",
20, maxY,
1, 0, 0,
idStr + "keys",
viewportID);
// add option texts
int i = 0;
for (auto &state : m_states) {
viewer.addText(
(i == m_currentState ? ">" : " ") + state,
30, maxY - (i + 1) * c_textHeight,
0, 1, 0,
idStr + "option " + std::to_string(i),
viewportID);
i++;
}
}
// register keypressed callback
if (!m_callbackConnection.connected()) {
m_callbackConnection = viewer.registerKeyboardCallback(
[this, idStr, maxY, numStates, propertyNames, id2Str, &viewer](const pcl::visualization::KeyboardEvent keyEvent) {
if (!keyEvent.keyUp())
return;
int dir = 0;
if (m_keyForward == "Right") {
// update 4-switch
dir = 0;
if (keyEvent.getKeySym() == "Up")
dir = -1;
else if (keyEvent.getKeySym()== "Down")
dir = +1;
int modDir = 0;
if (keyEvent.getKeySym() == "Left")
modDir = -1;
else if (keyEvent.getKeySym()== "Right")
modDir = +1;
if (keyEvent.isShiftPressed())
modDir *= 10;
if (dir != 0 || modDir != 0) {
m_props[m_selectedProp] = m_props[m_selectedProp] + modDir;
viewer.updateText(" " + propertyNames[m_selectedProp] + ": " +
std::to_string(m_props[m_selectedProp]), 200,
maxY - (m_selectedProp + 1) * c_textHeight, 0, 1, 0,
id2Str + "option " + std::to_string(m_selectedProp));
m_selectedProp = (m_selectedProp + propertyNames.size() + dir) % propertyNames.size();
viewer.updateText(">" + propertyNames[m_selectedProp] + ": " +
std::to_string(m_props[m_selectedProp]), 200,
maxY - (m_selectedProp + 1) * c_textHeight, 0, 1, 0,
id2Str + "option " + std::to_string(m_selectedProp));
m_callee(m_currentState, m_props);
}
} else {
if (keyEvent.getKeySym() == m_keyBackward)
dir = -1;
// Note that tolower doesn't work for non-ASCII characters, because C++ is horrible at it.
else if (keyEvent.getKeySym() == m_keyForward || std::tolower(keyEvent.getKeySym()[0]) == m_keyForward[0])
dir = (keyEvent.isShiftPressed() ? -1 : +1);
else
return;
// update currently active text
viewer.updateText(
" " + m_states[m_currentState],
30, maxY - (m_currentState + 1) * c_textHeight,
0, 1, 0,
idStr + "option " + std::to_string(m_currentState));
m_currentState = (m_currentState + numStates + dir) % numStates;
// update now active text
viewer.updateText(
">" + m_states[m_currentState],
30, maxY - (m_currentState + 1) * c_textHeight,
0, 1, 0,
idStr + "option " + std::to_string(m_currentState));
m_callee(m_currentState, m_props);
}
});
}
}
int SwitchControl::getState() {
return m_currentState;
}