From dee52fa2fd47f99f743029c29ebd56f35c6a2e78 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Thu, 10 Oct 2024 17:17:32 +0200 Subject: [PATCH] grow board margins when placing near but inside the edge --- src/editor.rs | 16 ++++++++++++---- src/marble_engine/board.rs | 8 ++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index ab4cfb7..c27324a 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -25,6 +25,7 @@ const HEADER_HEIGHT: i32 = 40; const FOOTER_HEIGHT: i32 = 95; const SIDEBAR_WIDTH: i32 = 200 + 32 * 2 + 5 * 4; const MAX_ZOOM_IN: i32 = 3; +const BOARD_MARGIN: isize = 3; #[derive(Debug)] pub struct Editor { @@ -285,7 +286,7 @@ impl Editor { self.active_tool = Tool::Blueprint; } - fn grow_board_and_move_view(&mut self, pos: &mut Pos) { + fn grow_board_and_update_view(&mut self, pos: &mut Pos) { let tile_size = (16 << self.zoom) as f32; let (x, y) = self.source_board.grow_to_include(*pos); if x != 0 || y != 0 { @@ -311,7 +312,14 @@ impl Editor { fn set_tile(&mut self, mut pos: Pos, tile: Tile) { let tile_size = (16 << self.zoom) as f32; - self.grow_board_and_move_view(&mut pos); + pos.x -= BOARD_MARGIN; + pos.y -= BOARD_MARGIN; + self.grow_board_and_update_view(&mut pos); + pos.x += BOARD_MARGIN * 2; + pos.y += BOARD_MARGIN * 2; + self.grow_board_and_update_view(&mut pos); + pos.x -= BOARD_MARGIN; + pos.y -= BOARD_MARGIN; self.source_board.set(pos, tile); if tile.is_blank() { let (x, y) = self.source_board.trim_size(); @@ -876,8 +884,8 @@ impl Editor { if let Some(bp) = self.blueprints.get(self.selected_blueprint) { let board = bp.get_board().unwrap().clone(); let mut pos = pos; - self.grow_board_and_move_view(&mut pos); - self.grow_board_and_move_view( + self.grow_board_and_update_view(&mut pos); + self.grow_board_and_update_view( &mut (pos + (board.width() - 1, board.height() - 1).into()), ); for x in 0..board.width() { diff --git a/src/marble_engine/board.rs b/src/marble_engine/board.rs index aa7fa8f..04bf0df 100644 --- a/src/marble_engine/board.rs +++ b/src/marble_engine/board.rs @@ -120,7 +120,7 @@ impl Board { let mut offset_x = 0; let mut offset_y = 0; if p.x < 0 { - let len = p.x.unsigned_abs() + 2; + let len = p.x.unsigned_abs(); for row in &mut self.rows { let mut new_row = vec![Tile::Blank; len]; new_row.append(row); @@ -129,7 +129,7 @@ impl Board { offset_x = len; self.width += len; } else if p.x as usize >= self.width { - let new_width = p.x as usize + 3; + let new_width = p.x as usize + 1; for row in &mut self.rows { row.resize(new_width, Tile::Blank); } @@ -137,14 +137,14 @@ impl Board { } if p.y < 0 { - let len = p.y.unsigned_abs() + 2; + let len = p.y.unsigned_abs(); let mut new_rows = vec![vec![Tile::Blank; self.width]; len]; new_rows.append(&mut self.rows); self.rows = new_rows; offset_y = len; self.height += len; } else if p.y as usize >= self.height { - let new_height = p.y as usize + 3; + let new_height = p.y as usize + 1; self.rows.resize(new_height, vec![Tile::Blank; self.width]); self.height = new_height; }