Initial GUECS refactor is done, it's now on its own with no other dependencies, but now I need to trim it down to do only what it needs.

master
Zed A. Shaw 2 days ago
parent 20176cf54a
commit 1780a758b3
  1. 2
      boss_fight_ui.cpp
  2. 2
      combat_ui.cpp
  3. 200
      guecs.hpp
  4. 2
      status_ui.cpp
  5. 9
      tests/guecs.cpp

@ -54,7 +54,7 @@ namespace gui {
$boss_background = textures::get(boss.background);
$boss_background.sprite->setPosition({BOSS_VIEW_X, BOSS_VIEW_Y});
$status.world().set_the<Background>({$status.$parser});
$status.set_the<Background>({$status.$parser});
if(boss.stage) {
$boss_has_stage = true;

@ -36,7 +36,7 @@ namespace gui {
}
void CombatUI::init() {
$gui.world().set_the<Background>({$gui.$parser, ColorValue::DARK_MID});
$gui.set_the<Background>({$gui.$parser, ColorValue::DARK_MID});
auto& the_belt = $level.world->get_the<ritual::Belt>();
for(int slot = 0; slot < the_belt.max_slots; slot++) {

@ -16,7 +16,7 @@
namespace guecs {
using std::shared_ptr, std::make_shared, std::wstring, std::string;
using Entity = DinkyECS::Entity;
using Entity = unsigned long;
using EntityMap = std::unordered_map<Entity, size_t>;
@ -41,7 +41,7 @@ namespace guecs {
struct Label : public Textual {
template<typename... Args>
Label(Args... args) : Textual(args...)
Label(Args... args) : Textual(args...)
{
centered = true;
}
@ -140,7 +140,6 @@ namespace guecs {
class UI {
public:
DinkyECS::World $world;
unsigned long entity_count = 0;
std::unordered_map<std::type_index, EntityMap> $components;
std::unordered_map<std::type_index, std::any> $facts;
@ -165,10 +164,6 @@ namespace guecs {
return $parser.cells;
}
inline DinkyECS::World& world() {
return $world;
}
void init();
void render(sf::RenderWindow& window);
bool mouse(float x, float y, bool hover);
@ -176,111 +171,183 @@ namespace guecs {
void click_on(Entity slot_id);
void debug_layout(sf::RenderWindow& window);
Entity entity() { return ++entity_count; }
template <typename Comp>
size_t make_component() {
auto &storage = component_storage_for<Comp>();
size_t index;
if(!storage.free_indices.empty()) {
index = storage.free_indices.front();
storage.free_indices.pop();
} else {
storage.data.emplace_back();
index = storage.data.size() - 1;
}
return index;
}
Entity entity() { return $world.entity(); }
template <typename Comp>
ComponentStorage<Comp> &component_storage_for() {
auto type_index = std::type_index(typeid(Comp));
$component_storages.try_emplace(type_index, ComponentStorage<Comp>{});
return std::any_cast<ComponentStorage<Comp> &>(
$component_storages.at(type_index));
}
template <typename Comp>
EntityMap &entity_map_for() {
return $components[std::type_index(typeid(Comp))];
}
template <typename Comp>
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>
bool has_the() {
auto comp_id = std::type_index(typeid(Comp));
return $facts.contains(comp_id);
}
template <typename Comp>
Comp &get_the() {
return $world.get_the<Comp>();
}
template <typename Comp>
void set_the(Comp val) {
$facts.insert_or_assign(std::type_index(typeid(Comp)), val);
}
template <typename Comp>
void set(Entity ent, Comp val) {
$world.set<Comp>(ent, val);
}
Comp &get_the() {
auto comp_id = std::type_index(typeid(Comp));
dbc::check($facts.contains(comp_id),
fmt::format("!!!! ATTEMPT to access world fact that hasn't "
"been set yet: {}",
typeid(Comp).name()));
// use .at to get std::out_of_range if fact not set
std::any &res = $facts.at(comp_id);
return std::any_cast<Comp &>(res);
}
template <typename Comp>
void set(Entity ent, Comp val) {
EntityMap &map = entity_map_for<Comp>();
if(has<Comp>(ent)) {
get<Comp>(ent) = val;
return;
}
map.insert_or_assign(ent, make_component<Comp>());
get<Comp>(ent) = val;
}
template <typename Comp>
Comp& get(Entity entity) {
return $world.get<Comp>(entity);
Comp& get(Entity ent) {
EntityMap &map = entity_map_for<Comp>();
auto &storage = component_storage_for<Comp>();
auto index = map.at(ent);
return storage.data[index];
}
template <typename Comp>
Comp* get_if(Entity entity) {
return $world.get_if<Comp>(entity);
EntityMap &map = entity_map_for<Comp>();
auto &storage = component_storage_for<Comp>();
if(map.contains(entity)) {
auto index = map.at(entity);
return &storage.data[index];
} else {
return nullptr;
}
}
template <typename Comp>
bool has(Entity entity) {
return $world.has<Comp>(entity);
bool has(Entity ent) {
EntityMap &map = entity_map_for<Comp>();
return map.contains(ent);
}
template <typename Comp>
void remove(Entity ent) {
$world.remove<Comp>(ent);
EntityMap &map = entity_map_for<Comp>();
if(map.contains(ent)) {
size_t index = map.at(ent);
component_storage_for<Comp>().free_indices.push(index);
}
map.erase(ent);
}
template <typename Comp>
void query(std::function<void(Entity, Comp &)> cb) {
$world.query<Comp>(cb);
}
template <typename Comp>
void query(std::function<void(Entity, Comp &)> cb) {
EntityMap &map = entity_map_for<Comp>();
template <typename CompA, typename CompB>
void query(std::function<void(Entity, CompA &, CompB &)> cb) {
$world.query<CompA, CompB>(cb);
}
for(auto &[entity, index] : map) {
cb(entity, get<Comp>(entity));
}
}
template <typename CompA, typename CompB>
void query(std::function<void(Entity, CompA &, CompB &)> cb) {
EntityMap &map_a = entity_map_for<CompA>();
EntityMap &map_b = entity_map_for<CompB>();
for(auto &[entity, index_a] : map_a) {
if(map_b.contains(entity)) {
cb(entity, get<CompA>(entity), get<CompB>(entity));
}
}
}
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);
$world.set<Comp>(ent, val);
}
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);
set<Comp>(ent, val);
}
template <typename Comp>
void do_if(Entity ent, std::function<void(Comp &)> cb) {
if($world.has<Comp>(ent)) {
cb($world.get<Comp>(ent));
void do_if(Entity ent, std::function<void(Comp &)> cb) {
if(has<Comp>(ent)) {
cb(get<Comp>(ent));
}
}
}
lel::Cell& cell_for(Entity ent) {
return $world.get<lel::Cell>(ent);
return get<lel::Cell>(ent);
}
lel::Cell& cell_for(const string& name) {
Entity ent = entity(name);
return $world.get<lel::Cell>(ent);
return get<lel::Cell>(ent);
}
// BUG: close could just be remove with overload
template <typename Comp>
void close(string region) {
auto ent = entity(region);
if(has<Comp>(ent)) {
remove<Comp>(ent);
void close(string region) {
auto ent = entity(region);
if(has<Comp>(ent)) {
remove<Comp>(ent);
}
}
}
template<typename T>
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)) {
if(shader->$active && !is_shape) {
auto ptr = shader->checkout_ptr();
ptr->setUniform("is_shape", is_shape);
// NOTE: this is needed because SFML doesn't handle shared_ptr
shader_ptr = ptr.get();
void render_helper(sf::RenderWindow& window, Entity ent, bool is_shape, T& target) {
sf::Shader *shader_ptr = nullptr;
if(auto shader = get_if<Effect>(ent)) {
if(shader->$active && !is_shape) {
auto ptr = shader->checkout_ptr();
ptr->setUniform("is_shape", is_shape);
// NOTE: this is needed because SFML doesn't handle shared_ptr
shader_ptr = ptr.get();
}
}
}
window.draw(*target, shader_ptr);
}
window.draw(*target, shader_ptr);
}
void show_sprite(const string& region, const string& sprite_name);
void show_text(const string& region, const wstring& content);
@ -289,5 +356,4 @@ namespace guecs {
Clickable make_action(DinkyECS::World& target, Events::GUI event);
Clickable make_action(DinkyECS::World& target, Events::GUI event, std::any data);
}

@ -31,7 +31,7 @@ namespace gui {
}
void StatusUI::init() {
$gui.world().set_the<Background>({$gui.$parser});
$gui.set_the<Background>({$gui.$parser});
for(auto& [name, cell] : $gui.cells()) {
if(name == "log_view") {

@ -15,12 +15,11 @@ TEST_CASE("prototype one gui", "[ecs-gui]") {
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, {L"whatever"});
gui.set<lel::Cell>(button, cell);
gui.set<Rectangle>(button, {});
gui.set<Clickable>(button, {});
gui.set<Textual>(button, {L"whatever"});
}
gui.init();

Loading…
Cancel
Save