Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
GridSnapping.cpp
Go to the documentation of this file.
1/**
2 * @file GridSnapping.cpp
3 * @brief Grid-snapping helper implementation (Phase 7).
4 * @author Olympe Engine
5 * @date 2026-03-10
6 */
7
8#include "GridSnapping.h"
9
10#include <cmath>
11
12namespace Olympe {
13
14// ============================================================================
15// Singleton
16// ============================================================================
17
19{
20 static GridSnapping s_Instance;
21 return s_Instance;
22}
23
24// ============================================================================
25// Construction
26// ============================================================================
27
29 : m_Enabled(true)
30 , m_GridSize(16)
31{
32}
33
34// ============================================================================
35// State
36// ============================================================================
37
39{
40 return m_Enabled;
41}
42
43void GridSnapping::SetEnabled(bool enabled)
44{
45 m_Enabled = enabled;
46}
47
52
53// ============================================================================
54// Grid size
55// ============================================================================
56
58{
59 return m_GridSize;
60}
61
63{
64 if (size > 0)
65 m_GridSize = size;
66}
67
68// ============================================================================
69// Snapping
70// ============================================================================
71
72float GridSnapping::SnapX(float x) const
73{
74 if (!m_Enabled || m_GridSize <= 0)
75 return x;
76
77 float gs = static_cast<float>(m_GridSize);
78 return std::floor((x / gs) + 0.5f) * gs;
79}
80
81float GridSnapping::SnapY(float y) const
82{
83 if (!m_Enabled || m_GridSize <= 0)
84 return y;
85
86 float gs = static_cast<float>(m_GridSize);
87 return std::floor((y / gs) + 0.5f) * gs;
88}
89
90void GridSnapping::Snap(float& x, float& y) const
91{
92 x = SnapX(x);
93 y = SnapY(y);
94}
95
96} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
Grid-snapping helper for the VS graph canvas (Phase 7).
Singleton grid-snapping helper.
void Toggle()
Toggles snapping on/off.
static GridSnapping & Get()
Returns the single shared instance.
float SnapX(float x) const
Snaps a single x coordinate to the nearest grid line.
float SnapY(float y) const
Snaps a single y coordinate to the nearest grid line.
void SetEnabled(bool enabled)
void SetGridSize(int size)
Sets the grid cell size.
void Snap(float &x, float &y) const
Snaps both x and y coordinates in place.
int GetGridSize() const
Returns the current grid cell size in pixels (default: 16).
< Provides AssetID and INVALID_ASSET_ID