diff --git a/src/app/keybindings/keybindings.rs b/src/app/keybindings/keybindings.rs index 8101699..6aa75a9 100644 --- a/src/app/keybindings/keybindings.rs +++ b/src/app/keybindings/keybindings.rs @@ -251,7 +251,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[ modifiers: KeyModifiers::NONE, group: Group::Music, symbol: "m", - description: "Mute music", + description: "🔈", }, KeyBinding { action: Action::VolumeUp, @@ -260,7 +260,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[ modifiers: KeyModifiers::NONE, group: Group::Music, symbol: "b", - description: "Increase music volume", + description: "🔊", }, KeyBinding { action: Action::VolumeDown, @@ -269,7 +269,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[ modifiers: KeyModifiers::NONE, group: Group::Music, symbol: "n", - description: "Decrease music volume", + description: "🔉", }, KeyBinding { action: Action::WildCard('_'), diff --git a/src/app/states/skirmish_states/board.rs b/src/app/states/skirmish_states/board.rs index 900d19b..4a842e7 100644 --- a/src/app/states/skirmish_states/board.rs +++ b/src/app/states/skirmish_states/board.rs @@ -45,7 +45,7 @@ impl BoardState { for row in 0..map_height { for col in 0..map_width { - cells.push(CellWidget::new(row, col)); + cells.push(CellWidget::new(row, col, zoom_level)); } } @@ -65,6 +65,10 @@ impl BoardState { } } + fn get_mut_cell(&mut self, row: usize, col: usize) -> &mut CellWidget { + &mut self.cells[row * self.map_width + col] + } + pub fn zoom_change(&mut self, new_zoom_level: ZoomLevel) { self.zoom_level = new_zoom_level; @@ -88,6 +92,12 @@ impl BoardState { self.map_height, self.map_width, ); + + for row in 0..self.map_height { + for col in 0..self.map_width { + self.get_mut_cell(row, col).set_zoom_level(new_zoom_level); + } + } } fn max_offset(map_size: usize, size: usize) -> usize { diff --git a/src/app/views/default.rs b/src/app/views/default.rs index 57c0a05..129b387 100644 --- a/src/app/views/default.rs +++ b/src/app/views/default.rs @@ -35,6 +35,6 @@ pub fn default_view(area: Rect, buf: &mut Buffer) { } { - KeybindingsWidget::new(ACTIONS).render(keybindings_area, buf); + KeybindingsWidget::new(ACTIONS, keybindings_area.height).render(keybindings_area, buf); } } diff --git a/src/app/views/main_menu.rs b/src/app/views/main_menu.rs index deb9546..8e7a5cf 100644 --- a/src/app/views/main_menu.rs +++ b/src/app/views/main_menu.rs @@ -93,6 +93,6 @@ pub fn main_menu_view(app: &App, area: Rect, buf: &mut Buffer) { } { - KeybindingsWidget::new(ACTIONS).render(keybindings_area, buf); + KeybindingsWidget::new(ACTIONS, keybindings_area.height).render(keybindings_area, buf); } } diff --git a/src/app/views/skirmish.rs b/src/app/views/skirmish.rs index 72e9d3d..21567f9 100644 --- a/src/app/views/skirmish.rs +++ b/src/app/views/skirmish.rs @@ -97,6 +97,6 @@ pub fn skirmish_view(app: &App, area: Rect, buf: &mut Buffer) { } { - KeybindingsWidget::new(ACTIONS).render(keybindings_area, buf); + KeybindingsWidget::new(ACTIONS, keybindings_area.height).render(keybindings_area, buf); } } diff --git a/src/app/widgets/board.rs b/src/app/widgets/board.rs index 86a9ff4..17b1d69 100644 --- a/src/app/widgets/board.rs +++ b/src/app/widgets/board.rs @@ -42,7 +42,7 @@ impl Widget for BoardWidget<'_> { .cells .get(map_row * self.state.map_width + map_col) { - let mut cell: CellWidget = *template; + let mut cell: CellWidget = template.clone(); if map_row == self.state.focused_cell.get_row() && map_col == self.state.focused_cell.get_col() diff --git a/src/app/widgets/cell.rs b/src/app/widgets/cell.rs index 2dfc31e..4306fcc 100644 --- a/src/app/widgets/cell.rs +++ b/src/app/widgets/cell.rs @@ -1,8 +1,10 @@ +use crate::app::states::ZoomLevel; use ratatui::{ buffer::Buffer, layout::{Alignment, Rect}, style::{Color, Style, Stylize}, - widgets::{Block, Borders, Padding, Paragraph, Widget}, + text::Line, + widgets::{Block, Borders, Paragraph, Widget}, }; // pub enum CellTags { @@ -13,20 +15,23 @@ use ratatui::{ // Base, // } -#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct CellWidget { - pub row: usize, - pub col: usize, + row: usize, + col: usize, selected: bool, + zoom_level: ZoomLevel, + // text_area: Vec, // pub tags: Vec, } impl CellWidget { - pub fn new(row: usize, col: usize) -> Self { + pub fn new(row: usize, col: usize, zoom_level: ZoomLevel) -> Self { Self { row, col, selected: false, + zoom_level, } } @@ -34,6 +39,10 @@ impl CellWidget { self.selected = selected; } + pub fn set_zoom_level(&mut self, zoom_level: ZoomLevel) { + self.zoom_level = zoom_level; + } + fn col_to_letters(&self) -> String { let mut col: usize = self.col + 1; let mut letters: Vec = Vec::new(); @@ -46,7 +55,7 @@ impl CellWidget { letters.iter().rev().collect() } - pub fn display_coords(&self) -> String { + fn display_coords(&self) -> String { format!("{}{}", self.col_to_letters(), self.row) } @@ -57,24 +66,44 @@ impl CellWidget { Color::White } } + + fn get_text_area(&self) -> Vec> { + let mut text_area: Vec> = Vec::new(); + + match self.zoom_level { + ZoomLevel::ZoomedIn => { + text_area.push(Line::from(" ")); + text_area.push(Line::from(" ")); + text_area.push(Line::from(" ")); + text_area.push(Line::from(" ")); + text_area.push(Line::from(" ")); + } + ZoomLevel::Default => { + text_area.push(Line::from(" ")); + text_area.push(Line::from(" ")); + text_area.push(Line::from(" ")); + } + ZoomLevel::ZoomedOut => { + text_area.push(Line::from(" ")); + } + } + + text_area + } + + fn get_block(&self) -> Block<'_> { + Block::default() + .borders(Borders::ALL) + .style(Style::default().fg(self.fg_color())) + .title(self.display_coords().green()) + } } impl Widget for CellWidget { fn render(self, area: Rect, buf: &mut Buffer) { - Paragraph::new("") + Paragraph::new(self.get_text_area()) .alignment(Alignment::Center) - .block( - Block::default() - .borders(Borders::ALL) - .style(Style::default().fg(self.fg_color())) - .title(self.display_coords().green()) - .padding(Padding { - left: 0, - right: 0, - top: if area.height <= 3 { 0 } else { area.height / 3 }, - bottom: 0, - }), - ) + .block(self.get_block()) .render(area, buf); } } diff --git a/src/app/widgets/keybindings.rs b/src/app/widgets/keybindings.rs index 9dc1f6b..8760c39 100644 --- a/src/app/widgets/keybindings.rs +++ b/src/app/widgets/keybindings.rs @@ -33,11 +33,15 @@ impl KeybindingsWidget { /// # Parameters /// /// * `actions` – A slice of actions for which keybindings should be shown. + /// * `area_height` – The height of the area in which the widget will be rendered. + /// This value is used to calculate vertical padding so that the keybinding rows + /// are vertically centered when the widget's allocated height is larger than the + /// number of lines needed to display the bindings. /// /// # Returns /// /// A fully‑initialised `KeybindingsWidget` ready for rendering. - pub fn new(actions: &[Action]) -> Self { + pub fn new(actions: &[Action], area_height: u16) -> Self { let keybindings: Vec> = actions.iter().map(|a| binding_for(*a)).collect(); @@ -47,7 +51,8 @@ impl KeybindingsWidget { .collect(); let mut grouped_keybindings: Vec> = Vec::new(); - for (i, group) in Group::value_variants().iter().enumerate() { + + for group in Group::value_variants().iter() { if !used_groups.contains(group) { continue; } @@ -66,9 +71,18 @@ impl KeybindingsWidget { }) .collect(); - let mut block: Block<'_> = Block::default().padding(Padding::new(1, 1, 0, 0)); + let height_diff: u16 = + area_height.saturating_sub(grouped_lines.len().saturating_add(2) as u16); - if i != 0 { + let padding_top: u16 = if height_diff <= 1 { + height_diff + } else { + height_diff / 2 + }; + + let mut block: Block<'_> = Block::default().padding(Padding::new(1, 1, padding_top, 0)); + + if !grouped_keybindings.is_empty() { block = block.borders(Borders::LEFT); }