Implement a way to map entities to their names, which comes up often enough.

main
Zed A. Shaw 22 hours ago
parent e1d61dc2c1
commit ad78c186c6
  1. 2
      demos/calc.cpp
  2. 2
      include/guecs/ui.hpp
  3. 8
      src/guecs/ui.cpp

@ -153,7 +153,9 @@ struct CalculatorUI {
$gui.set<guecs::Background>($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<guecs::Rectangle>(id, {});

@ -47,6 +47,7 @@ namespace guecs {
std::unordered_map<std::type_index, EntityMap> $components;
std::unordered_map<std::type_index, std::any> $component_storages;
std::unordered_map<string, Entity> $name_ents;
std::unordered_map<Entity, string> $ents_name;
shared_ptr<sf::Font> $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;

@ -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<CellName>(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?");

Loading…
Cancel
Save