dummy pause menu popup

This commit is contained in:
Crispy 2024-12-23 23:45:25 +01:00
parent 8863912179
commit cfadd8a0b6
2 changed files with 43 additions and 24 deletions

View file

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

View file

@ -51,6 +51,17 @@ pub fn get_free_id<T>(items: &[T], id_fn: fn(&T) -> usize) -> usize {
id 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 { pub fn screen_centered_rect_dyn(rl: &RaylibHandle, margin_x: i32, margin_y: i32) -> Rectangle {
let w = rl.get_screen_width(); let w = rl.get_screen_width();
let h = rl.get_screen_height(); let h = rl.get_screen_height();