From 092a7b70ff769239673cf98f92b8e757e20de6ad Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Tue, 10 Dec 2024 21:59:04 +0100 Subject: [PATCH] reduce magic number constants --- src/editor.rs | 93 +++++++++++++++++++++++++-------------------------- 1 file changed, 45 insertions(+), 48 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index c104217..f4ad52b 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -24,9 +24,13 @@ use crate::{ userdata_dir, Scroll, Textures, }; +const TILE_TEXTURE_SIZE: usize = 16; const HEADER_HEIGHT: i32 = 40; const FOOTER_HEIGHT: i32 = 95; const SIDEBAR_WIDTH: i32 = 200 + 32 * 2 + 5 * 4; +const POPUP_WIDTH: i32 = 320; +const POPUP_HEIGHT: i32 = 165; + const MAX_ZOOM_IN: i32 = 3; const BOARD_MARGIN: PosInt = 3; const MAX_SPEED_POWER: u8 = 16; @@ -166,7 +170,7 @@ impl Editor { } fn pos_to_screen(&self, pos: Vector2) -> Vector2 { - pos * (16 << self.zoom) as f32 + self.view_offset + pos * (TILE_TEXTURE_SIZE << self.zoom) as f32 + self.view_offset } fn start_sim(&mut self) { @@ -234,7 +238,7 @@ impl Editor { } pub fn center_view(&mut self, d: &RaylibHandle) { - let tile_size = (16 << self.zoom) as f32; + let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32; let tile_x = self.source_board.width() as f32 / 2. * tile_size; let tile_y = self.source_board.height() as f32 / 2. * tile_size; let screen_x = d.get_screen_width() as f32 / 2.; @@ -244,11 +248,11 @@ impl Editor { } fn change_zoom_level(&mut self, d: &RaylibHandle, delta: i32) { - let tile_size = (16 << self.zoom) as f32; + let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32; let mouse_pos = d.get_mouse_position(); let tile_pos_of_mouse = (mouse_pos - self.view_offset) / tile_size; self.zoom += delta; - let tile_size = (16 << self.zoom) as f32; + let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32; self.view_offset = mouse_pos - tile_pos_of_mouse * tile_size; self.view_offset.x = self.view_offset.x.floor(); self.view_offset.y = self.view_offset.y.floor(); @@ -288,7 +292,7 @@ impl Editor { } fn grow_board_and_update_view(&mut self, pos: &mut Pos) { - let tile_size = (16 << self.zoom) as f32; + let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32; let (x, y) = self.source_board.grow_to_include(*pos); if x != 0 || y != 0 { self.view_offset.x -= x as f32 * tile_size; @@ -315,7 +319,7 @@ impl Editor { } fn set_tile(&mut self, mut pos: Pos, tile: Tile) { - let tile_size = (16 << self.zoom) as f32; + let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32; pos.x -= BOARD_MARGIN; pos.y -= BOARD_MARGIN; self.grow_board_and_update_view(&mut pos); @@ -443,7 +447,7 @@ impl Editor { d.clear_background(Color::new(64, 64, 64, 255)); if self.draw_overlay { - let tile_size = (16 << self.zoom) as f32; + let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32; let grid_spill_x = (self.view_offset.x).rem(tile_size) - tile_size; let grid_spill_y = (self.view_offset.y).rem(tile_size) - tile_size; d.gui_grid( @@ -517,48 +521,41 @@ impl Editor { } } - if self.popup == EndPopup::Success { - let width = 320; - let height = 165; - let x = d.get_screen_width() / 2 - width / 2; - let y = d.get_screen_height() / 2 - height / 2; - d.draw_rectangle(x, y, width, height, BG_DARK); - d.draw_text("Level Complete!", x + 45, y + 10, 30, Color::LIME); - if let Some(score) = &self.score { - d.draw_text("cycles", x + 15, y + 45, 20, Color::WHITE); - draw_usize(d, textures, score.cycles, x + 10, y + 70, 9, 2); - d.draw_text("tiles", x + 215, y + 45, 20, Color::WHITE); - draw_usize(d, textures, score.tiles, x + 210, y + 70, 5, 2); - } - if simple_button(d, x + 10, y + 110, 140, 45) { - self.popup = EndPopup::Dismissed; - } - d.draw_text("continue\nediting", x + 15, y + 115, 20, Color::WHITE); + if matches!(self.popup, EndPopup::Success | EndPopup::Failure) { + let x = d.get_screen_width() / 2 - POPUP_WIDTH / 2; + let y = d.get_screen_height() / 2 - POPUP_HEIGHT / 2; + d.draw_rectangle(x, y, POPUP_WIDTH, POPUP_HEIGHT, BG_DARK); + if self.popup == EndPopup::Success { + d.draw_text("Level Complete!", x + 45, y + 10, 30, Color::LIME); + if let Some(score) = &self.score { + d.draw_text("cycles", x + 15, y + 45, 20, Color::WHITE); + draw_usize(d, textures, score.cycles, x + 10, y + 70, 9, 2); + d.draw_text("tiles", x + 215, y + 45, 20, Color::WHITE); + draw_usize(d, textures, score.tiles, x + 210, y + 70, 5, 2); + } + if simple_button(d, x + 10, y + 110, 140, 45) { + self.popup = EndPopup::Dismissed; + } + d.draw_text("continue\nediting", x + 15, y + 115, 20, Color::WHITE); - if simple_button(d, x + width / 2 + 5, y + 110, 140, 45) { - self.exit_state = ExitState::ExitAndSave; + if simple_button(d, x + POPUP_WIDTH / 2 + 5, y + 110, 140, 45) { + self.exit_state = ExitState::ExitAndSave; + } + d.draw_text( + "return to\nlevel list", + x + POPUP_WIDTH / 2 + 10, + y + 115, + 20, + Color::WHITE, + ); + } else { + d.draw_text("Level Failed!", x + 45, y + 10, 30, Color::RED); + d.draw_text("incorrect output", x + 45, y + 45, 20, Color::WHITE); + if simple_button(d, x + 10, y + 110, 300, 25) { + self.popup = EndPopup::Dismissed; + } + d.draw_text("ok :(", x + 15, y + 115, 20, Color::WHITE); } - d.draw_text( - "return to\nlevel list", - x + width / 2 + 10, - y + 115, - 20, - Color::WHITE, - ); - } - - if self.popup == EndPopup::Failure { - let width = 320; - let height = 165; - let x = d.get_screen_width() / 2 - width / 2; - let y = d.get_screen_height() / 2 - height / 2; - d.draw_rectangle(x, y, width, height, BG_DARK); - d.draw_text("Level Failed!", x + 45, y + 10, 30, Color::RED); - d.draw_text("incorrect output", x + 45, y + 45, 20, Color::WHITE); - if simple_button(d, x + 10, y + 110, 300, 25) { - self.popup = EndPopup::Dismissed; - } - d.draw_text("ok :(", x + 15, y + 115, 20, Color::WHITE); } } @@ -904,7 +901,7 @@ impl Editor { let mouse_pos = d.get_mouse_position(); let scroll_delta = d.get_mouse_wheel_move(); let tile_scale = (1 << self.zoom) as f32; - let tile_size = 16 << self.zoom; + let tile_size = TILE_TEXTURE_SIZE << self.zoom; if self.sim_state == SimState::Editing { if let Some(board) = &self.pasting_board { if d.is_key_pressed(KeyboardKey::KEY_ESCAPE) {