Ember
Loading...
Searching...
No Matches
BehaviorTreeScene.cpp
Go to the documentation of this file.
3#include "App/App.h"
4#include "App/MainFrame.h"
6#include "Utils/Logger.h"
7
8BehaviorTreeScene::BehaviorTreeScene(wxWindow *parent, const wxString &title)
9 : m_panel(nullptr), m_treeVisualization(nullptr), m_behaviorTreeRoot(nullptr), m_treeAdapter(nullptr),
10 m_title(title), m_hasUnsavedChanges(false) {
11 m_panel = new wxPanel(parent, wxID_ANY);
12 m_panel->SetBackgroundColour(wxColour(45, 45, 50));
13
14 MainFrame *mainFrame = dynamic_cast<MainFrame *>(wxGetApp().GetTopWindow());
15 if (mainFrame) {
16 m_panel->SetDropTarget(
17 new EmberForge::XMLFileDropTarget([mainFrame](const wxString &filePath, bool loadInCurrentScene) {
18 if (loadInCurrentScene) {
19 mainFrame->LoadXMLFile(filePath, true);
20 } else {
21 mainFrame->LoadXMLFileInNewScene(filePath);
22 }
23 }));
24 }
25
27}
28
31 m_treeVisualization->SetNodeSelectionCallback(nullptr);
32 m_treeVisualization->SetVisibilityChangeCallback(nullptr);
33 m_treeVisualization->SetTree(nullptr);
34 m_treeVisualization = nullptr;
35 }
37}
38
40 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
41
43
44 m_treeVisualization->SetNodeSelectionCallback([this](EmberCore::ITreeNode *node) {
47 }
48 });
49
50 if (m_treeAdapter) {
52 }
53
54 sizer->Add(m_treeVisualization, 1, wxEXPAND | wxALL, 5);
55
56 m_panel->SetSizer(sizer);
57}
58
61 m_treeVisualization->RefreshCanvas();
62 m_treeVisualization->SetFocus();
63 }
64}
65
67
70 m_treeVisualization->RefreshCanvas();
71 }
72}
73
75 m_hasUnsavedChanges = false;
76 return true;
77}
78
79void BehaviorTreeScene::SetBehaviorTree(std::shared_ptr<EmberCore::Node> root) {
80 m_behaviorTreeRoot = root;
81
83 auto behaviorTree = std::make_shared<EmberCore::BehaviorTree>("Scene Tree");
84 behaviorTree->SetRootNode(m_behaviorTreeRoot);
85 m_treeAdapter = std::make_shared<EmberCore::DirectTreeAdapter>(behaviorTree);
86 LOG_INFO("Scene",
87 "Using DirectTreeAdapter with BehaviorTree wrapper: " + std::string(m_behaviorTreeRoot->GetName()));
88 } else {
89 m_treeAdapter = nullptr;
90 }
91
95 }
96
97 LOG_INFO("Scene", "Behavior tree updated with shared root node: " +
98 std::string(m_behaviorTreeRoot ? m_behaviorTreeRoot->GetName() : "null"));
99}
100
101void BehaviorTreeScene::SetBehaviorTree(std::shared_ptr<EmberCore::ITreeStructure> tree) {
102 m_treeAdapter = tree;
103 m_behaviorTreeRoot = nullptr;
104
107 m_hasUnsavedChanges = true;
108 }
109
110 LOG_INFO("Scene", "Behavior tree updated with abstracted tree: " + std::string(tree ? tree->GetName() : "null"));
111}
112
115 m_behaviorTreeRoot = std::shared_ptr<EmberCore::Node>(uniqueTree.release());
116
117 if (m_behaviorTreeRoot) {
118 auto behaviorTree = std::make_shared<EmberCore::BehaviorTree>("Sample Tree");
119 behaviorTree->SetRootNode(m_behaviorTreeRoot);
120 m_treeAdapter = std::make_shared<EmberCore::DirectTreeAdapter>(behaviorTree);
121 LOG_INFO("Scene", "Using DirectTreeAdapter with BehaviorTree wrapper for sample tree: " +
122 std::string(m_behaviorTreeRoot->GetName()));
123 }
124
127 }
128
129 m_hasUnsavedChanges = true;
130
131 LOG_INFO("Scene", "Created sample behavior tree with " +
132 std::to_string(m_behaviorTreeRoot ? m_behaviorTreeRoot->GetSubtreeNodeCount() : 0) +
133 " nodes");
134}
135
136void BehaviorTreeScene::SetTitle(const wxString &title) { m_title = title; }
137
139 if (m_panel) {
140 m_panel->SetBackgroundColour(color);
141 m_panel->Refresh();
142 }
143}
#define LOG_INFO(category, message)
Definition Logger.h:114
NodeSelectionCallback m_nodeSelectionCallback
BehaviorTreeScene(wxWindow *parent, const wxString &title="Behavior Tree")
Construct a new BehaviorTreeScene.
std::shared_ptr< EmberCore::ITreeStructure > m_treeAdapter
void OnDeactivated() override
Called when the scene becomes inactive.
void CreateSampleBehaviorTree()
Create a sample behavior tree for testing/demo purposes.
std::shared_ptr< EmberCore::Node > m_behaviorTreeRoot
void Refresh() override
Refreshes the scene content.
void OnActivated() override
Called when the scene becomes active.
void SetTitle(const wxString &title)
Set the title of this scene.
void SetCanvasBackgroundColor(const wxColour &color)
Set the canvas background color.
void SetBehaviorTree(std::shared_ptr< EmberCore::Node > root)
Set the behavior tree for this scene using legacy Node interface.
EmberForge::ForgeTreeCanvas * m_treeVisualization
bool Save() override
Saves the scene content; returns true on success.
Abstract interface for tree nodes that can be visualized.
Definition ITreeNode.h:31
static std::unique_ptr< Node > CreateSampleTree()
Definition Node.cpp:420
File drop target for XML files.
Main application window for EmberForge.
Definition MainFrame.h:67
void LoadXMLFile(const wxString &filePath, bool confirmOverride=false)
void LoadXMLFileInNewScene(const wxString &filePath)