diff --git a/src/editor.rs b/src/editor.rs index eb83436..fe94c67 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -57,6 +57,7 @@ pub struct Editor { exit_menu: bool, total_steps: usize, popup: Popup, + dismissed_end: bool, score: Option, tooltip: Tooltip, mouse: MouseInput, @@ -88,7 +89,6 @@ enum Popup { Failure, LevelInfo, PauseMenu, - Dismissed, } #[derive(Debug, Clone)] @@ -165,6 +165,7 @@ impl Editor { exit_state: ExitState::Dont, exit_menu: false, popup: Popup::None, + dismissed_end: false, info_text, score: solution.score, total_steps: 0, @@ -270,6 +271,8 @@ impl Editor { self.max_step_time = 0; self.total_steps = 0; self.start_time = Instant::now(); + self.popup = Popup::None; + self.dismissed_end = false; if !self.level.is_sandbox() { self.stage = Some(0); } @@ -300,11 +303,12 @@ impl Editor { self.machine.step(); if let Some(i) = self.stage { - if self.popup != Popup::None { - self.popup = Popup::Dismissed; + if matches!(self.popup, Popup::Failure | Popup::Success) { + self.popup = Popup::None; + self.dismissed_end = true; } let stage = &self.level.stages()[i]; - if self.popup == Popup::None { + if self.popup == Popup::None && !self.dismissed_end { if stage.output().as_bytes() == self.machine.output() { if i + 1 < self.level.stages().len() { self.stage = Some(i + 1); @@ -445,21 +449,21 @@ impl Editor { pub fn update(&mut self, rl: &RaylibHandle) { self.tooltip.init_frame(rl); self.mouse = MouseInput::get(rl); - if !self.popup.is_hidden() { + if self.popup != Popup::None { self.mouse.clear(); } if rl.is_key_pressed(KeyboardKey::KEY_ESCAPE) { - if self.popup.is_hidden() { - self.sim_state = SimState::Editing; - self.popup = Popup::None; - } self.popup = match self.popup { - Popup::Success | Popup::Failure => Popup::Dismissed, + Popup::Success | Popup::Failure => { + self.dismissed_end = true; + Popup::None + } + Popup::None => Popup::PauseMenu, _ => Popup::None, }; } - if self.sim_state == SimState::Running && self.popup.is_hidden() { + if self.sim_state == SimState::Running && self.popup == Popup::None { self.time_since_step += rl.get_frame_time(); let step_size = 1. / (1 << self.sim_speed) as f32; let mut steps_taken = 0; @@ -592,7 +596,7 @@ impl Editor { self.mouse = MouseInput::get(d); - if !self.popup.is_hidden() { + if self.popup != Popup::None { self.tooltip.reset(); d.draw_rectangle( 0, @@ -614,7 +618,14 @@ impl Editor { self.info_text.update_width(d, bounds.width as i32 - 20); self.info_text.draw(d, 110, 140); } - _ => (), + Popup::PauseMenu => { + let bounds = screen_centered_rect(d, 300, 400); + d.draw_rectangle_rec(bounds, BG_DARK); + let x = bounds.x as i32; + let y = bounds.y as i32; + d.draw_text("Menu", x + 5, y+5, 30, Color::LIGHTBLUE); + } + Popup::None => (), } self.tooltip.draw(d); @@ -686,9 +697,10 @@ impl Editor { } fn draw_end_popup(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) { - let x = d.get_screen_width() / 2 - END_POPUP_WIDTH / 2; - let y = d.get_screen_height() / 2 - END_POPUP_HEIGHT / 2; - d.draw_rectangle(x, y, END_POPUP_WIDTH, END_POPUP_HEIGHT, BG_DARK); + let bounds = screen_centered_rect(d, END_POPUP_WIDTH, END_POPUP_HEIGHT); + let x = bounds.x as i32; + let y = bounds.y as i32; + d.draw_rectangle_rec(bounds, BG_DARK); if self.popup == Popup::Success { d.draw_text("Level Complete!", x + 45, y + 10, 30, Color::LIME); if let Some(score) = &self.score { @@ -698,7 +710,8 @@ impl Editor { draw_usize(d, textures, score.tiles, x + 210, y + 70, 5, 2); } if simple_button(d, &self.mouse, x + 10, y + 110, 140, 45) { - self.popup = Popup::Dismissed; + self.popup = Popup::None; + self.dismissed_end = true; } d.draw_text("continue\nediting", x + 15, y + 115, 20, Color::WHITE); @@ -723,7 +736,8 @@ impl Editor { d.draw_text("Level Failed!", x + 45, y + 10, 30, Color::RED); d.draw_text("incorrect output", x + 45, y + 45, 20, Color::WHITE); if simple_button(d, &self.mouse, x + 10, y + 110, 300, 25) { - self.popup = Popup::Dismissed; + self.popup = Popup::None; + self.dismissed_end = true; } d.draw_text("ok :(", x + 15, y + 115, 20, Color::WHITE); } @@ -1397,9 +1411,3 @@ fn get_blueprints() -> Vec { blueprints.sort_by(|a, b| a.name.cmp(&b.name)); blueprints } - -impl Popup { - fn is_hidden(self) -> bool { - matches!(self, Popup::None | Popup::Dismissed) - } -} diff --git a/src/util.rs b/src/util.rs index de8cd9d..e5b6d87 100644 --- a/src/util.rs +++ b/src/util.rs @@ -51,6 +51,17 @@ pub fn get_free_id(items: &[T], id_fn: fn(&T) -> usize) -> usize { id } +pub fn screen_centered_rect(rl: &RaylibHandle, width: i32, height: i32) -> Rectangle { + let w = rl.get_screen_width(); + let h = rl.get_screen_height(); + Rectangle { + x: (w / 2 - width / 2) as f32, + y: (h / 2 - height / 2) as f32, + width: width as f32, + height: height as f32, + } +} + pub fn screen_centered_rect_dyn(rl: &RaylibHandle, margin_x: i32, margin_y: i32) -> Rectangle { let w = rl.get_screen_width(); let h = rl.get_screen_height();