Show error messages when settings boats disposition

This commit is contained in:
Pierre HUBERT 2022-10-08 10:16:53 +02:00
parent c3f2b5e767
commit 6e423f41ef
2 changed files with 42 additions and 7 deletions

View File

@ -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<B: Backend>(
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<B: Backend>(
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<B: Backend>(
}
}
})
.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
}

View File

@ -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,
}
}