diff --git a/src/main.rs b/src/main.rs index 3e87783..f683a9b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,4 @@ -use std::{ - collections::HashMap, - fs::{read_dir, read_to_string}, - ops::Rem, -}; +use std::{fs::read_to_string, ops::Rem}; use marble_engine::{ board::Board, @@ -54,24 +50,6 @@ 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() @@ -82,9 +60,9 @@ fn main() { rl.set_mouse_cursor(MouseCursor::MOUSE_CURSOR_CROSSHAIR); rl.set_exit_key(None); - let mut textures: HashMap = HashMap::new(); - load_textures_from("assets", &mut rl, &thread, &mut textures); - load_textures_from("assets/tiles", &mut rl, &thread, &mut textures); + let mut textures = Textures::default(); + textures.load_dir("assets", &mut rl, &thread); + textures.load_dir("assets/tiles", &mut rl, &thread); let mut game = Game::new_sandbox(); let board = parse(&read_to_string("boards/adder.mbl").unwrap()); @@ -213,7 +191,7 @@ impl Game { } } - fn draw_board(&self, d: &mut RaylibDrawHandle, textures: &HashMap) { + fn draw_board(&self, d: &mut RaylibDrawHandle, textures: &Textures) { if self.sim_state == SimState::Editing { self.source_board .draw(d, textures, self.view_offset, self.zoom); @@ -226,7 +204,7 @@ impl Game { } } - fn gui(&mut self, d: &mut RaylibDrawHandle, textures: &HashMap) { + fn gui(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) { self.draw_board(d, textures); let height = d.get_screen_height(); @@ -295,7 +273,7 @@ impl Game { ); }; tool_button((0, -1), "eraser", Tool::SetTile(tile_from_char(' '))); - tool_button((1, -1), "", Tool::None); + tool_button((1, -1), "transparent", Tool::None); tool_button((0, 0), "marble", Tool::SetTile(tile_from_char('o'))); tool_button((0, 1), "block", Tool::SetTile(tile_from_char('#'))); @@ -359,18 +337,15 @@ impl Game { t.texture() } } - Tool::Math => format!("{}_off",self.tool_menu_math.texture_name()), - Tool::Gate => format!("{}_off",self.tool_menu_gate.texture_name()), + Tool::Math => format!("{}_off", self.tool_menu_math.texture_name()), + Tool::Gate => format!("{}_off", self.tool_menu_gate.texture_name()), Tool::Arrow => self.tool_menu_arrow.arrow_texture_name().into(), Tool::Mirror => self.tool_menu_mirror.texture_name().into(), Tool::Number => todo!(), }; - let tex = textures - .get(&tex) - .unwrap_or_else(|| textures.get("missing").unwrap()); d.draw_texture_ex( - tex, + textures.get(&tex), tile_screen_pos, 0., (1 << self.zoom) as f32, diff --git a/src/marble_engine/board.rs b/src/marble_engine/board.rs index 1b59de0..7566725 100644 --- a/src/marble_engine/board.rs +++ b/src/marble_engine/board.rs @@ -1,4 +1,4 @@ -use std::collections::HashMap; +use crate::Textures; use super::tile::*; use raylib::prelude::*; @@ -97,22 +97,14 @@ impl Board { self.height } - pub fn draw( - &self, - d: &mut RaylibDrawHandle, - textures: &HashMap, - offset: Vector2, - zoom: i32, - ) { + pub fn draw(&self, d: &mut RaylibDrawHandle, textures: &Textures, offset: Vector2, zoom: i32) { let tile_size = 16 << zoom; for x in 0..self.width { for y in 0..self.height { 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()); d.draw_texture_ex( texture, Vector2::new((px - tile_size / 2) as f32, (py - tile_size / 2) as f32), diff --git a/src/util.rs b/src/util.rs index 74c02d5..ea54f63 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,33 @@ +use std::{collections::HashMap, fs::read_dir}; + use raylib::prelude::*; +#[derive(Default)] +pub struct Textures { + map: HashMap, +} + +impl Textures { + pub fn load_dir(&mut self, folder: &str, rl: &mut RaylibHandle, thread: &RaylibThread) { + 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(); + self.map.insert(name.to_string(), texture); + } + } + } + + pub fn get(&self, name: &str) -> &Texture2D { + self.map + .get(name) + .unwrap_or_else(|| self.map.get("missing").unwrap()) + } +} + pub fn text_input( d: &mut RaylibDrawHandle, bounds: Rectangle, @@ -53,7 +81,7 @@ pub fn text_input( pub fn texture_button( d: &mut RaylibDrawHandle, pos: Vector2, - texture: Option<&Texture2D>, + texture: &Texture2D, option: T, current: &mut T, tex_size: f32, @@ -74,15 +102,13 @@ pub fn texture_button( 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, - ); - } + 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)