Add pixel-precise grid for any grid size

This commit is contained in:
Liebranca 2025-02-19 14:34:37 -03:00
parent 0a18e5ffc6
commit 14b64f341e
1 changed files with 25 additions and 57 deletions

View File

@ -58,6 +58,7 @@
#include "base/chrono.h" #include "base/chrono.h"
#include "base/convert_to.h" #include "base/convert_to.h"
#include "base/pi.h" #include "base/pi.h"
#include "doc/algo.h"
#include "doc/doc.h" #include "doc/doc.h"
#include "doc/mask_boundaries.h" #include "doc/mask_boundaries.h"
#include "doc/slice.h" #include "doc/slice.h"
@ -1186,7 +1187,7 @@ void Editor::drawGrid(Graphics* g,
y1 -= dy; y1 -= dy;
// Draw pixel-precise isometric grid when zoomed in // Draw pixel-precise isometric grid when zoomed in
if (m_proj.zoom().scale() > 8.00) { if (m_proj.zoom().scale() > 6.00) {
ui::Paint paint; ui::Paint paint;
paint.style(ui::Paint::Stroke); paint.style(ui::Paint::Stroke);
paint.antialias(false); paint.antialias(false);
@ -1249,6 +1250,11 @@ void Editor::drawGrid(Graphics* g,
} }
} }
static void push_line_pixel(const int x, const int y, std::vector<Point>* const data)
{
data->push_back(Point(x, y));
};
gfx::Path& Editor::getIsometricGridPath(Rect& grid) gfx::Path& Editor::getIsometricGridPath(Rect& grid)
{ {
static Path path; static Path path;
@ -1269,66 +1275,28 @@ gfx::Path& Editor::getIsometricGridPath(Rect& grid)
// Prepare bitmap // Prepare bitmap
im->clear(0x00); im->clear(0x00);
int x = 0; const Point a(0, std::round(grid.h * 0.5));
int y = int(std::round(grid.h * 0.5)); const Point b(std::floor(grid.w * 0.5), grid.h);
const int lx = grid.w; std::vector<Point> line;
int ly = y - 1;
im->fillRect(0, std::round(y * pix.h), std::round(lx * pix.w), std::round(y * pix.h), 0x01); // We use the line drawing algorithm to find the points
// for a single pixel-precise line
doc::algo_line_continuous_with_fix_for_line_brush(a.x,
a.y,
b.x,
b.y,
&line,
(doc::AlgoPixel)&push_line_pixel);
y++; // Iterating on said points, we fill in the bitmap
x++; // for a single cell grid
for (auto p : line)
// 2:1 im->fillRect(std::round(p.x * pix.w),
if (grid.w == grid.h * 2) { std::round((grid.h - p.y) * pix.h),
for (; y < grid.h; y++, x += 2) std::floor((grid.w - p.x) * pix.w),
im->fillRect(std::round(x * pix.w), std::floor(p.y * pix.h),
std::round((y - (x + 1)) * pix.h),
std::round((lx - x) * pix.w),
std::round(y * pix.h),
0x01); 0x01);
im->fillRect(std::round(x * pix.w),
0,
std::round((x + 2) * pix.w),
std::round(y * pix.h),
0x01);
}
// 1:1
else if (grid.w == grid.h) {
for (; y < grid.h; y++, x++, ly--)
im->fillRect(std::round(x * pix.w),
std::round(ly * pix.h),
std::round((lx - x) * pix.w),
std::round(y * pix.h),
0x01);
im->fillRect(std::round(x * pix.w), 0, std::round(x * pix.w), std::round(y * pix.h), 0x01);
}
// Quick test for other ratios
else if (grid.w > grid.h * 2) {
float step = 0.00;
float rem = 0.00;
const float len = float(grid.w) / (grid.h + 1);
for (; y < grid.h; y++, x += step, ly--) {
step = len + rem;
rem = (step - int(step)) * int(std::round(step) > std::floor(step));
step = std::round(step);
im->fillRect(std::round(x * pix.w),
std::round(ly * pix.h),
std::round((lx - x) * pix.w),
std::round(y * pix.h),
0x01);
}
im->fillRect(std::round((grid.w * 0.5 - 1) * pix.w),
0,
std::round((grid.w * 0.5 + 1) * pix.w),
std::round(y * pix.h),
0x01);
}
doc::MaskBoundaries immask; doc::MaskBoundaries immask;
immask.regen(im); immask.regen(im);
immask.createPathIfNeeeded(); immask.createPathIfNeeeded();