Can now undo/redo entity deletion (working afaik)

This commit is contained in:
Sri Sham Haran 2022-11-13 13:55:30 +08:00
parent 19f0c0ea70
commit 276e0806fa
4 changed files with 72 additions and 9 deletions

View File

@ -106,6 +106,10 @@ namespace SHADE
PasteEntities(editor->selectedEntities.back());
}
}
if(ImGui::IsKeyReleased(ImGuiKey_Delete))
{
DeleteSelectedEntities();
}
}
}
@ -259,9 +263,10 @@ namespace SHADE
PasteEntities(eid);
skipFrame = true;
}
if (ImGui::Selectable(std::format("{} Delete", ICON_MD_DELETE).data()))
if (ImGui::Selectable(std::format("{} Delete selected", ICON_MD_DELETE).data()))
{
SHEntityManager::DestroyEntity(eid);
//SHEntityManager::DestroyEntity(eid);
DeleteSelectedEntities();
}
if ((currentNode->GetParent() != sceneGraph.GetRoot()) && ImGui::Selectable(std::format("{} Unparent Selected", ICON_MD_NORTH_WEST).data()))
@ -420,6 +425,25 @@ namespace SHADE
void SHHierarchyPanel::PasteEntities(EntityID parentEID)
{
//SetScrollTo(SHSerialization::DeserializeEntitiesFromString(SHClipboardUtilities::GetDataFromClipboard(), parentEID).front());
SHCommandManager::PerformCommand(std::make_shared<SHPasteEntityCommand>(SHClipboardUtilities::GetDataFromClipboard(), parentEID));
SHCommandManager::PerformCommand(std::make_shared<SHPasteEntitiesCommand>(SHClipboardUtilities::GetDataFromClipboard(), parentEID));
}
void SHHierarchyPanel::DeleteSelectedEntities()
{
const auto editor = SHSystemManager::GetSystem<SHEditor>();
auto const& sceneGraph = SHSceneManager::GetCurrentSceneGraph();
std::vector<EntityID> entitiesToDelete{};
std::ranges::copy_if(editor->selectedEntities, std::back_inserter(entitiesToDelete), [&sceneGraph, &selectedEntities = editor->selectedEntities](EntityID const& eid)
{
EntityID parentEID = sceneGraph.GetParent(eid)->GetEntityID();
if (parentEID == MAX_EID)
return true;
else if(std::ranges::find(selectedEntities, parentEID) == selectedEntities.end())
return true;
return false;
});
SHCommandManager::PerformCommand(std::make_shared<SHDeleteEntitiesCommand>(entitiesToDelete));
}
}//namespace SHADE

View File

@ -32,6 +32,7 @@ namespace SHADE
void SelectAllEntities();
void CopySelectedEntities();
void PasteEntities(EntityID parentEID = MAX_EID);
void DeleteSelectedEntities();
bool skipFrame = false;
std::string filter;
bool isAnyNodeSelected = false;

View File

@ -44,7 +44,7 @@ namespace SHADE
}
}
void SHPasteEntityCommand::Execute()
void SHPasteEntitiesCommand::Execute()
{
data.createdEntities.clear();
data.createdEntities = SHSerialization::DeserializeEntitiesFromString(data.entityData, data.parentEID);
@ -52,7 +52,7 @@ namespace SHADE
SHEditorWindowManager::GetEditorWindow<SHHierarchyPanel>()->SetScrollTo(data.createdEntities.begin()->second);
}
void SHPasteEntityCommand::Undo()
void SHPasteEntitiesCommand::Undo()
{
for (auto const& [oldEID, newEID] : data.createdEntities)
{
@ -60,4 +60,25 @@ namespace SHADE
}
}
void SHDeleteEntitiesCommand::Execute()
{
if(!data.createdEntities.empty())
{
for(auto& eid : data.entitiesToDelete)
{
eid = data.createdEntities[eid];
}
}
data.entityData = SHSerialization::SerializeEntitiesToString(data.entitiesToDelete);
for (auto const& eid : data.entitiesToDelete)
{
SHEntityManager::DestroyEntity(eid);
}
}
void SHDeleteEntitiesCommand::Undo()
{
data.createdEntities = SHSerialization::DeserializeEntitiesFromString(data.entityData);
data.entityData = SHSerialization::ResolveSerializedEntityIndices(data.entityData, data.createdEntities);
}
}

View File

@ -35,21 +35,38 @@ namespace SHADE
std::unordered_map<EntityID, Data> entityParentData{};
};
class SHPasteEntityCommand final : public SHBaseCommand
class SHPasteEntitiesCommand final : public SHBaseCommand
{
public:
struct Data
{
SHSerialization::CreatedEntitiesList createdEntities{};
EntityID parentEID{MAX_EID};
std::string entityData{};
SHSerialization::CreatedEntitiesList createdEntities{};
};
SHPasteEntityCommand() = delete;
SHPasteEntityCommand(std::string const& serializedEntityData, EntityID parentEid = MAX_EID):data({{}, parentEid, serializedEntityData}){}
SHPasteEntitiesCommand() = delete;
SHPasteEntitiesCommand(std::string const& serializedEntityData, EntityID parentEid = MAX_EID):data({parentEid, serializedEntityData, {}}){}
void Execute() override;
void Undo() override;
private:
Data data;
};
class SHDeleteEntitiesCommand final : public SHBaseCommand
{
public:
struct Data
{
std::vector<EntityID> entitiesToDelete{};
SHSerialization::CreatedEntitiesList createdEntities{};
std::string entityData{};
};
SHDeleteEntitiesCommand() = delete;
SHDeleteEntitiesCommand(std::vector<EntityID> entitiesToBeDeleted): data{entitiesToBeDeleted}{}
void Execute() override;
void Undo() override;
private:
Data data;
};
}