Combat UI's elements are now pulled out into gui_gadgets so we have an initial prototype on basic UI elements needed.
This commit is contained in:
parent
7c1f05c801
commit
8d9c2d8c05
5 changed files with 99 additions and 43 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 "[lel]"
|
./builddir/runtests
|
||||||
|
|
||||||
run: build test
|
run: build test
|
||||||
powershell "cp ./builddir/zedcaster.exe ."
|
powershell "cp ./builddir/zedcaster.exe ."
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
"player": {
|
"player": {
|
||||||
},
|
},
|
||||||
"worldgen": {
|
"worldgen": {
|
||||||
"enemy_probability": 20,
|
"enemy_probability": 40,
|
||||||
"empty_room_probability": 10,
|
"empty_room_probability": 10,
|
||||||
"device_probability": 20
|
"device_probability": 20
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,65 +20,45 @@ namespace gui {
|
||||||
$background.setFillColor({0, 0, 0});
|
$background.setFillColor({0, 0, 0});
|
||||||
|
|
||||||
for(auto& [name, cell] : $layout.cells) {
|
for(auto& [name, cell] : $layout.cells) {
|
||||||
sf::RectangleShape shape;
|
if(name == "bar_hp") {
|
||||||
shape.setPosition({float(cell.x + 3), float(cell.y + 3)});
|
$meters.try_emplace(name, cell);
|
||||||
shape.setSize({float(cell.w - 6), float(cell.h - 6)});
|
|
||||||
shape.setFillColor(ColorValue::DARK_MID);
|
|
||||||
shape.setOutlineColor(ColorValue::MID);
|
|
||||||
shape.setOutlineThickness(1);
|
|
||||||
|
|
||||||
if(name.starts_with("button_")) {
|
|
||||||
sf::Text label($font, name);
|
|
||||||
auto bounds = label.getLocalBounds();
|
|
||||||
auto label_cell = lel::center(bounds.size.x, bounds.size.y, cell);
|
|
||||||
// this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box
|
|
||||||
label.setPosition({float(label_cell.x), float(label_cell.y) - label_cell.h / 2});
|
|
||||||
$labels.push_back(label);
|
|
||||||
} else if(name == "bar_hp") {
|
|
||||||
shape.setFillColor({150, 30, 30});
|
|
||||||
} else if(name == "label_hp") {
|
} else if(name == "label_hp") {
|
||||||
sf::Text label($font, "hp:");
|
gui::Label label(cell, "hp:", $font);
|
||||||
label.scale({0.8, 0.8});
|
$labels.emplace_back(cell, "hp:", $font);
|
||||||
auto bounds = label.getGlobalBounds();
|
} else if(name.starts_with("button_")) {
|
||||||
auto label_cell = lel::center(bounds.size.x, bounds.size.y, cell);
|
$buttons.try_emplace(name, cell, name, $font);
|
||||||
// this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box
|
|
||||||
label.setPosition({float(label_cell.x), float(label_cell.y + bounds.size.y)});
|
|
||||||
$labels.push_back(label);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$shapes.insert_or_assign(name, shape);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CombatUI::draw(sf::RenderWindow& window) {
|
void CombatUI::draw(sf::RenderWindow& window) {
|
||||||
|
window.draw($background);
|
||||||
auto &player = $level.world->get_the<components::Player>();
|
auto &player = $level.world->get_the<components::Player>();
|
||||||
auto &combat = $level.world->get<components::Combat>(player.entity);
|
auto &combat = $level.world->get<components::Combat>(player.entity);
|
||||||
|
|
||||||
auto &hp_shape = $shapes.at("bar_hp");
|
float hp_now = float(combat.hp) / float(combat.max_hp);
|
||||||
auto &hp_cell = $layout.cells.at("bar_hp");
|
auto& bar_hp = $meters.at("bar_hp");
|
||||||
float hp_now = float(combat.hp) / float(combat.max_hp) * float(hp_cell.w);
|
bar_hp.set_percent(hp_now);
|
||||||
hp_shape.setSize({std::max(hp_now, 0.0f), float(hp_cell.h - 6)});
|
|
||||||
|
|
||||||
window.draw($background);
|
for(auto& [name, button] : $buttons) {
|
||||||
|
button.draw(window);
|
||||||
for(auto& [name, shape] : $shapes) {
|
|
||||||
window.draw(shape);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& [name, shape] : $label_boxes) {
|
for(auto& [name, meter] : $meters) {
|
||||||
window.draw(shape);
|
meter.draw(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(auto& label : $labels) {
|
for(auto& label : $labels) {
|
||||||
window.draw(label);
|
label.draw(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CombatUI::click(int x, int y) {
|
void CombatUI::click(int x, int y) {
|
||||||
if(auto name = $layout.hit(x, y)) {
|
if(auto name = $layout.hit(x, y)) {
|
||||||
if((*name).starts_with("button_")) {
|
if((*name).starts_with("button_")) {
|
||||||
auto& shape = $shapes.at(*name);
|
auto& button = $buttons.at(*name);
|
||||||
shape.setFillColor({100, 0, 0});
|
button.shape.setFillColor({100, 0, 0});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@
|
||||||
#include <SFML/Graphics/Font.hpp>
|
#include <SFML/Graphics/Font.hpp>
|
||||||
#include <SFML/Graphics/Text.hpp>
|
#include <SFML/Graphics/Text.hpp>
|
||||||
#include "lel.hpp"
|
#include "lel.hpp"
|
||||||
|
#include "gui_gadgets.hpp"
|
||||||
|
|
||||||
namespace gui {
|
namespace gui {
|
||||||
class CombatUI {
|
class CombatUI {
|
||||||
|
@ -17,9 +18,9 @@ namespace gui {
|
||||||
GameLevel $level;
|
GameLevel $level;
|
||||||
sf::Font $font;
|
sf::Font $font;
|
||||||
sf::RectangleShape $background;
|
sf::RectangleShape $background;
|
||||||
std::unordered_map<std::string, sf::RectangleShape> $shapes;
|
std::unordered_map<std::string, gui::Button> $buttons;
|
||||||
std::unordered_map<std::string, sf::RectangleShape> $label_boxes;
|
std::vector<gui::Label> $labels;
|
||||||
std::vector<sf::Text> $labels;
|
std::unordered_map<std::string, gui::Meter> $meters;
|
||||||
CombatUI(GameLevel level);
|
CombatUI(GameLevel level);
|
||||||
|
|
||||||
void render();
|
void render();
|
||||||
|
|
75
gui_gadgets.hpp
Normal file
75
gui_gadgets.hpp
Normal file
|
@ -0,0 +1,75 @@
|
||||||
|
#pragma once
|
||||||
|
#include <SFML/Graphics/RectangleShape.hpp>
|
||||||
|
#include <SFML/Graphics/Font.hpp>
|
||||||
|
#include <SFML/Graphics/Text.hpp>
|
||||||
|
#include "lel.hpp"
|
||||||
|
|
||||||
|
namespace gui {
|
||||||
|
struct Label {
|
||||||
|
sf::Font &font;
|
||||||
|
sf::Text text;
|
||||||
|
lel::Cell& cell;
|
||||||
|
|
||||||
|
Label(lel::Cell& cell, std::string label_text, sf::Font &font) :
|
||||||
|
font(font),
|
||||||
|
text(font, label_text),
|
||||||
|
cell(cell)
|
||||||
|
{
|
||||||
|
auto bounds = text.getLocalBounds();
|
||||||
|
auto text_cell = lel::center(bounds.size.x, bounds.size.y, cell);
|
||||||
|
// this stupid / 2 is because SFML renders from baseline rather than from the claimed bounding box
|
||||||
|
text.setPosition({float(text_cell.x), float(text_cell.y) - text_cell.h / 2});
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(sf::RenderWindow& window) {
|
||||||
|
window.draw(text);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Button {
|
||||||
|
Label label;
|
||||||
|
lel::Cell& cell;
|
||||||
|
sf::RectangleShape shape;
|
||||||
|
|
||||||
|
Button(lel::Cell& cell, std::string label_text, sf::Font &font) :
|
||||||
|
label(cell, label_text, font),
|
||||||
|
cell(cell)
|
||||||
|
{
|
||||||
|
shape.setPosition({float(cell.x + 3), float(cell.y + 3)});
|
||||||
|
shape.setSize({float(cell.w - 6), float(cell.h - 6)});
|
||||||
|
shape.setFillColor(ColorValue::DARK_MID);
|
||||||
|
shape.setOutlineColor(ColorValue::MID);
|
||||||
|
shape.setOutlineThickness(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(sf::RenderWindow& window) {
|
||||||
|
window.draw(shape);
|
||||||
|
label.draw(window);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Meter {
|
||||||
|
sf::RectangleShape shape;
|
||||||
|
lel::Cell& cell;
|
||||||
|
|
||||||
|
Meter(lel::Cell& cell) :
|
||||||
|
cell(cell)
|
||||||
|
{
|
||||||
|
shape.setPosition({float(cell.x + 3), float(cell.y + 3)});
|
||||||
|
shape.setSize({float(cell.w - 6), float(cell.h - 6)});
|
||||||
|
shape.setFillColor(ColorValue::DARK_MID);
|
||||||
|
shape.setOutlineColor(ColorValue::MID);
|
||||||
|
shape.setOutlineThickness(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(sf::RenderWindow& window) {
|
||||||
|
window.draw(shape);
|
||||||
|
}
|
||||||
|
|
||||||
|
void set_percent(float percent) {
|
||||||
|
float level = percent * float(cell.w);
|
||||||
|
// ZED: this 6 is a border to make it a thing
|
||||||
|
shape.setSize({std::max(level, 0.0f), float(cell.h - 6)});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue