Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
CanvasMinimapRenderer.cpp
Go to the documentation of this file.
1/**
2 * @file CanvasMinimapRenderer.cpp
3 * @brief Centralized minimap rendering implementation
4 * @author Olympe Engine
5 * @date 2026-03-15
6 */
7
9#include "../../third_party/imnodes/imnodes.h"
10#include <algorithm>
11#include <cmath>
12
13namespace Olympe
14{
15
17 : m_visible(false)
18 , m_sizeRatio(0.15f)
19 , m_position(MinimapPosition::TopRight)
20 , m_backgroundColor(IM_COL32(40, 40, 50, 200))
21 , m_nodeColor(IM_COL32(100, 150, 255, 255))
22 , m_viewportColor(IM_COL32(200, 200, 200, 100))
23{
24}
25
26// ============================================================================
27// Configuration
28// ============================================================================
29
31{
32 m_visible = visible;
33}
34
36{
37 return m_visible;
38}
39
41{
42 // Clamp between 0.05 (5%) and 0.5 (50%)
43 m_sizeRatio = std::max(0.05f, std::min(0.5f, ratio));
44}
45
47{
48 return m_sizeRatio;
49}
50
55
60
61// ============================================================================
62// Data Updates
63// ============================================================================
64
65float CanvasMinimapRenderer::NormaliseCoord(float value, float minVal, float maxVal)
66{
67 float range = maxVal - minVal;
68 if (range <= 0.0f)
69 return 0.0f;
70
71 float norm = (value - minVal) / range;
72
73 // Clamp to [0, 1]
74 if (norm < 0.0f) norm = 0.0f;
75 if (norm > 1.0f) norm = 1.0f;
76
77 return norm;
78}
79
81 const std::vector<std::tuple<int, float, float, float, float>>& nodes,
82 float graphMinX, float graphMaxX, float graphMinY, float graphMaxY)
83{
84 m_normalisedNodes.clear();
85 m_normalisedNodes.reserve(nodes.size());
86
87 for (size_t i = 0; i < nodes.size(); ++i)
88 {
89 int nodeId = std::get<0>(nodes[i]);
90 float posX = std::get<1>(nodes[i]);
91 float posY = std::get<2>(nodes[i]);
92 float width = std::get<3>(nodes[i]);
93 float height = std::get<4>(nodes[i]);
94
96 normNode.nodeId = nodeId;
99 normNode.normW = NormaliseCoord(posX + width, graphMinX, graphMaxX) - normNode.normX;
100 normNode.normH = NormaliseCoord(posY + height, graphMinY, graphMaxY) - normNode.normY;
101
102 // Clamp size to avoid negative dimensions
103 if (normNode.normW < 0.0f) normNode.normW = 0.0f;
104 if (normNode.normH < 0.0f) normNode.normH = 0.0f;
105
106 m_normalisedNodes.push_back(normNode);
107 }
108}
109
125
126// ============================================================================
127// Rendering
128// ============================================================================
129
131{
132 if (!m_visible)
133 return;
134
135 // Use ImNodes native MiniMap
136 // Position enum matches: TopLeft=0, TopRight=1, BottomLeft=2, BottomRight=3
137 ImNodes::MiniMap(m_sizeRatio, static_cast<ImNodesMiniMapLocation>(m_position));
138}
139
141{
142 if (!m_visible || canvasSize.x <= 0.0f || canvasSize.y <= 0.0f)
143 return;
144
145 ImGuiIO& io = ImGui::GetIO();
146 ImDrawList* drawList = ImGui::GetForegroundDrawList();
147 if (!drawList)
148 return;
149
150 // Compute minimap dimensions
151 float minimapW = canvasSize.x * m_sizeRatio;
152 float minimapH = canvasSize.y * m_sizeRatio;
153
154 // Compute minimap position based on m_position
156 switch (m_position)
157 {
159 minimapPos.x = canvasScreenPos.x + canvasSize.x - minimapW - 5.0f;
160 minimapPos.y = canvasScreenPos.y + 5.0f;
161 break;
163 minimapPos.x = canvasScreenPos.x + 5.0f;
164 minimapPos.y = canvasScreenPos.y + 5.0f;
165 break;
167 minimapPos.x = canvasScreenPos.x + 5.0f;
168 minimapPos.y = canvasScreenPos.y + canvasSize.y - minimapH - 5.0f;
169 break;
171 minimapPos.x = canvasScreenPos.x + canvasSize.x - minimapW - 5.0f;
172 minimapPos.y = canvasScreenPos.y + canvasSize.y - minimapH - 5.0f;
173 break;
174 }
175
177
178 // Draw background
179 drawList->AddRectFilled(minimapPos, minimapMax, m_backgroundColor, 4.0f);
180 drawList->AddRect(minimapPos, minimapMax, IM_COL32(150, 150, 150, 255), 4.0f, 0, 1.0f);
181
182 // Draw normalised nodes as small rectangles
183 for (const auto& node : m_normalisedNodes)
184 {
185 float nodeScreenX = minimapPos.x + node.normX * minimapW;
186 float nodeScreenY = minimapPos.y + node.normY * minimapH;
187 float nodeScreenW = node.normW * minimapW;
188 float nodeScreenH = node.normH * minimapH;
189
190 // Clamp node dimensions (minimum 2 pixels for visibility)
191 if (nodeScreenW < 2.0f) nodeScreenW = 2.0f;
192 if (nodeScreenH < 2.0f) nodeScreenH = 2.0f;
193
196
197 drawList->AddRectFilled(nodeMin, nodeMax, m_nodeColor, 1.0f);
198 drawList->AddRect(nodeMin, nodeMax, IM_COL32(200, 200, 255, 255), 1.0f);
199 }
200
201 // Draw viewport rectangle (with transparency)
202 {
207
210
211 drawList->AddRectFilled(vpMin, vpMax, m_viewportColor, 1.0f);
212 drawList->AddRect(vpMin, vpMax, IM_COL32(255, 255, 255, 255), 1.0f, 0, 2.0f);
213 }
214}
215
216// ============================================================================
217// Data Access
218// ============================================================================
219
220const std::vector<MinimapNodeData>& CanvasMinimapRenderer::GetNormalisedNodes() const
221{
222 return m_normalisedNodes;
223}
224
229
230// ============================================================================
231// Colors
232// ============================================================================
233
238
240{
241 m_nodeColor = color;
242}
243
248
249} // namespace Olympe
Centralized minimap rendering helper for all canvas types.
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
void SetPosition(MinimapPosition pos)
Set minimap corner position.
void SetViewportColor(ImU32 color)
Set minimap viewport rectangle color (RGBA)
void RenderImNodes() const
Render minimap using ImNodes native API.
void SetNodeColor(ImU32 color)
Set minimap node color (RGBA)
void SetBackgroundColor(ImU32 color)
Set minimap background color (RGBA)
float GetSize() const
Get current size ratio.
std::vector< MinimapNodeData > m_normalisedNodes
void RenderCustom(const ImVec2 &canvasScreenPos, const ImVec2 &canvasSize) const
Render minimap using custom ImGui drawing.
MinimapPosition GetPosition() const
Get current position.
bool IsVisible() const
Check if minimap should be rendered.
static float NormaliseCoord(float value, float minVal, float maxVal)
float m_sizeRatio
0.05 - 0.5 ratio of canvas
void SetSize(float ratio)
Set minimap size as ratio of canvas (0.05 - 0.5)
const MinimapViewportData & GetNormalisedViewport() const
Get normalised viewport rectangle for custom rendering.
const std::vector< MinimapNodeData > & GetNormalisedNodes() const
Get normalised node positions for custom rendering.
void SetVisible(bool visible)
Set minimap visibility.
void UpdateViewport(float viewMinX, float viewMaxX, float viewMinY, float viewMaxY, float graphMinX, float graphMaxX, float graphMinY, float graphMaxY)
Update visible viewport.
void UpdateNodes(const std::vector< std::tuple< int, float, float, float, float > > &nodes, float graphMinX, float graphMaxX, float graphMinY, float graphMaxY)
Update node positions from raw graph data.
< Provides AssetID and INVALID_ASSET_ID
MinimapPosition
Screen corner positions for minimap overlay.
int nodeId
Original graph node ID.
Viewport region in normalised coordinates.