From cebf61a7947dcf772e61c41f970ac434ec3740a7 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 15 Feb 2025 10:59:51 -0500 Subject: [PATCH] LEL is able to position right/left/top/bottom and expand out too. --- combat_ui.cpp | 3 +-- lel.cpp | 24 +++++++++++++++++------- lel.hpp | 4 ++-- tests/lel.cpp | 14 +++++++------- 4 files changed, 27 insertions(+), 18 deletions(-) diff --git a/combat_ui.cpp b/combat_ui.cpp index 73807dc..b65c928 100644 --- a/combat_ui.cpp +++ b/combat_ui.cpp @@ -10,7 +10,6 @@ namespace gui { { bool good = $layout.parse( "[attack1 | attack2 | attack3 | heal]" - "[move1 | move2 | move3 | move4]" ); dbc::check(good, "failed to parse combat layout"); @@ -19,7 +18,7 @@ namespace gui { sf::RectangleShape button; button.setPosition({float(cell.x + 10), float(cell.y + 10)}); button.setSize({float(cell.w - 20), float(cell.h - 20)}); - button.setFillColor({uint8_t(cell.col * 75), 100, 100}); + button.setFillColor({100, 100, 100}); button.setOutlineColor({200, 200, 200}); button.setOutlineThickness(5); $shapes[name] = button; diff --git a/lel.cpp b/lel.cpp index 52f8f67..3371c40 100644 --- a/lel.cpp +++ b/lel.cpp @@ -20,18 +20,20 @@ void LELParser::col() { } void LELParser::valign(char dir) { - cur.top = dir == '^'; + cur.bottom = dir == '.'; } void LELParser::align(char dir) { - cur.left = dir == '<'; + cur.right = dir == '>'; } void LELParser::id(std::string name) { - dbc::check(!cells.contains(name), - fmt::format("duplicate cell name {}", name)); + if(name != "_") { + dbc::check(!cells.contains(name), + fmt::format("duplicate cell name {}", name)); + cells.insert_or_assign(name, cur); + } - cells.insert_or_assign(name, cur); cur = {cur.col + 1, cur.row}; } @@ -67,8 +69,16 @@ void LELParser::finalize() { 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 ? cell.max_w : std::min(cell_width, cell.max_w); - cell.h = cell.expand ? cell.max_h : std::min(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; + } dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name)); dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name)); diff --git a/lel.hpp b/lel.hpp index 484b4a7..5316024 100644 --- a/lel.hpp +++ b/lel.hpp @@ -11,8 +11,8 @@ struct Cell { int max_h = 0; int col = 0; int row = 0; - bool left = true; - bool top = true; + bool right = false; + bool bottom = false; bool expand = false; Cell(int col, int row) : col(col), row(row) {} diff --git a/tests/lel.cpp b/tests/lel.cpp index 49c8fb3..861f054 100644 --- a/tests/lel.cpp +++ b/tests/lel.cpp @@ -11,20 +11,20 @@ TEST_CASE("test basic ops", "[lel]") { LELParser parser(0, 0, 500, 500); std::vector labels{ "label_1", "label3", "text1", "people", - "label2", "_", "message", "buttons"}; + "label2", "message", "buttons"}; bool good = parser.parse( "[ label_1 | label3 | test1]" - "[ *(300,300)text1 | (150)people | test2]" - "[ >label2 | _ | test3]" + "[ *(300,300)text1 | (150)people | ^test2]" + "[ >label2 | _ | .test3]" "[ message | buttons | test4]"); REQUIRE(good); REQUIRE(parser.rows == 4); REQUIRE(parser.columns == 3); - REQUIRE(parser.cells.size() == 12); - REQUIRE(parser.cells.at("label2").left == false); + REQUIRE(parser.cells.size() == 11); + REQUIRE(parser.cells.at("label2").right == true); REQUIRE(parser.cells.at("text1").expand == true); REQUIRE(parser.cells.at("text1").w == 300); REQUIRE(parser.cells.at("text1").h == 300); @@ -34,8 +34,8 @@ TEST_CASE("test basic ops", "[lel]") { for(auto name : labels) { auto& cell = parser.cells.at(name); - fmt::println("name={}; col/row={},{}; x/y={},{} w/h={},{}; left={}, top={}, expand={}", - name, cell.col, cell.row, cell.x, cell.y, cell.w, cell.h, cell.left, cell.top, cell.expand); + fmt::println("name={}; col/row={},{}; x/y={},{} w/h={},{}; right={}, bottom={}, expand={}", + name, cell.col, cell.row, cell.x, cell.y, cell.w, cell.h, cell.right, cell.bottom, cell.expand); REQUIRE(cell.w > 0); REQUIRE(cell.h > 0);