Skip to content

Commit

Permalink
#5613: Start implementing the easy cases. Fix destruction order probl…
Browse files Browse the repository at this point in the history
…ems.
  • Loading branch information
codereader committed Oct 16, 2021
1 parent 585880a commit d122fef
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 9 deletions.
15 changes: 14 additions & 1 deletion libs/selection/CollectiveSpawnargs.h
Expand Up @@ -61,7 +61,18 @@ class CollectiveSpawnargs
kv.first->second.emplace(key, value.get());

auto entityList = _entitiesByKey.try_emplace(key);
entityList.first->second.emplace(entity);
auto result = entityList.first->second.emplace(entity);

if (result.second)
{
// The set was newly created, this was a new (and therefore unique) keyvalue
_sigKeyAdded.emit(key, value.get());
}
else
{
// The set was already present, check if the values are unique
// TODO
}
}

void onKeyChange(Entity* entity, const std::string& key, const std::string& value)
Expand All @@ -86,6 +97,8 @@ class CollectiveSpawnargs
if (entityList->second.empty())
{
_entitiesByKey.erase(entityList);
// This was the last occurrence of this key, remove it
_sigKeyRemoved.emit(key);
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions libs/selection/EntitySelection.h
Expand Up @@ -74,6 +74,11 @@ class EntitySelection final
CollectiveSpawnargs _spawnargs;

public:
~EntitySelection()
{
_trackedEntities.clear();
}

CollectiveSpawnargs& getSpawnargs()
{
return _spawnargs;
Expand Down
55 changes: 47 additions & 8 deletions test/EntityInspector.cpp
Expand Up @@ -15,8 +15,8 @@ namespace

scene::INodePtr selectEntity(const std::string& name)
{
auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
auto entity = algorithm::getEntityByName(worldspawn, name);
auto parent = GlobalMapModule().getRoot();
auto entity = algorithm::getEntityByName(parent, name);
Node_setSelected(entity, true);

return entity;
Expand All @@ -27,34 +27,41 @@ class KeyValueStore :
public sigc::trackable
{
private:
selection::EntitySelection _selectionTracker;
std::unique_ptr<selection::EntitySelection> _selectionTracker;

public:
static constexpr const char* DifferingValues = "[differing values]";

KeyValueStore()
{
_selectionTracker.getSpawnargs().signal_KeyAdded().connect(
_selectionTracker.reset(new selection::EntitySelection);

_selectionTracker->getSpawnargs().signal_KeyAdded().connect(
sigc::mem_fun(this, &KeyValueStore::onKeyAdded)
);
_selectionTracker.getSpawnargs().signal_KeyRemoved().connect(
_selectionTracker->getSpawnargs().signal_KeyRemoved().connect(
sigc::mem_fun(this, &KeyValueStore::onKeyRemoved)
);
_selectionTracker.getSpawnargs().signal_KeyValueSetChanged().connect(
_selectionTracker->getSpawnargs().signal_KeyValueSetChanged().connect(
sigc::mem_fun(this, &KeyValueStore::onKeyValueSetChanged)
);
}

~KeyValueStore()
{
_selectionTracker.reset();
}

std::map<std::string, std::string> store;

selection::CollectiveSpawnargs& getSpawnargs()
{
return _selectionTracker.getSpawnargs();
return _selectionTracker->getSpawnargs();
}

void rescanSelection()
{
_selectionTracker.update();
_selectionTracker->update();
}

private:
Expand Down Expand Up @@ -267,6 +274,38 @@ TEST_F(EntityInspectorTest, AssignDifferingKeyValues)
expectNonUnique(keyValueStore, "light_center");
}

TEST_F(EntityInspectorTest, RemoveUniqueKeyValue)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));

auto entity1 = selectEntity("light_torchflame_1");
keyValueStore.rescanSelection();

expectUnique(keyValueStore, "light_center", "0 0 0");

// Remove the light_center key from entity 2, it should disappear
Node_getEntity(entity1)->setKeyValue("light_center", "");
keyValueStore.rescanSelection();
expectNotListed(keyValueStore, "light_center");
}

TEST_F(EntityInspectorTest, AddKeyValueToSingleSelectedEntity)
{
KeyValueStore keyValueStore;
GlobalCommandSystem().executeCommand("OpenMap", cmd::Argument("maps/entityinspector.map"));

auto entity1 = selectEntity("light_torchflame_1");
keyValueStore.rescanSelection();

assumeLightTorchflame1Spawnargs(keyValueStore);

// Add a new key, it should appear
Node_getEntity(entity1)->setKeyValue("custom_key", "do it");
keyValueStore.rescanSelection();
expectUnique(keyValueStore, "custom_key", "do it");
}

TEST_F(EntityInspectorTest, RemoveOneSharedKeyValue)
{
KeyValueStore keyValueStore;
Expand Down

0 comments on commit d122fef

Please sign in to comment.