From c8c3b79ac2dd33b5dc1628614025549eb6590aff Mon Sep 17 00:00:00 2001 From: RustyStriker Date: Wed, 7 Sep 2022 23:34:17 +0300 Subject: [PATCH] undo is really almost done :D --- src/ui.rs | 21 +++++++++++++++++---- src/undo.rs | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index eafe14b..eb3db37 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -330,10 +330,23 @@ pub fn inspector_sys( let gt = global_transforms.get(*edge).unwrap(); let mut gt_x = gt.translation().x; let mut gt_y = gt.translation().y; - let c1 = hui.add(egui::DragValue::new(&mut gt_x)).changed(); - let c2 = hui.add(egui::DragValue::new(&mut gt_y)).changed(); + let rx = hui.add(egui::DragValue::new(&mut gt_x)); + let ry = hui.add(egui::DragValue::new(&mut gt_y)); - if c1 || c2 { + if started_edit(&rx) { + changes.0 = gt_x; + } + if started_edit(&ry) { + changes.0 = gt_y; + } + if stopped_edit(&rx) { + undo.push(UndoItem::MoveDot { shape_data: e, dot: *edge, from: Vec2::new(changes.0, gt_y), to: Vec2::new(gt_x, gt_y) }); + } + if stopped_edit(&ry) { + undo.push(UndoItem::MoveDot { shape_data: e, dot: *edge, from: Vec2::new(gt_x, changes.0), to: Vec2::new(gt_x, gt_y) }); + } + + if rx.changed() || ry.changed() { let rot = gt.to_scale_rotation_translation().1; let ang = rot.to_euler(EulerRot::XYZ).2; let delta = Mat2::from_angle(-ang) * (Vec2::new(gt_x, gt_y) - gt.translation().xy()); @@ -362,7 +375,7 @@ pub fn inspector_sys( if let Ok(mut v) = visible.get_mut(e) { ui.checkbox(&mut v.is_visible, "Visible"); } - if let Ok(ref mut sd) = shape { + if shape.is_ok() { if let Ok(mut dm) = draw_modes.get_mut(e) { ui.separator(); if let DrawMode::Outlined { fill_mode: f, outline_mode: _ } = &mut *dm { diff --git a/src/undo.rs b/src/undo.rs index 54bb791..f299a09 100644 --- a/src/undo.rs +++ b/src/undo.rs @@ -1,7 +1,7 @@ +use bevy::math::Vec3Swizzles; use bevy::prelude::*; -use crate::ui::SelectedItem; -use crate::{CreateShape, ShapeData, ImageData}; +use crate::{ShapeData, ImageData}; #[derive(Debug, Clone)] pub enum UndoItem { @@ -73,23 +73,24 @@ impl UndoStack { pub fn undo_redo_sys( mut coms: Commands, mut stack: ResMut, - mut inspector: ResMut, keyboard: Res>, mut shapes: Query<&mut ShapeData>, mut transforms: Query<&mut Transform>, + global_transforms: Query<&GlobalTransform>, mut images: Query<(&mut ImageData, &Handle)>, + mut paths: Query<&mut bevy_prototype_lyon::prelude::Path>, ) { if keyboard.just_pressed(KeyCode::Z) && keyboard.pressed(KeyCode::LControl) { let item = if keyboard.pressed(KeyCode::LShift) { stack.unpop() } else { stack.pop() }; if let Some(item) = item { match item { UndoItem::Base => bevy::log::error!("POP/UNPOP: Got UndoItem::Base as a result!"), - UndoItem::CreateShape { entity } => { + UndoItem::CreateShape { entity: _ } => { bevy::log::error!("NOT IMPLEMENTED : CreateShape"); }, UndoItem::DeleteShape { - sd, - transform, + sd: _, + transform: _, } => { bevy::log::error!("NOT IMPLEMENTED : DeleteShape"); }, @@ -102,7 +103,33 @@ pub fn undo_redo_sys( *to = t; }, UndoItem::MoveDot { shape_data, dot, from, to } => { - bevy::log::error!("NOT IMPLEMENTED : MoveDot"); + let gt = global_transforms.get(*dot).unwrap(); + + let sd = shapes.get(*shape_data).unwrap(); + let rot = gt.to_scale_rotation_translation().1; + let ang = rot.to_euler(EulerRot::XYZ).2; + let delta = Mat2::from_angle(-ang) * (*from - gt.translation().xy()); + if let Ok(mut t) = transforms.get_mut(*dot) { + t.translation += delta.extend(0.0); + } + // We need to recalculate the center, and update the points to be the new relevant point from the center + let center_offset = crate::modify::calc_shape_center_offset(&transforms, &*sd); + // Update each edge's offset, and then move the main shape's translation + for edge in sd.edges.iter() { + if let Ok(mut t) = transforms.get_mut(*edge) { + t.translation -= center_offset.extend(0.0); + } + } + if let Ok(mut t) = transforms.get_mut(sd.main_shape) { + t.translation += (Mat2::from_angle(ang) * center_offset).extend(0.0); + } + + // Now we need to update the shape itself + crate::modify::update_main_shape(&mut paths, &transforms, &*sd); + + let t = *from; + *from = *to; + *to = t; }, UndoItem::Rotate { entity, from, to } => { if let Ok(mut t) = transforms.get_mut(*entity) {