add undo/redo for single tile placement/erasing
This commit is contained in:
parent
6d8bfa03b0
commit
5b6113780a
2 changed files with 144 additions and 22 deletions
|
@ -13,6 +13,14 @@ pub struct Board {
|
|||
height: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ResizeDeltas {
|
||||
pub x_pos: usize,
|
||||
pub x_neg: usize,
|
||||
pub y_pos: usize,
|
||||
pub y_neg: usize,
|
||||
}
|
||||
|
||||
impl Board {
|
||||
pub fn parse(source: &str) -> Self {
|
||||
let mut rows = Vec::new();
|
||||
|
@ -226,6 +234,34 @@ impl Board {
|
|||
(offset_x, offset_y)
|
||||
}
|
||||
|
||||
pub fn grow(&mut self, deltas: &ResizeDeltas) {
|
||||
// dbg!(&deltas);
|
||||
let new_width = self.width + deltas.x_neg + deltas.x_pos;
|
||||
let new_height = self.height + deltas.y_neg + deltas.y_pos;
|
||||
let mut new_board = Board::new_empty(new_width, new_height);
|
||||
for x in 0..self.width {
|
||||
for y in 0..self.height {
|
||||
let tile = self.rows[y][x];
|
||||
new_board.rows[y + deltas.y_neg][x + deltas.x_neg] = tile;
|
||||
}
|
||||
}
|
||||
*self = new_board;
|
||||
}
|
||||
|
||||
pub fn shrink(&mut self, deltas: &ResizeDeltas) {
|
||||
// dbg!(&deltas);
|
||||
let new_width = self.width - deltas.x_neg - deltas.x_pos;
|
||||
let new_height = self.height - deltas.y_neg - deltas.y_pos;
|
||||
let mut new_board = Board::new_empty(new_width, new_height);
|
||||
for x in 0..new_width {
|
||||
for y in 0..new_height {
|
||||
let tile = self.rows[y + deltas.y_neg][x + deltas.x_neg];
|
||||
new_board.rows[y][x] = tile;
|
||||
}
|
||||
}
|
||||
*self = new_board;
|
||||
}
|
||||
|
||||
pub fn width(&self) -> usize {
|
||||
self.width
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue