Skip to content

Commit

Permalink
Enable to create Modal content dynamically
Browse files Browse the repository at this point in the history
  • Loading branch information
ryym committed Aug 9, 2018
1 parent 26c7cd7 commit d5bc1eb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 30 deletions.
30 changes: 15 additions & 15 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const UPDATE: u64 = TICK / 2;
pub struct Game<W: Write> {
inputs: Inputs,
screen: Screen<W>,
help_modal: Modal<'static>,
help_modal: Modal,
}

impl<W: Write> Game<W> {
Expand All @@ -23,15 +23,15 @@ impl<W: Write> Game<W> {
inputs,
screen,
help_modal: Modal {
title: "HELP",
content: &[
"h - Move left",
"l - Move right",
"j - Speed up",
"d,f - Rotate",
"q - Quit",
title: "HELP".to_string(),
content: vec![
String::from("h - Move left"),
String::from("l - Move right"),
String::from("j - Speed up"),
String::from("d,f - Rotate"),
String::from("q - Quit"),
],
actions: Some(&[Action::Ok, Action::Reset, Action::Quit]),
actions: vec![Action::Ok, Action::Reset, Action::Quit],
},
}
}
Expand All @@ -41,13 +41,13 @@ impl<W: Write> Game<W> {
.show_modal(
&mut self.inputs,
&Modal {
title: "ERROR",
content: &[
"Sorry, unexpected error occurred.",
"details:",
&err.to_string(),
title: "ERROR".to_string(),
content: vec![
String::from("Sorry, unexpected error occurred."),
String::from("details:"),
err.to_string(),
],
actions: None,
actions: vec![Action::Ok],
},
)
.expect(&format!("show error dialog ({})", err));
Expand Down
27 changes: 12 additions & 15 deletions src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use termion as tm;
use termion::color;
use termion::cursor::Goto;

pub struct Modal<'a> {
pub title: &'a str,
pub content: &'a [&'a str],
pub actions: Option<&'a [Action]>,
pub struct Modal {
pub title: String,
pub content: Vec<String>,
pub actions: Vec<Action>,
}

const TITLE: &'static str = "- T E X T R I S -";
Expand Down Expand Up @@ -115,12 +115,12 @@ impl<W: Write> Screen<W> {
self.show_modal(
inputs,
&Modal {
title: "GAME OVER",
content: &[
&format!("Time: {}", play.elapsed()),
&format!("Score: {}", play.score()),
title: "GAME OVER".to_string(),
content: vec![
format!("Time: {}", play.elapsed()),
format!("Score: {}", play.score()),
],
actions: Some(&[Action::Retry, Action::Quit]),
actions: vec![Action::Retry, Action::Quit],
},
)
}
Expand Down Expand Up @@ -154,13 +154,10 @@ impl<W: Write> Screen<W> {

let y_actions = y;

let actions = match modal.actions {
Some(actions) => actions,
None => &[Action::Ok],
};
let actions = &modal.actions;
let mut select = 0;

let action_btns = self.write_inline_actions(&actions, select);
let action_btns = self.write_inline_actions(actions, select);
write!(self.stdout, "{}{}", Goto(x + 1, y_actions), action_btns)?;
y += 1;
write!(self.stdout, "{}{}", Goto(x, y), border)?;
Expand All @@ -186,7 +183,7 @@ impl<W: Write> Screen<W> {
_ => {}
}

let action_btns = self.write_inline_actions(&actions, select);
let action_btns = self.write_inline_actions(actions, select);
write!(self.stdout, "{}{}", Goto(x + 1, y_actions), action_btns)?;
self.stdout.flush()?;
}
Expand Down

0 comments on commit d5bc1eb

Please sign in to comment.