Can change bot type in rules screen

This commit is contained in:
2022-10-15 15:07:17 +02:00
parent 26d5f85c3c
commit 454dff923b
4 changed files with 64 additions and 29 deletions

View File

@ -13,7 +13,8 @@ use tui::{Frame, Terminal};
use sea_battle_backend::data::GameRules;
use crate::constants::TICK_RATE;
use crate::constants::{HIGHLIGHT_COLOR, TICK_RATE};
use crate::ui_screens::select_bot_type::SelectBotTypeScreen;
use crate::ui_screens::utils::centered_rect_size;
use crate::ui_screens::ScreenResult;
use crate::ui_widgets::button_widget::ButtonWidget;
@ -27,6 +28,7 @@ enum EditingField {
BoatsList,
BoatsCanTouch,
PlayerContinueOnHit,
BotType,
Cancel,
OK,
}
@ -70,6 +72,14 @@ impl GameRulesConfigurationScreen {
// Submit results
KeyCode::Enter => {
if self.curr_field == EditingField::BotType {
if let ScreenResult::Ok(t) =
SelectBotTypeScreen::default().show(terminal)?
{
self.rules.bot_type = t;
}
}
if self.curr_field == EditingField::Cancel {
return Ok(ScreenResult::Canceled);
}
@ -143,7 +153,7 @@ impl GameRulesConfigurationScreen {
}
fn ui<B: Backend>(&mut self, f: &mut Frame<B>) {
let area = centered_rect_size(50, 16, &f.size());
let area = centered_rect_size(50, 20, &f.size());
let block = Block::default().title("Game rules").borders(Borders::ALL);
f.render_widget(block, area);
@ -156,7 +166,8 @@ impl GameRulesConfigurationScreen {
Constraint::Length(3),
Constraint::Length(1),
Constraint::Length(1),
Constraint::Length(1),
Constraint::Length(3), // Bot type
Constraint::Length(1), // Margin
Constraint::Length(1), // Buttons
Constraint::Length(1), // Error message (if any)
])
@ -206,6 +217,22 @@ impl GameRulesConfigurationScreen {
);
f.render_widget(editor, chunks[EditingField::PlayerContinueOnHit as usize]);
// Select bot type
let bot_type_text = format!("Bot type: {}", self.rules.bot_type.description().name);
let text = Paragraph::new(bot_type_text.as_str()).style(
match self.curr_field == EditingField::BotType {
false => Style::default(),
true => Style::default().fg(HIGHLIGHT_COLOR),
},
);
f.render_widget(
text,
chunks[EditingField::BotType as usize].inner(&Margin {
horizontal: 0,
vertical: 1,
}),
);
// Buttons
let buttons_chunk = Layout::default()
.direction(Direction::Horizontal)

View File

@ -27,7 +27,7 @@ impl Default for SelectBotTypeScreen {
Self {
state: Default::default(),
curr_selection: types.len() - 1,
types,
types: types.to_vec(),
}
}
}