prompt() helper fn, help screen with placeholder text

This commit is contained in:
Llywelwyn 2023-10-25 00:22:54 +01:00
parent e3a8c545fb
commit 6d4ab70c7b
3 changed files with 44 additions and 23 deletions

View file

@ -75,22 +75,6 @@ pub use main_menu::*;
mod inventory;
pub use inventory::*;
/// Gives a popup box with a message and a title, and waits for a keypress.
#[allow(unused)]
pub fn yes_no(ctx: &mut BTerm, question: String) -> Option<bool> {
ctx.print_color_centered(15, RGB::named(YELLOW), RGB::named(BLACK), question);
ctx.print_color_centered(17, RGB::named(CYAN), RGB::named(BLACK), "(y)es or (n)o");
match ctx.key {
None => None,
Some(key) =>
match key {
VirtualKeyCode::Y => Some(true),
VirtualKeyCode::N => Some(false),
_ => None,
}
}
}
pub fn draw_lerping_bar(
ctx: &mut BTerm,
sx: i32,
@ -1854,13 +1838,38 @@ pub enum YesNoResult {
No,
}
pub fn game_over(ctx: &mut App) -> YesNoResult {
/// Prompts for a YesNoResult with default keys of 'y' and 'n'.
pub fn yes_no(ctx: &mut App) -> YesNoResult {
prompt(ctx, None, None, false, false)
}
pub fn help_prompt(ctx: &mut App) -> YesNoResult {
prompt(ctx, Some((KeyCode::Slash, true)), Some((KeyCode::Escape, false)), false, false)
}
/// Prompts for a YesNoResult, where 'yes' and 'no' keys can either be specified,
/// or defaulted to 'y' and 'n'.
pub fn prompt(
ctx: &mut App,
yes: Option<(KeyCode, bool)>,
no: Option<(KeyCode, bool)>,
allow_esc: bool,
allow_enter: bool
) -> YesNoResult {
let (y, y_shift) = yes.unwrap_or((KeyCode::Y, false));
let (n, n_shift) = no.unwrap_or((KeyCode::N, false));
for keycode in &ctx.keyboard.pressed {
match *keycode {
KeyCode::N => {
match (keycode, ctx.keyboard.shift()) {
(k, s) if *k == y && s == y_shift => {
return YesNoResult::Yes;
}
(k, s) if *k == n && s == n_shift => {
return YesNoResult::No;
}
KeyCode::Y => {
(k, _s) if *k == KeyCode::Escape && allow_esc => {
return YesNoResult::No;
}
(k, _s) if *k == KeyCode::Return && allow_enter => {
return YesNoResult::Yes;
}
_ => {}

View file

@ -548,7 +548,7 @@ fn draw_bg(_ecs: &World, draw: &mut Draw, atlas: &HashMap<String, Texture>) {
draw_spritebox(sidebox, draw, atlas);
}
fn draw(app: &mut App, gfx: &mut Graphics, gs: &mut State) {
fn draw(_app: &mut App, gfx: &mut Graphics, gs: &mut State) {
let mut draw = gfx.create_draw();
draw.clear(Color::BLACK);
let mut log = false;
@ -631,6 +631,9 @@ fn draw(app: &mut App, gfx: &mut Graphics, gs: &mut State) {
RunState::ShowTargeting { .. } => {
corner_text("Targeting which tile? [0-9]/[YUHJKLBN]", &mut draw, &gs.font);
}
RunState::HelpScreen => {
corner_text("The help screen is a placeholder! [?]", &mut draw, &gs.font);
}
_ => {}
}
// TODO: Once the rest of drawing is finalised, this should be abstracted

View file

@ -473,7 +473,7 @@ impl State {
};
}
RunState::GameOver => {
let result = gui::game_over(ctx);
let result = gui::yes_no(ctx);
let write_to_morgue: Option<bool> = match result {
gui::YesNoResult::NoSelection => None,
gui::YesNoResult::No => Some(false),
@ -495,7 +495,16 @@ impl State {
self.mapgen_next_state = Some(RunState::PreRun);
new_runstate = RunState::MapGeneration;
}
// RunState::HelpScreen
RunState::HelpScreen => {
let result = gui::help_prompt(ctx);
match result {
gui::YesNoResult::NoSelection => {}
_ => {
gamelog::record_event(EVENT::LookedForHelp(1));
new_runstate = RunState::AwaitingInput;
}
}
}
RunState::MagicMapReveal { row, cursed } => {
let mut map = self.ecs.fetch_mut::<Map>();
// Could probably toss this into a function somewhere, and/or