use crate::graphics::graphic_block::Position;
use ratatui::layout::Rect;
use ratatui::widgets::{Block, BorderType};
#[derive(Clone)]
pub struct Map<'a> {
block: Block<'a>,
case_size: u16,
viewport: Rect,
}
impl Map<'_> {
#[must_use]
pub fn new<'a>(case_size_in_px: u16, viewport: Rect) -> Map<'a> {
Map {
block: Block::bordered().border_type(BorderType::Double),
case_size: case_size_in_px,
viewport,
}
}
pub fn resize_to_terminal(&mut self, viewport: Rect) {
self.viewport = viewport;
}
#[must_use]
pub fn out_of_map(&self, Position { x, y }: &Position) -> bool {
let x_max = self.viewport.width - (self.case_size + 2);
let y_max = self.viewport.height - 2;
let x_min = self.case_size;
let y_min = 1;
*x < x_min || *x > x_max || *y < y_min || *y > y_max
}
#[must_use]
pub fn out_of_map_reverse_position(&self, Position { x, y }: &Position) -> Position {
let x_max = self.viewport.width - (self.case_size + 2);
let y_max = self.viewport.height - 2;
let x_min = self.case_size;
let y_min = 1;
let x_remainder = (x_max - x_min) % self.case_size;
let x_max = x_max - x_remainder;
if *y > y_max {
Position { x: *x, y: y_min }
} else if *y < y_min {
Position { x: *x, y: y_max }
} else if *x > x_max {
Position { x: x_min, y: *y }
} else if *x < x_min {
Position { x: x_max, y: *y }
} else {
Position { x: *x, y: *y }
}
}
#[must_use]
pub fn area(&self) -> &Rect {
&self.viewport
}
#[must_use]
pub fn get_widget(&self) -> &Block<'_> {
&self.block
}
#[must_use]
pub fn get_case_size(&self) -> u16 {
self.case_size
}
}