Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BehaviorTreeDebugWindow.h
Go to the documentation of this file.
1/**
2 * @file BehaviorTreeDebugWindow.h
3 * @brief Runtime debugger for behavior tree visualization and inspection
4 * @author Olympe Engine - Behavior Tree Debugger
5 * @date 2025
6 *
7 * @details
8 * Provides real-time visualization of AI decision-making with:
9 * - Entity list with filtering and sorting
10 * - Interactive node graph with execution token highlighting
11 * - Inspector panel with blackboard and execution log
12 */
13
14#pragma once
15
16#include "../ECS_Entity.h"
17#include "../vector.h"
18#include "../json_helper.h"
19#include "BTGraphLayoutEngine.h"
20#include "BehaviorTree.h"
21#include "BTEditorCommand.h"
22#include <string>
23#include <vector>
24#include <deque>
25#include <map>
26#include <cstdint>
27
28 // Forward declarations for SDL3
29struct SDL_Window;
30struct SDL_Renderer;
31union SDL_Event;
32struct ImGuiContext;
33
34namespace Olympe
35{
36 /**
37 * @enum PinType
38 * @brief Type of connection pin on a node
39 */
40 enum class PinType
41 {
42 Input, ///< Input pin (left side of node in horizontal layout)
43 Output ///< Output pin (right side of node in horizontal layout)
44 };
45
46 /**
47 * @struct NodePin
48 * @brief Visual connection pin on a behavior tree node
49 */
50 struct NodePin
51 {
52 uint32_t nodeId = 0; ///< ID of the node this pin belongs to
53 PinType type = PinType::Input; ///< Pin type (input or output)
54 Vector worldPos; ///< World position of the pin
55 std::vector<uint32_t> connectedTo; ///< IDs of connected nodes
56 };
57
58 /**
59 * @struct BTConfig
60 * @brief Configuration loaded from BT_config.json
61 */
62 struct BTConfig
63 {
64 // Layout settings
65 bool defaultHorizontal = true;
66 float gridSize = 16.0f;
68 float horizontalSpacing = 280.0f;
69 float verticalSpacing = 120.0f;
70
71 // Rendering settings
72 float pinRadius = 6.0f;
73 float pinOutlineThickness = 2.0f;
74 float bezierTangent = 80.0f;
75 float connectionThickness = 2.0f;
76
77 // Node colors by type and status (RGBA values 0-255)
78 // nodeColors[nodeType][status] = {r, g, b, a}
79 struct Color { uint8_t r, g, b, a; };
80 std::map<std::string, std::map<std::string, Color>> nodeColors;
81 };
82
83 /**
84 * @struct BTColor
85 * @brief RGBA color value (0-255 range)
86 */
87 struct BTColor
88 {
89 uint8_t r = 255;
90 uint8_t g = 255;
91 uint8_t b = 255;
92 uint8_t a = 255;
93 };
94
95 /**
96 * @struct ExecutionLogEntry
97 * @brief Single entry in the execution log
98 */
100 {
101 float timeAgo = 0.0f; ///< Time since entry (seconds)
102 EntityID entity = 0; ///< Entity that executed
103 uint32_t nodeId = 0; ///< Node that was executed
104 std::string nodeName; ///< Node name
105 BTStatus status = BTStatus::Running; ///< Execution result
106 };
107
108 /**
109 * @struct EntityDebugInfo
110 * @brief Cached debug information for a single entity
111 */
113 {
115 std::string entityName;
117 std::string treeName;
118 bool isActive = false;
119 bool hasTarget = false;
122 std::string aiMode;
123 float lastUpdateTime = 0.0f;
124 };
125
126 /**
127 * @class BehaviorTreeDebugWindow
128 * @brief Main debug window for behavior tree runtime visualization
129 *
130 * Provides comprehensive debugging capabilities for AI behavior trees,
131 * including entity selection, graph visualization, and blackboard inspection.
132 *
133 * Renders in a separate SDL3 native window (not embedded in main engine window).
134 */
136 {
137 public:
140
141 /**
142 * @brief Initialize the debug window
143 */
144 void Initialize();
145
146 /**
147 * @brief Shutdown and cleanup
148 */
149 void Shutdown();
150
151 /**
152 * @brief Render the debug window (in separate SDL3 window)
153 */
154 void Render();
155
156 /**
157 * @brief Toggle window visibility (creates/destroys separate window)
158 */
159 void ToggleVisibility();
160
161 /**
162 * @brief Check if window is visible
163 */
164 bool IsVisible() const { return m_isVisible; }
165
166 /**
167 * @brief Process SDL events for separate window
168 * @param event SDL event to process
169 */
171
172 /**
173 * @brief Add an execution log entry
174 * @param entity Entity that executed
175 * @param nodeId Node that was executed
176 * @param nodeName Node name
177 * @param status Execution result
178 */
179 void AddExecutionEntry(EntityID entity, uint32_t nodeId, const std::string& nodeName, BTStatus status);
180
181 private:
182 // Main panel rendering
186
187 // Separate window management
191
192 // Entity list helpers
193 void RefreshEntityList();
195 void UpdateEntitySorting();
197
198 // Node graph helpers
200 void RenderNode(const BTNode* node, const BTNodeLayout* layout, bool isCurrentNode);
202 void RenderBezierConnection(const Vector& start, const Vector& end, uint32_t color, float thickness, float tangent);
203 void RenderNodePins(const BTNode* node, const BTNodeLayout* layout);
204 uint32_t GetNodeColor(BTNodeType type) const;
206 const char* GetNodeIcon(BTNodeType type) const;
207
208 // Editor mode helpers
209 void RenderNodePalette();
210 void RenderEditorToolbar();
212 void HandleNodeDeletion();
214 bool ValidateConnection(uint32_t parentId, uint32_t childId) const;
215 void SaveEditedTree();
216 void UndoLastAction();
217 void RedoLastAction();
218
219 // Validation helpers
222 bool IsConnectionValid(uint32_t parentId, uint32_t childId) const;
223
224 // Node properties editor
226
227 // Save system
228 void Save();
229 void SaveAs();
230 void RenderFileMenu();
232 std::string GetCurrentTimestamp() const;
233
234 // New BT creation
235 void RenderNewBTDialog();
236 BehaviorTreeAsset CreateFromTemplate(int templateIndex, const std::string& name);
237
238 // Edit menu
239 void RenderEditMenu();
240
241 // Configuration helpers
242 void LoadBTConfig();
243 void ApplyConfigToLayout();
244 Vector SnapToGrid(const Vector& pos) const;
245
246 // Inspector helpers
247 void RenderRuntimeInfo();
249 void RenderExecutionLog();
250
251 // Camera control helpers
252 void FitGraphToView();
253 void CenterViewOnGraph();
254 void ResetZoom();
255 void RenderMinimap();
256
257 // Camera helper utilities
258 void ApplyZoomToStyle();
260 float GetSafeZoom() const;
262
263 // Data management
264 std::vector<EntityDebugInfo> m_entities;
265 std::vector<EntityDebugInfo> m_filteredEntities;
267
268 // Layout engine
270 std::vector<BTNodeLayout> m_currentLayout;
271
272 // Camera state tracking
273 EntityID m_lastCenteredEntity = 0; // Track which entity was last centered
274 float m_currentZoom = 1.0f; // Current zoom level (0.3 to 3.0)
275 bool m_showMinimap = true; // Show minimap overlay
276
277 // Execution log (circular buffer with max 100 entries)
278 std::deque<ExecutionLogEntry> m_executionLog;
279 const size_t MAX_LOG_ENTRIES = 100;
280
281 // UI state
282 bool m_isVisible = false;
283 bool m_isInitialized = false;
285
286 // Filtering
287 char m_filterText[256] = "";
288 bool m_filterActiveOnly = false;
289 bool m_filterHasTarget = false;
290
291 // Sorting
292 enum class SortMode
293 {
294 Name,
295 TreeName,
297 AIMode
298 };
300 bool m_sortAscending = true;
301
302 // Panel layout
303 float m_entityListWidth = 250.0f;
304 float m_inspectorWidth = 350.0f;
305 float m_nodeSpacingX = 180.0f;
306 float m_nodeSpacingY = 120.0f;
307
308 // Graph view state
312
313 // Animation
314 float m_pulseTimer = 0.0f;
315
316 // Layout update flags
318 bool m_autoFitOnLoad = true;
319
320 // Configuration
322 bool m_configLoaded = false;
323
324 // Node colors by type and status (loaded from BT_config.json)
325 std::map<BTNodeType, std::map<BTStatus, BTColor>> m_nodeColors;
326
327 // Separate SDL3 window for debugger
331
332 // Separate ImGui context for this window
334
335 // Editor mode state
336 bool m_editorMode = false;
337 bool m_treeModified = false;
338 bool m_isDirty = false;
341 std::string m_currentFilePath;
342
343 // Editor interaction state
344 std::vector<uint32_t> m_selectedNodes;
345 bool m_showNodePalette = false;
347
348 // Pin dragging for connections
349 bool m_isDraggingPin = false;
353
354 // Undo/Redo system (command pattern)
356
357 // Link ID tracking for connection deletion
363 std::vector<LinkInfo> m_linkMap;
364 int m_nextLinkId = 100000;
365
366 // Validation
367 std::vector<BTValidationMessage> m_validationMessages;
369
370 // Node properties
373
374 // New BT dialog
375 bool m_showNewBTDialog = false;
376 char m_newBTName[256] = "";
378 };
379}
Command pattern implementation for behavior tree editor undo/redo.
Graph layout engine for behavior tree visualization.
Data-driven behavior tree system for AI decision making.
BTStatus
Behavior tree node execution status.
@ Running
Node is currently executing.
BTNodeType
Behavior tree node types.
AIMode
AI behavior modes for NPCs.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::uint64_t EntityID
Definition ECS_Entity.h:21
Manages undo/redo stacks for editor commands.
Computes clean hierarchical layouts for behavior trees.
Main debug window for behavior tree runtime visualization.
void RenderBezierConnection(const Vector &start, const Vector &end, uint32_t color, float thickness, float tangent)
uint32_t GetNodeColorByStatus(BTNodeType type, BTStatus status) const
Vector CalculatePanOffset(const Vector &graphCenter, const Vector &viewportSize) const
bool IsConnectionValid(uint32_t parentId, uint32_t childId) const
std::vector< BTValidationMessage > m_validationMessages
uint32_t GetPinColor(uint32_t nodeId, PinType pinType) const
void Initialize()
Initialize the debug window.
BehaviorTreeAsset CreateFromTemplate(int templateIndex, const std::string &name)
bool ValidateConnection(uint32_t parentId, uint32_t childId) const
std::vector< EntityDebugInfo > m_filteredEntities
void ToggleVisibility()
Toggle window visibility (creates/destroys separate window)
std::deque< ExecutionLogEntry > m_executionLog
void RenderNode(const BTNode *node, const BTNodeLayout *layout, bool isCurrentNode)
json SerializeTreeToJson(const BehaviorTreeAsset &tree) const
void GetGraphBounds(Vector &outMin, Vector &outMax) const
std::map< BTNodeType, std::map< BTStatus, BTColor > > m_nodeColors
void RenderNodePins(const BTNode *node, const BTNodeLayout *layout)
void Render()
Render the debug window (in separate SDL3 window)
std::vector< BTNodeLayout > m_currentLayout
Vector SnapToGrid(const Vector &pos) const
void RenderEntityEntry(const EntityDebugInfo &info)
void RenderNodeConnections(const BTNode *node, const BTNodeLayout *layout, const BehaviorTreeAsset *tree)
std::vector< EntityDebugInfo > m_entities
uint32_t GetNodeColor(BTNodeType type) const
void AddExecutionEntry(EntityID entity, uint32_t nodeId, const std::string &nodeName, BTStatus status)
Add an execution log entry.
bool IsVisible() const
Check if window is visible.
void ProcessEvent(SDL_Event *event)
Process SDL events for separate window.
const char * GetNodeIcon(BTNodeType type) const
nlohmann::json json
PinType
Type of connection pin on a node.
@ Output
Output pin (right side of node in horizontal layout)
@ Input
Input pin (left side of node in horizontal layout)
BTLayoutDirection
Layout direction for behavior tree visualization.
@ TopToBottom
Traditional top-down layout (vertical)
Represents a single node in a behavior tree.
RGBA color value (0-255 range)
Configuration loaded from BT_config.json.
std::map< std::string, std::map< std::string, Color > > nodeColors
Layout information for a single behavior tree node.
Cached debug information for a single entity.
Single entry in the execution log.
float timeAgo
Time since entry (seconds)
EntityID entity
Entity that executed.
BTStatus status
Execution result.
uint32_t nodeId
Node that was executed.
Visual connection pin on a behavior tree node.
PinType type
Pin type (input or output)
uint32_t nodeId
ID of the node this pin belongs to.
Vector worldPos
World position of the pin.
std::vector< uint32_t > connectedTo
IDs of connected nodes.