undo is really almost done :D

This commit is contained in:
RustyStriker 2022-09-07 23:34:17 +03:00
parent 32c438ce16
commit c8c3b79ac2
2 changed files with 51 additions and 11 deletions

View file

@ -330,10 +330,23 @@ pub fn inspector_sys(
let gt = global_transforms.get(*edge).unwrap(); let gt = global_transforms.get(*edge).unwrap();
let mut gt_x = gt.translation().x; let mut gt_x = gt.translation().x;
let mut gt_y = gt.translation().y; let mut gt_y = gt.translation().y;
let c1 = hui.add(egui::DragValue::new(&mut gt_x)).changed(); let rx = hui.add(egui::DragValue::new(&mut gt_x));
let c2 = hui.add(egui::DragValue::new(&mut gt_y)).changed(); 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 rot = gt.to_scale_rotation_translation().1;
let ang = rot.to_euler(EulerRot::XYZ).2; let ang = rot.to_euler(EulerRot::XYZ).2;
let delta = Mat2::from_angle(-ang) * (Vec2::new(gt_x, gt_y) - gt.translation().xy()); 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) { if let Ok(mut v) = visible.get_mut(e) {
ui.checkbox(&mut v.is_visible, "Visible"); 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) { if let Ok(mut dm) = draw_modes.get_mut(e) {
ui.separator(); ui.separator();
if let DrawMode::Outlined { fill_mode: f, outline_mode: _ } = &mut *dm { if let DrawMode::Outlined { fill_mode: f, outline_mode: _ } = &mut *dm {

View file

@ -1,7 +1,7 @@
use bevy::math::Vec3Swizzles;
use bevy::prelude::*; use bevy::prelude::*;
use crate::ui::SelectedItem; use crate::{ShapeData, ImageData};
use crate::{CreateShape, ShapeData, ImageData};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum UndoItem { pub enum UndoItem {
@ -73,23 +73,24 @@ impl UndoStack {
pub fn undo_redo_sys( pub fn undo_redo_sys(
mut coms: Commands, mut coms: Commands,
mut stack: ResMut<UndoStack>, mut stack: ResMut<UndoStack>,
mut inspector: ResMut<SelectedItem>,
keyboard: Res<Input<KeyCode>>, keyboard: Res<Input<KeyCode>>,
mut shapes: Query<&mut ShapeData>, mut shapes: Query<&mut ShapeData>,
mut transforms: Query<&mut Transform>, mut transforms: Query<&mut Transform>,
global_transforms: Query<&GlobalTransform>,
mut images: Query<(&mut ImageData, &Handle<Image>)>, mut images: Query<(&mut ImageData, &Handle<Image>)>,
mut paths: Query<&mut bevy_prototype_lyon::prelude::Path>,
) { ) {
if keyboard.just_pressed(KeyCode::Z) && keyboard.pressed(KeyCode::LControl) { if keyboard.just_pressed(KeyCode::Z) && keyboard.pressed(KeyCode::LControl) {
let item = if keyboard.pressed(KeyCode::LShift) { stack.unpop() } else { stack.pop() }; let item = if keyboard.pressed(KeyCode::LShift) { stack.unpop() } else { stack.pop() };
if let Some(item) = item { if let Some(item) = item {
match item { match item {
UndoItem::Base => bevy::log::error!("POP/UNPOP: Got UndoItem::Base as a result!"), 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"); bevy::log::error!("NOT IMPLEMENTED : CreateShape");
}, },
UndoItem::DeleteShape { UndoItem::DeleteShape {
sd, sd: _,
transform, transform: _,
} => { } => {
bevy::log::error!("NOT IMPLEMENTED : DeleteShape"); bevy::log::error!("NOT IMPLEMENTED : DeleteShape");
}, },
@ -102,7 +103,33 @@ pub fn undo_redo_sys(
*to = t; *to = t;
}, },
UndoItem::MoveDot { shape_data, dot, from, to } => { 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 } => { UndoItem::Rotate { entity, from, to } => {
if let Ok(mut t) = transforms.get_mut(*entity) { if let Ok(mut t) = transforms.get_mut(*entity) {