Ember
Loading...
Searching...
No Matches
TreeVisualization.h
Go to the documentation of this file.
1#pragma once
2
5#include <functional>
6#include <map>
7#include <memory>
8#include <set>
9#include <vector>
10#include <wx/wx.h>
11
12namespace EmberCore {
13
18 int node_width = 175;
19 int node_height = 75;
22 wxSize title_padding = {0, 5};
23 int grid_size = 20;
24
25 // Colors
26 wxColour background_color = wxColour(0x2A, 0x2A, 0x2A);
27 wxColour grid_color = wxColour(0x36, 0x36, 0x36);
28 wxColour text_color = wxColour(0xB4, 0xB4, 0xB4);
29 wxColour node_color = wxColour(0x4B, 0x4B, 0x4B);
30 wxColour selected_color = wxColour(0x32, 0x32, 0x32);
31 wxColour hovered_color = wxColour(0x41, 0x41, 0x41);
32 wxColour border_color = wxColour(0x78, 0x78, 0x78);
33 wxColour selected_border_color = wxColour(0x30, 0x5C, 0xDE);
34 wxColour hovered_border_color = wxColour(0x90, 0xEE, 0x90);
35 wxColour connection_color = wxColour(0xFF, 0xFF, 0xFF);
36};
37
44class TreeVisualization : public wxPanel {
45 public:
46 // Callback type for node selection events
47 using NodeSelectionCallback = std::function<void(ITreeNode *)>;
48
49 TreeVisualization(wxWindow *parent, wxWindowID id = wxID_ANY);
50 virtual ~TreeVisualization();
51
52 // Tree management
53 void SetBehaviorTree(std::shared_ptr<ITreeStructure> tree);
54 std::shared_ptr<ITreeStructure> GetBehaviorTree() const { return tree_; }
55
56 // Configuration
58 const VisualizationConfig &GetConfig() const { return config_; }
59
60 // Selection and interaction
61 ITreeNode *GetSelectedNode() const { return selected_node_.get(); }
63 void SetSelectedNode(ITreeNode *node);
64
65 // Callback management
67
68 // View manipulation
69 void ResetView();
70 void CenterOnNode(ITreeNode *node);
71 void SetZoom(float zoom);
72 float GetZoom() const { return zoom_factor_; }
73
74 // Utility
75 void RefreshVisualization() { Refresh(); }
76
77 // Preferences
78 void LoadPreferences();
79
80 // Helper methods
81 int ParseHotkeyString(const std::string &hotkey);
82
83 private:
84 // Event handlers
85 void OnPaint(wxPaintEvent &event);
86 void OnSize(wxSizeEvent &event);
87 void OnMouseLeftDown(wxMouseEvent &event);
88 void OnMouseLeftDClick(wxMouseEvent &event);
89 void OnMouseRightDown(wxMouseEvent &event);
90 void OnMouseMotion(wxMouseEvent &event);
91 void OnMouseWheel(wxMouseEvent &event);
92 void OnMouseMiddleDown(wxMouseEvent &event);
93 void OnMouseMiddleUp(wxMouseEvent &event);
94 void OnKeyDown(wxKeyEvent &event);
95 void OnKeyUp(wxKeyEvent &event);
96
97 // Drawing methods
98 void DrawGrid(wxDC &dc);
99 void DrawNode(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y, int level, bool draw_children = true);
100 void DrawNodeBox(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y);
101 void DrawNodeText(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y);
102 void DrawNodeConnections(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y);
103 void CollectHighlightedConnections(ITreeNode *node, wxCoord x, wxCoord y);
104 void DrawCollapseArrow(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y);
105 void DrawNavigationInfo(wxDC &dc);
106
107 // Utility methods
109 void ClearWidthCache() { width_cache_.clear(); }
110 wxPoint CalculateRootPosition();
111 void UpdateScaledIcons();
112 ITreeNode *FindNodeAtPosition(ITreeNode *node, wxPoint node_pos, wxPoint target_pos, int level = 0);
113 wxPoint FindNodeWorldPosition(ITreeNode *target_node);
114 bool IsPointInViewport(wxCoord x, wxCoord y) const;
115 wxPoint ScreenToWorld(const wxPoint &screen_pos) const;
116 wxPoint WorldToScreen(const wxPoint &world_pos) const;
117 bool IsNodeInViewport(wxCoord x, wxCoord y) const;
118 wxRect GetViewportBounds() const;
119
120 // Configuration and state
122 std::shared_ptr<ITreeStructure> tree_;
123
124 // Interaction state
125 std::shared_ptr<ITreeNode> selected_node_;
127
128 // View state
129 wxPoint view_offset_ = {0, 0};
130 wxPoint target_offset_ = {0, 0}; // Target offset for smooth panning
131 float zoom_factor_ = 1.0f;
132 float min_zoom_ = 0.1f;
133 float max_zoom_ = 3.0f;
134
135 // Smooth panning settings
137 float pan_smoothness_ = 30.0f; // Smoothness (0-100, higher = smoother/slower)
138 float pan_step_size_ = 40.0f; // Arrow key pan step size in pixels
139 wxLongLong last_frame_time_; // For calculating deltaTime
140
141 // Zoom/Pan preferences (loaded from AppPreferences)
142 float zoom_step_ = 0.1f;
144 float pan_sensitivity_ = 1.51f;
146 int pan_key_code_ = -1; // Key code for pan key (-1 = use mouse button instead)
147 int pan_mouse_button_ = 2; // Mouse button for pan (1=left, 2=middle, 3=right)
148 bool pan_key_held_ = false; // Track if pan key is currently held
149
150 // Grid preferences (loaded from AppPreferences)
151 bool show_grid_ = true;
152 int grid_size_ = 20;
153
154 // Viewport culling preferences (loaded from AppPreferences)
157
158 // Path highlighting (updated each frame in OnPaint)
159 // Store node IDs instead of pointers because tree uses different instances
161
162 // Highlighted path line segments (collected during tree traversal, drawn at end)
163 struct LineSegment {
164 wxCoord x1, y1, x2, y2;
165 };
166 std::vector<LineSegment> highlighted_path_segments_;
167
168 // Collapse arrows to draw (collected during tree traversal, drawn last for proper z-order)
171 wxCoord x, y;
172 };
173 std::vector<CollapseArrowInfo> collapse_arrows_to_draw_;
174
175 // Pan state
176 bool is_panning_ = false;
178
179 // Fonts
181
182 // Node type icons (original)
183 wxBitmap icon_action_;
187 wxBitmap icon_tree_;
189
190 // Cached scaled icons for current zoom level
191 float cached_icon_zoom_ = 0.0f; // 0 = uninitialized, forces first update
198
199 // Performance monitoring
201
202 // Callback for node selection events
204
205 // Performance optimization - cache subtree widths
206 mutable std::map<ITreeNode *, int> width_cache_;
207
209};
210
211} // namespace EmberCore
Abstract interface for tree nodes that can be visualized.
Definition ITreeNode.h:31
void DrawCollapseArrow(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y)
void OnMouseLeftDClick(wxMouseEvent &event)
void OnSize(wxSizeEvent &event)
ITreeNode * FindNodeAtPosition(ITreeNode *node, wxPoint node_pos, wxPoint target_pos, int level=0)
wxPoint ScreenToWorld(const wxPoint &screen_pos) const
std::shared_ptr< ITreeNode > selected_node_
bool IsNodeInViewport(wxCoord x, wxCoord y) const
void DrawNodeText(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y)
void SetNodeSelectionCallback(NodeSelectionCallback callback)
ITreeNode * GetSelectedNode() const
void DrawNodeConnections(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y)
wxPoint WorldToScreen(const wxPoint &world_pos) const
void SetBehaviorTree(std::shared_ptr< ITreeStructure > tree)
const VisualizationConfig & GetConfig() const
void OnMouseWheel(wxMouseEvent &event)
ITreeNode * GetHoveredNode() const
std::shared_ptr< ITreeStructure > GetBehaviorTree() const
void DrawNodeBox(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y)
void SetSelectedNode(ITreeNode *node)
void OnMouseMotion(wxMouseEvent &event)
int CalculateSubtreeWidth(ITreeNode *node)
std::vector< LineSegment > highlighted_path_segments_
void OnMouseLeftDown(wxMouseEvent &event)
void CollectHighlightedConnections(ITreeNode *node, wxCoord x, wxCoord y)
void DrawNode(wxDC &dc, ITreeNode *node, wxCoord x, wxCoord y, int level, bool draw_children=true)
TreeVisualization(wxWindow *parent, wxWindowID id=wxID_ANY)
void OnMouseMiddleUp(wxMouseEvent &event)
std::map< ITreeNode *, int > width_cache_
bool IsPointInViewport(wxCoord x, wxCoord y) const
VisualizationConfig & GetConfig()
NodeSelectionCallback m_nodeSelectionCallback
int ParseHotkeyString(const std::string &hotkey)
void OnPaint(wxPaintEvent &event)
void CenterOnNode(ITreeNode *node)
std::shared_ptr< ITreeStructure > tree_
std::function< void(ITreeNode *)> NodeSelectionCallback
void OnMouseRightDown(wxMouseEvent &event)
void OnKeyUp(wxKeyEvent &event)
std::vector< CollapseArrowInfo > collapse_arrows_to_draw_
void OnMouseMiddleDown(wxMouseEvent &event)
void OnKeyDown(wxKeyEvent &event)
wxPoint FindNodeWorldPosition(ITreeNode *target_node)
Main types header for EmberCore.
Configuration settings for tree visualization.