Already better than CSS because I can center stuff.

Zed A. Shaw 3 weeks ago
parent 1620a5420f
commit 60ed686eb0
  1. 2
      combat_ui.cpp
  2. 16
      lel.cpp
  3. 2
      lel.hpp
  4. 73
      lel_parser.cpp
  5. 4
      lel_parser.rl
  6. 3
      tests/lel.cpp

@ -9,7 +9,7 @@ namespace gui {
$level(level) $level(level)
{ {
bool good = $layout.parse( bool good = $layout.parse(
"[attack1 | attack2 | attack3 | heal]" "[attack1 | =(90)attack2 | attack3 | heal]"
); );
dbc::check(good, "failed to parse combat layout"); dbc::check(good, "failed to parse combat layout");

@ -1,6 +1,7 @@
#include "lel.hpp" #include "lel.hpp"
#include <fmt/core.h> #include <fmt/core.h>
#include "dbc.hpp" #include "dbc.hpp"
#include <numeric>
#include "lel_parser.cpp" #include "lel_parser.cpp"
@ -58,6 +59,10 @@ namespace lel {
cur.expand = true; cur.expand = true;
} }
void Parser::center() {
cur.center = true;
}
void Parser::finalize() { void Parser::finalize() {
dbc::check(columns > 0, "columns are 0"); dbc::check(columns > 0, "columns are 0");
dbc::check(rows > 0, "rows 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.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.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h);
if(cell.right) { if(cell.right) cell.x += cell_width - cell.w;
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;
if(cell.bottom) { cell.y = std::midpoint(cell.y, cell.y + cell_height) - cell.h / 2;
cell.y += cell_height - cell.h;
} }
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name)); dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));

@ -16,6 +16,7 @@ namespace lel {
bool right = false; bool right = false;
bool bottom = false; bool bottom = false;
bool expand = false; bool expand = false;
bool center = false;
Cell(int col, int row) : col(col), row(row) {} Cell(int col, int row) : col(col), row(row) {}
}; };
@ -41,6 +42,7 @@ namespace lel {
void setheight(int height); void setheight(int height);
void expand(); void expand();
void reset(); void reset();
void center();
bool parse(std::string input); bool parse(std::string input);
void finalize(); void finalize();
}; };

@ -7,7 +7,7 @@
namespace lel { namespace lel {
#line 36 "lel_parser.rl" #line 38 "lel_parser.rl"
@ -15,33 +15,34 @@ namespace lel {
static const char _Parser_actions[] = { static const char _Parser_actions[] = {
0, 1, 1, 1, 2, 1, 3, 1, 0, 1, 1, 1, 2, 1, 3, 1,
4, 1, 5, 1, 6, 1, 9, 1, 4, 1, 5, 1, 6, 1, 9, 1,
10, 1, 11, 2, 0, 7, 2, 0, 10, 1, 11, 1, 12, 2, 0, 7,
8, 2, 4, 1, 2, 4, 5 2, 0, 8, 2, 4, 1, 2, 4,
5
}; };
static const char _Parser_key_offsets[] = { static const char _Parser_key_offsets[] = {
0, 0, 4, 18, 20, 24, 35, 47, 0, 0, 4, 19, 21, 25, 37, 49,
52, 54, 57 54, 56, 59
}; };
static const char _Parser_trans_keys[] = { static const char _Parser_trans_keys[] = {
32, 91, 9, 13, 32, 40, 42, 46, 32, 91, 9, 13, 32, 40, 42, 46,
60, 62, 94, 95, 9, 13, 65, 90, 61, 94, 95, 9, 13, 60, 62, 65,
97, 122, 48, 57, 41, 44, 48, 57, 90, 97, 122, 48, 57, 41, 44, 48,
40, 42, 46, 60, 62, 94, 95, 65, 57, 40, 42, 46, 61, 94, 95, 60,
90, 97, 122, 32, 93, 95, 124, 9, 62, 65, 90, 97, 122, 32, 93, 95,
13, 48, 57, 65, 90, 97, 122, 32, 124, 9, 13, 48, 57, 65, 90, 97,
93, 124, 9, 13, 48, 57, 41, 48, 122, 32, 93, 124, 9, 13, 48, 57,
57, 32, 91, 9, 13, 0 41, 48, 57, 32, 91, 9, 13, 0
}; };
static const char _Parser_single_lengths[] = { 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 0, 1, 2
}; };
static const char _Parser_range_lengths[] = { 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 1, 1, 1
}; };
@ -52,24 +53,26 @@ static const char _Parser_index_offsets[] = {
static const char _Parser_indicies[] = { static const char _Parser_indicies[] = {
0, 2, 0, 1, 3, 4, 5, 6, 0, 2, 0, 1, 3, 4, 5, 6,
7, 7, 6, 8, 3, 8, 8, 1, 8, 6, 9, 3, 7, 9, 9, 1,
9, 1, 10, 11, 12, 1, 4, 5, 10, 1, 11, 12, 13, 1, 4, 5,
6, 7, 7, 6, 8, 8, 8, 1, 6, 8, 6, 9, 7, 9, 9, 1,
13, 15, 14, 16, 13, 14, 14, 14, 14, 16, 15, 17, 14, 15, 15, 15,
1, 17, 18, 19, 17, 1, 20, 1, 1, 18, 19, 20, 18, 1, 21, 1,
21, 22, 1, 23, 2, 23, 1, 0 22, 23, 1, 24, 2, 24, 1, 0
}; };
static const char _Parser_trans_targs[] = { static const char _Parser_trans_targs[] = {
1, 0, 2, 2, 3, 5, 5, 5, 1, 0, 2, 2, 3, 5, 5, 5,
6, 4, 5, 8, 4, 7, 6, 10, 5, 6, 4, 5, 8, 4, 7, 6,
2, 7, 10, 2, 9, 5, 9, 10 10, 2, 7, 10, 2, 9, 5, 9,
10
}; };
static const char _Parser_trans_actions[] = { static const char _Parser_trans_actions[] = {
0, 0, 3, 0, 0, 13, 5, 11, 0, 0, 3, 0, 0, 13, 5, 11,
17, 15, 19, 19, 0, 7, 0, 28, 15, 19, 17, 21, 21, 0, 7, 0,
25, 0, 9, 1, 15, 22, 0, 0 30, 27, 0, 9, 1, 17, 24, 0,
0
}; };
static const int Parser_start = 1; static const int Parser_start = 1;
@ -79,7 +82,7 @@ static const int Parser_error = 0;
static const int Parser_en_main = 1; static const int Parser_en_main = 1;
#line 39 "lel_parser.rl" #line 41 "lel_parser.rl"
bool Parser::parse(std::string input) { bool Parser::parse(std::string input) {
reset(); reset();
@ -91,14 +94,14 @@ bool Parser::parse(std::string input) {
std::string tk; std::string tk;
#line 86 "lel_parser.cpp" #line 89 "lel_parser.cpp"
{ {
cs = Parser_start; 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; int _klen;
unsigned int _trans; unsigned int _trans;
@ -213,14 +216,18 @@ _match:
{ expand(); } { expand(); }
break; break;
case 10: case 10:
#line 29 "lel_parser.rl" #line 22 "lel_parser.rl"
{ start = p; } { center(); }
break; break;
case 11: case 11:
#line 32 "lel_parser.rl" #line 31 "lel_parser.rl"
{ start = p; }
break;
case 12:
#line 34 "lel_parser.rl"
{start = p;} {start = p;}
break; break;
#line 198 "lel_parser.cpp" #line 204 "lel_parser.cpp"
} }
} }
@ -233,7 +240,7 @@ _again:
_out: {} _out: {}
} }
#line 51 "lel_parser.rl" #line 53 "lel_parser.rl"
bool good = pe - p == 0; bool good = pe - p == 0;
if(good) { if(good) {

@ -19,16 +19,18 @@ namespace lel {
action setwidth { setwidth(std::stoi(tk)); } action setwidth { setwidth(std::stoi(tk)); }
action setheight { setheight(std::stoi(tk)); } action setheight { setheight(std::stoi(tk)); }
action expand { expand(); } action expand { expand(); }
action center { center(); }
col = "|" $col; col = "|" $col;
ltab = "[" $ltab; ltab = "[" $ltab;
rtab = "]" $row; rtab = "]" $row;
valign = ("^" | ".") $valign; valign = ("^" | ".") $valign;
expand = "*" $expand; expand = "*" $expand;
center = "=" $center;
halign = ("<" | ">") $align; halign = ("<" | ">") $align;
number = digit+ >{ start = fpc; } %token; number = digit+ >{ start = fpc; } %token;
setw = ("(" number %setwidth ("," number %setheight)? ")") ; setw = ("(" number %setwidth ("," number %setheight)? ")") ;
modifiers = (expand | valign | halign | setw); modifiers = (center | expand | valign | halign | setw);
id = modifiers* ((alpha | '_')+ :>> (alnum | '_')*) >{start = fpc;} %id; id = modifiers* ((alpha | '_')+ :>> (alnum | '_')*) >{start = fpc;} %id;
row = space* ltab space* id space* (col space* id space*)* space* rtab space*; row = space* ltab space* id space* (col space* id space*)* space* rtab space*;

@ -14,7 +14,7 @@ TEST_CASE("test basic ops", "[lel]") {
"[ label_1 | label3 | test1]" "[ label_1 | label3 | test1]"
"[ *(300,300)text1 | (150)people | ^test2]" "[ *(300,300)text1 | (150)people | ^test2]"
"[ >label2 | _ | .test3]" "[ >label2 | _ | .test3]"
"[ message | buttons | test4]"); "[ =message | buttons | test4]");
REQUIRE(good); REQUIRE(good);
@ -27,6 +27,7 @@ TEST_CASE("test basic ops", "[lel]") {
REQUIRE(parser.cells.at("text1").h == 300); REQUIRE(parser.cells.at("text1").h == 300);
REQUIRE(parser.cells.at("people").expand == false); REQUIRE(parser.cells.at("people").expand == false);
REQUIRE(parser.cells.at("message").expand == false); REQUIRE(parser.cells.at("message").expand == false);
REQUIRE(parser.cells.at("message").center == true);
for(auto& [name, cell] : parser.cells) { for(auto& [name, cell] : parser.cells) {
REQUIRE(cell.w > 0); REQUIRE(cell.w > 0);

Loading…
Cancel
Save