Can now use lel::center to center something inside a cell.

Zed A. Shaw 3 weeks ago
parent c03a384372
commit aa149b6574
  1. 13
      combat_ui.cpp
  2. 2
      combat_ui.hpp
  3. 18
      lel.cpp
  4. 4
      lel.hpp

@ -15,18 +15,27 @@ namespace gui {
dbc::check(good, "failed to parse combat layout");
for(auto& [name, cell] : $layout.cells) {
(void)name;
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({100, 100, 100});
button.setOutlineColor({200, 200, 200});
button.setOutlineThickness(5);
$shapes[name] = button;
$shapes.push_back(button);
sf::RectangleShape inner;
auto inner_cell = lel::center(30, 40, cell);
inner.setPosition({float(inner_cell.x), float(inner_cell.y)});
inner.setSize({float(inner_cell.w), float(inner_cell.h)});
inner.setOutlineColor({100, 100, 100});
inner.setOutlineThickness(5);
$shapes.push_back(inner);
}
}
void CombatUI::draw(sf::RenderWindow& window) {
for(auto& [name, shape] : $shapes) {
for(auto& shape : $shapes) {
window.draw(shape);
}
}

@ -10,7 +10,7 @@ namespace gui {
public:
lel::Parser $layout;
GameLevel $level;
std::unordered_map<std::string, sf::RectangleShape> $shapes;
std::vector<sf::RectangleShape> $shapes;
CombatUI(GameLevel level);
void draw(sf::RenderWindow& window);

@ -26,6 +26,7 @@ namespace lel {
cur = {cur.col + 1, cur.row};
}
void Parser::finalize() {
dbc::check(columns > 0, "columns are 0");
dbc::check(rows > 0, "rows are 0");
@ -47,12 +48,14 @@ 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);
cell.mid_x = std::midpoint(cell.x, cell.x + cell_width);
cell.mid_y = std::midpoint(cell.y, cell.y + cell_height);
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;
cell.x = cell.mid_x - cell.w / 2;
cell.y = cell.mid_y - cell.h / 2;
}
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
@ -65,4 +68,15 @@ namespace lel {
columns = 0;
cur = {0, 0};
}
Cell center(int width, int height, Cell &parent) {
Cell copy = parent;
copy.x = parent.mid_x - width / 2;
copy.y = parent.mid_y - height / 2;
copy.w = width;
copy.h = height;
return copy;
}
}

@ -9,6 +9,8 @@ namespace lel {
int y = 0;
int w = 0;
int h = 0;
int mid_x = 0;
int mid_y = 0;
int max_w = 0;
int max_h = 0;
int col = 0;
@ -38,4 +40,6 @@ namespace lel {
bool parse(std::string input);
void finalize();
};
Cell center(int width, int height, Cell &parent);
}

Loading…
Cancel
Save