From ba1f404250892e7fa0f416033d770c1c8069eb0c Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Mon, 16 Dec 2024 22:24:27 +0100 Subject: [PATCH] merge undo/redo logic for areas and single tiles --- README.md | 1 - src/editor.rs | 54 +++++--------------------------------- src/marble_engine/board.rs | 8 ++++++ 3 files changed, 14 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index d60eeba..439e700 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,6 @@ logic mostly like https://git.crispypin.cc/CrispyPin/marble ## todo -- undo/redo - more levels - make direct power (comparator -> machine) work, (needs storing power direction in machine tiles) - cut selections, copy to system clipboard diff --git a/src/editor.rs b/src/editor.rs index 3ad00da..4e9dbbc 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -76,7 +76,6 @@ pub struct Editor { #[derive(Debug, Clone)] enum Action { - SetTile(ResizeDeltas, Pos, Tile, Tile), SetArea(ResizeDeltas, Pos, Board, Board), } @@ -181,13 +180,8 @@ impl Editor { fn do_action(&mut self, action: Action) { match action { - Action::SetTile(deltas, pos, _old, new) => { - self.shift_view(deltas.x_neg as f32, deltas.y_neg as f32); - self.source_board.grow(&deltas); - self.source_board.set(pos, new); - } Action::SetArea(deltas, pos, _old, new) => { - self.shift_view(deltas.x_neg as f32, deltas.y_neg as f32); + self.shift_world(deltas.x_neg as f32, deltas.y_neg as f32); self.source_board.grow(&deltas); self.source_board.paste_board(pos, &new); } @@ -199,22 +193,17 @@ impl Editor { return; } self.undo_index -= 1; - let action = self.undo_history[self.undo_index].clone(); + let action = &self.undo_history[self.undo_index]; match action { - Action::SetTile(deltas, pos, old, _new) => { - self.source_board.set(pos, old); - self.source_board.shrink(&deltas); - self.shift_view(-(deltas.x_neg as f32), -(deltas.y_neg as f32)); - } Action::SetArea(deltas, pos, old, _new) => { - self.source_board.paste_board(pos, &old); + self.source_board.paste_board(*pos, &old); self.source_board.shrink(&deltas); - self.shift_view(-(deltas.x_neg as f32), -(deltas.y_neg as f32)); + self.shift_world(-(deltas.x_neg as f32), -(deltas.y_neg as f32)); } } } - fn shift_view(&mut self, x: f32, y: f32) { + fn shift_world(&mut self, x: f32, y: f32) { match &mut self.active_tool { Tool::SelectArea(Selection { area: Some((a, b)), @@ -410,38 +399,7 @@ impl Editor { } fn set_tile(&mut self, pos: Pos, tile: Tile) { - let old_tile = self.source_board.get_or_blank(pos); - if old_tile == tile { - return; - } - let width = self.source_board.width() as PosInt; - let height = self.source_board.height() as PosInt; - let resize = ResizeDeltas { - x_pos: if (pos.x + BOARD_MARGIN + 1) > width { - pos.x + BOARD_MARGIN + 1 - width - } else { - 0 - } as usize, - x_neg: if pos.x < BOARD_MARGIN { - BOARD_MARGIN - pos.x - } else { - 0 - } as usize, - y_pos: if (pos.y + BOARD_MARGIN + 1) > height { - pos.y + BOARD_MARGIN + 1 - height - } else { - 0 - } as usize, - y_neg: if pos.y < BOARD_MARGIN { - BOARD_MARGIN - pos.y - } else { - 0 - } as usize, - }; - let mut pos = pos; - pos.x += resize.x_neg as PosInt; - pos.y += resize.y_neg as PosInt; - self.push_action(Action::SetTile(resize, pos, old_tile, tile)); + self.set_area(pos, Board::new_single(tile)); } pub fn update(&mut self, rl: &RaylibHandle) { diff --git a/src/marble_engine/board.rs b/src/marble_engine/board.rs index b7539de..ef70aba 100644 --- a/src/marble_engine/board.rs +++ b/src/marble_engine/board.rs @@ -61,6 +61,14 @@ impl Board { } } + pub fn new_single(tile: Tile) -> Self { + Self { + rows: vec![vec![tile]], + width: 1, + height: 1, + } + } + pub fn new(rows: Vec>) -> Self { Self { width: rows[0].len(),