show speed number and slider

This commit is contained in:
Crispy 2024-10-07 14:16:50 +02:00
parent 038bf365c4
commit 92d540c29b
3 changed files with 38 additions and 5 deletions

View file

@ -10,7 +10,7 @@ use crate::{
tile::{Direction, GateType, MathOp, MirrorType, PTile, Tile, WireType},
Machine,
},
simple_button,
simple_button, slider,
solution::{Score, Solution},
text_input, texture_option_button, Textures,
};
@ -35,7 +35,7 @@ pub struct Editor {
tool_menu_mirror: MirrorType,
tool_menu_wire: WireType,
input_text_selected: bool,
sim_speed: f32,
sim_speed: u8,
time_since_step: f32,
exit_state: ExitState,
exit_menu: bool,
@ -91,7 +91,7 @@ impl Editor {
output_as_text: false,
input_as_text: false,
input_text_selected: false,
sim_speed: 8.,
sim_speed: 8,
time_since_step: 0.,
tool_menu_math: MathOp::Add,
tool_menu_gate: GateType::Equal,
@ -217,7 +217,7 @@ impl Editor {
}
if self.sim_state == SimState::Running {
self.time_since_step += rl.get_frame_time();
if self.time_since_step > 1. / self.sim_speed {
if self.time_since_step > 1. / self.sim_speed as f32 {
self.time_since_step = 0.;
self.step();
}
@ -419,7 +419,11 @@ impl Editor {
}
draw_scaled_texture(d, textures.get("step"), 332, 4, 2.);
draw_usize(d, textures, self.machine.step_count(), 372, 4, 5, 2);
d.draw_text("speed", 368, 6, 10, Color::WHITE);
draw_usize(d, textures, self.sim_speed as usize, 398, 4, 2, 1);
slider(d, &mut self.sim_speed, 1, 33, 368, 24, 48, 12);
draw_usize(d, textures, self.machine.step_count(), 420, 4, 5, 2);
let mut input_text = String::from_utf8_lossy(self.machine.input()).to_string();
let width = d.get_screen_width();

View file

@ -189,6 +189,7 @@ impl Game {
if simple_button(d, level_list_width + 10, solution_y, entry_width, 30) {
let n = solutions.len();
self.selected_solution = solutions.len();
solutions.push(Solution::new(level, n));
}
d.draw_text(

View file

@ -215,6 +215,34 @@ pub fn draw_usize(
}
}
pub fn slider(
d: &mut RaylibDrawHandle,
value: &mut u8,
min: u8,
max: u8,
x: i32,
y: i32,
width: i32,
height: i32,
) -> bool {
// the +1 makes the lowest state look slightly filled and the max state fully filled
let percent = (*value - min + 1) as f32 / (max - min) as f32;
d.draw_rectangle(x, y, width, height, Color::DIMGRAY);
d.draw_rectangle(x, y, (width as f32 * percent) as i32, height, Color::CYAN);
let mouse_pos = d.get_mouse_position();
let bounds = Rectangle::new(x as f32, y as f32, width as f32, height as f32);
if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT)
&& bounds.check_collision_point_rec(mouse_pos)
{
let percent = (mouse_pos.x - bounds.x) / bounds.width;
let new_value = min + (percent * (max - min) as f32) as u8;
if *value != new_value {
*value = new_value
}
}
false
}
pub fn userdata_dir() -> PathBuf {
PathBuf::from("user")
}