Ember
Loading...
Searching...
No Matches
NodeAdapter.cpp
Go to the documentation of this file.
2#include <algorithm>
3#include <stdexcept>
4
5namespace EmberCore {
6
8 if (!wrapped_node_) {
9 throw std::invalid_argument("NodeAdapter: wrapped_node cannot be null");
10 }
11}
12
13const EmberCore::String &NodeAdapter::GetName() const { return wrapped_node_->GetName(); }
14
16
18
19size_t NodeAdapter::GetId() const { return wrapped_node_->GetId(); }
20
21size_t NodeAdapter::GetChildCount() const { return wrapped_node_->GetChildCount(); }
22
23ITreeNode *NodeAdapter::GetChild(size_t index) const {
25
26 if (index >= child_adapters_.size()) {
27 return nullptr;
28 }
29
30 return child_adapters_[index].get();
31}
32
34 if (!wrapped_node_->GetParent()) {
35 return nullptr;
36 }
37
39 return parent_adapter_.get();
40}
41
42const EmberCore::Point &NodeAdapter::GetPosition() const { return wrapped_node_->GetPosition(); }
43
44void NodeAdapter::SetPosition(const EmberCore::Point &position) { wrapped_node_->SetPosition(position); }
45
49
51
52bool NodeAdapter::AreChildrenVisible() const { return wrapped_node_->AreChildrenVisible(); }
53
54void NodeAdapter::SetChildrenVisible(bool visible) { wrapped_node_->SetChildrenVisible(visible); }
55
56int NodeAdapter::GetChildrenWidth() const { return wrapped_node_->GetChildrenWidth(); }
57
58void NodeAdapter::SetChildrenWidth(int width) { wrapped_node_->SetChildrenWidth(width); }
59
60int NodeAdapter::CalculateSubtreeWidth(int node_width, int horizontal_spacing) const {
61 return wrapped_node_->CalculateSubtreeWidth(node_width, horizontal_spacing);
62}
63
64size_t NodeAdapter::GetDepth() const { return wrapped_node_->GetDepth(); }
65
66size_t NodeAdapter::GetSubtreeNodeCount() const { return wrapped_node_->GetSubtreeNodeCount(); }
67
68void NodeAdapter::Accept(std::function<void(ITreeNode *)> visitor) {
69 visitor(this);
70
71 // Visit children
73 for (auto &child_adapter : child_adapters_) {
74 if (child_adapter) {
75 child_adapter->Accept(visitor);
76 }
77 }
78}
79
80void NodeAdapter::Accept(std::function<void(const ITreeNode *)> visitor) const {
81 visitor(this);
82
83 // Visit children
85 for (const auto &child_adapter : child_adapters_) {
86 if (child_adapter) {
87 static_cast<const ITreeNode *>(child_adapter.get())->Accept(visitor);
88 }
89 }
90}
91
92// Helper methods
93
95 switch (type) {
97 return NodeType::Action;
99 return NodeType::Control;
101 return NodeType::Condition;
103 return NodeType::Decorator;
106 default:
107 return NodeType::Unknown;
108 }
109}
110
112 switch (status) {
114 return NodeStatus::Idle;
116 return NodeStatus::Running;
118 return NodeStatus::Success;
120 return NodeStatus::Failure;
121 default:
122 return NodeStatus::Idle;
123 }
124}
125
140
155
157 if (child_adapters_.size() != wrapped_node_->GetChildCount()) {
158 child_adapters_.clear();
159 child_adapters_.reserve(wrapped_node_->GetChildCount());
160
161 for (size_t i = 0; i < wrapped_node_->GetChildCount(); ++i) {
162 Node *child = wrapped_node_->GetChild(i);
163 if (child) {
164 child_adapters_.push_back(std::make_unique<NodeAdapter>(child));
165 } else {
166 child_adapters_.push_back(nullptr);
167 }
168 }
169 }
170}
171
173 if (!parent_adapter_ && wrapped_node_->GetParent()) {
174 parent_adapter_ = std::make_unique<NodeAdapter>(wrapped_node_->GetParent());
175 }
176}
177
179 // Check if this node has a SubTree reference attribute
180 String subtree_ref = wrapped_node_->GetAttribute("__subtree_ref__", "");
181 return !subtree_ref.empty();
182}
183
185 // Check if this node is marked as an unimplemented reference
186 String unimplemented = wrapped_node_->GetAttribute("__unimplemented__", "");
187 return unimplemented == "true";
188}
189
190String NodeAdapter::GetSubTreeReference() const { return wrapped_node_->GetAttribute("__subtree_ref__", ""); }
191
192} // namespace EmberCore
Abstract interface for tree nodes that can be visualized.
Definition ITreeNode.h:31
VisualState
Visual state for UI rendering.
Definition ITreeNode.h:58
virtual void Accept(std::function< void(ITreeNode *)> visitor)=0
NodeType
Node types for visualization categorization.
Definition ITreeNode.h:36
NodeStatus
Node status for runtime visualization.
Definition ITreeNode.h:48
size_t GetDepth() const override
void EnsureChildAdapters() const
NodeAdapter(Node *node)
Constructor that wraps an existing Node.
size_t GetSubtreeNodeCount() const override
ITreeNode * GetParent() const override
bool IsUnimplementedReference() const override
Check if this node references an unimplemented tree.
String GetSubTreeReference() const override
Get the ID of the referenced SubTree (if this is a SubTree node)
int GetChildrenWidth() const override
void SetChildrenWidth(int width) override
int CalculateSubtreeWidth(int node_width, int horizontal_spacing) const override
void EnsureParentAdapter() const
const EmberCore::Point & GetPosition() const override
bool AreChildrenVisible() const override
void Accept(std::function< void(ITreeNode *)> visitor) override
void SetPosition(const EmberCore::Point &position) override
size_t GetChildCount() const override
NodeType GetType() const override
VisualState GetVisualState() const override
NodeStatus GetStatus() const override
NodeStatus ConvertNodeStatus(Node::Status status) const
std::vector< std::unique_ptr< NodeAdapter > > child_adapters_
Definition NodeAdapter.h:71
NodeType ConvertNodeType(Node::Type type) const
std::unique_ptr< NodeAdapter > parent_adapter_
Definition NodeAdapter.h:72
bool IsSubTreeReference() const override
Check if this node is a SubTree reference.
VisualState ConvertVisualState(Node::VisualState state) const
void SetChildrenVisible(bool visible) override
const EmberCore::String & GetName() const override
void SetVisualState(VisualState state) override
size_t GetId() const override
ITreeNode * GetChild(size_t index) const override
Represents a node in a behavior tree structure.
Definition Node.h:20
Status
Node status for runtime execution tracking.
Definition Node.h:37
VisualState
Visual state for UI rendering.
Definition Node.h:47
Type
Node types for behavior tree classification.
Definition Node.h:25
Main types header for EmberCore.
std::string String
Framework-agnostic string type.
Definition String.h:14
2D point with integer coordinates
Definition Geometry.h:8