fix image deletion makes other undos impossible

This commit is contained in:
RustyStriker 2022-09-07 23:47:55 +03:00
parent c8c3b79ac2
commit 0b36b71b86
2 changed files with 21 additions and 5 deletions

View file

@ -418,7 +418,7 @@ pub fn inspector_sys(
std::mem::swap(&mut nid, &mut *id); std::mem::swap(&mut nid, &mut *id);
let transform = transforms.get(e).map(|t| *t).unwrap_or_default(); let transform = transforms.get(e).map(|t| *t).unwrap_or_default();
let handle = image_handles.get(e).unwrap().clone(); let handle = image_handles.get(e).unwrap().clone();
undo.push(UndoItem::DeleteImage { id: nid, transform: transform, handle }); undo.push(UndoItem::DeleteImage { id: nid, transform: transform, handle, old_entity: e });
} }
coms.entity(e).despawn_recursive(); coms.entity(e).despawn_recursive();
**selected = None; **selected = None;

View file

@ -16,7 +16,7 @@ pub enum UndoItem {
ChangeZ { entity: Entity, from: f32, to: f32 }, ChangeZ { entity: Entity, from: f32, to: f32 },
ReScale { entity: Entity, from: Vec2, to: Vec2 }, ReScale { entity: Entity, from: Vec2, to: Vec2 },
CreateImage { entity: Entity }, CreateImage { entity: Entity },
DeleteImage { id: ImageData, transform: Transform, handle: Handle<Image> }, DeleteImage { id: ImageData, transform: Transform, handle: Handle<Image>, old_entity: Entity },
} }
pub struct UndoStack { pub struct UndoStack {
items: Vec<UndoItem>, items: Vec<UndoItem>,
@ -170,11 +170,11 @@ pub fn undo_redo_sys(
let mut nid = ImageData::default(); let mut nid = ImageData::default();
std::mem::swap(&mut *id, &mut nid); std::mem::swap(&mut *id, &mut nid);
coms.entity(*entity).despawn(); coms.entity(*entity).despawn_recursive();
*item = UndoItem::DeleteImage { id: nid, transform, handle: handle.clone() }; *item = UndoItem::DeleteImage { id: nid, transform, handle: handle.clone(), old_entity: *entity };
} }
}, },
UndoItem::DeleteImage { transform, handle, id } => { UndoItem::DeleteImage { transform, handle, id, old_entity } => {
let mut nid = ImageData::default(); let mut nid = ImageData::default();
std::mem::swap(id, &mut nid); std::mem::swap(id, &mut nid);
@ -185,7 +185,23 @@ pub fn undo_redo_sys(
}) })
.insert(nid).id(); .insert(nid).id();
let oe = *old_entity;
*item = UndoItem::CreateImage { entity: e }; *item = UndoItem::CreateImage { entity: e };
// Update each item with the same entity id
let f = |ne: &mut Entity| { if *ne == oe { *ne = e; }};
for i in stack.items.iter_mut() {
match i {
UndoItem::CreateShape { entity } => f(entity),
UndoItem::MoveItem { shape, .. } => f(shape),
UndoItem::Rotate { entity, .. } => f(entity),
UndoItem::NameChange { entity, .. } => f(entity),
UndoItem::NoteChange { entity, .. } => f(entity),
UndoItem::ChangeZ { entity, .. } => f(entity),
UndoItem::ReScale { entity, .. } => f(entity),
_ => {},
}
}
}, },
UndoItem::NoteChange { entity, from } => { UndoItem::NoteChange { entity, from } => {
if let Ok(mut sd) = shapes.get_mut(*entity) { if let Ok(mut sd) = shapes.get_mut(*entity) {