use crate::bot::Difficulty;
use crate::game::BoardDimensions;
use crate::game::Cell;
use ratatui::layout::Rect;
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Scale {
#[default]
Normal,
Large,
}
impl Scale {
pub const ALL: [Self; 2] = [Self::Normal, Self::Large];
pub const fn cell_width(self) -> u16 {
match self {
Self::Normal => 5,
Self::Large => 7,
}
}
pub const fn cell_height(self) -> u16 {
match self {
Self::Normal => 2,
Self::Large => 3,
}
}
}
pub const TITLE: &str = "F O U R S";
pub const HELP: &str = "←/→ move ⏎ place (d)ifficulty (r)estart (q)uit";
const ROWS_ABOVE_BOARD: u16 = 3;
const ROWS_BELOW_BOARD: u16 = 3;
const ROWS_AROUND_BOARD: u16 = ROWS_ABOVE_BOARD + ROWS_BELOW_BOARD;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Geometry {
dimensions: BoardDimensions,
scale: Scale,
pub content: Rect,
pub title: Rect,
pub subtitle: Rect,
pub indicator: Rect,
pub board: Rect,
pub notice: Rect,
pub status: Rect,
pub help: Rect,
}
impl Geometry {
pub fn new(area: Rect, dimensions: BoardDimensions) -> Option<Self> {
Scale::ALL
.into_iter()
.rev()
.find_map(|scale| Self::at_scale(area, dimensions, scale))
}
pub fn at_scale(area: Rect, dimensions: BoardDimensions, scale: Scale) -> Option<Self> {
let (width, height) = required_size_at(dimensions, scale);
if area.width < width || area.height < height {
return None;
}
let content = Rect {
x: area.x + (area.width - width) / 2,
y: area.y + (area.height - height) / 2,
width,
height,
};
let board = centred_board(content, dimensions, scale);
Some(Self {
dimensions,
scale,
content,
title: line(content, 0),
subtitle: line(content, 1),
indicator: line(content, ROWS_ABOVE_BOARD - 1),
board,
notice: line(content, ROWS_ABOVE_BOARD + board.height),
status: line(content, ROWS_ABOVE_BOARD + board.height + 1),
help: line(content, ROWS_ABOVE_BOARD + board.height + 2),
})
}
pub const fn dimensions(&self) -> BoardDimensions {
self.dimensions
}
pub const fn scale(&self) -> Scale {
self.scale
}
pub fn cell(&self, cell: Cell) -> Option<Rect> {
let is_on_board =
cell.column < self.dimensions.column_count() && cell.row < self.dimensions.row_count();
if !is_on_board {
return None;
}
let column = u16::try_from(cell.column).ok()?;
let rows_from_top = u16::try_from(self.dimensions.row_count() - 1 - cell.row).ok()?;
Some(Rect {
x: self.board.x + 1 + column * self.scale.cell_width(),
y: self.board.y + 1 + rows_from_top * self.scale.cell_height(),
width: self.scale.cell_width(),
height: self.scale.cell_height(),
})
}
pub fn column_middle(&self, column: usize) -> Option<u16> {
let cell = self.cell(Cell::new(column, 0))?;
Some(cell.x + self.scale.cell_width() / 2)
}
}
pub fn required_size(dimensions: BoardDimensions) -> (u16, u16) {
required_size_at(dimensions, Scale::default())
}
pub fn required_size_at(dimensions: BoardDimensions, scale: Scale) -> (u16, u16) {
let (board_width, board_height) = board_size(dimensions, scale);
let width = board_width
.max(text_width(HELP))
.max(text_width(TITLE))
.max(widest(&opponent_text));
(width, board_height + ROWS_AROUND_BOARD)
}
pub fn opponent_text(difficulty: Difficulty) -> String {
format!("vs {difficulty}")
}
fn widest(text_of: &dyn Fn(Difficulty) -> String) -> u16 {
Difficulty::ALL
.into_iter()
.map(|difficulty| text_width(&text_of(difficulty)))
.max()
.unwrap_or_default()
}
fn board_size(dimensions: BoardDimensions, scale: Scale) -> (u16, u16) {
(
side_length(dimensions.column_count(), scale.cell_width()),
side_length(dimensions.row_count(), scale.cell_height()),
)
}
fn side_length(slot_count: usize, slot_size: u16) -> u16 {
u16::try_from(slot_count)
.unwrap_or(u16::MAX)
.saturating_mul(slot_size)
.saturating_add(2)
}
fn centred_board(content: Rect, dimensions: BoardDimensions, scale: Scale) -> Rect {
let (width, height) = board_size(dimensions, scale);
Rect {
x: content.x + content.width.saturating_sub(width) / 2,
y: content.y + ROWS_ABOVE_BOARD,
width,
height,
}
}
fn text_width(text: &str) -> u16 {
u16::try_from(text.chars().count()).unwrap_or(u16::MAX)
}
fn line(content: Rect, offset: u16) -> Rect {
Rect {
x: content.x,
y: content.y + offset,
width: content.width,
height: 1,
}
}