Can now use lel::center to center something inside a cell.

This commit is contained in:
Zed A. Shaw 2025-02-15 13:22:36 -05:00
parent c03a384372
commit aa149b6574
4 changed files with 32 additions and 5 deletions

18
lel.cpp
View file

@ -26,6 +26,7 @@ namespace lel {
cur = {cur.col + 1, cur.row};
}
void Parser::finalize() {
dbc::check(columns > 0, "columns are 0");
dbc::check(rows > 0, "rows are 0");
@ -47,12 +48,14 @@ namespace lel {
cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w);
cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h);
cell.mid_x = std::midpoint(cell.x, cell.x + cell_width);
cell.mid_y = std::midpoint(cell.y, cell.y + cell_height);
if(cell.right) cell.x += cell_width - cell.w;
if(cell.bottom) cell.y += cell_height - cell.h;
if(cell.center) {
cell.x = std::midpoint(cell.x, cell.x + cell_width) - cell.w / 2;
cell.y = std::midpoint(cell.y, cell.y + cell_height) - cell.h / 2;
cell.x = cell.mid_x - cell.w / 2;
cell.y = cell.mid_y - cell.h / 2;
}
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
@ -65,4 +68,15 @@ namespace lel {
columns = 0;
cur = {0, 0};
}
Cell center(int width, int height, Cell &parent) {
Cell copy = parent;
copy.x = parent.mid_x - width / 2;
copy.y = parent.mid_y - height / 2;
copy.w = width;
copy.h = height;
return copy;
}
}