From d5bc1ebb6d2a1020c881a848b384225cadfeb628 Mon Sep 17 00:00:00 2001 From: ryym Date: Thu, 9 Aug 2018 21:10:42 +0900 Subject: [PATCH] Enable to create Modal content dynamically --- src/game.rs | 30 +++++++++++++++--------------- src/screen.rs | 27 ++++++++++++--------------- 2 files changed, 27 insertions(+), 30 deletions(-) diff --git a/src/game.rs b/src/game.rs index a0f8133..ef33cfb 100644 --- a/src/game.rs +++ b/src/game.rs @@ -14,7 +14,7 @@ const UPDATE: u64 = TICK / 2; pub struct Game { inputs: Inputs, screen: Screen, - help_modal: Modal<'static>, + help_modal: Modal, } impl Game { @@ -23,15 +23,15 @@ impl Game { 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], }, } } @@ -41,13 +41,13 @@ impl Game { .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)); diff --git a/src/screen.rs b/src/screen.rs index 796ef14..cfb1831 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -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, + pub actions: Vec, } const TITLE: &'static str = "- T E X T R I S -"; @@ -115,12 +115,12 @@ impl Screen { 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], }, ) } @@ -154,13 +154,10 @@ impl Screen { 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)?; @@ -186,7 +183,7 @@ impl Screen { _ => {} } - 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()?; }