GUECS refactor part 1.
This commit is contained in:
parent
4e7f837240
commit
20176cf54a
2 changed files with 108 additions and 60 deletions
58
guecs.cpp
58
guecs.cpp
|
@ -155,70 +155,70 @@ namespace guecs {
|
|||
|
||||
for(auto& [name, cell] : $parser.cells) {
|
||||
auto ent = init_entity(name);
|
||||
$world.set<lel::Cell>(ent, cell);
|
||||
set<lel::Cell>(ent, cell);
|
||||
}
|
||||
}
|
||||
|
||||
DinkyECS::Entity UI::init_entity(const string& name) {
|
||||
auto entity = $world.entity();
|
||||
Entity UI::init_entity(const string& name) {
|
||||
auto ent = entity();
|
||||
// this lets you look up an entity by name
|
||||
$name_ents.insert_or_assign(name, entity);
|
||||
$name_ents.insert_or_assign(name, ent);
|
||||
// this makes it easier to get the name during querying
|
||||
$world.set<CellName>(entity, {name});
|
||||
return entity;
|
||||
set<CellName>(ent, {name});
|
||||
return ent;
|
||||
}
|
||||
|
||||
DinkyECS::Entity UI::entity(const string& name) {
|
||||
Entity UI::entity(const string& name) {
|
||||
dbc::check($name_ents.contains(name),
|
||||
fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name));
|
||||
return $name_ents.at(name);
|
||||
}
|
||||
|
||||
DinkyECS::Entity UI::entity(const string& name, int id) {
|
||||
Entity UI::entity(const string& name, int id) {
|
||||
return entity(fmt::format("{}{}", name, id));
|
||||
}
|
||||
|
||||
void UI::init() {
|
||||
if($world.has_the<Background>()) {
|
||||
auto& bg = $world.get_the<Background>();
|
||||
if(has_the<Background>()) {
|
||||
auto& bg = get_the<Background>();
|
||||
bg.init();
|
||||
}
|
||||
|
||||
$world.query<Background>([](auto, auto& bg) {
|
||||
query<Background>([](auto, auto& bg) {
|
||||
bg.init();
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
|
||||
query<lel::Cell, Rectangle>([](auto, auto& cell, auto& rect) {
|
||||
rect.init(cell);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) {
|
||||
query<lel::Cell, Effect>([](auto, auto& cell, auto& shader) {
|
||||
shader.init(cell);
|
||||
});
|
||||
|
||||
$world.query<Rectangle, Meter>([](auto, auto& bg, auto &meter) {
|
||||
query<Rectangle, Meter>([](auto, auto& bg, auto &meter) {
|
||||
bg.shape->setFillColor(meter.color);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
|
||||
query<lel::Cell, Meter>([](auto, auto &cell, auto& meter) {
|
||||
meter.init(cell);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
|
||||
query<lel::Cell, Textual>([this](auto, auto& cell, auto& text) {
|
||||
text.init(cell, $font);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
|
||||
query<lel::Cell, Label>([this](auto, auto& cell, auto& text) {
|
||||
text.init(cell, $font);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
|
||||
query<lel::Cell, Sprite>([&](auto, auto &cell, auto &sprite) {
|
||||
sprite.init(cell);
|
||||
});
|
||||
}
|
||||
|
||||
void UI::debug_layout(sf::RenderWindow& window) {
|
||||
$world.query<lel::Cell>([&](const auto, auto &cell) {
|
||||
query<lel::Cell>([&](const auto, auto &cell) {
|
||||
sf::RectangleShape rect{{float(cell.w), float(cell.h)}};
|
||||
rect.setPosition({float(cell.x), float(cell.y)});
|
||||
rect.setFillColor(sf::Color::Transparent);
|
||||
|
@ -229,33 +229,33 @@ namespace guecs {
|
|||
}
|
||||
|
||||
void UI::render(sf::RenderWindow& window) {
|
||||
if($world.has_the<Background>()) {
|
||||
auto& bg = $world.get_the<Background>();
|
||||
if(has_the<Background>()) {
|
||||
auto& bg = get_the<Background>();
|
||||
window.draw(*bg.shape);
|
||||
}
|
||||
|
||||
$world.query<Effect>([&](auto, auto& shader) {
|
||||
query<Effect>([&](auto, auto& shader) {
|
||||
if(shader.$active) shader.step();
|
||||
});
|
||||
|
||||
$world.query<Rectangle>([&](auto ent, auto& rect) {
|
||||
query<Rectangle>([&](auto ent, auto& rect) {
|
||||
render_helper(window, ent, true, rect.shape);
|
||||
});
|
||||
|
||||
$world.query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) {
|
||||
query<lel::Cell, Meter>([&](auto ent, auto& cell, auto &meter) {
|
||||
meter.render(cell);
|
||||
render_helper(window, ent, true, meter.bar.shape);
|
||||
});
|
||||
|
||||
$world.query<Sprite>([&](auto ent, auto& sprite) {
|
||||
query<Sprite>([&](auto ent, auto& sprite) {
|
||||
render_helper(window, ent, false, sprite.sprite);
|
||||
});
|
||||
|
||||
$world.query<Label>([&](auto ent, auto& text) {
|
||||
query<Label>([&](auto ent, auto& text) {
|
||||
render_helper(window, ent, false, text.text);
|
||||
});
|
||||
|
||||
$world.query<Textual>([&](auto ent, auto& text) {
|
||||
query<Textual>([&](auto ent, auto& text) {
|
||||
render_helper(window, ent, true, text.text);
|
||||
});
|
||||
}
|
||||
|
@ -263,7 +263,7 @@ namespace guecs {
|
|||
bool UI::mouse(float x, float y, bool hover) {
|
||||
int action_count = 0;
|
||||
|
||||
$world.query<lel::Cell>([&](auto ent, auto& cell) {
|
||||
query<lel::Cell>([&](auto ent, auto& cell) {
|
||||
if((x >= cell.x && x <= cell.x + cell.w) &&
|
||||
(y >= cell.y && y <= cell.y + cell.h))
|
||||
{
|
||||
|
@ -331,7 +331,7 @@ namespace guecs {
|
|||
click_on(ent);
|
||||
}
|
||||
|
||||
void UI::click_on(DinkyECS::Entity ent) {
|
||||
void UI::click_on(Entity ent) {
|
||||
if(auto clicked = get_if<Clickable>(ent)) {
|
||||
if(auto action_data = get_if<ActionData>(ent)) {
|
||||
clicked->action(ent, action_data->data);
|
||||
|
|
110
guecs.hpp
110
guecs.hpp
|
@ -16,6 +16,16 @@
|
|||
namespace guecs {
|
||||
using std::shared_ptr, std::make_shared, std::wstring, std::string;
|
||||
|
||||
using Entity = DinkyECS::Entity;
|
||||
|
||||
using EntityMap = std::unordered_map<Entity, size_t>;
|
||||
|
||||
template <typename T>
|
||||
struct ComponentStorage {
|
||||
std::vector<T> data;
|
||||
std::queue<size_t> free_indices;
|
||||
};
|
||||
|
||||
struct Textual {
|
||||
std::wstring content;
|
||||
unsigned int size = GUECS_FONT_SIZE;
|
||||
|
@ -45,7 +55,7 @@ namespace guecs {
|
|||
/* This is actually called by UI::mouse and passed the entity ID of the
|
||||
* button pressed so you can interact with it in the event handler.
|
||||
*/
|
||||
std::function<void(DinkyECS::Entity ent, std::any data)> action;
|
||||
std::function<void(Entity ent, std::any data)> action;
|
||||
};
|
||||
|
||||
struct Sprite {
|
||||
|
@ -127,10 +137,16 @@ namespace guecs {
|
|||
void init();
|
||||
};
|
||||
|
||||
|
||||
class UI {
|
||||
public:
|
||||
DinkyECS::World $world;
|
||||
std::unordered_map<string, DinkyECS::Entity> $name_ents;
|
||||
unsigned long entity_count = 0;
|
||||
std::unordered_map<std::type_index, EntityMap> $components;
|
||||
std::unordered_map<std::type_index, std::any> $facts;
|
||||
std::unordered_map<std::type_index, std::any> $component_storages;
|
||||
|
||||
std::unordered_map<string, Entity> $name_ents;
|
||||
shared_ptr<sf::Font> $font = nullptr;
|
||||
lel::Parser $parser;
|
||||
string $grid = "";
|
||||
|
@ -141,9 +157,9 @@ namespace guecs {
|
|||
sf::Vector2f get_position();
|
||||
sf::Vector2f get_size();
|
||||
void layout(const string& grid);
|
||||
DinkyECS::Entity init_entity(const string& name);
|
||||
DinkyECS::Entity entity(const string& name);
|
||||
DinkyECS::Entity entity(const string& name, int id);
|
||||
Entity init_entity(const string& name);
|
||||
Entity entity(const string& name);
|
||||
Entity entity(const string& name, int id);
|
||||
|
||||
inline lel::CellMap& cells() {
|
||||
return $parser.cells;
|
||||
|
@ -157,16 +173,67 @@ namespace guecs {
|
|||
void render(sf::RenderWindow& window);
|
||||
bool mouse(float x, float y, bool hover);
|
||||
void click_on(const string& name, bool required=false);
|
||||
void click_on(DinkyECS::Entity slot_id);
|
||||
void click_on(Entity slot_id);
|
||||
void debug_layout(sf::RenderWindow& window);
|
||||
|
||||
|
||||
|
||||
|
||||
Entity entity() { return $world.entity(); }
|
||||
|
||||
|
||||
template <typename Comp>
|
||||
void set(DinkyECS::Entity ent, Comp val) {
|
||||
bool has_the() {
|
||||
return $world.has_the<Comp>();
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void set_the(Comp val) {
|
||||
return $world.set_the<Comp>(val);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
Comp &get_the() {
|
||||
return $world.get_the<Comp>();
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void set(Entity ent, Comp val) {
|
||||
$world.set<Comp>(ent, val);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void set_init(DinkyECS::Entity ent, Comp val) {
|
||||
Comp& get(Entity entity) {
|
||||
return $world.get<Comp>(entity);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
Comp* get_if(Entity entity) {
|
||||
return $world.get_if<Comp>(entity);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
bool has(Entity entity) {
|
||||
return $world.has<Comp>(entity);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void remove(Entity ent) {
|
||||
$world.remove<Comp>(ent);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void query(std::function<void(Entity, Comp &)> cb) {
|
||||
$world.query<Comp>(cb);
|
||||
}
|
||||
|
||||
template <typename CompA, typename CompB>
|
||||
void query(std::function<void(Entity, CompA &, CompB &)> cb) {
|
||||
$world.query<CompA, CompB>(cb);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void set_init(Entity ent, Comp val) {
|
||||
dbc::check(has<lel::Cell>(ent),"WRONG! slot is missing its cell?!");
|
||||
auto& cell = get<lel::Cell>(ent);
|
||||
val.init(cell);
|
||||
|
@ -174,40 +241,21 @@ namespace guecs {
|
|||
}
|
||||
|
||||
template <typename Comp>
|
||||
void do_if(DinkyECS::Entity ent, std::function<void(Comp &)> cb) {
|
||||
void do_if(Entity ent, std::function<void(Comp &)> cb) {
|
||||
if($world.has<Comp>(ent)) {
|
||||
cb($world.get<Comp>(ent));
|
||||
}
|
||||
}
|
||||
|
||||
lel::Cell& cell_for(DinkyECS::Entity ent) {
|
||||
lel::Cell& cell_for(Entity ent) {
|
||||
return $world.get<lel::Cell>(ent);
|
||||
}
|
||||
|
||||
lel::Cell& cell_for(const string& name) {
|
||||
DinkyECS::Entity ent = entity(name);
|
||||
Entity ent = entity(name);
|
||||
return $world.get<lel::Cell>(ent);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
Comp& get(DinkyECS::Entity entity) {
|
||||
return $world.get<Comp>(entity);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
Comp* get_if(DinkyECS::Entity entity) {
|
||||
return $world.get_if<Comp>(entity);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
bool has(DinkyECS::Entity entity) {
|
||||
return $world.has<Comp>(entity);
|
||||
}
|
||||
|
||||
template <typename Comp>
|
||||
void remove(DinkyECS::Entity ent) {
|
||||
$world.remove<Comp>(ent);
|
||||
}
|
||||
|
||||
// BUG: close could just be remove with overload
|
||||
template <typename Comp>
|
||||
|
@ -219,7 +267,7 @@ namespace guecs {
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
void render_helper(sf::RenderWindow& window, DinkyECS::Entity ent, bool is_shape, T& target) {
|
||||
void render_helper(sf::RenderWindow& window, Entity ent, bool is_shape, T& target) {
|
||||
sf::Shader *shader_ptr = nullptr;
|
||||
|
||||
if(auto shader = $world.get_if<Effect>(ent)) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue