diff --git a/Makefile b/Makefile index a1533ce..097aa95 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ tracy_build: meson compile -j 10 -C builddir test: build - ./builddir/runtests + ./builddir/runtests "[lel]" run: build test powershell "cp ./builddir/zedcaster.exe ." diff --git a/combat_ui.hpp b/combat_ui.hpp index 00b9974..49a1e03 100644 --- a/combat_ui.hpp +++ b/combat_ui.hpp @@ -11,7 +11,7 @@ namespace gui { class CombatUI { public: std::string $grid = - "[*%(200,90)hp | _ | *%(200,90)ap | _ ]" + "[hp | ap ]" "[attack1 | attack2 | attack3 | heal]"; lel::Parser $layout; GameLevel $level; diff --git a/gui.cpp b/gui.cpp index 248add2..4fcdc03 100644 --- a/gui.cpp +++ b/gui.cpp @@ -354,7 +354,7 @@ namespace gui { void FSM::mouse() { if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { - sf::Vector2i pos = sf::Mouse::getPosition($window); + sf::Vector2f pos = $window.mapPixelToCoords(sf::Mouse::getPosition($window)); $combat_view.click(pos.x, pos.y); } } diff --git a/lel.cpp b/lel.cpp index 22f8028..8fca207 100644 --- a/lel.cpp +++ b/lel.cpp @@ -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& [name, cell] : cells) { - cell.x = grid_x + (cell.col * cell_width); - cell.y = grid_y + (cell.row * cell_height); - - // 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.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); - - dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name)); - dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name)); - - // 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; + for(auto& row : grid) { + for(auto& name : row) { + size_t columns = row.size(); + auto& cell = cells.at(name); + + int cell_width = grid_w / columns; + dbc::check(cell_width > 0, "invalid cell width calc"); + dbc::check(cell_height > 0, "invalid cell height calc"); + + cell.x = grid_x + (cell.col * cell_width); + cell.y = grid_y + (cell.row * cell_height); + + // 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.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); + + dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name)); + dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name)); + + // 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 Parser::hit(int x, int y) { diff --git a/lel.hpp b/lel.hpp index 63f73ec..5401473 100644 --- a/lel.hpp +++ b/lel.hpp @@ -5,6 +5,8 @@ #include namespace lel { + using Row = std::vector; + struct Cell { int x = 0; int y = 0; @@ -30,9 +32,8 @@ namespace lel { int grid_y = 0; int grid_w = 0; int grid_h = 0; - int rows = 0; - int columns = 0; Cell cur; + std::vector grid; std::unordered_map cells; Parser(int x, int y, int width, int height); diff --git a/lel_parser.cpp b/lel_parser.cpp index 31c03d4..0d0efac 100644 --- a/lel_parser.cpp +++ b/lel_parser.cpp @@ -7,7 +7,7 @@ namespace lel { -#line 44 "lel_parser.rl" +#line 40 "lel_parser.rl" @@ -84,7 +84,7 @@ static const int Parser_error = 0; static const int Parser_en_main = 1; -#line 47 "lel_parser.rl" +#line 43 "lel_parser.rl" bool Parser::parse(std::string input) { reset(); @@ -101,7 +101,7 @@ bool Parser::parse(std::string input) { cs = Parser_start; } -#line 58 "lel_parser.rl" +#line 54 "lel_parser.rl" #line 94 "lel_parser.cpp" { @@ -183,11 +183,11 @@ _match: break; case 1: #line 13 "lel_parser.rl" - { } + {} break; case 2: #line 14 "lel_parser.rl" - { cur.row = rows; } + { grid.push_back(Row()); } break; case 3: #line 15 "lel_parser.rl" @@ -199,45 +199,41 @@ _match: break; case 5: #line 17 "lel_parser.rl" - { - rows++; - columns = std::max(columns, cur.col); - cur.col = 0; - } + { cur.col = 0; cur.row++; } break; case 6: -#line 22 "lel_parser.rl" +#line 18 "lel_parser.rl" { cur.right = (*p) == '>'; } break; case 7: -#line 23 "lel_parser.rl" +#line 19 "lel_parser.rl" {cur.max_w = std::stoi(tk); } break; case 8: -#line 24 "lel_parser.rl" +#line 20 "lel_parser.rl" { cur.max_h = std::stoi(tk); } break; case 9: -#line 25 "lel_parser.rl" +#line 21 "lel_parser.rl" { cur.expand = true; } break; case 10: -#line 26 "lel_parser.rl" +#line 22 "lel_parser.rl" { cur.center = true; } break; case 11: -#line 27 "lel_parser.rl" +#line 23 "lel_parser.rl" { cur.percent = true; } break; case 12: -#line 37 "lel_parser.rl" +#line 33 "lel_parser.rl" { start = p; } break; case 13: -#line 40 "lel_parser.rl" +#line 36 "lel_parser.rl" {start = p;} break; -#line 213 "lel_parser.cpp" +#line 209 "lel_parser.cpp" } } @@ -250,7 +246,7 @@ _again: _out: {} } -#line 59 "lel_parser.rl" +#line 55 "lel_parser.rl" bool good = pe - p == 0; if(good) { diff --git a/lel_parser.rl b/lel_parser.rl index 9924ee6..a41c5ea 100644 --- a/lel_parser.rl +++ b/lel_parser.rl @@ -10,15 +10,11 @@ namespace lel { action token {tk = input.substr(start - begin, fpc - start); } - action col { } - action ltab { cur.row = rows; } + action col {} + action ltab { grid.push_back(Row()); } action valign { cur.bottom = fc == '.'; } action id { id(input.substr(start - begin, fpc - start)); } - action row { - rows++; - columns = std::max(columns, cur.col); - cur.col = 0; - } + action row { cur.col = 0; cur.row++; } action align { cur.right = fc == '>'; } action setwidth {cur.max_w = std::stoi(tk); } action setheight { cur.max_h = std::stoi(tk); } diff --git a/tests/lel.cpp b/tests/lel.cpp index 625cc53..2254d13 100644 --- a/tests/lel.cpp +++ b/tests/lel.cpp @@ -18,9 +18,18 @@ TEST_CASE("test basic ops", "[lel]") { REQUIRE(good); - REQUIRE(parser.rows == 4); - REQUIRE(parser.columns == 3); - REQUIRE(parser.cells.size() == 11); + for(size_t rowcount = 0; rowcount < parser.grid.size(); rowcount++) { + auto& row = parser.grid[rowcount]; + + for(size_t colcount = 0; colcount < row.size(); colcount++) { + auto &name = row[colcount]; + auto &cell = parser.cells.at(name); + REQUIRE(cell.row == int(rowcount)); + REQUIRE(cell.col == int(colcount)); + } + } + + REQUIRE(parser.cells.size() == 12); REQUIRE(parser.cells.at("label2").right == true); REQUIRE(parser.cells.at("text1").expand == true); REQUIRE(parser.cells.at("text1").w == 300); @@ -32,12 +41,10 @@ TEST_CASE("test basic ops", "[lel]") { for(auto& [name, cell] : parser.cells) { REQUIRE(cell.w > 0); REQUIRE(cell.h > 0); - REQUIRE(cell.row < parser.rows); - REQUIRE(cell.col < parser.columns); } - auto hit = parser.hit(250, 250); - REQUIRE(*hit == "people"); + auto hit = parser.hit(10, 10); + REQUIRE(*hit == "label_1"); auto nohit = parser.hit(1000, 1000); REQUIRE(!nohit);