manual_node.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015-2018 Michele Colledanchise - All Rights Reserved
2  * Copyright (C) 2018-2020 Davide Faconti, Eurecat - All Rights Reserved
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
5 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
6 * 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:
7 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
10 * 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,
11 * 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.
12 */
13 
16 #include <ncurses.h>
17 
18 namespace BT
19 {
21  const NodeConfiguration& config) :
22  ControlNode::ControlNode(name, config),
23  running_child_idx_(-1),
24  previously_executed_idx_(-1)
25 {
26  setRegistrationID("ManualSelector");
27 }
28 
30 {
31  if (running_child_idx_ >= 0)
32  {
34  }
35  running_child_idx_ = -1;
37 }
38 
40 {
41  const size_t children_count = children_nodes_.size();
42 
43  if (children_count == 0)
44  {
45  return selectStatus();
46  }
47 
48  bool repeat_last = false;
49  getInput(REPEAT_LAST_SELECTION, repeat_last);
50 
51  int idx = 0;
52 
53  if (repeat_last && previously_executed_idx_ >= 0)
54  {
56  }
57  else
58  {
60  idx = selectChild();
62 
63  if (idx == NUM_SUCCESS)
64  {
65  return NodeStatus::SUCCESS;
66  }
67  if (idx == NUM_FAILURE)
68  {
69  return NodeStatus::FAILURE;
70  }
71  if (idx == NUM_RUNNING)
72  {
73  return NodeStatus::RUNNING;
74  }
75  }
76 
77  NodeStatus ret = children_nodes_[idx]->executeTick();
78  if (ret == NodeStatus::RUNNING)
79  {
80  running_child_idx_ = idx;
81  }
82  return ret;
83 }
84 
86 {
87  WINDOW* win;
88  initscr();
89  cbreak();
90 
91  win = newwin(6, 70, 1, 1); // create a new window
92 
93  mvwprintw(win, 0, 0, "No children.");
94  mvwprintw(win, 1, 0, "Press: S to return SUCCESFULL,");
95  mvwprintw(win, 2, 0, " F to return FAILURE, or");
96  mvwprintw(win, 3, 0, " R to return RUNNING.");
97 
98  wrefresh(win); // update the terminal screen
99  noecho(); // disable echoing of characters on the screen
100  keypad(win, TRUE); // enable keyboard input for the window.
101  curs_set(0); // hide the default screen cursor.
102 
103  int ch = 0;
104  NodeStatus ret;
105  while (1)
106  {
107  if (ch == 's' || ch == 'S')
108  {
109  ret = NodeStatus::SUCCESS;
110  break;
111  }
112  else if (ch == 'f' || ch == 'F')
113  {
114  ret = NodeStatus::FAILURE;
115  break;
116  }
117  else if (ch == 'r' || ch == 'R')
118  {
119  ret = NodeStatus::RUNNING;
120  break;
121  }
122  ch = wgetch(win);
123  }
124  werase(win);
125  wrefresh(win);
126  delwin(win);
127  endwin();
128  return ret;
129 }
130 
132 {
133  const size_t children_count = children_nodes_.size();
134 
135  std::vector<std::string> list;
136  list.reserve(children_count);
137  for (const auto& child : children_nodes_)
138  {
139  list.push_back(child->name());
140  }
141 
142  size_t width = 10;
143  for (const auto& str : list)
144  {
145  width = std::max(width, str.size() + 2);
146  }
147 
148  WINDOW* win;
149  initscr();
150  cbreak();
151 
152  win = newwin(children_count + 6, 70, 1, 1); // create a new window
153 
154  mvwprintw(win, 0, 0, "Use UP/DOWN arrow to select the child, Enter to confirm.");
155  mvwprintw(win, 1, 0, "Press: S to skip and return SUCCESFULL,");
156  mvwprintw(win, 2, 0, " F to skip and return FAILURE, or");
157  mvwprintw(win, 3, 0, " R to skip and return RUNNING.");
158 
159  // now print all the menu items and highlight the first one
160  for (size_t i = 0; i < list.size(); i++)
161  {
162  mvwprintw(win, i + 5, 0, "%2ld. %s", i + 1, list[i].c_str());
163  }
164 
165  wrefresh(win); // update the terminal screen
166  noecho(); // disable echoing of characters on the screen
167  keypad(win, TRUE); // enable keyboard input for the window.
168  curs_set(0); // hide the default screen cursor.
169 
170  uint8_t row = 0;
171  int ch = 0;
172  while (1)
173  {
174  // right pad with spaces to make the items appear with even width.
175  wattroff(win, A_STANDOUT);
176  mvwprintw(win, row + 5, 4, "%s", list[row].c_str());
177  // use a variable to increment or decrement the value based on the input.
178  if (ch == KEY_DOWN)
179  {
180  row = (row == children_count - 1) ? 0 : row + 1;
181  }
182  else if (ch == KEY_UP)
183  {
184  row = (row == 0) ? (children_count - 1) : row - 1;
185  }
186  else if (ch == KEY_ENTER || ch == 10)
187  {
188  break;
189  }
190  else if (ch == 's' || ch == 'S')
191  {
192  row = NUM_SUCCESS;
193  break;
194  }
195  else if (ch == 'f' || ch == 'F')
196  {
197  row = NUM_FAILURE;
198  break;
199  }
200  else if (ch == 'r' || ch == 'R')
201  {
202  row = NUM_RUNNING;
203  break;
204  }
205 
206  // now highlight the next item in the list.
207  wattron(win, A_STANDOUT);
208  mvwprintw(win, row + 5, 4, "%s", list[row].c_str());
209  ch = wgetch(win);
210  };
211 
212  werase(win);
213  wrefresh(win);
214  delwin(win);
215  endwin();
216  return row;
217 }
218 
219 } // namespace BT
virtual void halt() override
Definition: manual_node.cpp:29
const TreeNode * child(size_t index) const
Definition: control_node.h:38
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:24
virtual void halt() override
static constexpr const char * REPEAT_LAST_SELECTION
Definition: manual_node.h:40
uint8_t selectChild() const
ManualSelectorNode(const std::string &name, const NodeConfiguration &config)
Definition: manual_node.cpp:20
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:101
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:232
NodeStatus selectStatus() const
Definition: manual_node.cpp:85
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:201
void haltChild(size_t i)
NodeStatus
Definition: basic_types.h:35
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: manual_node.cpp:39
void setStatus(NodeStatus new_status)
Definition: tree_node.cpp:63


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Mon Jul 3 2023 02:50:14