Ember
Loading...
Searching...
No Matches
TreeCanvas.h
Go to the documentation of this file.
1#pragma once
2
5#include "Utils/DPI.h"
7#include <functional>
8#include <map>
9#include <memory>
10#include <set>
11#include <unordered_map>
12#include <vector>
13#include <wx/dcbuffer.h>
14#include <wx/wx.h>
15
16namespace EmberUI {
17
20 int node_width = 175;
21 int node_height = 75;
24 wxSize title_padding = {0, 5};
25 int grid_size = 20;
26
29
30 wxColour background_color = wxColour(0x2A, 0x2A, 0x2A);
31 wxColour grid_color = wxColour(0x3E, 0x3E, 0x3E);
32 wxColour text_color = wxColour(0xB4, 0xB4, 0xB4);
33 wxColour node_color = wxColour(0x4B, 0x4B, 0x4B);
34 wxColour border_color = wxColour(0x78, 0x78, 0x78);
35 wxColour selected_border_color = wxColour(0x30, 0x5C, 0xDE);
36 wxColour hovered_border_color = wxColour(0x90, 0xEE, 0x90);
37 wxColour connection_color = wxColour(0xFF, 0xFF, 0xFF);
38
39 wxColour action_type_color = wxColour(0x4A, 0x90, 0xD9);
40 wxColour control_type_color = wxColour(0x5C, 0xB8, 0x5C);
41 wxColour condition_type_color = wxColour(0xF0, 0xAD, 0x4E);
42 wxColour decorator_type_color = wxColour(0x9B, 0x59, 0xB6);
43 wxColour behavior_tree_type_color = wxColour(0xD9, 0x53, 0x4F);
44
46 bool show_type_icons = true;
47 bool show_type_text = true;
48 wxColour type_header_text_color = wxColour(0xFF, 0xFF, 0xFF);
50
51 float zoom_step = 0.1f;
53 float pan_sensitivity = 1.51f;
55 float pan_step_size = 40.0f;
56
58 float pan_smoothness = 15.0f;
59
61 int pan_key_code = -1;
62
65
67 wxColour path_highlight_color = wxColour(0x90, 0xEE, 0x90);
68
70};
71
76class TreeCanvas : public wxPanel {
77 public:
78 using NodeSelectionCallback = std::function<void(EmberCore::ITreeNode *)>;
79 using VisibilityChangeCallback = std::function<void()>;
80
82 TreeCanvas(wxWindow *parent, wxWindowID id = wxID_ANY);
84 virtual ~TreeCanvas();
85
87 void SetTree(std::shared_ptr<EmberCore::ITreeStructure> tree);
89 std::shared_ptr<EmberCore::ITreeStructure> GetTree() const { return m_tree; }
90
94 const TreeCanvasConfig &GetConfig() const { return m_config; }
95
97 void SetStatusProvider(IStatusProvider *provider) { m_statusProvider = provider; }
100
105
110
112 void ResetView();
114 void FitTreeInView();
118 void SetZoom(float zoom);
120 float GetZoom() const { return m_zoomFactor; }
121
123 void MarkDirty() { m_dirty = true; }
126
129 m_widthCache.clear();
130 m_minimapDirty = true;
131 MarkDirty();
132 Refresh();
133 }
134
136 void SetShowGrid(bool show) { m_showGrid = show; }
138 void SetShowOverlayInfo(bool show) { m_showOverlayInfo = show; }
140 void SetShowMinimap(bool show) { m_showMinimap = show; }
142 void SetShowBreadcrumb(bool show) { m_showBreadcrumb = show; }
143
147 void ExitFocusMode();
149 bool IsInFocusMode() const { return m_focusRoot != nullptr; }
152
154 void LoadTypeIcons();
155
156 protected:
158 virtual void OnNodeSelected(EmberCore::ITreeNode *node);
160 virtual void OnBeforePaintOverlays(wxDC &dc);
162 virtual wxColour GetNodeFillColor(EmberCore::ITreeNode *node, bool selected, bool hovered);
164 virtual wxColour GetNodeBorderColor(EmberCore::ITreeNode *node, bool selected, bool hovered);
166 virtual wxColour GetNodeTextColor(EmberCore::ITreeNode *node, bool selected, bool hovered);
168 virtual void OnKeyDown(wxKeyEvent &event);
170 virtual void OnKeyUp(wxKeyEvent &event);
171
173 wxPoint GetViewOffset() const { return m_viewOffset; }
175 wxPoint GetTargetOffset() const { return m_targetOffset; }
177 void SetTargetOffset(const wxPoint &offset) { m_targetOffset = offset; }
179 void ClearWidthCache() { m_widthCache.clear(); }
181 bool GetShowMinimap() const { return m_showMinimap; }
182
183 private:
184 void OnPaint(wxPaintEvent &event);
185 void OnSize(wxSizeEvent &event);
186 void OnMouseLeftDown(wxMouseEvent &event);
187 void OnMouseLeftDClick(wxMouseEvent &event);
188 void OnMouseMotion(wxMouseEvent &event);
189 void OnMouseWheel(wxMouseEvent &event);
190 void OnMouseMiddleDown(wxMouseEvent &event);
191 void OnMouseMiddleUp(wxMouseEvent &event);
192 void OnMouseLeftUp(wxMouseEvent &event);
193 void OnMouseRightUp(wxMouseEvent &event);
194
195 void DrawGrid(wxDC &dc, const wxRect &viewport);
196 void DrawNode(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y, int level, const wxRect &viewport);
197 void DrawNodeBox(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y);
198 void DrawNodeText(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y);
199 void DrawTypeHeader(wxDC &dc, EmberCore::ITreeNode *node, const wxRect &nodeRect);
200 void DrawNodeConnections(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y);
201 void DrawCollapseArrow(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y);
202 void DrawOverlayInfo(wxDC &dc);
203 void DrawMinimap(wxDC &dc);
204 void DrawMinimapNode(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y, float scaleX, float scaleY,
205 int offsetX, int offsetY, const wxRect &minimapArea);
206 void DrawBreadcrumb(wxDC &dc);
207
208 void AutoCollapseTree(EmberCore::ITreeNode *node, int depth);
211 void ExpandToDepth(EmberCore::ITreeNode *node, int relativeDepth);
214 void ComputeTreeExtent(EmberCore::ITreeNode *node, wxCoord x, wxCoord y, int &minX, int &maxX, int &maxY);
215
217 wxPoint CalculateRootPosition();
218 EmberCore::ITreeNode *FindNodeAtPosition(EmberCore::ITreeNode *node, wxPoint node_pos, wxPoint target_pos);
219 EmberCore::ITreeNode *FindArrowAtPosition(const wxPoint &world_pos) const;
220 wxPoint FindNodeWorldPosition(EmberCore::ITreeNode *target_node);
221 wxPoint ScreenToWorld(const wxPoint &screen_pos) const;
222 wxPoint WorldToScreen(const wxPoint &world_pos) const;
223 wxRect GetViewportBounds() const;
224
225 void UpdateScaledConfig();
226 void BuildPathToSelected();
227
230 std::shared_ptr<EmberCore::ITreeStructure> m_tree;
232
238
239 wxPoint m_viewOffset = {0, 0};
240 wxPoint m_targetOffset = {0, 0};
241 float m_zoomFactor = 1.0f;
242 float m_minZoom = 0.05f;
243 float m_maxZoom = 3.0f;
244
245 bool m_isPanning = false;
246 bool m_panKeyHeld = false;
248 wxLongLong m_lastFrameTime;
249
250 bool m_dirty = true;
251 bool m_showGrid = true;
252 bool m_showOverlayInfo = true;
253 bool m_showMinimap = true;
254 bool m_showBreadcrumb = true;
255
259
260 std::set<int64_t> m_executionPathIds;
261 std::set<int> m_pathToSelectedIds;
262
263 struct LineSegment {
264 wxCoord x1, y1, x2, y2;
265 };
266 std::vector<LineSegment> m_highlightedSegments;
267 std::vector<LineSegment> m_selectedPathSegments;
268
273 std::vector<CollapseArrowInfo> m_collapseArrows;
274
277
280 bool m_minimapDirty = true;
286
287 std::vector<std::pair<wxRect, EmberCore::ITreeNode *>> m_breadcrumbHitTargets;
288
289 mutable std::unordered_map<EmberCore::ITreeNode *, int> m_widthCache;
290
291 std::map<EmberCore::ITreeNode::NodeType, wxBitmap> m_typeIcons;
292
294
296};
297
298} // namespace EmberUI
Abstract interface for tree nodes that can be visualized.
Definition ITreeNode.h:31
NodeType
Node types for visualization categorization.
Definition ITreeNode.h:36
Interface for querying node status and execution path.
void CenterOnNode(EmberCore::ITreeNode *node)
Centers the view on the given node.
void DrawOverlayInfo(wxDC &dc)
void OnMouseMotion(wxMouseEvent &event)
void DrawMinimapNode(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y, float scaleX, float scaleY, int offsetX, int offsetY, const wxRect &minimapArea)
std::set< int64_t > m_executionPathIds
Definition TreeCanvas.h:260
virtual ~TreeCanvas()
Destructor.
void OnMouseWheel(wxMouseEvent &event)
void SetNodeSelectionCallback(NodeSelectionCallback callback)
Sets callback invoked when a node is selected.
Definition TreeCanvas.h:107
wxPoint FindNodeWorldPosition(EmberCore::ITreeNode *target_node)
void EnterFocusMode(EmberCore::ITreeNode *node)
Enters focus mode, showing only the subtree rooted at the given node.
std::unordered_map< EmberCore::ITreeNode *, int > m_widthCache
Definition TreeCanvas.h:289
std::vector< CollapseArrowInfo > m_collapseArrows
Definition TreeCanvas.h:273
EmberCore::ITreeNode * FindNodeAtPosition(EmberCore::ITreeNode *node, wxPoint node_pos, wxPoint target_pos)
std::shared_ptr< EmberCore::ITreeStructure > m_tree
Definition TreeCanvas.h:230
void OnSize(wxSizeEvent &event)
void DrawTypeHeader(wxDC &dc, EmberCore::ITreeNode *node, const wxRect &nodeRect)
float GetZoom() const
Returns the current zoom factor.
Definition TreeCanvas.h:120
EmberCore::ITreeNode * m_hoveredArrowNode
Definition TreeCanvas.h:235
virtual void OnNodeSelected(EmberCore::ITreeNode *node)
Called when a node is selected; override for app-specific behavior.
TreeCanvas(wxWindow *parent, wxWindowID id=wxID_ANY)
Constructor.
wxColour GetNodeTypeColor(EmberCore::ITreeNode::NodeType type) const
void RefreshCanvas()
Marks canvas dirty (alias for MarkDirty).
Definition TreeCanvas.h:125
void ClearWidthCache()
Clears the subtree width cache.
Definition TreeCanvas.h:179
void OnMouseLeftDClick(wxMouseEvent &event)
void OnMouseRightUp(wxMouseEvent &event)
TreeCanvasConfig m_scaled
Definition TreeCanvas.h:229
std::map< EmberCore::ITreeNode::NodeType, wxBitmap > m_typeIcons
Definition TreeCanvas.h:291
void SetShowMinimap(bool show)
Enables or disables minimap display.
Definition TreeCanvas.h:140
EmberCore::ITreeNode * GetFocusRoot() const
Returns the focus mode root node, or nullptr if not in focus mode.
Definition TreeCanvas.h:151
void DrawNodeText(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y)
wxBitmap m_minimapCache
Definition TreeCanvas.h:279
void DrawGrid(wxDC &dc, const wxRect &viewport)
void DrawNodeBox(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y)
virtual void OnKeyUp(wxKeyEvent &event)
Key up handler; override for custom key handling.
void DrawNodeConnections(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y)
std::vector< std::pair< wxRect, EmberCore::ITreeNode * > > m_breadcrumbHitTargets
Definition TreeCanvas.h:287
wxPoint WorldToScreen(const wxPoint &world_pos) const
void ExpandAllChildren(EmberCore::ITreeNode *node)
void SetTree(std::shared_ptr< EmberCore::ITreeStructure > tree)
Sets the tree structure to display.
void InvalidateLayout()
Invalidates layout cache and triggers refresh.
Definition TreeCanvas.h:128
IStatusProvider * GetStatusProvider() const
Returns the status overlay provider.
Definition TreeCanvas.h:99
virtual wxColour GetNodeFillColor(EmberCore::ITreeNode *node, bool selected, bool hovered)
Returns the fill color for a node; override for custom coloring.
void OnMouseMiddleUp(wxMouseEvent &event)
void SetShowOverlayInfo(bool show)
Enables or disables overlay info display.
Definition TreeCanvas.h:138
void DrawBreadcrumb(wxDC &dc)
void AutoCollapseTree(EmberCore::ITreeNode *node, int depth)
void SetTargetOffset(const wxPoint &offset)
Sets the target offset for smooth panning.
Definition TreeCanvas.h:177
void ResetView()
Resets zoom and pan to default values.
virtual void OnKeyDown(wxKeyEvent &event)
Key down handler; override for custom key handling.
const TreeCanvasConfig & GetConfig() const
Returns const configuration.
Definition TreeCanvas.h:94
EmberCore::ITreeNode * m_hoveredNode
Definition TreeCanvas.h:234
NodeSelectionCallback m_selectionCallback
Definition TreeCanvas.h:275
void MarkDirty()
Marks the canvas for repaint.
Definition TreeCanvas.h:123
std::vector< LineSegment > m_selectedPathSegments
Definition TreeCanvas.h:267
void DrawNode(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y, int level, const wxRect &viewport)
EmberCore::ITreeNode * m_selectedNode
Definition TreeCanvas.h:233
wxPoint GetTargetOffset() const
Returns the target offset for smooth panning.
Definition TreeCanvas.h:175
void LoadTypeIcons()
Loads type icons from the configured icon directory.
VisibilityChangeCallback m_visibilityChangeCallback
Definition TreeCanvas.h:276
void SetShowBreadcrumb(bool show)
Enables or disables breadcrumb display.
Definition TreeCanvas.h:142
bool GetShowMinimap() const
Returns whether the minimap is shown.
Definition TreeCanvas.h:181
std::function< void(EmberCore::ITreeNode *)> NodeSelectionCallback
Definition TreeCanvas.h:78
void ExpandToDepth(EmberCore::ITreeNode *node, int relativeDepth)
void SetZoom(float zoom)
Sets the zoom factor.
void SetStatusProvider(IStatusProvider *provider)
Sets the status overlay provider.
Definition TreeCanvas.h:97
TreeCanvasConfig m_config
Definition TreeCanvas.h:228
void OnMouseLeftUp(wxMouseEvent &event)
virtual wxColour GetNodeBorderColor(EmberCore::ITreeNode *node, bool selected, bool hovered)
Returns the border color for a node; override for custom coloring.
void CollapseAllChildren(EmberCore::ITreeNode *node)
void ComputeTreeExtent(EmberCore::ITreeNode *node, wxCoord x, wxCoord y, int &minX, int &maxX, int &maxY)
IStatusProvider * m_statusProvider
Definition TreeCanvas.h:231
EmberCore::ITreeNode * GetEffectiveRoot() const
std::set< int > m_pathToSelectedIds
Definition TreeCanvas.h:261
wxLongLong m_lastFrameTime
Definition TreeCanvas.h:248
EmberCore::ITreeNode * GetSelectedNode() const
Returns the currently selected node.
Definition TreeCanvas.h:102
void FitTreeInView()
Adjusts view to fit the entire tree.
void DrawMinimap(wxDC &dc)
wxPoint CalculateRootPosition()
EmberCore::ITreeNode * m_focusRoot
Definition TreeCanvas.h:237
void SetShowGrid(bool show)
Enables or disables grid display.
Definition TreeCanvas.h:136
wxPoint ScreenToWorld(const wxPoint &screen_pos) const
void OnPaint(wxPaintEvent &event)
void ExitFocusMode()
Exits focus mode.
int CalculateSubtreeWidth(EmberCore::ITreeNode *node)
TreeCanvasConfig & GetConfig()
Returns mutable configuration.
Definition TreeCanvas.h:92
void SetVisibilityChangeCallback(VisibilityChangeCallback callback)
Sets callback invoked when node visibility changes.
Definition TreeCanvas.h:109
void OnMouseMiddleDown(wxMouseEvent &event)
bool IsInFocusMode() const
Returns whether focus mode is active.
Definition TreeCanvas.h:149
void OnMouseLeftDown(wxMouseEvent &event)
virtual void OnBeforePaintOverlays(wxDC &dc)
Called before painting overlays; override to draw custom overlays.
std::shared_ptr< EmberCore::ITreeStructure > GetTree() const
Returns the current tree structure.
Definition TreeCanvas.h:89
wxTimer * m_refreshTimer
Definition TreeCanvas.h:293
EmberCore::ITreeNode * m_pressedArrowNode
Definition TreeCanvas.h:236
wxRect GetViewportBounds() const
void SetSelectedNode(EmberCore::ITreeNode *node)
Sets the selected node.
virtual wxColour GetNodeTextColor(EmberCore::ITreeNode *node, bool selected, bool hovered)
Returns the text color for a node; override for custom coloring.
std::vector< LineSegment > m_highlightedSegments
Definition TreeCanvas.h:266
void DrawCollapseArrow(wxDC &dc, EmberCore::ITreeNode *node, wxCoord x, wxCoord y)
wxPoint GetViewOffset() const
Returns the current view offset.
Definition TreeCanvas.h:173
std::function< void()> VisibilityChangeCallback
Definition TreeCanvas.h:79
EmberCore::ITreeNode * FindArrowAtPosition(const wxPoint &world_pos) const
Definition Panel.h:8
Configuration for tree canvas (node sizes, spacing, colors, zoom settings, etc.)
Definition TreeCanvas.h:19
wxColour behavior_tree_type_color
Definition TreeCanvas.h:43