Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
GridSnapping.h
Go to the documentation of this file.
1/**
2 * @file GridSnapping.h
3 * @brief Grid-snapping helper for the VS graph canvas (Phase 7).
4 * @author Olympe Engine
5 * @date 2026-03-10
6 *
7 * @details
8 * GridSnapping is a lightweight singleton that aligns node positions to a
9 * configurable grid. When disabled, Snap() / SnapX() / SnapY() return the
10 * input value unchanged so callers need no extra branch.
11 *
12 * C++14 compliant — no std::optional, structured bindings, std::filesystem.
13 */
14
15#pragma once
16
17namespace Olympe {
18
19// ============================================================================
20// GridSnapping
21// ============================================================================
22
23/**
24 * @class GridSnapping
25 * @brief Singleton grid-snapping helper.
26 *
27 * Typical usage:
28 * @code
29 * auto& gs = GridSnapping::Get();
30 * gs.SetEnabled(true);
31 * gs.SetGridSize(16);
32 * float x = 123.7f, y = 88.2f;
33 * gs.Snap(x, y); // x == 128.0f, y == 80.0f
34 * @endcode
35 */
37public:
38
39 // -----------------------------------------------------------------------
40 // Singleton access
41 // -----------------------------------------------------------------------
42
43 /**
44 * @brief Returns the single shared instance.
45 */
46 static GridSnapping& Get();
47
48 // -----------------------------------------------------------------------
49 // State
50 // -----------------------------------------------------------------------
51
52 bool IsEnabled() const;
53 void SetEnabled(bool enabled);
54
55 /**
56 * @brief Toggles snapping on/off.
57 */
58 void Toggle();
59
60 // -----------------------------------------------------------------------
61 // Grid size
62 // -----------------------------------------------------------------------
63
64 /**
65 * @brief Returns the current grid cell size in pixels (default: 16).
66 */
67 int GetGridSize() const;
68
69 /**
70 * @brief Sets the grid cell size. Values <= 0 are ignored.
71 */
72 void SetGridSize(int size);
73
74 // -----------------------------------------------------------------------
75 // Snapping
76 // -----------------------------------------------------------------------
77
78 /**
79 * @brief Snaps a single x coordinate to the nearest grid line.
80 */
81 float SnapX(float x) const;
82
83 /**
84 * @brief Snaps a single y coordinate to the nearest grid line.
85 */
86 float SnapY(float y) const;
87
88 /**
89 * @brief Snaps both x and y coordinates in place.
90 */
91 void Snap(float& x, float& y) const;
92
93private:
94
96
99};
100
101} // namespace Olympe
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