Files
war-in-tunnels/src/app/widgets/board.rs
T
GarandPLG 923af91aeb Lazy init game states and extract board module
App now stores CLI arguments and an optional GameStates, initializing
the
states lazily on the first window resize. Skirmish board logic is moved
to
a new BoardState module with dedicated helpers (cells_area_helper and
updated cell size handling). Views and keybindings are updated to use
the
optional state accessors.
2026-04-02 00:45:57 +02:00

59 lines
1.8 KiB
Rust

use crate::app::states::skirmish_states::BoardState;
use ratatui::{
buffer::Buffer,
layout::{Constraint, Layout, Rect},
widgets::Widget,
};
use std::rc::Rc;
pub struct BoardWidget<'a> {
state: &'a BoardState,
}
impl<'a> BoardWidget<'a> {
pub fn new(state: &'a BoardState) -> Self {
Self { state }
}
}
impl Widget for BoardWidget<'_> {
fn render(self, area: Rect, buf: &mut Buffer) {
let horizontal: Rc<[Rect]> =
Layout::horizontal(vec![
Constraint::Length(self.state.cell_width as u16);
self.state.cols
])
.split(area);
for (col_idx, col_area) in horizontal.iter().enumerate() {
let vertical: Rc<[Rect]> =
Layout::vertical(vec![
Constraint::Length(self.state.cell_height as u16);
self.state.rows
])
.split(*col_area);
for (row_idx, cell_area) in vertical.iter().enumerate() {
if let Some(cell) = self.state.cells.get(
(row_idx + self.state.vertical_offset.get_value()) * self.state.map_width
+ (col_idx + self.state.horizontal_offset.get_value()),
) {
// FIXME: Fix showing selected cell
// if row_idx + self.state.vertical_offset.get_value()
// == self.state.focused_cell.row
// && col_idx + self.state.horizontal_offset.get_value()
// == self.state.focused_cell.col
// {
// cell.selected = true;
// } else {
// cell.selected = false;
// }
cell.render(*cell_area, buf);
}
}
}
}
}