diff --git a/assets/digits_small.png b/assets/digits_small.png new file mode 100644 index 0000000..a603378 Binary files /dev/null and b/assets/digits_small.png differ diff --git a/assets/direction_down.png b/assets/direction_down.png new file mode 100644 index 0000000..db6184f Binary files /dev/null and b/assets/direction_down.png differ diff --git a/assets/direction_left.png b/assets/direction_left.png new file mode 100644 index 0000000..4a46f80 Binary files /dev/null and b/assets/direction_left.png differ diff --git a/assets/direction_right.png b/assets/direction_right.png new file mode 100644 index 0000000..04ef45e Binary files /dev/null and b/assets/direction_right.png differ diff --git a/assets/direction_up.png b/assets/direction_up.png new file mode 100644 index 0000000..9b2f891 Binary files /dev/null and b/assets/direction_up.png differ diff --git a/src/editor.rs b/src/editor.rs index 0e04fae..08099c2 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -413,7 +413,7 @@ impl Editor { .board() .draw(d, textures, self.view_offset, self.zoom); self.machine - .draw_marble_values(d, self.view_offset, self.zoom); + .draw_marble_values(d, textures, self.view_offset, self.zoom); } } @@ -622,7 +622,7 @@ impl Editor { draw_scaled_texture(d, textures.get("step"), 332, 4, 2.); d.draw_text("spd", 368, 6, 10, Color::WHITE); - draw_usize(d, textures, (1 << self.sim_speed) as usize, 388, 4, 3, 1); + draw_usize(d, textures, 1 << self.sim_speed, 388, 4, 3, 1); slider(d, &mut self.sim_speed, 0, 9, 368, 24, 48, 12); draw_usize(d, textures, self.machine.step_count(), 420, 4, 5, 2); @@ -876,7 +876,7 @@ impl Editor { Tool::Math => format!("{}_off", self.tool_math.texture_name()), Tool::Gate => format!("{}_off", self.tool_gate.texture_name()), Tool::Wire => format!("{}_off", self.tool_wire.texture_name()), - Tool::Arrow => self.tool_arrow.arrow_texture_name().into(), + Tool::Arrow => self.tool_arrow.arrow_tile_texture_name().into(), Tool::Mirror => self.tool_mirror.texture_name().into(), Tool::Digits(_) => "selection".into(), Tool::SelectArea(_, false) => "area_full".into(), diff --git a/src/marble_engine.rs b/src/marble_engine.rs index b1a80cd..c5ad829 100644 --- a/src/marble_engine.rs +++ b/src/marble_engine.rs @@ -7,6 +7,8 @@ use board::Board; use pos::*; use tile::*; +use crate::{draw_usize_small, Textures}; + #[derive(Debug)] pub struct Machine { board: Board, @@ -66,35 +68,27 @@ impl Machine { self.input = bytes; } - pub fn draw_marble_values(&self, d: &mut RaylibDrawHandle, offset: Vector2, zoom: i32) { + pub fn draw_marble_values( + &self, + d: &mut RaylibDrawHandle, + textures: &Textures, + offset: Vector2, + zoom: i32, + ) { let tile_size = 16 << zoom; for marble in &self.marbles { let x = marble.x; let y = marble.y; if let Some(tile) = self.board.get(*marble) { - 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 px = x as i32 * tile_size + offset.x as i32; + let py = y as i32 * tile_size + offset.y as i32; if let Tile::Marble { value, dir } = tile { - let fontsize = (zoom + 1) * 10; - d.draw_text( - &format!("{value}"), - px - tile_size / 2 + 2, - py - tile_size / 2 + 2, - fontsize, - Color::MAGENTA, - ); - d.draw_text( - match dir { - Direction::Up => "^", - Direction::Down => "v", - Direction::Left => "<", - Direction::Right => ">", - }, - px - tile_size / 2 + 2, - py - tile_size / 2 + fontsize, - fontsize, - Color::MAGENTA, - ); + let scale = 1 << zoom; + let texture = textures.get(dir.arrow_texture_name()); + let pos = Vector2::new(px as f32, py as f32); + let faded = Color::new(255, 255, 255, 100); + d.draw_texture_ex(texture, pos, 0., scale as f32, faded); + draw_usize_small(d, textures, value as usize, px, py, scale); } } } @@ -111,6 +105,10 @@ impl Machine { } } + if self.marbles.is_empty() { + return; + } + #[derive(Debug, Clone, Copy)] enum Event { Stay, diff --git a/src/marble_engine/tile.rs b/src/marble_engine/tile.rs index 4209f47..1c9645f 100644 --- a/src/marble_engine/tile.rs +++ b/src/marble_engine/tile.rs @@ -164,7 +164,7 @@ impl Tile { Tile::Marble { value: _, dir: _ } => "marble", Tile::Digit(n) => return format!("tile_digit_{n}"), Tile::Mirror(mirror) => mirror.texture_name(), - Tile::Arrow(dir) => dir.arrow_texture_name(), + Tile::Arrow(dir) => dir.arrow_tile_texture_name(), Tile::Powerable(tile, state) => { let root = match tile { PTile::Trigger => "trigger", @@ -222,7 +222,7 @@ impl Direction { pos } - pub const fn arrow_texture_name(&self) -> &'static str { + pub const fn arrow_tile_texture_name(&self) -> &'static str { match self { Direction::Up => "arrow_up", Direction::Down => "arrow_down", @@ -230,6 +230,15 @@ impl Direction { Direction::Right => "arrow_right", } } + + pub const fn arrow_texture_name(&self) -> &'static str { + match self { + Direction::Up => "direction_up", + Direction::Down => "direction_down", + Direction::Left => "direction_left", + Direction::Right => "direction_right", + } + } } impl WireType { diff --git a/src/util.rs b/src/util.rs index 5f0dca0..1c05fe5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -218,6 +218,36 @@ pub fn draw_usize( } } +pub fn draw_usize_small( + d: &mut RaylibDrawHandle, + textures: &Textures, + mut num: usize, + mut x: i32, + y: i32, + scale: u8, +) { + const MAX_DIGITS: usize = 8; + let mut digits = [0u8; MAX_DIGITS]; + let mut i = 0; + while (num != 0 || i == 0) && i < MAX_DIGITS { + digits[MAX_DIGITS - i - 1] = (num % 10) as u8; + num /= 10; + i += 1; + } + let texture = textures.get("digits_small"); + for digit in (MAX_DIGITS - i)..MAX_DIGITS { + d.draw_texture_pro( + texture, + Rectangle::new(4. * digits[digit] as f32, 0., 4., 6.), + Rectangle::new(x as f32, y as f32, 4. * scale as f32, 6. * scale as f32), + Vector2::zero(), + 0., + Color::RED, + ); + x += 4 * scale as i32; + } +} + pub fn slider( d: &mut RaylibDrawHandle, value: &mut u8,