Decreased memory usage taken by undo actions

This commit is contained in:
Yousef Khadadeh 2026-01-06 15:15:36 +03:00
parent aa9bb19fd5
commit a9c7278102
5 changed files with 114 additions and 98 deletions

View file

@ -185,10 +185,12 @@ void DrawingProgramLayerManager::add_undo_place_component(CanvasComponentContain
auto compList = undoMan.world.netObjMan.get_obj_temporary_ref_from_id<NetObjOrderedList<CanvasComponentContainer>>(toEraseCompListID.value());
auto insertedIt = compList->emplace_direct(compList, compList->at(pos), undoMan.world.netObjMan, *copyData);
undoMan.register_new_netid_to_existing_undoid(undoID, insertedIt->obj.get_net_id());
copyData = nullptr;
return true;
}
void scale_up(const WorldScalar& scaleAmount) override {
copyData->scale_up(scaleAmount);
if(copyData)
copyData->scale_up(scaleAmount);
}
~AddCanvasComponentWorldUndoAction() {}
@ -260,6 +262,7 @@ void DrawingProgramLayerManager::add_undo_erase_components(const std::unordered_
undoMan.register_new_netid_to_existing_undoid(insertData.undoID->at(i), toInsert[i].second.get_net_id());
}
parentListPtr->insert_ordered_list_and_send_create(parentListPtr, toInsert);
insertData.copyData->clear();
}
}, NetworkingObjects::NetObjManager::SendUpdateType::SEND_TO_ALL, nullptr);
@ -279,6 +282,8 @@ void DrawingProgramLayerManager::add_undo_erase_components(const std::unordered_
if(!undoMan.fill_netid_list_from_undoid_list(netIDList, eData.undoID))
return false;
eraseData = parentListPtr->get_list(netIDList);
for(auto& it : eraseData)
eData.copyData.emplace_back(it->obj->get_data_copy());
}
}
@ -352,6 +357,8 @@ void DrawingProgramLayerManager::add_undo_place_components(DrawingProgramLayerLi
if(!undoMan.fill_netid_list_from_undoid_list(netIDList, undoPlaceData.undoID))
return false;
eraseData = parentListPtr->get_list(netIDList);
for(auto& it : eraseData)
undoPlaceData.copyData.emplace_back(it->obj->get_data_copy());
}
parentListPtr->erase_list(parentListPtr, eraseData);
@ -381,6 +388,7 @@ void DrawingProgramLayerManager::add_undo_place_components(DrawingProgramLayerLi
}
parentListPtr->insert_ordered_list_and_send_create(parentListPtr, toInsert);
undoPlaceData.copyData.clear();
return true;
}
void scale_up(const WorldScalar& scaleAmount) override {
@ -399,7 +407,6 @@ void DrawingProgramLayerManager::add_undo_place_components(DrawingProgramLayerLi
uint32_t i = 0;
for(auto& comp : placeList) {
undoData.undoID.emplace_back(undoMan.get_undoid_from_netid(comp->obj.get_net_id()));
undoData.copyData.emplace_back(comp->obj->get_data_copy());
undoData.pos.emplace_back(comp->pos - i);
++i;
}

View file

@ -384,14 +384,14 @@ NetworkingObjects::NetObjOrderedListIterator<DrawingProgramLayerListItem> Drawin
auto& world = layerMan.drawP.world;
class AddLayerWorldUndoAction : public WorldUndoAction {
public:
AddLayerWorldUndoAction(DrawingProgramLayerListItemUndoData initLayerData, uint32_t newPos, WorldUndoManager::UndoObjectID initParentUndoID, WorldUndoManager::UndoObjectID initUndoID):
AddLayerWorldUndoAction(std::unique_ptr<DrawingProgramLayerListItemUndoData> initLayerData, uint32_t newPos, WorldUndoManager::UndoObjectID initParentUndoID, WorldUndoManager::UndoObjectID initUndoID):
layerData(std::move(initLayerData)),
pos(newPos),
parentUndoID(initParentUndoID),
undoID(initUndoID)
{}
std::string get_name() const override {
return layerData.folderData ? "Add Layer Folder" : "Add Layer";
return "Add Layer Item";
}
bool undo(WorldUndoManager& undoMan) override {
std::optional<NetworkingObjects::NetObjID> toEraseParentID = undoMan.get_netid_from_undoid(parentUndoID);
@ -402,7 +402,9 @@ NetworkingObjects::NetObjOrderedListIterator<DrawingProgramLayerListItem> Drawin
return false;
auto& layerList = undoMan.world.netObjMan.get_obj_temporary_ref_from_id<DrawingProgramLayerListItem>(toEraseParentID.value())->get_folder().folderList;
layerList->erase(layerList, layerList->get(toEraseID.value()));
auto it = layerList->get(toEraseID.value());
layerData = std::make_unique<DrawingProgramLayerListItemUndoData>(it->obj->get_undo_data(undoMan));
layerList->erase(layerList, it);
return true;
}
bool redo(WorldUndoManager& undoMan) override {
@ -414,16 +416,18 @@ NetworkingObjects::NetObjOrderedListIterator<DrawingProgramLayerListItem> Drawin
return false;
auto& layerList = undoMan.world.netObjMan.get_obj_temporary_ref_from_id<DrawingProgramLayerListItem>(toInsertParentID.value())->get_folder().folderList;
auto insertedIt = layerList->emplace_direct(layerList, layerList->at(pos), undoMan.world, layerData);
auto insertedIt = layerList->emplace_direct(layerList, layerList->at(pos), undoMan.world, *layerData);
undoMan.register_new_netid_to_existing_undoid(undoID, insertedIt->obj.get_net_id());
layerData = nullptr;
return true;
}
void scale_up(const WorldScalar& scaleAmount) override {
layerData.scale_up(scaleAmount);
if(layerData)
layerData->scale_up(scaleAmount);
}
~AddLayerWorldUndoAction() {}
DrawingProgramLayerListItemUndoData layerData;
std::unique_ptr<DrawingProgramLayerListItemUndoData> layerData;
uint32_t pos;
WorldUndoManager::UndoObjectID parentUndoID;
WorldUndoManager::UndoObjectID undoID;
@ -431,7 +435,7 @@ NetworkingObjects::NetObjOrderedListIterator<DrawingProgramLayerListItem> Drawin
auto insertedLayerPair = create_in_proper_position(newItem);
auto& it = insertedLayerPair.second;
world.undo.push(std::make_unique<AddLayerWorldUndoAction>(it->obj->get_undo_data(world.undo), it->pos, world.undo.get_undoid_from_netid(insertedLayerPair.first), world.undo.get_undoid_from_netid(it->obj.get_net_id())));
world.undo.push(std::make_unique<AddLayerWorldUndoAction>(std::make_unique<DrawingProgramLayerListItemUndoData>(it->obj->get_undo_data(world.undo)), it->pos, world.undo.get_undoid_from_netid(insertedLayerPair.first), world.undo.get_undoid_from_netid(it->obj.get_net_id())));
return it;
}
@ -439,14 +443,14 @@ void DrawingProgramLayerManagerGUI::remove_layer(const NetworkingObjects::NetObj
auto& world = layerMan.drawP.world;
class DeleteLayerWorldUndoAction : public WorldUndoAction {
public:
DeleteLayerWorldUndoAction(DrawingProgramLayerListItemUndoData initLayerData, uint32_t newPos, WorldUndoManager::UndoObjectID initParentUndoID, WorldUndoManager::UndoObjectID initUndoID):
DeleteLayerWorldUndoAction(std::unique_ptr<DrawingProgramLayerListItemUndoData> initLayerData, uint32_t newPos, WorldUndoManager::UndoObjectID initParentUndoID, WorldUndoManager::UndoObjectID initUndoID):
layerData(std::move(initLayerData)),
pos(newPos),
parentUndoID(initParentUndoID),
undoID(initUndoID)
{}
std::string get_name() const override {
return layerData.folderData ? "Delete Layer Folder" : "Delete Layer";
return "Delete Layer Item";
}
bool undo(WorldUndoManager& undoMan) override {
std::optional<NetworkingObjects::NetObjID> toInsertParentID = undoMan.get_netid_from_undoid(parentUndoID);
@ -457,8 +461,9 @@ void DrawingProgramLayerManagerGUI::remove_layer(const NetworkingObjects::NetObj
return false;
auto& layerList = undoMan.world.netObjMan.get_obj_temporary_ref_from_id<DrawingProgramLayerListItem>(toInsertParentID.value())->get_folder().folderList;
auto insertedIt = layerList->emplace_direct(layerList, layerList->at(pos), undoMan.world, layerData);
auto insertedIt = layerList->emplace_direct(layerList, layerList->at(pos), undoMan.world, *layerData);
undoMan.register_new_netid_to_existing_undoid(undoID, insertedIt->obj.get_net_id());
layerData = nullptr;
return true;
}
bool redo(WorldUndoManager& undoMan) override {
@ -470,15 +475,18 @@ void DrawingProgramLayerManagerGUI::remove_layer(const NetworkingObjects::NetObj
return false;
auto& layerList = undoMan.world.netObjMan.get_obj_temporary_ref_from_id<DrawingProgramLayerListItem>(toEraseParentID.value())->get_folder().folderList;
layerList->erase(layerList, layerList->get(toEraseID.value()));
auto it = layerList->get(toEraseID.value());
layerData = std::make_unique<DrawingProgramLayerListItemUndoData>(it->obj->get_undo_data(undoMan));
layerList->erase(layerList, it);
return true;
}
void scale_up(const WorldScalar& scaleAmount) override {
layerData.scale_up(scaleAmount);
if(layerData)
layerData->scale_up(scaleAmount);
}
~DeleteLayerWorldUndoAction() {}
DrawingProgramLayerListItemUndoData layerData;
std::unique_ptr<DrawingProgramLayerListItemUndoData> layerData;
uint32_t pos;
WorldUndoManager::UndoObjectID parentUndoID;
WorldUndoManager::UndoObjectID undoID;
@ -486,7 +494,7 @@ void DrawingProgramLayerManagerGUI::remove_layer(const NetworkingObjects::NetObj
auto& folderList = world.netObjMan.get_obj_temporary_ref_from_id<DrawingProgramLayerListItem>(parentID)->get_folder().folderList;
auto it = folderList->get(objectID);
world.undo.push(std::make_unique<DeleteLayerWorldUndoAction>(it->obj->get_undo_data(world.undo), it->pos, world.undo.get_undoid_from_netid(parentID), world.undo.get_undoid_from_netid(objectID)));
world.undo.push(std::make_unique<DeleteLayerWorldUndoAction>(std::make_unique<DrawingProgramLayerListItemUndoData>(it->obj->get_undo_data(world.undo)), it->pos, world.undo.get_undoid_from_netid(parentID), world.undo.get_undoid_from_netid(objectID)));
folderList->erase(folderList, it);
}
@ -494,34 +502,32 @@ void DrawingProgramLayerManagerGUI::editing_layer_check() {
auto& world = layerMan.drawP.world;
class EditLayerWorldUndoAction : public WorldUndoAction {
public:
EditLayerWorldUndoAction(const DrawingProgramLayerListItemMetaInfo& initDataOld, const DrawingProgramLayerListItemMetaInfo& initDataNew, WorldUndoManager::UndoObjectID initUndoID):
dataOld(initDataOld),
dataNew(initDataNew),
EditLayerWorldUndoAction(std::unique_ptr<DrawingProgramLayerListItemMetaInfo> initData, WorldUndoManager::UndoObjectID initUndoID):
data(std::move(initData)),
undoID(initUndoID)
{}
std::string get_name() const override {
return "Edit Layer";
}
bool undo(WorldUndoManager& undoMan) override {
std::optional<NetworkingObjects::NetObjID> toModifyID = undoMan.get_netid_from_undoid(undoID);
if(!toModifyID.has_value())
return false;
auto objPtr = undoMan.world.netObjMan.get_obj_temporary_ref_from_id<DrawingProgramLayerListItem>(toModifyID.value());
objPtr->set_metainfo(undoMan.world.drawProg.layerMan, dataOld);
return true;
return undo_redo(undoMan);
}
bool redo(WorldUndoManager& undoMan) override {
return undo_redo(undoMan);
}
bool undo_redo(WorldUndoManager& undoMan) {
std::optional<NetworkingObjects::NetObjID> toModifyID = undoMan.get_netid_from_undoid(undoID);
if(!toModifyID.has_value())
return false;
auto objPtr = undoMan.world.netObjMan.get_obj_temporary_ref_from_id<DrawingProgramLayerListItem>(toModifyID.value());
objPtr->set_metainfo(undoMan.world.drawProg.layerMan, dataNew);
auto newData = std::make_unique<DrawingProgramLayerListItemMetaInfo>(objPtr->get_metainfo());
objPtr->set_metainfo(undoMan.world.drawProg.layerMan, *data);
data = std::move(newData);
return true;
}
~EditLayerWorldUndoAction() {}
DrawingProgramLayerListItemMetaInfo dataOld;
DrawingProgramLayerListItemMetaInfo dataNew;
std::unique_ptr<DrawingProgramLayerListItemMetaInfo> data;
WorldUndoManager::UndoObjectID undoID;
};
@ -531,7 +537,7 @@ void DrawingProgramLayerManagerGUI::editing_layer_check() {
if(tempPtr) {
const DrawingProgramLayerListItemMetaInfo& currentMetaData = tempPtr->get_metainfo();
if(currentMetaData != editingData.value())
world.undo.push(std::make_unique<EditLayerWorldUndoAction>(editingData.value(), currentMetaData, world.undo.get_undoid_from_netid(oldNetObjID)));
world.undo.push(std::make_unique<EditLayerWorldUndoAction>(std::make_unique<DrawingProgramLayerListItemMetaInfo>(editingData.value()), world.undo.get_undoid_from_netid(oldNetObjID)));
}
}
editingData = std::nullopt;

View file

@ -69,18 +69,12 @@ void EditTool::switch_tool(DrawingProgramToolType newTool) {
return "Edit Canvas Component";
}
bool undo(WorldUndoManager& undoMan) override {
std::optional<NetworkingObjects::NetObjID> toEditID = undoMan.get_netid_from_undoid(undoID);
if(!toEditID.has_value())
return false;
auto objPtr = undoMan.world.netObjMan.get_obj_temporary_ref_from_id<CanvasComponentContainer>(toEditID.value());
std::unique_ptr<CanvasComponent> newData = objPtr->get_comp().get_data_copy();
objPtr->get_comp().set_data_from(*data);
data = std::move(newData);
objPtr->commit_update(undoMan.world.drawProg);
objPtr->send_comp_update(undoMan.world.drawProg, true);
return true;
return undo_redo(undoMan);
}
bool redo(WorldUndoManager& undoMan) override {
return undo_redo(undoMan);
}
bool undo_redo(WorldUndoManager& undoMan) {
std::optional<NetworkingObjects::NetObjID> toEditID = undoMan.get_netid_from_undoid(undoID);
if(!toEditID.has_value())
return false;