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 {
20 ManualSelectorNode::ManualSelectorNode(const std::string& name, const NodeConfig& config)
21  : ControlNode::ControlNode(name, config)
22  , running_child_idx_(-1)
23  , previously_executed_idx_(-1)
24 {
25  setRegistrationID("ManualSelector");
26 }
27 
29 {
30  if(running_child_idx_ >= 0)
31  {
33  }
34  running_child_idx_ = -1;
36 }
37 
39 {
40  const size_t children_count = children_nodes_.size();
41 
42  if(children_count == 0)
43  {
44  return selectStatus();
45  }
46 
47  bool repeat_last = false;
48  getInput(REPEAT_LAST_SELECTION, repeat_last);
49 
50  int idx = 0;
51 
52  if(repeat_last && previously_executed_idx_ >= 0)
53  {
55  }
56  else
57  {
59  idx = selectChild();
61 
62  if(idx == NUM_SUCCESS)
63  {
64  return NodeStatus::SUCCESS;
65  }
66  if(idx == NUM_FAILURE)
67  {
68  return NodeStatus::FAILURE;
69  }
70  if(idx == NUM_RUNNING)
71  {
72  return NodeStatus::RUNNING;
73  }
74  }
75 
76  NodeStatus ret = children_nodes_[idx]->executeTick();
77  if(ret == NodeStatus::RUNNING)
78  {
79  running_child_idx_ = idx;
80  }
81  return ret;
82 }
83 
85 {
86  WINDOW* win;
87  initscr();
88  cbreak();
89 
90  win = newwin(6, 70, 1, 1); // create a new window
91 
92  mvwprintw(win, 0, 0, "No children.");
93  mvwprintw(win, 1, 0, "Press: S to return SUCCESSFUL,");
94  mvwprintw(win, 2, 0, " F to return FAILURE, or");
95  mvwprintw(win, 3, 0, " R to return RUNNING.");
96 
97  wrefresh(win); // update the terminal screen
98  noecho(); // disable echoing of characters on the screen
99  keypad(win, TRUE); // enable keyboard input for the window.
100  curs_set(0); // hide the default screen cursor.
101 
102  int ch = 0;
103  NodeStatus ret;
104  while(1)
105  {
106  if(ch == 's' || ch == 'S')
107  {
108  ret = NodeStatus::SUCCESS;
109  break;
110  }
111  else if(ch == 'f' || ch == 'F')
112  {
113  ret = NodeStatus::FAILURE;
114  break;
115  }
116  else if(ch == 'r' || ch == 'R')
117  {
118  ret = NodeStatus::RUNNING;
119  break;
120  }
121  ch = wgetch(win);
122  }
123  werase(win);
124  wrefresh(win);
125  delwin(win);
126  endwin();
127  return ret;
128 }
129 
131 {
132  const size_t children_count = children_nodes_.size();
133 
134  std::vector<std::string> list;
135  list.reserve(children_count);
136  for(const auto& child : children_nodes_)
137  {
138  list.push_back(child->name());
139  }
140 
141  size_t width = 10;
142  for(const auto& str : list)
143  {
144  width = std::max(width, str.size() + 2);
145  }
146 
147  WINDOW* win;
148  initscr();
149  cbreak();
150 
151  win = newwin(children_count + 6, 70, 1, 1); // create a new window
152 
153  mvwprintw(win, 0, 0, "Use UP/DOWN arrow to select the child, Enter to confirm.");
154  mvwprintw(win, 1, 0, "Press: S to skip and return SUCCESSFUL,");
155  mvwprintw(win, 2, 0, " F to skip and return FAILURE, or");
156  mvwprintw(win, 3, 0, " R to skip and return RUNNING.");
157 
158  // now print all the menu items and highlight the first one
159  for(size_t i = 0; i < list.size(); i++)
160  {
161  mvwprintw(win, i + 5, 0, "%2ld. %s", i + 1, list[i].c_str());
162  }
163 
164  wrefresh(win); // update the terminal screen
165  noecho(); // disable echoing of characters on the screen
166  keypad(win, TRUE); // enable keyboard input for the window.
167  curs_set(0); // hide the default screen cursor.
168 
169  uint8_t row = 0;
170  int ch = 0;
171  while(1)
172  {
173  // right pad with spaces to make the items appear with even width.
174  wattroff(win, A_STANDOUT);
175  mvwprintw(win, row + 5, 4, "%s", list[row].c_str());
176  // use a variable to increment or decrement the value based on the input.
177  if(ch == KEY_DOWN)
178  {
179  row = (row == children_count - 1) ? 0 : row + 1;
180  }
181  else if(ch == KEY_UP)
182  {
183  row = (row == 0) ? (children_count - 1) : row - 1;
184  }
185  else if(ch == KEY_ENTER || ch == 10)
186  {
187  break;
188  }
189  else if(ch == 's' || ch == 'S')
190  {
191  row = NUM_SUCCESS;
192  break;
193  }
194  else if(ch == 'f' || ch == 'F')
195  {
196  row = NUM_FAILURE;
197  break;
198  }
199  else if(ch == 'r' || ch == 'R')
200  {
201  row = NUM_RUNNING;
202  break;
203  }
204 
205  // now highlight the next item in the list.
206  wattron(win, A_STANDOUT);
207  mvwprintw(win, row + 5, 4, "%s", list[row].c_str());
208  ch = wgetch(win);
209  }
210 
211  werase(win);
212  wrefresh(win);
213  delwin(win);
214  endwin();
215  return row;
216 }
217 
218 } // namespace BT
BT::TreeNode::getInput
Result getInput(const std::string &key, T &destination) const
Definition: tree_node.h:547
BT
Definition: ex01_wrap_legacy.cpp:29
BT::ManualSelectorNode::NUM_FAILURE
@ NUM_FAILURE
Definition: manual_node.h:50
BT::ManualSelectorNode::NUM_RUNNING
@ NUM_RUNNING
Definition: manual_node.h:51
BT::ManualSelectorNode::halt
virtual void halt() override
Definition: manual_node.cpp:28
BT::ManualSelectorNode::REPEAT_LAST_SELECTION
static constexpr const char * REPEAT_LAST_SELECTION
Definition: manual_node.h:41
BT::ControlNode::children_nodes_
std::vector< TreeNode * > children_nodes_
Definition: control_node.h:24
lexyd::list
constexpr auto list(Item)
Parses a list of items without a separator.
Definition: list.hpp:133
BT::ManualSelectorNode::ManualSelectorNode
ManualSelectorNode(const std::string &name, const NodeConfig &config)
Definition: manual_node.cpp:20
BT::ManualSelectorNode::running_child_idx_
int running_child_idx_
Definition: manual_node.h:44
BT::ManualSelectorNode::previously_executed_idx_
int previously_executed_idx_
Definition: manual_node.h:45
BT::ManualSelectorNode::selectChild
uint8_t selectChild() const
Definition: manual_node.cpp:130
BT::ManualSelectorNode::NUM_SUCCESS
@ NUM_SUCCESS
Definition: manual_node.h:49
BT::NodeStatus::FAILURE
@ FAILURE
BT::TreeNode::setStatus
void setStatus(NodeStatus new_status)
setStatus changes the status of the node. it will throw if you try to change the status to IDLE,...
Definition: tree_node.cpp:160
action_node.h
BT::TreeNode::setRegistrationID
void setRegistrationID(StringView ID)
Definition: tree_node.cpp:439
BT::TreeNode::name
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:302
BT::NodeStatus::SUCCESS
@ SUCCESS
BT::ControlNode::haltChild
void haltChild(size_t i)
Definition: control_node.cpp:55
BT::NodeStatus::RUNNING
@ RUNNING
manual_node.h
BT::ControlNode::child
const TreeNode * child(size_t index) const
Definition: control_node.h:38
BT::ManualSelectorNode::tick
virtual BT::NodeStatus tick() override
Method to be implemented by the user.
Definition: manual_node.cpp:38
BT::ManualSelectorNode::selectStatus
NodeStatus selectStatus() const
Definition: manual_node.cpp:84
BT::ControlNode::halt
virtual void halt() override
Definition: control_node.cpp:32
BT::NodeConfig
Definition: tree_node.h:73
TRUE
#define TRUE
Definition: minitrace.cpp:44
BT::ControlNode
Definition: control_node.h:21
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:17