From dc634fc6bcae3539ab468833a5c0393e07b64875 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 8 Oct 2022 14:38:33 +0200 Subject: [PATCH] Create a popup screen --- rust/cli_player/src/main.rs | 5 +- rust/cli_player/src/ui_screens/mod.rs | 1 + .../cli_player/src/ui_screens/popup_screen.rs | 77 +++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 rust/cli_player/src/ui_screens/popup_screen.rs diff --git a/rust/cli_player/src/main.rs b/rust/cli_player/src/main.rs index de20e65..eab7a2f 100644 --- a/rust/cli_player/src/main.rs +++ b/rust/cli_player/src/main.rs @@ -19,9 +19,10 @@ use sea_battle_backend::data::GameRules; async fn run_app(terminal: &mut Terminal) -> Result<(), Box> { // Temporary code // let res = configure_game_rules::configure_play_rules(GameRules::default(), terminal)?; // select_bot_type::select_bot_type(terminal)?; - let mut rules = GameRules::default(); + /*let mut rules = GameRules::default(); rules.boats_can_touch = true; - let res = set_boats_layout::set_boat_layout(&rules, terminal)?; // select_bot_type::select_bot_type(terminal)?; + let res = set_boats_layout::set_boat_layout(&rules, terminal)?; // select_bot_type::select_bot_type(terminal)?;*/ + let res = popup_screen::popup_screen("Hello you are my guest today!", terminal); Err(io::Error::new( ErrorKind::Other, format!("result: {:?}", res), diff --git a/rust/cli_player/src/ui_screens/mod.rs b/rust/cli_player/src/ui_screens/mod.rs index 3f3df92..48d7495 100644 --- a/rust/cli_player/src/ui_screens/mod.rs +++ b/rust/cli_player/src/ui_screens/mod.rs @@ -1,4 +1,5 @@ pub mod configure_game_rules; +pub mod popup_screen; pub mod select_bot_type; pub mod select_play_mode; pub mod set_boats_layout; diff --git a/rust/cli_player/src/ui_screens/popup_screen.rs b/rust/cli_player/src/ui_screens/popup_screen.rs new file mode 100644 index 0000000..9306f8e --- /dev/null +++ b/rust/cli_player/src/ui_screens/popup_screen.rs @@ -0,0 +1,77 @@ +use std::io; +use std::time::{Duration, Instant}; + +use crossterm::event; +use crossterm::event::{Event, KeyCode}; +use tui::backend::Backend; +use tui::layout::*; +use tui::style::*; +use tui::text::*; +use tui::widgets::*; +use tui::{Frame, Terminal}; + +use crate::constants::*; +use crate::ui_screens::utils::centered_rect_size; +use crate::ui_screens::ScreenResult; +use crate::ui_widgets::button_widget::ButtonWidget; + +struct PopupScreen<'a> { + title: &'a str, + msg: &'a str, +} + +pub fn popup_screen( + msg: &str, + terminal: &mut Terminal, +) -> io::Result> { + let mut model = PopupScreen { + title: "Message", + msg, + }; + + let mut last_tick = Instant::now(); + loop { + terminal.draw(|f| ui(f, &mut model))?; + + let timeout = TICK_RATE + .checked_sub(last_tick.elapsed()) + .unwrap_or_else(|| Duration::from_secs(0)); + + if event::poll(timeout)? { + if let Event::Key(key) = event::read()? { + match key.code { + KeyCode::Char('q') => return Ok(ScreenResult::Canceled), + KeyCode::Enter => { + return Ok(ScreenResult::Ok(())); + } + _ => {} + } + } + } + if last_tick.elapsed() >= TICK_RATE { + last_tick = Instant::now(); + } + } +} + +fn ui(f: &mut Frame, model: &mut PopupScreen) { + let area = centered_rect_size(model.msg.len() as u16 + 2, 5, &f.size()); + + let block = Block::default().borders(Borders::ALL).title(model.title); + f.render_widget(block, area); + + // Create two chunks with equal horizontal screen space + let chunks = Layout::default() + .direction(Direction::Vertical) + .constraints([Constraint::Percentage(70), Constraint::Percentage(30)].as_ref()) + .split(area.inner(&Margin { + horizontal: 1, + vertical: 1, + })); + + let text = Paragraph::new(model.msg); + f.render_widget(text, chunks[0]); + + let ok_button = ButtonWidget::new("OK", true); + f.render_widget(ok_button, chunks[1]); +}