Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
BTGraphLayoutEngine.h
Go to the documentation of this file.
1/**
2 * @file BTGraphLayoutEngine.h
3 * @brief Graph layout engine for behavior tree visualization
4 * @author Olympe Engine - Behavior Tree Debugger
5 * @date 2025
6 *
7 * @details
8 * Implements a 2-phase hierarchical graph layout algorithm:
9 * 1. Layering: Assign nodes to layers via BFS
10 * 2. Fixed Grid Placement: Place nodes on a fixed 400x200 grid (horizontal layout)
11 *
12 * Produces a clean horizontal layout with no overlaps.
13 */
14
15#pragma once
16
17#include "../vector.h"
18#include "../AI/BehaviorTree.h"
19#include <vector>
20#include <map>
21
22namespace Olympe
23{
24 /**
25 * @enum BTLayoutDirection
26 * @brief Layout direction for behavior tree visualization
27 */
29 {
30 TopToBottom, ///< Traditional top-down layout (vertical)
31 LeftToRight ///< Horizontal left-to-right layout
32 };
33
34 /**
35 * @struct BTNodeLayout
36 * @brief Layout information for a single behavior tree node
37 */
38 struct BTNodeLayout
39 {
40 uint32_t nodeId = 0; ///< BT node ID
41 Vector position; ///< Final position (x, y)
42 int layer = 0; ///< Hierarchical layer (0 = root)
43 int orderInLayer = 0; ///< Order within the layer
44 float width = 200.0f; ///< Node visual width (increased for readability)
45 float height = 100.0f; ///< Node visual height (increased for readability)
46 };
47
48 /**
49 * @class BTGraphLayoutEngine
50 * @brief Computes clean hierarchical layouts for behavior trees
51 *
52 * Uses BFS layering followed by a fixed 400x200 grid to produce
53 * a horizontal layout with no overlaps.
54 */
55 class BTGraphLayoutEngine
56 {
57 public:
60
61 /**
62 * @brief Set layout direction
63 * @param direction Layout direction (TopToBottom or LeftToRight)
64 */
66
67 /**
68 * @brief Get current layout direction
69 * @return Current layout direction
70 */
72
73 /**
74 * @brief Compute layout for a behavior tree
75 * @param tree The behavior tree asset to layout
76 * @param nodeSpacingX Horizontal spacing between nodes (unused, fixed grid used)
77 * @param nodeSpacingY Vertical spacing between layers (unused, fixed grid used)
78 * @param zoomFactor Zoom multiplier (unused, fixed grid used)
79 * @return Vector of node layouts with computed positions
80 */
81 std::vector<BTNodeLayout> ComputeLayout(
83 float nodeSpacingX = 320.0f,
84 float nodeSpacingY = 180.0f,
85 float zoomFactor = 1.0f
86 );
87
88 /**
89 * @brief Get computed layout for a specific node
90 * @param nodeId The BT node ID
91 * @return Pointer to layout, or nullptr if not found
92 */
93 const BTNodeLayout* GetNodeLayout(uint32_t nodeId) const;
94
95 /**
96 * @brief Update the stored position for a node (e.g. after user drag).
97 * @param nodeId The BT node ID.
98 * @param x New X position in canvas (grid) units.
99 * @param y New Y position in canvas (grid) units.
100 * @return true if the node was found and updated, false otherwise.
101 */
102 bool UpdateNodePosition(uint32_t nodeId, float x, float y);
103
104 private:
105 // Phase 1: Assign nodes to layers via BFS from root
107
108 // Helper: Get children of a node
109 std::vector<uint32_t> GetChildren(const BTNode* node) const;
110
111 // Helper: Get parent nodes (reverse lookup)
113
114 // Layout configuration
116
117 // Computed layouts
118 std::vector<BTNodeLayout> m_layouts;
119 std::map<uint32_t, size_t> m_nodeIdToIndex; // nodeId -> index in m_layouts
120
121 // Temporary data structures for algorithm
122 std::vector<std::vector<uint32_t>> m_layers; // layer -> [nodeIds]
123 std::map<uint32_t, std::vector<uint32_t>> m_parentMap; // nodeId -> [parentIds]
124 };
125}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
bool UpdateNodePosition(uint32_t nodeId, float x, float y)
Update the stored position for a node (e.g.
const BTNodeLayout * GetNodeLayout(uint32_t nodeId) const
Get computed layout for a specific node.
std::vector< BTNodeLayout > m_layouts
std::vector< uint32_t > GetChildren(const BTNode *node) const
void BuildParentMap(const BehaviorTreeAsset *tree)
void SetLayoutDirection(BTLayoutDirection direction)
Set layout direction.
BTLayoutDirection GetLayoutDirection() const
Get current layout direction.
BTLayoutDirection m_layoutDirection
Default vertical.
std::map< uint32_t, size_t > m_nodeIdToIndex
std::map< uint32_t, std::vector< uint32_t > > m_parentMap
void AssignLayers(const BehaviorTreeAsset *tree)
std::vector< BTNodeLayout > ComputeLayout(const BehaviorTreeAsset *tree, float nodeSpacingX=320.0f, float nodeSpacingY=180.0f, float zoomFactor=1.0f)
Compute layout for a behavior tree.
std::vector< std::vector< uint32_t > > m_layers
< Provides AssetID and INVALID_ASSET_ID
BTLayoutDirection
Layout direction for behavior tree visualization.
@ LeftToRight
Horizontal left-to-right layout.
@ TopToBottom
Traditional top-down layout (vertical)
Represents a single node in a behavior tree.
Layout information for a single behavior tree node.
int orderInLayer
Order within the layer.
Vector position
Final position (x, y)
float height
Node visual height (increased for readability)
uint32_t nodeId
BT node ID.
int layer
Hierarchical layer (0 = root)
float width
Node visual width (increased for readability)