Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
CustomCanvasEditor.h
Go to the documentation of this file.
1/**
2 * @file CustomCanvasEditor.h
3 * @brief ICanvasEditor implementation for custom canvas with zoom support (PrefabCanvas)
4 * @details Provides pan, zoom, and coordinate transformation without imnodes dependency
5 *
6 * @design
7 * - Full manual control of pan/zoom
8 * - Supports zoom range 0.1x - 3.0x (configurable)
9 * - Grid delegated to CanvasGridRenderer with zoom-aware scaling
10 * - Coordinate transformation includes zoom scaling
11 * - Input handling (middle-mouse pan, scroll zoom)
12 *
13 * @usage
14 * // In EntityPrefabRenderer::Render()
15 * m_canvasEditor = std::make_unique<CustomCanvasEditor>("ComponentCanvas", canvasScreenPos, canvasSize);
16 *
17 * m_canvasEditor->BeginRender();
18 * {
19 * // Render custom nodes
20 * RenderMyComponentNodes();
21 * }
22 * m_canvasEditor->EndRender();
23 *
24 * @compatibility C++14
25 */
26
27#pragma once
28
29#include "ICanvasEditor.h"
31#include <string>
32#include <memory>
33
34namespace Olympe
35{
37 {
38 public:
39 /**
40 * @brief Construct custom canvas editor with zoom support
41 * @param name Identifier for this editor (e.g., "ComponentCanvas")
42 * @param canvasScreenPos Top-left corner of canvas on screen (pixels)
43 * @param canvasSize Width/height of canvas area (pixels)
44 * @param initialZoom Initial zoom level (default 1.0f)
45 * @param minZoom Minimum zoom allowed (default 0.1f)
46 * @param maxZoom Maximum zoom allowed (default 3.0f)
47 */
49 const char* name,
51 ImVec2 canvasSize,
52 float initialZoom = 1.0f,
53 float minZoom = 0.1f,
54 float maxZoom = 3.0f
55 );
56
57 virtual ~CustomCanvasEditor() = default;
58
59 // ====================================================================
60 // Lifecycle Management
61 // ====================================================================
62
63 /**
64 * @brief Begin rendering cycle
65 * @details Updates pan/zoom from input (mouse/scroll)
66 */
67 virtual void BeginRender() override;
68
69 /**
70 * @brief End rendering cycle
71 * @details Finalizes input state for frame
72 */
73 virtual void EndRender() override;
74
75 // ====================================================================
76 // Coordinate Transformation Systems
77 // ====================================================================
78
79 /**
80 * @brief Transform screen space to canvas space
81 * @details Includes zoom scaling:
82 * canvas = (screen - canvasScreenPos - pan) / zoom
83 */
84 virtual ImVec2 ScreenToCanvas(const ImVec2& screenPos) const override;
85
86 /**
87 * @brief Transform canvas space to screen space
88 * @details Includes zoom scaling:
89 * screen = canvas * zoom + pan + canvasScreenPos
90 */
91 virtual ImVec2 CanvasToScreen(const ImVec2& canvasPos) const override;
92
93 /**
94 * @brief Transform editor space to grid space
95 * @details Grid space removes pan offset (but keeps zoom):
96 * grid = editor - pan
97 */
98 virtual ImVec2 EditorToGrid(const ImVec2& editorPos) const override;
99
100 /**
101 * @brief Transform grid space to editor space
102 * @details Adds pan offset back (keeps zoom):
103 * editor = grid + pan
104 */
105 virtual ImVec2 GridToEditor(const ImVec2& gridPos) const override;
106
107 // ====================================================================
108 // Pan Management
109 // ====================================================================
110
111 /**
112 * @brief Get current pan offset
113 * @return Current pan offset vector
114 */
115 virtual ImVec2 GetPan() const override { return m_canvasOffset; }
116
117 /**
118 * @brief Set pan offset directly
119 * @param offset New pan offset
120 */
121 virtual void SetPan(const ImVec2& offset) override { m_canvasOffset = offset; }
122
123 /**
124 * @brief Pan by delta
125 */
126 virtual void PanBy(const ImVec2& delta) override
127 {
129 }
130
131 /**
132 * @brief Reset pan to origin
133 */
134 virtual void ResetPan() override { m_canvasOffset = ImVec2(0.0f, 0.0f); }
135
136 // ====================================================================
137 // Zoom Management
138 // ====================================================================
139
140 /**
141 * @brief Get current zoom level
142 * @return Current zoom scale (1.0 = 100%)
143 */
144 virtual float GetZoom() const override { return m_canvasZoom; }
145
146 /**
147 * @brief Set zoom level directly
148 * @param scale New zoom scale (clamped to min/max)
149 * @param zoomCenter Optional screen-space point to maintain visual position during zoom
150 * @details If zoomCenter is provided, adjusts pan so that the point at zoomCenter
151 * maintains its visual position on screen after zoom changes.
152 */
153 virtual void SetZoom(float scale, const ImVec2* zoomCenter = nullptr) override;
154
155 /**
156 * @brief Zoom by multiplier
157 * @param factor Zoom factor (1.1 = 10% zoom in, 0.9 = 10% zoom out)
158 * @param zoomCenter Optional screen-space point to maintain visual position
159 */
160 virtual void ZoomBy(float factor, const ImVec2* zoomCenter = nullptr) override;
161
162 /**
163 * @brief Get zoom limits
164 * @return {minZoom, maxZoom}
165 */
166 virtual ImVec2 GetZoomLimits() const override { return ImVec2(m_minZoom, m_maxZoom); }
167
168 /**
169 * @brief Reset zoom to 1.0x
170 */
171 virtual void ResetZoom() override { m_canvasZoom = 1.0f; }
172
173 /**
174 * @brief Reset view (pan and zoom)
175 */
176 virtual void ResetView() override
177 {
178 ResetPan();
179 ResetZoom();
180 }
181
182 // ====================================================================
183 // Grid Management
184 // ====================================================================
185
186 /**
187 * @brief Get current grid configuration
188 * @return GridConfig with zoom-aware scaling
189 */
190 virtual CanvasGridRenderer::GridConfig GetGridConfig() const override;
191
192 /**
193 * @brief Render grid
194 * @param preset Style preset
195 */
197
198 /**
199 * @brief Set grid visibility
200 * @param enabled True to show grid
201 */
202 virtual void SetGridVisible(bool enabled) override;
203
204 /**
205 * @brief Check if grid is visible
206 * @return True if grid will be rendered
207 */
208 virtual bool IsGridVisible() const override { return m_gridVisible; }
209
210 // ====================================================================
211 // Canvas Properties
212 // ====================================================================
213
214 /**
215 * @brief Get canvas screen position
216 * @return Stored canvasScreenPos
217 */
218 virtual ImVec2 GetCanvasScreenPos() const override { return m_canvasScreenPos; }
219
220 /**
221 * @brief Get canvas size
222 * @return Stored canvasSize
223 */
224 virtual ImVec2 GetCanvasSize() const override { return m_canvasSize; }
225
226 /**
227 * @brief Set canvas screen position (updates each frame)
228 * @param screenPos New screen position
229 */
230 virtual void SetCanvasScreenPos(const ImVec2& screenPos) override { m_canvasScreenPos = screenPos; }
231
232 /**
233 * @brief Set canvas size (updates each frame)
234 * @param size New canvas size
235 */
236 virtual void SetCanvasSize(const ImVec2& size) override { m_canvasSize = size; }
237
238 /**
239 * @brief Get canvas visible bounds in canvas space
240 * @param outMin Top-left corner
241 * @param outMax Bottom-right corner
242 */
243 virtual void GetCanvasVisibleBounds(ImVec2& outMin, ImVec2& outMax) const override;
244
245 /**
246 * @brief Check if point is in canvas
247 * @param screenPos Position in screen space
248 * @return True if within canvas bounds
249 */
250 virtual bool IsPointInCanvas(const ImVec2& screenPos) const override;
251
252 // ====================================================================
253 // Context Information
254 // ====================================================================
255
256 /**
257 * @brief Check if canvas is hovered
258 * @return True if mouse is over canvas
259 */
260 virtual bool IsCanvasHovered() const override;
261
262 /**
263 * @brief Get canvas name
264 * @return Stored canvas name
265 */
266 virtual const char* GetCanvasName() const override { return m_name.c_str(); }
267
268 // ====================================================================
269 // Minimap Management
270 // ====================================================================
271
272 /**
273 * @brief Render minimap overlay for custom canvas
274 * @details Uses CanvasMinimapRenderer for unified appearance
275 */
276 virtual void RenderMinimap() override;
277
278 /**
279 * @brief Enable/disable minimap
280 * @param enabled True to show minimap
281 */
282 virtual void SetMinimapVisible(bool enabled) override;
283
284 /**
285 * @brief Check if minimap is visible
286 * @return True if minimap will be rendered
287 */
288 virtual bool IsMinimapVisible() const override;
289
290 /**
291 * @brief Set minimap size ratio
292 * @param scale Size ratio (0.05 - 0.5)
293 */
294 virtual void SetMinimapSize(float scale) override;
295
296 /**
297 * @brief Get minimap size ratio
298 * @return Current size ratio
299 */
300 virtual float GetMinimapSize() const override;
301
302 /**
303 * @brief Set minimap position (corner)
304 * @param position Position enum (0=TopLeft, 1=TopRight, 2=BottomLeft, 3=BottomRight)
305 */
306 virtual void SetMinimapPosition(int position) override;
307
308 /**
309 * @brief Get minimap position
310 * @return Position enum value
311 */
312 virtual int GetMinimapPosition() const override;
313
314 /**
315 * @brief Update minimap with current graph data
316 * @param nodes Vector of (nodeId, posX, posY, width, height) tuples
317 * @param graphMinX Graph bounds left
318 * @param graphMaxX Graph bounds right
319 * @param graphMinY Graph bounds top
320 * @param graphMaxY Graph bounds bottom
321 */
323 const std::vector<std::tuple<int, float, float, float, float>>& nodes,
324 float graphMinX, float graphMaxX, float graphMinY, float graphMaxY);
325
326 /**
327 * @brief Update minimap viewport from visible canvas area
328 */
330 float viewMinX, float viewMaxX, float viewMinY, float viewMaxY,
331 float graphMinX, float graphMaxX, float graphMinY, float graphMaxY);
332
333 // ====================================================================
334 // Input Configuration
335 // ====================================================================
336
337 /**
338 * @brief Enable/disable middle-mouse panning
339 * @param enabled True to allow pan via middle mouse
340 * @note Default: enabled
341 */
342 void SetMiddleMousePanEnabled(bool enabled) { m_middleMousePanEnabled = enabled; }
343
344 /**
345 * @brief Enable/disable scroll wheel zooming
346 * @param enabled True to allow zoom via scroll wheel
347 * @note Default: enabled
348 */
349 void SetScrollZoomEnabled(bool enabled) { m_scrollZoomEnabled = enabled; }
350
351 /**
352 * @brief Set scroll zoom speed (multiplier)
353 * @param speed Zoom factor per scroll notch (default 1.1)
354 * @note Higher = faster zoom response
355 */
356 void SetScrollZoomSpeed(float speed) { m_scrollZoomSpeed = speed; }
357
358 private:
359 // Canvas properties
360 std::string m_name;
363
364 // Pan and zoom state
365 ImVec2 m_canvasOffset; // Pan offset (where origin is panned to)
366 float m_canvasZoom; // Zoom level (1.0 = 100%)
367 float m_minZoom; // Minimum allowed zoom
368 float m_maxZoom; // Maximum allowed zoom
369
370 // Grid state
372
373 // Minimap state
374 std::unique_ptr<CanvasMinimapRenderer> m_minimapRenderer;
375
376 // Input state
377 ImVec2 m_lastMousePos; // Previous frame mouse position
378 bool m_isPanning; // True if currently middle-mouse dragging
379 bool m_middleMousePanEnabled; // Enable/disable middle-mouse pan
380 bool m_scrollZoomEnabled; // Enable/disable scroll zoom
381 float m_scrollZoomSpeed; // Zoom factor per scroll (default 1.1)
382
383 // Internal helper methods
384 /**
385 * @brief Update pan/zoom from current frame input
386 * @details Called in BeginRender()
387 */
388 void UpdateInputState();
389
390 /**
391 * @brief Handle middle-mouse panning
392 */
393 void HandlePanning();
394
395 /**
396 * @brief Handle scroll wheel zooming
397 */
398 void HandleZooming();
399
400 /**
401 * @brief Clamp zoom to valid range
402 * @param zoom Value to clamp
403 * @return Clamped value
404 */
405 float ClampZoom(float zoom) const;
406 };
407
408} // namespace Olympe
Centralized minimap rendering helper for all canvas types.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Standardized Canvas Editor interface for unified architecture.
GridStylePreset
Pre-configured grid styles matching professional editors.
void HandlePanning()
Handle middle-mouse panning.
virtual ~CustomCanvasEditor()=default
virtual void SetMinimapPosition(int position) override
Set minimap position (corner)
void HandleZooming()
Handle scroll wheel zooming.
virtual ImVec2 GetZoomLimits() const override
Get zoom limits.
virtual void ResetZoom() override
Reset zoom to 1.0x.
virtual CanvasGridRenderer::GridConfig GetGridConfig() const override
Get current grid configuration.
std::unique_ptr< CanvasMinimapRenderer > m_minimapRenderer
float ClampZoom(float zoom) const
Clamp zoom to valid range.
void UpdateMinimapNodes(const std::vector< std::tuple< int, float, float, float, float > > &nodes, float graphMinX, float graphMaxX, float graphMinY, float graphMaxY)
Update minimap with current graph data.
virtual float GetMinimapSize() const override
Get minimap size ratio.
virtual void EndRender() override
End rendering cycle.
virtual void RenderGrid(CanvasGridRenderer::GridStylePreset preset) override
Render grid.
virtual void BeginRender() override
Begin rendering cycle.
virtual void SetMinimapVisible(bool enabled) override
Enable/disable minimap.
virtual ImVec2 GetCanvasSize() const override
Get canvas size.
virtual const char * GetCanvasName() const override
Get canvas name.
virtual bool IsPointInCanvas(const ImVec2 &screenPos) const override
Check if point is in canvas.
virtual float GetZoom() const override
Get current zoom level.
virtual void ZoomBy(float factor, const ImVec2 *zoomCenter=nullptr) override
Zoom by multiplier.
virtual bool IsMinimapVisible() const override
Check if minimap is visible.
virtual void SetPan(const ImVec2 &offset) override
Set pan offset directly.
virtual void PanBy(const ImVec2 &delta) override
Pan by delta.
virtual void SetCanvasScreenPos(const ImVec2 &screenPos) override
Set canvas screen position (updates each frame)
virtual ImVec2 ScreenToCanvas(const ImVec2 &screenPos) const override
Transform screen space to canvas space.
virtual void ResetPan() override
Reset pan to origin.
virtual void SetCanvasSize(const ImVec2 &size) override
Set canvas size (updates each frame)
virtual bool IsCanvasHovered() const override
Check if canvas is hovered.
virtual ImVec2 GridToEditor(const ImVec2 &gridPos) const override
Transform grid space to editor space.
virtual ImVec2 GetPan() const override
Get current pan offset.
virtual void SetZoom(float scale, const ImVec2 *zoomCenter=nullptr) override
Set zoom level directly.
virtual ImVec2 CanvasToScreen(const ImVec2 &canvasPos) const override
Transform canvas space to screen space.
virtual bool IsGridVisible() const override
Check if grid is visible.
virtual ImVec2 EditorToGrid(const ImVec2 &editorPos) const override
Transform editor space to grid space.
void SetScrollZoomSpeed(float speed)
Set scroll zoom speed (multiplier)
virtual ImVec2 GetCanvasScreenPos() const override
Get canvas screen position.
virtual void GetCanvasVisibleBounds(ImVec2 &outMin, ImVec2 &outMax) const override
Get canvas visible bounds in canvas space.
virtual void RenderMinimap() override
Render minimap overlay for custom canvas.
void SetMiddleMousePanEnabled(bool enabled)
Enable/disable middle-mouse panning.
virtual void SetGridVisible(bool enabled) override
Set grid visibility.
void UpdateMinimapViewport(float viewMinX, float viewMaxX, float viewMinY, float viewMaxY, float graphMinX, float graphMaxX, float graphMinY, float graphMaxY)
Update minimap viewport from visible canvas area.
void UpdateInputState()
Update pan/zoom from current frame input.
virtual void ResetView() override
Reset view (pan and zoom)
void SetScrollZoomEnabled(bool enabled)
Enable/disable scroll wheel zooming.
virtual void SetMinimapSize(float scale) override
Set minimap size ratio.
virtual int GetMinimapPosition() const override
Get minimap position.
Abstract interface for canvas editors with unified pan/zoom/grid management.
< Provides AssetID and INVALID_ASSET_ID
Configuration parameters for grid rendering.