grow board margins when placing near but inside the edge

This commit is contained in:
Crispy 2024-10-10 17:17:32 +02:00
parent bc4cd5ccc9
commit dee52fa2fd
2 changed files with 16 additions and 8 deletions

View file

@ -25,6 +25,7 @@ const HEADER_HEIGHT: i32 = 40;
const FOOTER_HEIGHT: i32 = 95; const FOOTER_HEIGHT: i32 = 95;
const SIDEBAR_WIDTH: i32 = 200 + 32 * 2 + 5 * 4; const SIDEBAR_WIDTH: i32 = 200 + 32 * 2 + 5 * 4;
const MAX_ZOOM_IN: i32 = 3; const MAX_ZOOM_IN: i32 = 3;
const BOARD_MARGIN: isize = 3;
#[derive(Debug)] #[derive(Debug)]
pub struct Editor { pub struct Editor {
@ -285,7 +286,7 @@ impl Editor {
self.active_tool = Tool::Blueprint; 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 tile_size = (16 << self.zoom) as f32;
let (x, y) = self.source_board.grow_to_include(*pos); let (x, y) = self.source_board.grow_to_include(*pos);
if x != 0 || y != 0 { if x != 0 || y != 0 {
@ -311,7 +312,14 @@ impl Editor {
fn set_tile(&mut self, mut pos: Pos, tile: Tile) { fn set_tile(&mut self, mut pos: Pos, tile: Tile) {
let tile_size = (16 << self.zoom) as f32; 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); 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();
@ -876,8 +884,8 @@ impl Editor {
if let Some(bp) = self.blueprints.get(self.selected_blueprint) { if let Some(bp) = self.blueprints.get(self.selected_blueprint) {
let board = bp.get_board().unwrap().clone(); let board = bp.get_board().unwrap().clone();
let mut pos = pos; let mut pos = pos;
self.grow_board_and_move_view(&mut pos); self.grow_board_and_update_view(&mut pos);
self.grow_board_and_move_view( self.grow_board_and_update_view(
&mut (pos + (board.width() - 1, board.height() - 1).into()), &mut (pos + (board.width() - 1, board.height() - 1).into()),
); );
for x in 0..board.width() { for x in 0..board.width() {

View file

@ -120,7 +120,7 @@ impl Board {
let mut offset_x = 0; let mut offset_x = 0;
let mut offset_y = 0; let mut offset_y = 0;
if p.x < 0 { if p.x < 0 {
let len = p.x.unsigned_abs() + 2; let len = p.x.unsigned_abs();
for row in &mut self.rows { for row in &mut self.rows {
let mut new_row = vec![Tile::Blank; len]; let mut new_row = vec![Tile::Blank; len];
new_row.append(row); new_row.append(row);
@ -129,7 +129,7 @@ impl Board {
offset_x = len; offset_x = len;
self.width += len; self.width += len;
} else if p.x as usize >= self.width { } 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 { for row in &mut self.rows {
row.resize(new_width, Tile::Blank); row.resize(new_width, Tile::Blank);
} }
@ -137,14 +137,14 @@ impl Board {
} }
if p.y < 0 { 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]; let mut new_rows = vec![vec![Tile::Blank; self.width]; len];
new_rows.append(&mut self.rows); new_rows.append(&mut self.rows);
self.rows = new_rows; self.rows = new_rows;
offset_y = len; offset_y = len;
self.height += len; self.height += len;
} else if p.y as usize >= self.height { } 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.rows.resize(new_height, vec![Tile::Blank; self.width]);
self.height = new_height; self.height = new_height;
} }