From 60ed686eb02ad7cf352dd4a7599e243679b69398 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 15 Feb 2025 12:13:58 -0500 Subject: [PATCH] Already better than CSS because I can center stuff. --- combat_ui.cpp | 2 +- lel.cpp | 16 ++++++----- lel.hpp | 2 ++ lel_parser.cpp | 73 +++++++++++++++++++++++++++----------------------- lel_parser.rl | 4 ++- tests/lel.cpp | 3 ++- 6 files changed, 58 insertions(+), 42 deletions(-) diff --git a/combat_ui.cpp b/combat_ui.cpp index b65c928..9250bda 100644 --- a/combat_ui.cpp +++ b/combat_ui.cpp @@ -9,7 +9,7 @@ namespace gui { $level(level) { bool good = $layout.parse( - "[attack1 | attack2 | attack3 | heal]" + "[attack1 | =(90)attack2 | attack3 | heal]" ); dbc::check(good, "failed to parse combat layout"); diff --git a/lel.cpp b/lel.cpp index b815762..2f15d6b 100644 --- a/lel.cpp +++ b/lel.cpp @@ -1,6 +1,7 @@ #include "lel.hpp" #include #include "dbc.hpp" +#include #include "lel_parser.cpp" @@ -58,6 +59,10 @@ namespace lel { cur.expand = true; } + void Parser::center() { + cur.center = true; + } + void Parser::finalize() { dbc::check(columns > 0, "columns are 0"); dbc::check(rows > 0, "rows are 0"); @@ -75,12 +80,11 @@ 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); - if(cell.right) { - cell.x += cell_width - cell.w; - } - - if(cell.bottom) { - cell.y += cell_height - cell.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)); diff --git a/lel.hpp b/lel.hpp index 3584007..41f7476 100644 --- a/lel.hpp +++ b/lel.hpp @@ -16,6 +16,7 @@ namespace lel { bool right = false; bool bottom = false; bool expand = false; + bool center = false; Cell(int col, int row) : col(col), row(row) {} }; @@ -41,6 +42,7 @@ namespace lel { void setheight(int height); void expand(); void reset(); + void center(); bool parse(std::string input); void finalize(); }; diff --git a/lel_parser.cpp b/lel_parser.cpp index 70672c4..dcdc7cb 100644 --- a/lel_parser.cpp +++ b/lel_parser.cpp @@ -7,7 +7,7 @@ namespace lel { -#line 36 "lel_parser.rl" +#line 38 "lel_parser.rl" @@ -15,33 +15,34 @@ namespace lel { static const char _Parser_actions[] = { 0, 1, 1, 1, 2, 1, 3, 1, 4, 1, 5, 1, 6, 1, 9, 1, - 10, 1, 11, 2, 0, 7, 2, 0, - 8, 2, 4, 1, 2, 4, 5 + 10, 1, 11, 1, 12, 2, 0, 7, + 2, 0, 8, 2, 4, 1, 2, 4, + 5 }; static const char _Parser_key_offsets[] = { - 0, 0, 4, 18, 20, 24, 35, 47, - 52, 54, 57 + 0, 0, 4, 19, 21, 25, 37, 49, + 54, 56, 59 }; static const char _Parser_trans_keys[] = { 32, 91, 9, 13, 32, 40, 42, 46, - 60, 62, 94, 95, 9, 13, 65, 90, - 97, 122, 48, 57, 41, 44, 48, 57, - 40, 42, 46, 60, 62, 94, 95, 65, - 90, 97, 122, 32, 93, 95, 124, 9, - 13, 48, 57, 65, 90, 97, 122, 32, - 93, 124, 9, 13, 48, 57, 41, 48, - 57, 32, 91, 9, 13, 0 + 61, 94, 95, 9, 13, 60, 62, 65, + 90, 97, 122, 48, 57, 41, 44, 48, + 57, 40, 42, 46, 61, 94, 95, 60, + 62, 65, 90, 97, 122, 32, 93, 95, + 124, 9, 13, 48, 57, 65, 90, 97, + 122, 32, 93, 124, 9, 13, 48, 57, + 41, 48, 57, 32, 91, 9, 13, 0 }; static const char _Parser_single_lengths[] = { - 0, 2, 8, 0, 2, 7, 4, 3, + 0, 2, 7, 0, 2, 6, 4, 3, 0, 1, 2 }; static const char _Parser_range_lengths[] = { - 0, 1, 3, 1, 1, 2, 4, 1, + 0, 1, 4, 1, 1, 3, 4, 1, 1, 1, 1 }; @@ -52,24 +53,26 @@ static const char _Parser_index_offsets[] = { static const char _Parser_indicies[] = { 0, 2, 0, 1, 3, 4, 5, 6, - 7, 7, 6, 8, 3, 8, 8, 1, - 9, 1, 10, 11, 12, 1, 4, 5, - 6, 7, 7, 6, 8, 8, 8, 1, - 13, 15, 14, 16, 13, 14, 14, 14, - 1, 17, 18, 19, 17, 1, 20, 1, - 21, 22, 1, 23, 2, 23, 1, 0 + 8, 6, 9, 3, 7, 9, 9, 1, + 10, 1, 11, 12, 13, 1, 4, 5, + 6, 8, 6, 9, 7, 9, 9, 1, + 14, 16, 15, 17, 14, 15, 15, 15, + 1, 18, 19, 20, 18, 1, 21, 1, + 22, 23, 1, 24, 2, 24, 1, 0 }; static const char _Parser_trans_targs[] = { 1, 0, 2, 2, 3, 5, 5, 5, - 6, 4, 5, 8, 4, 7, 6, 10, - 2, 7, 10, 2, 9, 5, 9, 10 + 5, 6, 4, 5, 8, 4, 7, 6, + 10, 2, 7, 10, 2, 9, 5, 9, + 10 }; static const char _Parser_trans_actions[] = { 0, 0, 3, 0, 0, 13, 5, 11, - 17, 15, 19, 19, 0, 7, 0, 28, - 25, 0, 9, 1, 15, 22, 0, 0 + 15, 19, 17, 21, 21, 0, 7, 0, + 30, 27, 0, 9, 1, 17, 24, 0, + 0 }; static const int Parser_start = 1; @@ -79,7 +82,7 @@ static const int Parser_error = 0; static const int Parser_en_main = 1; -#line 39 "lel_parser.rl" +#line 41 "lel_parser.rl" bool Parser::parse(std::string input) { reset(); @@ -91,14 +94,14 @@ bool Parser::parse(std::string input) { std::string tk; -#line 86 "lel_parser.cpp" +#line 89 "lel_parser.cpp" { cs = Parser_start; } -#line 50 "lel_parser.rl" +#line 52 "lel_parser.rl" -#line 89 "lel_parser.cpp" +#line 92 "lel_parser.cpp" { int _klen; unsigned int _trans; @@ -213,14 +216,18 @@ _match: { expand(); } break; case 10: -#line 29 "lel_parser.rl" - { start = p; } +#line 22 "lel_parser.rl" + { center(); } break; case 11: -#line 32 "lel_parser.rl" +#line 31 "lel_parser.rl" + { start = p; } + break; + case 12: +#line 34 "lel_parser.rl" {start = p;} break; -#line 198 "lel_parser.cpp" +#line 204 "lel_parser.cpp" } } @@ -233,7 +240,7 @@ _again: _out: {} } -#line 51 "lel_parser.rl" +#line 53 "lel_parser.rl" bool good = pe - p == 0; if(good) { diff --git a/lel_parser.rl b/lel_parser.rl index 44b35e7..aa07da0 100644 --- a/lel_parser.rl +++ b/lel_parser.rl @@ -19,16 +19,18 @@ namespace lel { action setwidth { setwidth(std::stoi(tk)); } action setheight { setheight(std::stoi(tk)); } action expand { expand(); } + action center { center(); } col = "|" $col; ltab = "[" $ltab; rtab = "]" $row; valign = ("^" | ".") $valign; expand = "*" $expand; + center = "=" $center; halign = ("<" | ">") $align; number = digit+ >{ start = fpc; } %token; setw = ("(" number %setwidth ("," number %setheight)? ")") ; - modifiers = (expand | valign | halign | setw); + modifiers = (center | expand | valign | halign | setw); id = modifiers* ((alpha | '_')+ :>> (alnum | '_')*) >{start = fpc;} %id; row = space* ltab space* id space* (col space* id space*)* space* rtab space*; diff --git a/tests/lel.cpp b/tests/lel.cpp index 20db4aa..00b2872 100644 --- a/tests/lel.cpp +++ b/tests/lel.cpp @@ -14,7 +14,7 @@ TEST_CASE("test basic ops", "[lel]") { "[ label_1 | label3 | test1]" "[ *(300,300)text1 | (150)people | ^test2]" "[ >label2 | _ | .test3]" - "[ message | buttons | test4]"); + "[ =message | buttons | test4]"); REQUIRE(good); @@ -27,6 +27,7 @@ TEST_CASE("test basic ops", "[lel]") { REQUIRE(parser.cells.at("text1").h == 300); REQUIRE(parser.cells.at("people").expand == false); REQUIRE(parser.cells.at("message").expand == false); + REQUIRE(parser.cells.at("message").center == true); for(auto& [name, cell] : parser.cells) { REQUIRE(cell.w > 0);