From 27ff77f1337b3538d79115661009e8e3c58e7fd8 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 12 Oct 2024 20:53:47 +0200 Subject: [PATCH] prettify marble direction and value rendering --- assets/digits_small.png | Bin 0 -> 167 bytes assets/direction_down.png | Bin 0 -> 120 bytes assets/direction_left.png | Bin 0 -> 128 bytes assets/direction_right.png | Bin 0 -> 127 bytes assets/direction_up.png | Bin 0 -> 117 bytes src/editor.rs | 6 ++--- src/marble_engine.rs | 44 ++++++++++++++++++------------------- src/marble_engine/tile.rs | 13 +++++++++-- src/util.rs | 30 +++++++++++++++++++++++++ 9 files changed, 65 insertions(+), 28 deletions(-) create mode 100644 assets/digits_small.png create mode 100644 assets/direction_down.png create mode 100644 assets/direction_left.png create mode 100644 assets/direction_right.png create mode 100644 assets/direction_up.png diff --git a/assets/digits_small.png b/assets/digits_small.png new file mode 100644 index 0000000000000000000000000000000000000000..a60337877466ea5997d942e55ce3c18b15c5de96 GIT binary patch literal 167 zcmeAS@N?(olHy`uVBq!ia0vp^8bHj(!3HEZM%>W`QhA;(jv*Qo&jvd39Z=w4e*Ay` zo!E>+9**VG;+k8yt}-4_x^wsJ83u*tJJ%Q3`v1|dFibZT_IR{`YxN8hFZ+kGtdC<# zyR8b23NQcXwEf1HV_i*`_L{wt*-`l8*hjzpmS0L7%N#6M>{uRk-M+A1oZoU^zWtRA S!ty|y89ZJ6T-G@yGywozm_Qo< literal 0 HcmV?d00001 diff --git a/assets/direction_down.png b/assets/direction_down.png new file mode 100644 index 0000000000000000000000000000000000000000..db6184f63ea56a6b7300b1be499a8b7f35d68640 GIT binary patch literal 120 zcmeAS@N?(olHy`uVBq!ia0vp^0wB!61|;P_|4#%`_MR?|Ar`&K2@>}n*o!;}KJ2nm z_3!}(w-mmG49(d|DJ`#)U8bv~aBNCj!}h?KCDz!Fd4pt=ud$}n*o!;}KHTAN z+~wrvwkB&M*P~6MvlS(nSmySmad5RyNUPC1$koB=qPECK>K+>pGXujHSNXr+|Jr;6 Pn#17f>gTe~DWM4f0bV02 literal 0 HcmV?d00001 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,