diff --git a/src/editor.rs b/src/editor.rs index 24aa9e6..869a708 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -130,7 +130,10 @@ impl Editor { fn step(&mut self) { self.machine.step(); - if self.level.outputs() == self.machine.output() && self.complete_popup == Popup::Start { + if !self.level.outputs().is_empty() + && self.level.outputs() == self.machine.output() + && self.complete_popup == Popup::Start + { self.complete_popup = Popup::Visible; self.exit_state = ExitState::Save; self.sim_state = SimState::Stepping; @@ -190,6 +193,9 @@ impl Editor { fn set_tile(&mut self, pos: Pos, tile: Tile) { self.source_board.grow_to_include(pos); self.source_board.set(pos, tile); + if tile.is_blank() { + self.source_board.trim_size(); + } } pub fn update(&mut self, rl: &RaylibHandle) { diff --git a/src/marble_engine/board.rs b/src/marble_engine/board.rs index b3cc8ff..80ad250 100644 --- a/src/marble_engine/board.rs +++ b/src/marble_engine/board.rs @@ -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 }