Can click on buttons and make them change color.

Zed A. Shaw 3 weeks ago
parent 7f9e200abe
commit e106ad4be7
  1. 14
      combat_ui.cpp
  2. 3
      combat_ui.hpp
  3. 5
      gui.cpp
  4. 2
      tests/lel.cpp

@ -15,14 +15,13 @@ 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.push_back(button);
$shapes.insert_or_assign(name, button);
sf::RectangleShape inner;
auto inner_cell = lel::center(30, 40, cell);
@ -31,13 +30,20 @@ namespace gui {
inner.setOutlineColor({100, 0, 0});
inner.setOutlineThickness(5);
inner.setFillColor({50, 50, 50});
$shapes.push_back(inner);
$shapes.insert_or_assign(name + "_text", inner);
}
}
void CombatUI::draw(sf::RenderWindow& window) {
for(auto& shape : $shapes) {
for(auto& [name, shape] : $shapes) {
window.draw(shape);
}
}
void CombatUI::click(int x, int y) {
if(auto name = $layout.hit(x, y)) {
auto& shape = $shapes.at(*name);
shape.setFillColor({100, 0, 0});
}
}
}

@ -10,10 +10,11 @@ namespace gui {
public:
lel::Parser $layout;
GameLevel $level;
std::vector<sf::RectangleShape> $shapes;
std::unordered_map<std::string, sf::RectangleShape> $shapes;
CombatUI(GameLevel level);
void draw(sf::RenderWindow& window);
void update_level(GameLevel &level) { $level = level; }
void click(int x, int y);
};
}

@ -346,7 +346,10 @@ namespace gui {
}
void FSM::mouse() {
// temporarily gutted
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
sf::Vector2i pos = sf::Mouse::getPosition($window);
$combat_view.click(pos.x, pos.y);
}
}
void FSM::generate_map() {

@ -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);

Loading…
Cancel
Save