trim board size to same margin as when expanding

This commit is contained in:
Crispy 2024-10-10 17:20:07 +02:00
parent dee52fa2fd
commit 51f14014c5
2 changed files with 6 additions and 6 deletions

View file

@ -322,7 +322,7 @@ impl Editor {
pos.y -= BOARD_MARGIN; pos.y -= BOARD_MARGIN;
self.source_board.set(pos, tile); self.source_board.set(pos, tile);
if tile.is_blank() { if tile.is_blank() {
let (x, y) = self.source_board.trim_size(); let (x, y) = self.source_board.trim_size(BOARD_MARGIN as usize);
self.view_offset.x += x as f32 * tile_size; self.view_offset.x += x as f32 * tile_size;
self.view_offset.y += y as f32 * tile_size; self.view_offset.y += y as f32 * tile_size;
} }

View file

@ -151,7 +151,7 @@ impl Board {
(offset_x as isize, offset_y as isize) (offset_x as isize, offset_y as isize)
} }
pub fn trim_size(&mut self) -> (usize, usize) { pub fn trim_size(&mut self, margin: usize) -> (usize, usize) {
let (offset_x, offset_y); let (offset_x, offset_y);
// top // top
{ {
@ -159,7 +159,7 @@ impl Board {
while n < self.height && self.rows[n].iter().all(Tile::is_blank) { while n < self.height && self.rows[n].iter().all(Tile::is_blank) {
n += 1; n += 1;
} }
let trim_top = n.saturating_sub(2); let trim_top = n.saturating_sub(margin);
for _ in 0..trim_top { for _ in 0..trim_top {
self.rows.remove(0); self.rows.remove(0);
} }
@ -172,7 +172,7 @@ impl Board {
while n < self.height && self.rows[self.height - n - 1].iter().all(Tile::is_blank) { while n < self.height && self.rows[self.height - n - 1].iter().all(Tile::is_blank) {
n += 1; n += 1;
} }
let trim_bottom = n.saturating_sub(2); let trim_bottom = n.saturating_sub(margin);
for _ in 0..trim_bottom { for _ in 0..trim_bottom {
self.rows.pop(); self.rows.pop();
} }
@ -184,7 +184,7 @@ impl Board {
while n < self.width && self.rows.iter().all(|row| row[n].is_blank()) { while n < self.width && self.rows.iter().all(|row| row[n].is_blank()) {
n += 1; n += 1;
} }
let trim_left = n.saturating_sub(2); let trim_left = n.saturating_sub(margin);
for row in &mut self.rows { for row in &mut self.rows {
for _ in 0..trim_left { for _ in 0..trim_left {
row.remove(0); row.remove(0);
@ -199,7 +199,7 @@ impl Board {
while n < self.width && self.rows.iter().all(|r| r[self.width - n - 1].is_blank()) { while n < self.width && self.rows.iter().all(|r| r[self.width - n - 1].is_blank()) {
n += 1; n += 1;
} }
let trim_right = n.saturating_sub(2); let trim_right = n.saturating_sub(margin);
for row in &mut self.rows { for row in &mut self.rows {
for _ in 0..trim_right { for _ in 0..trim_right {
row.pop(); row.pop();