Show error messages when settings boats disposition
This commit is contained in:
parent
c3f2b5e767
commit
6e423f41ef
@ -5,13 +5,15 @@ use std::time::{Duration, Instant};
|
|||||||
use crossterm::event;
|
use crossterm::event;
|
||||||
use crossterm::event::{Event, KeyCode, MouseButton, MouseEventKind};
|
use crossterm::event::{Event, KeyCode, MouseButton, MouseEventKind};
|
||||||
use tui::backend::Backend;
|
use tui::backend::Backend;
|
||||||
|
use tui::layout::Rect;
|
||||||
use tui::style::*;
|
use tui::style::*;
|
||||||
|
use tui::widgets::*;
|
||||||
use tui::{Frame, Terminal};
|
use tui::{Frame, Terminal};
|
||||||
|
|
||||||
use sea_battle_backend::data::*;
|
use sea_battle_backend::data::*;
|
||||||
|
|
||||||
use crate::constants::*;
|
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_screens::ScreenResult;
|
||||||
use crate::ui_widgets::game_map_widget::{ColoredCells, GameMapWidget};
|
use crate::ui_widgets::game_map_widget::{ColoredCells, GameMapWidget};
|
||||||
|
|
||||||
@ -129,6 +131,8 @@ fn ui<B: Backend>(
|
|||||||
model: &mut SetBotsLayoutScreen,
|
model: &mut SetBotsLayoutScreen,
|
||||||
rules: &GameRules,
|
rules: &GameRules,
|
||||||
) -> CoordinatesMapper {
|
) -> CoordinatesMapper {
|
||||||
|
let errors = model.layout.errors(rules);
|
||||||
|
|
||||||
let current_boat = ColoredCells {
|
let current_boat = ColoredCells {
|
||||||
color: Color::Green,
|
color: Color::Green,
|
||||||
cells: model.layout.0[model.curr_boat].all_coordinates(),
|
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 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)
|
let game_map_widget = GameMapWidget::new(rules)
|
||||||
.set_default_empty_char(' ')
|
.set_default_empty_char(' ')
|
||||||
.add_colored_cells(current_boat)
|
.add_colored_cells(current_boat)
|
||||||
@ -160,16 +172,25 @@ fn ui<B: Backend>(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.set_legend(
|
.set_legend(legend);
|
||||||
"n next boat \n\
|
|
||||||
r rotate boat \n\n\
|
|
||||||
← ↓↑ → move boat \n\n\
|
|
||||||
Enter confirm layout",
|
|
||||||
);
|
|
||||||
|
|
||||||
let (w, h) = game_map_widget.estimated_size();
|
let (w, h) = game_map_widget.estimated_size();
|
||||||
let area = centered_rect_size(w, h, &f.size());
|
let area = centered_rect_size(w, h, &f.size());
|
||||||
f.render_widget(game_map_widget, area);
|
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
|
coordinates_mapper
|
||||||
}
|
}
|
||||||
|
@ -45,3 +45,17 @@ pub fn centered_rect_size(width: u16, height: u16, parent: &Rect) -> Rect {
|
|||||||
height,
|
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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user