#include "lel.hpp" #include #include "dbc.hpp" #include #include "lel_parser.cpp" namespace lel { Parser::Parser(int x, int y, int width, int height) : grid_x(x), grid_y(y), grid_w(width), grid_h(height), cur(0, 0) { } 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); } cur = {cur.col + 1, cur.row}; } void Parser::finalize() { dbc::check(columns > 0, "columns are 0"); dbc::check(rows > 0, "rows are 0"); int cell_width = grid_w / columns; 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& [name, cell] : cells) { cell.x = grid_x + (cell.col * cell_width); cell.y = grid_y + (cell.row * cell_height); cell.max_w = cell.max_w == 0 ? cell_width : cell.max_w; cell.max_h = cell.max_h == 0 ? cell_height : cell.max_h; 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); 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; } dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name)); dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name)); } } void Parser::reset() { rows = 0; columns = 0; cur = {0, 0}; } }