forked from BehaviorTree/BehaviorTree.CPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanual_node.cpp
More file actions
218 lines (191 loc) · 5.69 KB
/
manual_node.cpp
File metadata and controls
218 lines (191 loc) · 5.69 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
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
/* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
* Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "behaviortree_cpp/controls/manual_node.h"
#include "behaviortree_cpp/action_node.h"
#include <curses.h>
namespace BT
{
ManualSelectorNode::ManualSelectorNode(const std::string& name, const NodeConfig& config)
: ControlNode::ControlNode(name, config)
, running_child_idx_(-1)
, previously_executed_idx_(-1)
{
setRegistrationID("ManualSelector");
}
void ManualSelectorNode::halt()
{
if(running_child_idx_ >= 0)
{
haltChild(size_t(running_child_idx_));
}
running_child_idx_ = -1;
ControlNode::halt();
}
NodeStatus ManualSelectorNode::tick()
{
const size_t children_count = children_nodes_.size();
if(children_count == 0)
{
return selectStatus();
}
bool repeat_last = false;
getInput(REPEAT_LAST_SELECTION, repeat_last);
int idx = 0;
if(repeat_last && previously_executed_idx_ >= 0)
{
idx = previously_executed_idx_;
}
else
{
setStatus(NodeStatus::RUNNING);
idx = selectChild();
previously_executed_idx_ = idx;
if(idx == NUM_SUCCESS)
{
return NodeStatus::SUCCESS;
}
if(idx == NUM_FAILURE)
{
return NodeStatus::FAILURE;
}
if(idx == NUM_RUNNING)
{
return NodeStatus::RUNNING;
}
}
NodeStatus ret = children_nodes_[idx]->executeTick();
if(ret == NodeStatus::RUNNING)
{
running_child_idx_ = idx;
}
return ret;
}
NodeStatus ManualSelectorNode::selectStatus() const
{
WINDOW* win;
initscr();
cbreak();
win = newwin(6, 70, 1, 1); // create a new window
mvwprintw(win, 0, 0, "No children.");
mvwprintw(win, 1, 0, "Press: S to return SUCCESSFUL,");
mvwprintw(win, 2, 0, " F to return FAILURE, or");
mvwprintw(win, 3, 0, " R to return RUNNING.");
wrefresh(win); // update the terminal screen
noecho(); // disable echoing of characters on the screen
keypad(win, TRUE); // enable keyboard input for the window.
curs_set(0); // hide the default screen cursor.
int ch = 0;
NodeStatus ret;
while(1)
{
if(ch == 's' || ch == 'S')
{
ret = NodeStatus::SUCCESS;
break;
}
else if(ch == 'f' || ch == 'F')
{
ret = NodeStatus::FAILURE;
break;
}
else if(ch == 'r' || ch == 'R')
{
ret = NodeStatus::RUNNING;
break;
}
ch = wgetch(win);
}
werase(win);
wrefresh(win);
delwin(win);
endwin();
return ret;
}
uint8_t ManualSelectorNode::selectChild() const
{
const size_t children_count = children_nodes_.size();
std::vector<std::string> list;
list.reserve(children_count);
for(const auto& child : children_nodes_)
{
list.push_back(child->name());
}
size_t width = 10;
for(const auto& str : list)
{
width = std::max(width, str.size() + 2);
}
WINDOW* win;
initscr();
cbreak();
win = newwin(children_count + 6, 70, 1, 1); // create a new window
mvwprintw(win, 0, 0, "Use UP/DOWN arrow to select the child, Enter to confirm.");
mvwprintw(win, 1, 0, "Press: S to skip and return SUCCESSFUL,");
mvwprintw(win, 2, 0, " F to skip and return FAILURE, or");
mvwprintw(win, 3, 0, " R to skip and return RUNNING.");
// now print all the menu items and highlight the first one
for(size_t i = 0; i < list.size(); i++)
{
mvwprintw(win, i + 5, 0, "%2ld. %s", i + 1, list[i].c_str());
}
wrefresh(win); // update the terminal screen
noecho(); // disable echoing of characters on the screen
keypad(win, TRUE); // enable keyboard input for the window.
curs_set(0); // hide the default screen cursor.
uint8_t row = 0;
int ch = 0;
while(1)
{
// right pad with spaces to make the items appear with even width.
wattroff(win, A_STANDOUT);
mvwprintw(win, row + 5, 4, "%s", list[row].c_str());
// use a variable to increment or decrement the value based on the input.
if(ch == KEY_DOWN)
{
row = (row == children_count - 1) ? 0 : row + 1;
}
else if(ch == KEY_UP)
{
row = (row == 0) ? (children_count - 1) : row - 1;
}
else if(ch == KEY_ENTER || ch == 10)
{
break;
}
else if(ch == 's' || ch == 'S')
{
row = NUM_SUCCESS;
break;
}
else if(ch == 'f' || ch == 'F')
{
row = NUM_FAILURE;
break;
}
else if(ch == 'r' || ch == 'R')
{
row = NUM_RUNNING;
break;
}
// now highlight the next item in the list.
wattron(win, A_STANDOUT);
mvwprintw(win, row + 5, 4, "%s", list[row].c_str());
ch = wgetch(win);
}
werase(win);
wrefresh(win);
delwin(win);
endwin();
return row;
}
} // namespace BT