diff --git a/rust/cli_player/src/ui_screens/set_boats_layout.rs b/rust/cli_player/src/ui_screens/set_boats_layout.rs index 3a88691..64433a5 100644 --- a/rust/cli_player/src/ui_screens/set_boats_layout.rs +++ b/rust/cli_player/src/ui_screens/set_boats_layout.rs @@ -5,13 +5,15 @@ use std::time::{Duration, Instant}; use crossterm::event; use crossterm::event::{Event, KeyCode, MouseButton, MouseEventKind}; use tui::backend::Backend; +use tui::layout::Rect; use tui::style::*; +use tui::widgets::*; use tui::{Frame, Terminal}; use sea_battle_backend::data::*; use crate::constants::*; -use crate::ui_screens::utils::centered_rect_size; +use crate::ui_screens::utils::{centered_rect_size, centered_text}; use crate::ui_screens::ScreenResult; use crate::ui_widgets::game_map_widget::{ColoredCells, GameMapWidget}; @@ -129,6 +131,8 @@ fn ui( model: &mut SetBotsLayoutScreen, rules: &GameRules, ) -> CoordinatesMapper { + let errors = model.layout.errors(rules); + let current_boat = ColoredCells { color: Color::Green, cells: model.layout.0[model.curr_boat].all_coordinates(), @@ -148,6 +152,14 @@ fn ui( let mut coordinates_mapper = HashMap::new(); + let mut legend = "n next boat \n\ + r rotate boat \n\n\ + ← ↓↑ → move boat \n\n" + .to_string(); + if errors.is_empty() { + legend.push_str("Enter confirm layout"); + } + let game_map_widget = GameMapWidget::new(rules) .set_default_empty_char(' ') .add_colored_cells(current_boat) @@ -160,16 +172,25 @@ fn ui( } } }) - .set_legend( - "n next boat \n\ - r rotate boat \n\n\ - ← ↓↑ → move boat \n\n\ - Enter confirm layout", - ); + .set_legend(legend); let (w, h) = game_map_widget.estimated_size(); let area = centered_rect_size(w, h, &f.size()); f.render_widget(game_map_widget, area); + if !errors.is_empty() { + let messages = ["INVALID_LAYOUT", errors[0]]; + for (i, msg) in messages.iter().enumerate() { + let paragraph = Paragraph::new(*msg).style(Style::default().fg(Color::Red)); + f.render_widget( + paragraph, + centered_text( + msg, + &Rect::new(f.size().x, area.bottom() + i as u16, f.size().width, 1), + ), + ); + } + } + coordinates_mapper } diff --git a/rust/cli_player/src/ui_screens/utils.rs b/rust/cli_player/src/ui_screens/utils.rs index 69633a5..f1e7928 100644 --- a/rust/cli_player/src/ui_screens/utils.rs +++ b/rust/cli_player/src/ui_screens/utils.rs @@ -45,3 +45,17 @@ pub fn centered_rect_size(width: u16, height: u16, parent: &Rect) -> Rect { height, } } + +/// Get coordinates to render centered text +pub fn centered_text(text: &str, container: &Rect) -> Rect { + if text.len() > container.width as usize { + return *container; + } + + Rect { + x: container.x + (container.width - text.len() as u16) / 2, + y: container.y, + height: 1, + width: text.len() as u16, + } +}