prettify marble direction and value rendering
This commit is contained in:
parent
23ac416c7e
commit
27ff77f133
9 changed files with 65 additions and 28 deletions
BIN
assets/digits_small.png
Normal file
BIN
assets/digits_small.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 167 B |
BIN
assets/direction_down.png
Normal file
BIN
assets/direction_down.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 120 B |
BIN
assets/direction_left.png
Normal file
BIN
assets/direction_left.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 128 B |
BIN
assets/direction_right.png
Normal file
BIN
assets/direction_right.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 127 B |
BIN
assets/direction_up.png
Normal file
BIN
assets/direction_up.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 117 B |
|
@ -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(),
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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 {
|
||||
|
|
30
src/util.rs
30
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,
|
||||
|
|
Loading…
Reference in a new issue