New design on LEL that's way more sane and allows for more flexible columns and rows.

This commit is contained in:
Zed A. Shaw 2025-02-16 11:50:14 -05:00
parent f884b83809
commit 79c84ce885
8 changed files with 83 additions and 78 deletions

83
lel.cpp
View file

@ -17,64 +17,69 @@ namespace lel {
}
void Parser::id(std::string name) {
if(name != "_") {
dbc::check(!cells.contains(name),
fmt::format("duplicate cell name {}", name));
cells.insert_or_assign(name, cur);
}
dbc::check(!cells.contains(name),
fmt::format("duplicate cell name {}", name));
cells.insert_or_assign(name, cur);
cur = {cur.col + 1, cur.row};
auto& row = grid.back();
row.push_back(name);
}
void Parser::finalize() {
dbc::check(columns > 0, "columns are 0");
dbc::check(rows > 0, "rows are 0");
int cell_width = grid_w / columns;
size_t rows = grid.size();
int cell_height = grid_h / rows;
dbc::check(cell_width > 0, "invalid cell width calc");
dbc::check(cell_height > 0, "invalid cell height calc");
for(auto& row : grid) {
for(auto& name : row) {
size_t columns = row.size();
auto& cell = cells.at(name);
for(auto& [name, cell] : cells) {
cell.x = grid_x + (cell.col * cell_width);
cell.y = grid_y + (cell.row * cell_height);
int cell_width = grid_w / columns;
dbc::check(cell_width > 0, "invalid cell width calc");
dbc::check(cell_height > 0, "invalid cell height calc");
// ZED: getting a bit hairy but this should work
if(cell.percent) {
// when percent mode we have to take unset to 100%
if(cell.max_w == 0) cell.max_w = 100;
if(cell.max_h == 0) cell.max_h = 100;
cell.max_w *= cell_width * 0.01;
cell.max_h *= cell_height * 0.01;
} else {
if(cell.max_w == 0) cell.max_w = cell_width;
if(cell.max_h == 0) cell.max_h = cell_height;
}
cell.x = grid_x + (cell.col * cell_width);
cell.y = grid_y + (cell.row * cell_height);
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);
// ZED: getting a bit hairy but this should work
if(cell.percent) {
// when percent mode we have to take unset to 100%
if(cell.max_w == 0) cell.max_w = 100;
if(cell.max_h == 0) cell.max_h = 100;
cell.max_w *= cell_width * 0.01;
cell.max_h *= cell_height * 0.01;
} else {
if(cell.max_w == 0) cell.max_w = cell_width;
if(cell.max_h == 0) cell.max_h = cell_height;
}
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
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);
// keep the midpoint since it is used a lot
cell.mid_x = std::midpoint(cell.x, cell.x + cell.w);
cell.mid_y = std::midpoint(cell.y, cell.y + cell.h);
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
// perform alignments
if(cell.right) cell.x += cell_width - cell.w;
if(cell.bottom) cell.y += cell_height - cell.h;
if(cell.center) {
cell.x = cell.mid_x - cell.w / 2;
cell.y = cell.mid_y - cell.h / 2;
// keep the midpoint since it is used a lot
cell.mid_x = std::midpoint(cell.x, cell.x + cell.w);
cell.mid_y = std::midpoint(cell.y, cell.y + cell.h);
// perform alignments
if(cell.right) cell.x += cell_width - cell.w;
if(cell.bottom) cell.y += cell_height - cell.h;
if(cell.center) {
cell.x = cell.mid_x - cell.w / 2;
cell.y = cell.mid_y - cell.h / 2;
}
}
}
}
void Parser::reset() {
rows = 0;
columns = 0;
cur = {0, 0};
grid.clear();
cells.clear();
}
std::optional<std::string> Parser::hit(int x, int y) {