diff --git a/demos/calc.cpp b/demos/calc.cpp index 5268791..c8f69ab 100644 --- a/demos/calc.cpp +++ b/demos/calc.cpp @@ -153,7 +153,9 @@ struct CalculatorUI { $gui.set($gui.MAIN, {$gui.$parser}); for(auto& [name, cell] : $gui.cells()) { + // NOTE: quick test here for the ent->name functionality auto id = $gui.entity(name); + assert(name == $gui.name_for(id) && "BUG! name and entity don't match"); auto& label = LABELS.at(name); $gui.set(id, {}); diff --git a/include/guecs/ui.hpp b/include/guecs/ui.hpp index 94081aa..d66b063 100644 --- a/include/guecs/ui.hpp +++ b/include/guecs/ui.hpp @@ -47,6 +47,7 @@ namespace guecs { std::unordered_map $components; std::unordered_map $component_storages; std::unordered_map $name_ents; + std::unordered_map $ents_name; shared_ptr $font = nullptr; lel::Parser $parser; string $grid = ""; @@ -60,6 +61,7 @@ namespace guecs { Entity init_entity(const string& name); Entity entity(const string& name); Entity entity(const string& name, int id); + const std::string& name_for(Entity gui_id); inline lel::CellMap& cells() { return $parser.cells; diff --git a/src/guecs/ui.cpp b/src/guecs/ui.cpp index 5fbad51..620a68a 100644 --- a/src/guecs/ui.cpp +++ b/src/guecs/ui.cpp @@ -38,11 +38,19 @@ namespace guecs { auto ent = entity(); // this lets you look up an entity by name $name_ents.insert_or_assign(name, ent); + // this lets you get a name by entity + $ents_name.insert_or_assign(ent, name); // this makes it easier to get the name during querying set(ent, {name}); return ent; } + const std::string& UI::name_for(Entity gui_id) { + assert($ents_name.contains(gui_id) && + "Attempt to get name_for an Entity but that's not a valid ID."); + return $ents_name.at(gui_id); + } + Entity UI::entity(const string& name) { assert($name_ents.contains(name) && "GUECS entity does not exist. Mispelled cell name?");