Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BehaviorTreeDebugWindow_NodeGraph.cpp
Go to the documentation of this file.
1/**
2 * @file BehaviorTreeDebugWindow_NodeGraph.cpp
3 * @brief NodeGraph debug panel methods for BehaviorTreeDebugWindow (F10)
4 *
5 * @details
6 * Implements the three new methods added to BehaviorTreeDebugWindow:
7 * - InitNodeGraphDebugMode()
8 * - ShutdownNodeGraphDebugMode()
9 * - RenderNodeGraphDebugPanel()
10 *
11 * These replace the legacy RenderNodeGraphPanel() / RenderBehaviorTreeGraph()
12 * rendering path with the full Blueprint Editor pipeline:
13 * BTGraphDocumentConverter -> Olympe::NodeGraph -> NodeGraphPanel::RenderGraph()
14 *
15 * The NodeGraph registered here is READ-ONLY (Runtime EditorContext mode).
16 * No modifications are written back to the BehaviorTreeAsset.
17 */
18
20#include "../NodeGraphShared/BTGraphDocumentConverter.h"
21#include "../BlueprintEditor/BTNodeGraphManager.h"
22#include "../BlueprintEditor/NodeGraphPanel.h"
23#include "../BlueprintEditor/EditorContext.h"
24#include "BehaviorTree.h"
25#include "../World.h"
26#include "../ECS_Components_AI.h"
27#include "../third_party/imgui/imgui.h"
28#include <iostream>
29
30namespace Olympe {
31
32// ---------------------------------------------------------------------------
33// InitNodeGraphDebugMode
34// ---------------------------------------------------------------------------
35
37{
38 // Set EditorContext to Runtime mode (read-only — no create/edit/delete).
40
42
43 m_debugGraphId = -1;
45
46 SYSTEM_LOG << "[BTDebugWindow] NodeGraph debug mode initialized (Runtime/Read-Only)"
47 << std::endl;
48}
49
50// ---------------------------------------------------------------------------
51// ShutdownNodeGraphDebugMode
52// ---------------------------------------------------------------------------
53
55{
56 if (m_debugGraphId >= 0)
57 {
59 m_debugGraphId = -1;
60 }
61
63
64 SYSTEM_LOG << "[BTDebugWindow] NodeGraph debug mode shutdown" << std::endl;
65}
66
67// ---------------------------------------------------------------------------
68// RenderNodeGraphDebugPanel
69// ---------------------------------------------------------------------------
70
72{
73 // 1. Guard: entity must be selected
74 if (m_selectedEntity == 0)
75 {
76 ImGui::TextDisabled("Select an entity from the list to view its behavior tree");
77 return;
78 }
79
80 auto& world = World::Get();
81 if (!world.HasComponent<BehaviorTreeRuntime_data>(m_selectedEntity))
82 {
83 ImGui::TextColored(ImVec4(1.f, 0.4f, 0.4f, 1.f),
84 "Selected entity has no BehaviorTreeRuntime_data");
86 return;
87 }
88
89 const auto& btRuntime =
90 world.GetComponent<BehaviorTreeRuntime_data>(m_selectedEntity);
91
92 const BehaviorTreeAsset* tree =
94
95 if (!tree)
96 {
97 ImGui::TextColored(ImVec4(1.f, 0.5f, 0.f, 1.f),
98 "BehaviorTree asset not found (ID: %u)", btRuntime.AITreeAssetId);
99 return;
100 }
101
102 // 2. Reload graph when the tree changes
103 if (btRuntime.AITreeAssetId != m_lastDebugTreeId)
104 {
105 if (m_debugGraphId >= 0)
106 {
108 m_debugGraphId = -1;
109 }
110
113
114 if (!converted)
115 {
116 ImGui::TextColored(ImVec4(1.f, 0.3f, 0.3f, 1.f),
117 "Failed to convert BT graph");
118 return;
119 }
120
121 // Register the converted graph with the Blueprint Editor's NodeGraphManager
122 m_debugGraphId = NodeGraphManager::Get().CreateGraph(tree->name, "BehaviorTree");
123
125 if (managed)
126 *managed = std::move(*converted);
127
128 delete converted;
129 converted = nullptr;
130
132 m_lastDebugTreeId = btRuntime.AITreeAssetId;
133
134 SYSTEM_LOG << "[BTDebugWindow] Loaded graph for tree '" << tree->name
135 << "' (entity " << m_selectedEntity << ")" << std::endl;
136 }
137 else
138 {
139 // Keep the graph active across frames
141 }
142
143 // 3. Sync active node highlight each frame (no-op on NodeGraph side;
144 // SetActiveDebugNode drives the visual highlight in NodeGraphPanel)
147 btRuntime.AICurrentNodeIndex);
148
150 static_cast<int>(btRuntime.AICurrentNodeIndex));
151
152 // 4. Runtime status banner
153 const char* statusStr =
154 btRuntime.lastStatus == 0 ? "Running" :
155 btRuntime.lastStatus == 1 ? "Success" : "Failure";
156
157 ImGui::TextColored(ImVec4(0.4f, 1.f, 0.4f, 1.f),
158 "Tree: %s | Active node: #%u | Status: %s",
159 tree->name.c_str(),
160 btRuntime.AICurrentNodeIndex,
161 statusStr);
162 ImGui::Separator();
163
164 // 5. Render via NodeGraphPanel (Blueprint Editor pipeline)
165 // RenderGraph() embeds directly in the current child panel without
166 // creating a floating window (unlike Render()).
168}
169
170} // namespace Olympe
Runtime debugger for behavior tree visualization and inspection.
Data-driven behavior tree system for AI decision making.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
static BehaviorTreeManager & Get()
const BehaviorTreeAsset * GetTreeByAnyId(uint32_t treeId) const
static EditorContext & Get()
NodeGraph * GetGraph(int graphId)
int CreateGraph(const std::string &name, const std::string &type)
static NodeGraphManager & Get()
static void SetActiveDebugNode(int localNodeId)
Set the local node ID that is currently executing.
static NodeGraph * FromBehaviorTree(const BehaviorTreeAsset *tree)
Converts a BehaviorTreeAsset into a heap-allocated NodeGraph.
static void SyncActiveNode(NodeGraph *graph, uint32_t currentNodeId)
Synchronises the active-node metadata in an existing NodeGraph.
static World & Get()
Get singleton instance (short form)
Definition World.h:232
< Provides AssetID and INVALID_ASSET_ID
#define SYSTEM_LOG