Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
CustomCanvasEditor.cpp
Go to the documentation of this file.
1/**
2 * @file CustomCanvasEditor.cpp
3 * @brief Implementation of CustomCanvasEditor with pan/zoom support
4 */
5
7#include "../../third_party/imgui/imgui.h"
8
9namespace Olympe
10{
12 const char* name,
14 ImVec2 canvasSize,
15 float initialZoom,
16 float minZoom,
17 float maxZoom
18 )
19 : m_name(name)
20 , m_canvasScreenPos(canvasScreenPos)
21 , m_canvasSize(canvasSize)
22 , m_canvasOffset(0.0f, 0.0f)
23 , m_canvasZoom(initialZoom)
24 , m_minZoom(minZoom)
25 , m_maxZoom(maxZoom)
26 , m_gridVisible(true)
27 , m_lastMousePos(ImGui::GetMousePos())
28 , m_isPanning(false)
29 , m_middleMousePanEnabled(true)
30 , m_scrollZoomEnabled(true)
31 , m_scrollZoomSpeed(1.1f)
32 , m_minimapRenderer(std::make_unique<CanvasMinimapRenderer>())
33 {
35 }
36
37 // ========================================================================
38 // Lifecycle Management
39 // ========================================================================
40
42 {
43 // Update pan/zoom from input before rendering
45 }
46
48 {
49 // No-op: input handling is done in BeginRender
50 // Grid rendering happens separately
51 }
52
53 // ========================================================================
54 // Coordinate Transformation Systems
55 // ========================================================================
56
58 {
59 // screen → canvas: (screen - canvasScreenPos - pan) / zoom
63 return result;
64 }
65
67 {
68 // canvas → screen: canvas * zoom + pan + canvasScreenPos
69 ImVec2 scaledByZoom = ImVec2(canvasPos.x * m_canvasZoom, canvasPos.y * m_canvasZoom);
72 return result;
73 }
74
76 {
77 // Grid space = editor space - pan (no zoom applied to grid space)
79 }
80
82 {
83 // Editor space = grid space + pan
85 }
86
87 // ========================================================================
88 // Zoom Management
89 // ========================================================================
90
92 {
93 float oldZoom = m_canvasZoom;
95
96 // If zoom center provided, adjust pan to keep that point visually stationary
98 {
99 // In screen space, zoomCenter is the point we want to keep stationary
100 // Convert to canvas space at old zoom
104
105 // Calculate where this point would be at new zoom with current pan
109 );
110
111 // Adjust pan so the point ends up back at zoomCenter
115 }
116 }
117
122
123 float CustomCanvasEditor::ClampZoom(float zoom) const
124 {
125 if (zoom < m_minZoom) return m_minZoom;
126 if (zoom > m_maxZoom) return m_maxZoom;
127 return zoom;
128 }
129
130 // ========================================================================
131 // Grid Management
132 // ========================================================================
133
135 {
136 // Get default preset (VisualScript style for consistency)
139
140 // Apply canvas-specific and zoom-specific parameters
141 config.canvasPos = m_canvasScreenPos;
142 config.canvasSize = m_canvasSize;
143 config.zoom = m_canvasZoom; // Include zoom scaling
144 config.offsetX = m_canvasOffset.x;
145 config.offsetY = m_canvasOffset.y;
146
147 return config;
148 }
149
166
168 {
169 m_gridVisible = enabled;
170 }
171
172 // ========================================================================
173 // Canvas Properties
174 // ========================================================================
175
177 {
178 // Canvas visible area in screen space
181
182 // Convert to canvas space
185 }
186
194
195 // ========================================================================
196 // Context Information
197 // ========================================================================
198
200 {
201 ImGuiIO& io = ImGui::GetIO();
202 ImVec2 mousePos = io.MousePos;
203
204 // Check if mouse is over canvas
206
207 // Check if not blocked by other windows
208 bool hasWindowFocus = ImGui::IsWindowHovered(ImGuiHoveredFlags_RootAndChildWindows);
209
210 return inBounds && hasWindowFocus;
211 }
212
213 // ========================================================================
214 // Minimap Management
215 // ========================================================================
216
218 {
219 if (!m_minimapRenderer || !m_minimapRenderer->IsVisible())
220 return;
221
222 // Render custom minimap overlay for this canvas
224 }
225
227 {
229 m_minimapRenderer->SetVisible(enabled);
230 }
231
233 {
234 return m_minimapRenderer ? m_minimapRenderer->IsVisible() : false;
235 }
236
242
244 {
245 return m_minimapRenderer ? m_minimapRenderer->GetSize() : 0.15f;
246 }
247
249 {
251 {
252 // Clamp position to valid range [0, 3]
253 int pos = (position < 0) ? 0 : ((position > 3) ? 3 : position);
254 m_minimapRenderer->SetPosition(static_cast<MinimapPosition>(pos));
255 }
256 }
257
259 {
260 return m_minimapRenderer ? static_cast<int>(m_minimapRenderer->GetPosition()) : 0;
261 }
262
264 const std::vector<std::tuple<int, float, float, float, float>>& nodes,
265 float graphMinX, float graphMaxX, float graphMinY, float graphMaxY)
266 {
269 }
270
272 float viewMinX, float viewMaxX, float viewMinY, float viewMaxY,
273 float graphMinX, float graphMaxX, float graphMinY, float graphMaxY)
274 {
278 }
279
280 // ========================================================================
281 // Input Handling
282 // ========================================================================
283
285 {
286 ImGuiIO& io = ImGui::GetIO();
287
288 // Check if canvas area should receive input
289 // For child windows, IsPointInCanvas is more reliable than IsWindowHovered
290 bool isPointInCanvasArea = IsPointInCanvas(io.MousePos);
292
293 // Canvas should receive input if mouse is in canvas area
295
297 {
298 m_lastMousePos = io.MousePos;
299 m_isPanning = false;
300 return;
301 }
302
303 // Handle panning
305 {
307 }
308
309 // Handle zooming
311 {
313 }
314
315 m_lastMousePos = io.MousePos;
316 }
317
319 {
320 ImGuiIO& io = ImGui::GetIO();
321
322 // Middle mouse button (button 2)
323 if (ImGui::IsMouseDown(ImGuiMouseButton_Middle))
324 {
325 if (!m_isPanning)
326 {
327 m_isPanning = true;
328 m_lastMousePos = io.MousePos;
329 }
330
331 // Pan by mouse delta
332 ImVec2 delta = io.MouseDelta;
334 }
335 else
336 {
337 m_isPanning = false;
338 }
339 }
340
342 {
343 ImGuiIO& io = ImGui::GetIO();
344
345 // Check for scroll wheel input
346 if (io.MouseWheel != 0.0f)
347 {
348 // Calculate zoom factor from scroll
349 float factor = (io.MouseWheel > 0.0f) ? m_scrollZoomSpeed : (1.0f / m_scrollZoomSpeed);
350
351 // Get current mouse position (screen space) as zoom center
352 ImVec2 zoomCenter = io.MousePos;
353
354 // Apply zoom
356
357 // Debug: Log zoom event
358 // std::cout << "[CustomCanvasEditor] Zoom triggered: wheel=" << io.MouseWheel << " factor=" << factor << " newZoom=" << m_canvasZoom << "\n";
359 }
360 }
361
362} // namespace Olympe
ICanvasEditor implementation for custom canvas with zoom support (PrefabCanvas)
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
static GridConfig GetStylePreset(GridStylePreset preset)
Get pre-configured grid style (VisualScript/Compact/Spacious)
static void RenderGrid(ImDrawList *drawList, const GridConfig &config)
Render a grid on the given ImDrawList.
GridStylePreset
Pre-configured grid styles matching professional editors.
Unified minimap renderer for standardized appearance.
void HandlePanning()
Handle middle-mouse panning.
virtual void SetMinimapPosition(int position) override
Set minimap position (corner)
void HandleZooming()
Handle scroll wheel zooming.
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 bool IsPointInCanvas(const ImVec2 &screenPos) const override
Check if point is in canvas.
virtual void ZoomBy(float factor, const ImVec2 *zoomCenter=nullptr) override
Zoom by multiplier.
CustomCanvasEditor(const char *name, ImVec2 canvasScreenPos, ImVec2 canvasSize, float initialZoom=1.0f, float minZoom=0.1f, float maxZoom=3.0f)
Construct custom canvas editor with zoom support.
virtual bool IsMinimapVisible() const override
Check if minimap is visible.
virtual ImVec2 ScreenToCanvas(const ImVec2 &screenPos) const override
Transform screen space to canvas space.
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 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 ImVec2 EditorToGrid(const ImVec2 &editorPos) const override
Transform editor space to grid space.
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.
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 SetMinimapSize(float scale) override
Set minimap size ratio.
virtual int GetMinimapPosition() const override
Get minimap position.
< Provides AssetID and INVALID_ASSET_ID
MinimapPosition
Screen corner positions for minimap overlay.
Configuration parameters for grid rendering.