generated from GarandPLG/rust-flake-template
Add zoom support and emoji keybinding icons
- CellWidget now stores a ZoomLevel, renders size‑dependent text, and provides methods to set the zoom level and build its block. - BoardState creates cells with the current zoom level, adds a helper to get a mutable cell, and updates each cell’s zoom level in `zoom_change`. - KeybindingsWidget constructor now takes the widget height, calculates vertical padding to centre groups, and adds left borders between groups. - Views (default, main_menu, skirmish) pass the area height to the new constructor. - Keybinding descriptions are replaced with emoji symbols (🔈, 🔊, 🔉). - BoardWidget clones the cell template instead of dereferencing it.
This commit is contained in:
@@ -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('_'),
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
+47
-18
@@ -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<Line>,
|
||||
// pub tags: Vec<CellTags>,
|
||||
}
|
||||
|
||||
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<char> = 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<Line<'_>> {
|
||||
let mut text_area: Vec<Line<'_>> = 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(" "));
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for CellWidget {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
Paragraph::new("")
|
||||
.alignment(Alignment::Center)
|
||||
.block(
|
||||
text_area
|
||||
}
|
||||
|
||||
fn get_block(&self) -> 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,
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget for CellWidget {
|
||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||
Paragraph::new(self.get_text_area())
|
||||
.alignment(Alignment::Center)
|
||||
.block(self.get_block())
|
||||
.render(area, buf);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<Option<&'static KeyBinding>> =
|
||||
actions.iter().map(|a| binding_for(*a)).collect();
|
||||
|
||||
@@ -47,7 +51,8 @@ impl KeybindingsWidget {
|
||||
.collect();
|
||||
|
||||
let mut grouped_keybindings: Vec<Paragraph<'static>> = 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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user