New design on LEL that's way more sane and allows for more flexible columns and rows.
This commit is contained in:
parent
f884b83809
commit
79c84ce885
8 changed files with 83 additions and 78 deletions
2
Makefile
2
Makefile
|
@ -22,7 +22,7 @@ tracy_build:
|
||||||
meson compile -j 10 -C builddir
|
meson compile -j 10 -C builddir
|
||||||
|
|
||||||
test: build
|
test: build
|
||||||
./builddir/runtests
|
./builddir/runtests "[lel]"
|
||||||
|
|
||||||
run: build test
|
run: build test
|
||||||
powershell "cp ./builddir/zedcaster.exe ."
|
powershell "cp ./builddir/zedcaster.exe ."
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace gui {
|
||||||
class CombatUI {
|
class CombatUI {
|
||||||
public:
|
public:
|
||||||
std::string $grid =
|
std::string $grid =
|
||||||
"[*%(200,90)hp | _ | *%(200,90)ap | _ ]"
|
"[hp | ap ]"
|
||||||
"[attack1 | attack2 | attack3 | heal]";
|
"[attack1 | attack2 | attack3 | heal]";
|
||||||
lel::Parser $layout;
|
lel::Parser $layout;
|
||||||
GameLevel $level;
|
GameLevel $level;
|
||||||
|
|
2
gui.cpp
2
gui.cpp
|
@ -354,7 +354,7 @@ namespace gui {
|
||||||
|
|
||||||
void FSM::mouse() {
|
void FSM::mouse() {
|
||||||
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
|
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);
|
$combat_view.click(pos.x, pos.y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
83
lel.cpp
83
lel.cpp
|
@ -17,64 +17,69 @@ namespace lel {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parser::id(std::string name) {
|
void Parser::id(std::string name) {
|
||||||
if(name != "_") {
|
dbc::check(!cells.contains(name),
|
||||||
dbc::check(!cells.contains(name),
|
fmt::format("duplicate cell name {}", 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};
|
cur = {cur.col + 1, cur.row};
|
||||||
|
|
||||||
|
auto& row = grid.back();
|
||||||
|
row.push_back(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Parser::finalize() {
|
void Parser::finalize() {
|
||||||
dbc::check(columns > 0, "columns are 0");
|
size_t rows = grid.size();
|
||||||
dbc::check(rows > 0, "rows are 0");
|
|
||||||
int cell_width = grid_w / columns;
|
|
||||||
int cell_height = grid_h / rows;
|
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& row : grid) {
|
||||||
|
for(auto& name : row) {
|
||||||
|
size_t columns = row.size();
|
||||||
|
auto& cell = cells.at(name);
|
||||||
|
|
||||||
for(auto& [name, cell] : cells) {
|
int cell_width = grid_w / columns;
|
||||||
cell.x = grid_x + (cell.col * cell_width);
|
dbc::check(cell_width > 0, "invalid cell width calc");
|
||||||
cell.y = grid_y + (cell.row * cell_height);
|
dbc::check(cell_height > 0, "invalid cell height calc");
|
||||||
|
|
||||||
// ZED: getting a bit hairy but this should work
|
cell.x = grid_x + (cell.col * cell_width);
|
||||||
if(cell.percent) {
|
cell.y = grid_y + (cell.row * cell_height);
|
||||||
// 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);
|
// ZED: getting a bit hairy but this should work
|
||||||
cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h);
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
|
cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w);
|
||||||
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
|
cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h);
|
||||||
|
|
||||||
// keep the midpoint since it is used a lot
|
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
|
||||||
cell.mid_x = std::midpoint(cell.x, cell.x + cell.w);
|
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
|
||||||
cell.mid_y = std::midpoint(cell.y, cell.y + cell.h);
|
|
||||||
|
|
||||||
// perform alignments
|
// keep the midpoint since it is used a lot
|
||||||
if(cell.right) cell.x += cell_width - cell.w;
|
cell.mid_x = std::midpoint(cell.x, cell.x + cell.w);
|
||||||
if(cell.bottom) cell.y += cell_height - cell.h;
|
cell.mid_y = std::midpoint(cell.y, cell.y + cell.h);
|
||||||
if(cell.center) {
|
|
||||||
cell.x = cell.mid_x - cell.w / 2;
|
// perform alignments
|
||||||
cell.y = cell.mid_y - cell.h / 2;
|
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() {
|
void Parser::reset() {
|
||||||
rows = 0;
|
|
||||||
columns = 0;
|
|
||||||
cur = {0, 0};
|
cur = {0, 0};
|
||||||
|
grid.clear();
|
||||||
|
cells.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<std::string> Parser::hit(int x, int y) {
|
std::optional<std::string> Parser::hit(int x, int y) {
|
||||||
|
|
5
lel.hpp
5
lel.hpp
|
@ -5,6 +5,8 @@
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
namespace lel {
|
namespace lel {
|
||||||
|
using Row = std::vector<std::string>;
|
||||||
|
|
||||||
struct Cell {
|
struct Cell {
|
||||||
int x = 0;
|
int x = 0;
|
||||||
int y = 0;
|
int y = 0;
|
||||||
|
@ -30,9 +32,8 @@ namespace lel {
|
||||||
int grid_y = 0;
|
int grid_y = 0;
|
||||||
int grid_w = 0;
|
int grid_w = 0;
|
||||||
int grid_h = 0;
|
int grid_h = 0;
|
||||||
int rows = 0;
|
|
||||||
int columns = 0;
|
|
||||||
Cell cur;
|
Cell cur;
|
||||||
|
std::vector<Row> grid;
|
||||||
std::unordered_map<std::string, Cell> cells;
|
std::unordered_map<std::string, Cell> cells;
|
||||||
|
|
||||||
Parser(int x, int y, int width, int height);
|
Parser(int x, int y, int width, int height);
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
namespace lel {
|
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;
|
static const int Parser_en_main = 1;
|
||||||
|
|
||||||
|
|
||||||
#line 47 "lel_parser.rl"
|
#line 43 "lel_parser.rl"
|
||||||
|
|
||||||
bool Parser::parse(std::string input) {
|
bool Parser::parse(std::string input) {
|
||||||
reset();
|
reset();
|
||||||
|
@ -101,7 +101,7 @@ bool Parser::parse(std::string input) {
|
||||||
cs = Parser_start;
|
cs = Parser_start;
|
||||||
}
|
}
|
||||||
|
|
||||||
#line 58 "lel_parser.rl"
|
#line 54 "lel_parser.rl"
|
||||||
|
|
||||||
#line 94 "lel_parser.cpp"
|
#line 94 "lel_parser.cpp"
|
||||||
{
|
{
|
||||||
|
@ -183,11 +183,11 @@ _match:
|
||||||
break;
|
break;
|
||||||
case 1:
|
case 1:
|
||||||
#line 13 "lel_parser.rl"
|
#line 13 "lel_parser.rl"
|
||||||
{ }
|
{}
|
||||||
break;
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
#line 14 "lel_parser.rl"
|
#line 14 "lel_parser.rl"
|
||||||
{ cur.row = rows; }
|
{ grid.push_back(Row()); }
|
||||||
break;
|
break;
|
||||||
case 3:
|
case 3:
|
||||||
#line 15 "lel_parser.rl"
|
#line 15 "lel_parser.rl"
|
||||||
|
@ -199,45 +199,41 @@ _match:
|
||||||
break;
|
break;
|
||||||
case 5:
|
case 5:
|
||||||
#line 17 "lel_parser.rl"
|
#line 17 "lel_parser.rl"
|
||||||
{
|
{ cur.col = 0; cur.row++; }
|
||||||
rows++;
|
|
||||||
columns = std::max(columns, cur.col);
|
|
||||||
cur.col = 0;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case 6:
|
case 6:
|
||||||
#line 22 "lel_parser.rl"
|
#line 18 "lel_parser.rl"
|
||||||
{ cur.right = (*p) == '>'; }
|
{ cur.right = (*p) == '>'; }
|
||||||
break;
|
break;
|
||||||
case 7:
|
case 7:
|
||||||
#line 23 "lel_parser.rl"
|
#line 19 "lel_parser.rl"
|
||||||
{cur.max_w = std::stoi(tk); }
|
{cur.max_w = std::stoi(tk); }
|
||||||
break;
|
break;
|
||||||
case 8:
|
case 8:
|
||||||
#line 24 "lel_parser.rl"
|
#line 20 "lel_parser.rl"
|
||||||
{ cur.max_h = std::stoi(tk); }
|
{ cur.max_h = std::stoi(tk); }
|
||||||
break;
|
break;
|
||||||
case 9:
|
case 9:
|
||||||
#line 25 "lel_parser.rl"
|
#line 21 "lel_parser.rl"
|
||||||
{ cur.expand = true; }
|
{ cur.expand = true; }
|
||||||
break;
|
break;
|
||||||
case 10:
|
case 10:
|
||||||
#line 26 "lel_parser.rl"
|
#line 22 "lel_parser.rl"
|
||||||
{ cur.center = true; }
|
{ cur.center = true; }
|
||||||
break;
|
break;
|
||||||
case 11:
|
case 11:
|
||||||
#line 27 "lel_parser.rl"
|
#line 23 "lel_parser.rl"
|
||||||
{ cur.percent = true; }
|
{ cur.percent = true; }
|
||||||
break;
|
break;
|
||||||
case 12:
|
case 12:
|
||||||
#line 37 "lel_parser.rl"
|
#line 33 "lel_parser.rl"
|
||||||
{ start = p; }
|
{ start = p; }
|
||||||
break;
|
break;
|
||||||
case 13:
|
case 13:
|
||||||
#line 40 "lel_parser.rl"
|
#line 36 "lel_parser.rl"
|
||||||
{start = p;}
|
{start = p;}
|
||||||
break;
|
break;
|
||||||
#line 213 "lel_parser.cpp"
|
#line 209 "lel_parser.cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,7 +246,7 @@ _again:
|
||||||
_out: {}
|
_out: {}
|
||||||
}
|
}
|
||||||
|
|
||||||
#line 59 "lel_parser.rl"
|
#line 55 "lel_parser.rl"
|
||||||
|
|
||||||
bool good = pe - p == 0;
|
bool good = pe - p == 0;
|
||||||
if(good) {
|
if(good) {
|
||||||
|
|
|
@ -10,15 +10,11 @@ namespace lel {
|
||||||
|
|
||||||
action token {tk = input.substr(start - begin, fpc - start); }
|
action token {tk = input.substr(start - begin, fpc - start); }
|
||||||
|
|
||||||
action col { }
|
action col {}
|
||||||
action ltab { cur.row = rows; }
|
action ltab { grid.push_back(Row()); }
|
||||||
action valign { cur.bottom = fc == '.'; }
|
action valign { cur.bottom = fc == '.'; }
|
||||||
action id { id(input.substr(start - begin, fpc - start)); }
|
action id { id(input.substr(start - begin, fpc - start)); }
|
||||||
action row {
|
action row { cur.col = 0; cur.row++; }
|
||||||
rows++;
|
|
||||||
columns = std::max(columns, cur.col);
|
|
||||||
cur.col = 0;
|
|
||||||
}
|
|
||||||
action align { cur.right = fc == '>'; }
|
action align { cur.right = fc == '>'; }
|
||||||
action setwidth {cur.max_w = std::stoi(tk); }
|
action setwidth {cur.max_w = std::stoi(tk); }
|
||||||
action setheight { cur.max_h = std::stoi(tk); }
|
action setheight { cur.max_h = std::stoi(tk); }
|
||||||
|
|
|
@ -18,9 +18,18 @@ TEST_CASE("test basic ops", "[lel]") {
|
||||||
|
|
||||||
REQUIRE(good);
|
REQUIRE(good);
|
||||||
|
|
||||||
REQUIRE(parser.rows == 4);
|
for(size_t rowcount = 0; rowcount < parser.grid.size(); rowcount++) {
|
||||||
REQUIRE(parser.columns == 3);
|
auto& row = parser.grid[rowcount];
|
||||||
REQUIRE(parser.cells.size() == 11);
|
|
||||||
|
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("label2").right == true);
|
||||||
REQUIRE(parser.cells.at("text1").expand == true);
|
REQUIRE(parser.cells.at("text1").expand == true);
|
||||||
REQUIRE(parser.cells.at("text1").w == 300);
|
REQUIRE(parser.cells.at("text1").w == 300);
|
||||||
|
@ -32,12 +41,10 @@ TEST_CASE("test basic ops", "[lel]") {
|
||||||
for(auto& [name, cell] : parser.cells) {
|
for(auto& [name, cell] : parser.cells) {
|
||||||
REQUIRE(cell.w > 0);
|
REQUIRE(cell.w > 0);
|
||||||
REQUIRE(cell.h > 0);
|
REQUIRE(cell.h > 0);
|
||||||
REQUIRE(cell.row < parser.rows);
|
|
||||||
REQUIRE(cell.col < parser.columns);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
auto hit = parser.hit(250, 250);
|
auto hit = parser.hit(10, 10);
|
||||||
REQUIRE(*hit == "people");
|
REQUIRE(*hit == "label_1");
|
||||||
|
|
||||||
auto nohit = parser.hit(1000, 1000);
|
auto nohit = parser.hit(1000, 1000);
|
||||||
REQUIRE(!nohit);
|
REQUIRE(!nohit);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue