From a0f11b60fb2d4882ee6e35da17725eb6fd0cf9ba Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 5 Oct 2024 15:34:58 +0200 Subject: [PATCH] basic tile drawing --- assets/selection.png | Bin 0 -> 116 bytes src/main.rs | 35 +++++++++++++++++++++++++++++++---- src/marble_engine/board.rs | 13 ++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 assets/selection.png diff --git a/assets/selection.png b/assets/selection.png new file mode 100644 index 0000000000000000000000000000000000000000..32e7b35bc723af32605e8eb85b13e432946ea2de GIT binary patch literal 116 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`)}AhoAr`&KfA&v!XwR(5kj9|N zruaIsrBLFrq{PXMGgM}8lsx3JE=@whG~o)v4#p=-+Ij O!{F)a=d#Wzp$P!gUm?2y literal 0 HcmV?d00001 diff --git a/src/main.rs b/src/main.rs index 9cea17d..79498c8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,13 +41,18 @@ enum SimState { Stepping, } -fn load_textures_from(folder: &str, rl: &mut RaylibHandle, thread:&RaylibThread,textures:&mut HashMap){ +fn load_textures_from( + folder: &str, + rl: &mut RaylibHandle, + thread: &RaylibThread, + textures: &mut HashMap, +) { for d in read_dir(folder).unwrap().flatten() { let path = d.path(); if path.is_file() { let name = path.file_stem().unwrap().to_string_lossy(); let texture = rl - .load_texture(&thread, &format!("{folder}/{name}.png")) + .load_texture(thread, &format!("{folder}/{name}.png")) .unwrap(); textures.insert(name.to_string(), texture); } @@ -233,8 +238,8 @@ impl Game { border, ); }; - tool_button((0, -1), "", Tool::None); - tool_button((1, -1), "eraser", Tool::SetTile(tile_to_char(' '))); + tool_button((0, -1), "eraser", Tool::SetTile(tile_to_char(' '))); + tool_button((1, -1), "", Tool::None); tool_button((0, 0), "block", Tool::SetTile(tile_to_char('#'))); tool_button((0, 1), "bag_off", Tool::SetTile(tile_to_char('B'))); @@ -251,5 +256,27 @@ impl Game { Tool::SetTile(tile_to_char('|')), ); tool_button((1, 2), "wire_cross_off", Tool::SetTile(tile_to_char('+'))); + + let mouse_pos = d.get_mouse_position(); + if self.sim_state == SimState::Editing && mouse_pos.y < footer_top { + let tile_pos = (mouse_pos - self.view_offset) / (16 << self.zoom) as f32; + let tile_pos = Vector2::new(tile_pos.x.floor(), tile_pos.y.floor()); + + let tile_screen_pos = tile_pos * (16 << self.zoom) as f32 + self.view_offset; + + if let Tool::SetTile(tile) = self.active_tool { + d.draw_texture_ex( + textures.get("selection").unwrap(), + tile_screen_pos, + 0., + (1 << self.zoom) as f32, + Color::new(255, 255, 255, 150), + ); + + if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) { + self.source_board.set(tile_pos.into(), tile) + } + } + } } } diff --git a/src/marble_engine/board.rs b/src/marble_engine/board.rs index 0a7c4a9..1b59de0 100644 --- a/src/marble_engine/board.rs +++ b/src/marble_engine/board.rs @@ -18,6 +18,15 @@ impl From<(usize, usize)> for Pos { } } +impl From for Pos { + fn from(vec: Vector2) -> Self { + Self { + x: vec.x as isize, + y: vec.y as isize, + } + } +} + #[derive(Debug, Clone)] pub struct Board { rows: Vec>, @@ -101,7 +110,9 @@ impl Board { if let Some(tile) = self.get((x, y).into()) { let px = x as i32 * tile_size + offset.x as i32 + tile_size / 2; let py = y as i32 * tile_size + offset.y as i32 + tile_size / 2; - let texture = textures.get(&tile.texture()).unwrap_or_else(||textures.get("missing").unwrap()); + let texture = textures + .get(&tile.texture()) + .unwrap_or_else(|| textures.get("missing").unwrap()); d.draw_texture_ex( texture, Vector2::new((px - tile_size / 2) as f32, (py - tile_size / 2) as f32),