From 23ac416c7ed69ff4a89217de962223dbbbdb116c Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sat, 12 Oct 2024 19:24:33 +0200 Subject: [PATCH] make sim handle speeds faster than framerate and change speed slider from 1-32 linear to 1-512 powers of two --- src/editor.rs | 28 +++++++++++++++++++--------- src/util.rs | 20 ++++++++++++-------- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 7f26c2e..0e04fae 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -109,7 +109,7 @@ impl Editor { input_text_selected: false, new_blueprint_name: String::new(), blueprint_name_selected: false, - sim_speed: 8, + sim_speed: 3, time_since_step: 0., tool_math: MathOp::Add, tool_gate: GateType::Equal, @@ -348,9 +348,17 @@ 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 as f32 { - self.time_since_step = 0.; - self.step(); + let step_size = 1. / (1 << self.sim_speed) as f32; + if self.time_since_step > step_size { + let step_count = (self.time_since_step / step_size) as u32; + for _ in 0..step_count { + if self.sim_state != SimState::Running { + // pause on level completion + break; + } + self.step(); + } + self.time_since_step -= step_count as f32 * step_size; } } if rl.is_key_pressed(KeyboardKey::KEY_SPACE) { @@ -371,9 +379,11 @@ impl Editor { } let mouse_pos = rl.get_mouse_position(); - if self.active_tool != Tool::Blueprint + if (self.active_tool != Tool::Blueprint || self.sim_state != SimState::Editing - || mouse_pos.x > SIDEBAR_WIDTH as f32 + || mouse_pos.x > SIDEBAR_WIDTH as f32) + && mouse_pos.y > HEADER_HEIGHT as f32 + && (mouse_pos.y as i32) < (rl.get_screen_height() - FOOTER_HEIGHT) { if rl.get_mouse_wheel_move() > 0. { self.zoom_in(rl); @@ -611,9 +621,9 @@ impl Editor { } draw_scaled_texture(d, textures.get("step"), 332, 4, 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); + d.draw_text("spd", 368, 6, 10, Color::WHITE); + draw_usize(d, textures, (1 << self.sim_speed) as usize, 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); diff --git a/src/util.rs b/src/util.rs index 4d7e498..5f0dca0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -229,18 +229,22 @@ pub fn slider( 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; + let percent = (*value - min + 1) as f32 / (max - min + 1) 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 + if bounds.check_collision_point_rec(mouse_pos) { + if d.is_mouse_button_down(MouseButton::MOUSE_BUTTON_LEFT) { + let percent = (mouse_pos.x - bounds.x) / bounds.width; + let new_value = min + (percent * (max - min + 1) as f32) as u8; + if *value != new_value { + *value = new_value + } + } else if d.get_mouse_wheel_move() > 0.5 && *value < max { + *value += 1; + } else if d.get_mouse_wheel_move() < -0.5 && *value > min { + *value -= 1; } } false