This repository has been archived on 2025-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
Files
SeaBattle/rust/cli_player/src/ui_screens/game_screen.rs

50 lines
1.2 KiB
Rust

use std::io;
use std::time::{Duration, Instant};
use crossterm::event;
use crossterm::event::{Event, KeyCode};
use tui::backend::Backend;
use tui::{Frame, Terminal};
use crate::client::Client;
use crate::constants::*;
use crate::ui_screens::ScreenResult;
pub struct GameScreen {
client: Client,
}
impl GameScreen {
pub fn new(client: Client) -> Self {
Self { client }
}
pub async fn show<B: Backend>(
mut self,
terminal: &mut Terminal<B>,
) -> io::Result<ScreenResult> {
let mut last_tick = Instant::now();
loop {
terminal.draw(|f| self.ui(f))?;
let timeout = TICK_RATE
.checked_sub(last_tick.elapsed())
.unwrap_or_else(|| Duration::from_secs(0));
if crossterm::event::poll(timeout)? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => return Ok(ScreenResult::Canceled),
_ => {}
}
}
}
if last_tick.elapsed() >= TICK_RATE {
last_tick = Instant::now();
}
}
}
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {}
}