I now have a semi-functional GUI system that uses the ECS style to build gui elements rather than inheritance.
parent
615599084a
commit
46de98e6f4
@ -0,0 +1,65 @@ |
|||||||
|
#include "ecs_gui.hpp" |
||||||
|
#include "constants.hpp" |
||||||
|
|
||||||
|
GUECS::GUECS() { |
||||||
|
$font = make_shared<sf::Font>(FONT_FILE_NAME); |
||||||
|
} |
||||||
|
|
||||||
|
void GUECS::position(int x, int y, int width, int height) { |
||||||
|
$parser.position(x, y, width, height); |
||||||
|
} |
||||||
|
|
||||||
|
void GUECS::layout(std::string grid) { |
||||||
|
$grid = grid; |
||||||
|
$parser.parse($grid); |
||||||
|
} |
||||||
|
|
||||||
|
DinkyECS::Entity GUECS::entity(std::string name) { |
||||||
|
auto entity = $world.entity(); |
||||||
|
$world.set<CellName>(entity, {name}); |
||||||
|
return entity; |
||||||
|
} |
||||||
|
|
||||||
|
void GUECS::init() { |
||||||
|
$world.query<lel::Cell, Rectangle>([](const auto &, auto& cell, auto& rect) { |
||||||
|
rect.init(cell); |
||||||
|
}); |
||||||
|
|
||||||
|
$world.query<lel::Cell, Textual>([this](const auto &, auto& cell, auto& text) { |
||||||
|
text.init(cell, $font); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
void GUECS::render(sf::RenderWindow& window) { |
||||||
|
$world.query<lel::Cell, Meter>([&](const auto &ent, const auto& cell, const auto &meter) { |
||||||
|
if($world.has<Rectangle>(ent)) { |
||||||
|
float level = meter.percent * float(cell.w); |
||||||
|
auto& target = $world.get<Rectangle>(ent); |
||||||
|
// ZED: this 6 is a border width, make it a thing
|
||||||
|
target.shape->setSize({std::max(level, 0.0f), float(cell.h - 6)}); |
||||||
|
} |
||||||
|
}); |
||||||
|
|
||||||
|
$world.query<Rectangle>([&](const auto &, const auto& rect) { |
||||||
|
window.draw(*rect.shape); |
||||||
|
}); |
||||||
|
|
||||||
|
$world.query<Textual>([&](const auto &, const auto& text) { |
||||||
|
window.draw(*text.text); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
void GUECS::mouse(sf::RenderWindow &window) { |
||||||
|
if(sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) { |
||||||
|
sf::Vector2f pos = window.mapPixelToCoords(sf::Mouse::getPosition(window)); |
||||||
|
$world.query<lel::Cell, Clickable>([&](const auto &ent, auto& cell, auto &clicked) { |
||||||
|
if((pos.x >= cell.x && pos.x <= cell.x + cell.w) && |
||||||
|
(pos.y >= cell.y && pos.y <= cell.y + cell.h)) |
||||||
|
{ |
||||||
|
auto& cn = $world.get<CellName>(ent); |
||||||
|
fmt::println("clicked on entity {} with name {} and event {}", |
||||||
|
ent, cn.name, clicked.event); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,77 @@ |
|||||||
|
#pragma once |
||||||
|
#include "color.hpp" |
||||||
|
#include "dinkyecs.hpp" |
||||||
|
#include "lel.hpp" |
||||||
|
#include <string> |
||||||
|
#include <memory> |
||||||
|
#include <SFML/Graphics.hpp> |
||||||
|
|
||||||
|
using std::shared_ptr, std::make_shared; |
||||||
|
|
||||||
|
struct Textual { |
||||||
|
std::string label; |
||||||
|
shared_ptr<sf::Font> font = nullptr; |
||||||
|
shared_ptr<sf::Text> text = nullptr; |
||||||
|
|
||||||
|
void init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) { |
||||||
|
font = font_ptr; |
||||||
|
text = make_shared<sf::Text>(*font, label); |
||||||
|
|
||||||
|
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}); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
struct Clickable { |
||||||
|
int event = 0; |
||||||
|
}; |
||||||
|
|
||||||
|
struct Rectangle { |
||||||
|
shared_ptr<sf::RectangleShape> shape = nullptr; |
||||||
|
|
||||||
|
void init(lel::Cell& cell) { |
||||||
|
sf::Vector2f size{(float)cell.w, (float)cell.h}; |
||||||
|
if(shape == nullptr) shape = make_shared<sf::RectangleShape>(size); |
||||||
|
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); |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
struct Meter { |
||||||
|
float percent = 100.0; |
||||||
|
}; |
||||||
|
|
||||||
|
struct CellName { |
||||||
|
std::string name; |
||||||
|
}; |
||||||
|
|
||||||
|
class GUECS { |
||||||
|
public: |
||||||
|
DinkyECS::World $world; |
||||||
|
shared_ptr<sf::Font> $font = nullptr; |
||||||
|
lel::Parser $parser; |
||||||
|
std::string $grid = ""; |
||||||
|
|
||||||
|
GUECS(); |
||||||
|
|
||||||
|
void position(int x, int y, int width, int height); |
||||||
|
void layout(std::string grid); |
||||||
|
DinkyECS::Entity entity(std::string name); |
||||||
|
|
||||||
|
inline lel::CellMap& cells() { |
||||||
|
return $parser.cells; |
||||||
|
} |
||||||
|
|
||||||
|
inline DinkyECS::World& world() { |
||||||
|
return $world; |
||||||
|
} |
||||||
|
|
||||||
|
void init(); |
||||||
|
void render(sf::RenderWindow& window); |
||||||
|
void mouse(sf::RenderWindow &window); |
||||||
|
}; |
@ -1,75 +0,0 @@ |
|||||||
#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)}); |
|
||||||
} |
|
||||||
}; |
|
||||||
} |
|
@ -0,0 +1,28 @@ |
|||||||
|
#include <catch2/catch_test_macros.hpp> |
||||||
|
#include <fmt/core.h> |
||||||
|
#include "constants.hpp" |
||||||
|
#include "ecs_gui.hpp" |
||||||
|
|
||||||
|
|
||||||
|
TEST_CASE("prototype one gui", "[ecs-gui]") { |
||||||
|
GUECS gui; |
||||||
|
gui.position(0, 0, 1000, 500); |
||||||
|
gui.layout("[test1|test2|test3][test4|_|test5]"); |
||||||
|
|
||||||
|
for(auto& [name, cell] : gui.cells()) { |
||||||
|
auto& world = gui.world(); |
||||||
|
auto button = gui.entity(name); |
||||||
|
world.set<lel::Cell>(button, cell); |
||||||
|
world.set<Rectangle>(button, {}); |
||||||
|
world.set<Clickable>(button, {}); |
||||||
|
world.set<Textual>(button, {name}); |
||||||
|
} |
||||||
|
|
||||||
|
gui.init(); |
||||||
|
|
||||||
|
// at this point it's mostly ready but I'd need to render it to a window real quick
|
||||||
|
sf::RenderWindow window; |
||||||
|
window.setSize({SCREEN_WIDTH, SCREEN_HEIGHT}); |
||||||
|
gui.render(window); |
||||||
|
window.display(); |
||||||
|
} |
Loading…
Reference in new issue