From 1257388168ee325fd481c12456b05c4e6f128f6c Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 5 Oct 2024 15:19:27 +0200 Subject: [PATCH] toolbar gui parts --- assets/eraser.png | Bin 0 -> 248 bytes src/main.rs | 61 ++++++++++++++++++++++++++++++++++++++-------- src/util.rs | 44 ++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 assets/eraser.png diff --git a/assets/eraser.png b/assets/eraser.png new file mode 100644 index 0000000000000000000000000000000000000000..3c9e1af628118a3fc519858e06c9ab30b0e33d00 GIT binary patch literal 248 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%``#oJ8Lo9mFPBi3eG7xF2kNn6K zcZ{VhO}H;8Z3hdxLSagt(;{wRX729I>;|cIdi$2OhF?5#uzq3Y@7jL(hW-7T-c{xM zezV;ZKl-XJz`=!up*VEyxtNI>2{(AV+13`m*|ou;$@01hD}%p4E!X_nZg;k;eQ!SM z)%juJy1g&M1b&5X47&Fuc;0#~cZLrysy}|+QOvQ8Y1)oOj<0v-p_LM4U>yZTw`EZgI+nKH{p0=>fE>FVdQ&MBb@02g;)AOHXW literal 0 HcmV?d00001 diff --git a/src/main.rs b/src/main.rs index 9520b2d..9cea17d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -41,6 +41,19 @@ enum SimState { Stepping, } +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")) + .unwrap(); + textures.insert(name.to_string(), texture); + } + } +} + fn main() { let (mut rl, thread) = raylib::init() .resizable() @@ -50,16 +63,9 @@ fn main() { rl.set_exit_key(None); let mut textures: HashMap = HashMap::new(); - for d in read_dir("assets/tiles").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!("assets/tiles/{name}.png")) - .unwrap(); - textures.insert(name.to_string(), texture); - } - } + load_textures_from("assets", &mut rl, &thread, &mut textures); + load_textures_from("assets/tiles", &mut rl, &thread, &mut textures); + let mut game = Game::new_sandbox(); let board = parse(&read_to_string("boards/adder.mbl").unwrap()); game.load_board(board); @@ -210,5 +216,40 @@ impl Game { self.machine.set_input(input_text.into_bytes()); } + let mut tool_button = |(row, col): (i32, i32), texture: &str, tool_option: Tool| { + let border = 4.; + let gap = 2.; + let bound_offset = 32. + gap * 2. + border * 2.; + texture_button( + d, + Vector2 { + x: 300. + col as f32 * bound_offset, + y: footer_top + 5. + row as f32 * bound_offset, + }, + textures.get(texture), + tool_option, + &mut self.active_tool, + 32., + border, + ); + }; + tool_button((0, -1), "", Tool::None); + tool_button((1, -1), "eraser", Tool::SetTile(tile_to_char(' '))); + + tool_button((0, 0), "block", Tool::SetTile(tile_to_char('#'))); + tool_button((0, 1), "bag_off", Tool::SetTile(tile_to_char('B'))); + tool_button((0, 2), "trigger_off", Tool::SetTile(tile_to_char('*'))); + + tool_button( + (1, 0), + "wire_horizontal_off", + Tool::SetTile(tile_to_char('-')), + ); + tool_button( + (1, 1), + "wire_vertical_off", + Tool::SetTile(tile_to_char('|')), + ); + tool_button((1, 2), "wire_cross_off", Tool::SetTile(tile_to_char('+'))); } } diff --git a/src/util.rs b/src/util.rs index 4eb69e5..74c02d5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -18,7 +18,7 @@ pub fn text_input( text, bounds.x as i32 + 4, bounds.y as i32 + 4, - 10, + 20, Color::WHITE, ); let mouse_pos = d.get_mouse_position(); @@ -50,6 +50,48 @@ pub fn text_input( changed } +pub fn texture_button( + d: &mut RaylibDrawHandle, + pos: Vector2, + texture: Option<&Texture2D>, + option: T, + current: &mut T, + tex_size: f32, + border: f32, + // tooltip +) where + T: PartialEq, +{ + let color = if &option == current { + Color::DARKCYAN + } else { + Color::GRAY + }; + let bounds = Rectangle { + x: pos.x, + y: pos.y, + width: tex_size + border * 2., + height: tex_size + border * 2., + }; + d.draw_rectangle_rec(bounds, color); + if let Some(texture) = texture { + d.draw_texture_ex( + texture, + pos + Vector2::new(border, border), + 0., + tex_size / texture.width as f32, + Color::WHITE, + ); + } + + let mouse_pos = d.get_mouse_position(); + if d.is_mouse_button_pressed(MouseButton::MOUSE_BUTTON_LEFT) + && bounds.check_collision_point_rec(mouse_pos) + { + *current = option; + } +} + pub fn shrink_rec(rec: Rectangle, a: f32) -> Rectangle { Rectangle { x: rec.x + a,