Files
Editor2D/include/Editor/Grid.hpp

29 lines
1.3 KiB
C++

#pragma once
#include <J3ML/Geometry.hpp>
#include <JGL/JGL.h>
/// Draws a grid across the axis-aligned bounding-box, with a zoom level that auto-adjusts to the bounding-box's size.
inline void DrawGrid2(const AABB2D& bounds, const Vector2& cell_size, const Color4& color, float spacing = 4, float segment_length = 4) {
Vector2 viewport_topleft = bounds.minPoint;
Vector2 viewport_bottomright = bounds.maxPoint;
int nearest_grid_left = Math::Floor(viewport_topleft.x / cell_size.x);
int nearest_grid_right = Math::Floor(viewport_bottomright.x / cell_size.x);
for (int x = nearest_grid_left; x <= nearest_grid_right; x++) {
auto top = Vector2(x * cell_size.x, viewport_topleft.y);
auto bottom = Vector2(x * cell_size.x, viewport_bottomright.y);
JGL::J2D::DrawDashedLine(color, top, bottom, spacing, segment_length, 1 / bounds.Width());
}
int nearest_grid_top = Math::Floor(viewport_topleft.y / cell_size.y);
int nearest_grid_bottom = Math::Floor(viewport_bottomright.y / cell_size.y);
for (int y = nearest_grid_top; y <= nearest_grid_bottom; y++) {
auto left = Vector2(viewport_topleft.x, y * cell_size.y);
auto right = Vector2(viewport_bottomright.x, y * cell_size.y);
JGL::J2D::DrawDashedLine(color, left, right, spacing, segment_length, 1 / bounds.Height());
}
}