Create a popup screen
This commit is contained in:
parent
2f426a1540
commit
dc634fc6bc
@ -19,9 +19,10 @@ use sea_battle_backend::data::GameRules;
|
||||
async fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> Result<(), Box<dyn Error>> {
|
||||
// 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),
|
||||
|
@ -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;
|
||||
|
77
rust/cli_player/src/ui_screens/popup_screen.rs
Normal file
77
rust/cli_player/src/ui_screens/popup_screen.rs
Normal file
@ -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<B: Backend>(
|
||||
msg: &str,
|
||||
terminal: &mut Terminal<B>,
|
||||
) -> io::Result<ScreenResult<()>> {
|
||||
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<B: Backend>(f: &mut Frame<B>, 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]);
|
||||
}
|
Loading…
Reference in New Issue
Block a user