shrink board bounds when erasing

This commit is contained in:
Crispy 2024-10-07 12:44:15 +02:00
parent af66e68c5f
commit 28a54a231b
2 changed files with 71 additions and 1 deletions

View file

@ -204,6 +204,70 @@ impl Board {
}
}
pub fn trim_size(&mut self) {
{
let mut top_blanks = 0;
while top_blanks < self.height && self.rows[top_blanks].iter().all(Tile::is_blank) {
top_blanks += 1;
}
let trim_top = top_blanks.saturating_sub(2);
for _ in 0..trim_top {
self.rows.remove(0);
}
self.offset_y -= trim_top as isize;
self.height -= trim_top;
}
{
let mut bottom_blanks = 0;
while bottom_blanks < self.height
&& self.rows[self.height - bottom_blanks - 1]
.iter()
.all(Tile::is_blank)
{
bottom_blanks += 1;
}
let trim_bottom = bottom_blanks.saturating_sub(2);
for _ in 0..trim_bottom {
self.rows.pop();
}
self.height -= trim_bottom;
}
{
let mut left_blanks = 0;
while left_blanks < self.width
&& self.rows.iter().all(|row| row[left_blanks].is_blank())
{
left_blanks += 1;
}
let trim_left = left_blanks.saturating_sub(2);
for row in &mut self.rows {
for _ in 0..trim_left {
row.remove(0);
}
}
self.offset_x -= trim_left as isize;
self.width -= trim_left;
}
{
let mut right_blanks = 0;
while right_blanks < self.height
&& self
.rows
.iter()
.all(|row| row[self.width - right_blanks - 1].is_blank())
{
right_blanks += 1;
}
let trim_right = right_blanks.saturating_sub(2);
for row in &mut self.rows {
for _ in 0..trim_right {
row.pop();
}
}
self.width -= trim_right;
}
}
pub fn width(&self) -> usize {
self.width
}