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,
|
modifiers: KeyModifiers::NONE,
|
||||||
group: Group::Music,
|
group: Group::Music,
|
||||||
symbol: "m",
|
symbol: "m",
|
||||||
description: "Mute music",
|
description: "🔈",
|
||||||
},
|
},
|
||||||
KeyBinding {
|
KeyBinding {
|
||||||
action: Action::VolumeUp,
|
action: Action::VolumeUp,
|
||||||
@@ -260,7 +260,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
|
|||||||
modifiers: KeyModifiers::NONE,
|
modifiers: KeyModifiers::NONE,
|
||||||
group: Group::Music,
|
group: Group::Music,
|
||||||
symbol: "b",
|
symbol: "b",
|
||||||
description: "Increase music volume",
|
description: "🔊",
|
||||||
},
|
},
|
||||||
KeyBinding {
|
KeyBinding {
|
||||||
action: Action::VolumeDown,
|
action: Action::VolumeDown,
|
||||||
@@ -269,7 +269,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
|
|||||||
modifiers: KeyModifiers::NONE,
|
modifiers: KeyModifiers::NONE,
|
||||||
group: Group::Music,
|
group: Group::Music,
|
||||||
symbol: "n",
|
symbol: "n",
|
||||||
description: "Decrease music volume",
|
description: "🔉",
|
||||||
},
|
},
|
||||||
KeyBinding {
|
KeyBinding {
|
||||||
action: Action::WildCard('_'),
|
action: Action::WildCard('_'),
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ impl BoardState {
|
|||||||
|
|
||||||
for row in 0..map_height {
|
for row in 0..map_height {
|
||||||
for col in 0..map_width {
|
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) {
|
pub fn zoom_change(&mut self, new_zoom_level: ZoomLevel) {
|
||||||
self.zoom_level = new_zoom_level;
|
self.zoom_level = new_zoom_level;
|
||||||
|
|
||||||
@@ -88,6 +92,12 @@ impl BoardState {
|
|||||||
self.map_height,
|
self.map_height,
|
||||||
self.map_width,
|
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 {
|
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
|
.cells
|
||||||
.get(map_row * self.state.map_width + map_col)
|
.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()
|
if map_row == self.state.focused_cell.get_row()
|
||||||
&& map_col == self.state.focused_cell.get_col()
|
&& map_col == self.state.focused_cell.get_col()
|
||||||
|
|||||||
+48
-19
@@ -1,8 +1,10 @@
|
|||||||
|
use crate::app::states::ZoomLevel;
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
buffer::Buffer,
|
buffer::Buffer,
|
||||||
layout::{Alignment, Rect},
|
layout::{Alignment, Rect},
|
||||||
style::{Color, Style, Stylize},
|
style::{Color, Style, Stylize},
|
||||||
widgets::{Block, Borders, Padding, Paragraph, Widget},
|
text::Line,
|
||||||
|
widgets::{Block, Borders, Paragraph, Widget},
|
||||||
};
|
};
|
||||||
|
|
||||||
// pub enum CellTags {
|
// pub enum CellTags {
|
||||||
@@ -13,20 +15,23 @@ use ratatui::{
|
|||||||
// Base,
|
// Base,
|
||||||
// }
|
// }
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct CellWidget {
|
pub struct CellWidget {
|
||||||
pub row: usize,
|
row: usize,
|
||||||
pub col: usize,
|
col: usize,
|
||||||
selected: bool,
|
selected: bool,
|
||||||
|
zoom_level: ZoomLevel,
|
||||||
|
// text_area: Vec<Line>,
|
||||||
// pub tags: Vec<CellTags>,
|
// pub tags: Vec<CellTags>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl CellWidget {
|
impl CellWidget {
|
||||||
pub fn new(row: usize, col: usize) -> Self {
|
pub fn new(row: usize, col: usize, zoom_level: ZoomLevel) -> Self {
|
||||||
Self {
|
Self {
|
||||||
row,
|
row,
|
||||||
col,
|
col,
|
||||||
selected: false,
|
selected: false,
|
||||||
|
zoom_level,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -34,6 +39,10 @@ impl CellWidget {
|
|||||||
self.selected = selected;
|
self.selected = selected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn set_zoom_level(&mut self, zoom_level: ZoomLevel) {
|
||||||
|
self.zoom_level = zoom_level;
|
||||||
|
}
|
||||||
|
|
||||||
fn col_to_letters(&self) -> String {
|
fn col_to_letters(&self) -> String {
|
||||||
let mut col: usize = self.col + 1;
|
let mut col: usize = self.col + 1;
|
||||||
let mut letters: Vec<char> = Vec::new();
|
let mut letters: Vec<char> = Vec::new();
|
||||||
@@ -46,7 +55,7 @@ impl CellWidget {
|
|||||||
letters.iter().rev().collect()
|
letters.iter().rev().collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn display_coords(&self) -> String {
|
fn display_coords(&self) -> String {
|
||||||
format!("{}{}", self.col_to_letters(), self.row)
|
format!("{}{}", self.col_to_letters(), self.row)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,24 +66,44 @@ impl CellWidget {
|
|||||||
Color::White
|
Color::White
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl Widget for CellWidget {
|
fn get_text_area(&self) -> Vec<Line<'_>> {
|
||||||
fn render(self, area: Rect, buf: &mut Buffer) {
|
let mut text_area: Vec<Line<'_>> = Vec::new();
|
||||||
Paragraph::new("")
|
|
||||||
.alignment(Alignment::Center)
|
match self.zoom_level {
|
||||||
.block(
|
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()
|
Block::default()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.style(Style::default().fg(self.fg_color()))
|
.style(Style::default().fg(self.fg_color()))
|
||||||
.title(self.display_coords().green())
|
.title(self.display_coords().green())
|
||||||
.padding(Padding {
|
}
|
||||||
left: 0,
|
}
|
||||||
right: 0,
|
|
||||||
top: if area.height <= 3 { 0 } else { area.height / 3 },
|
impl Widget for CellWidget {
|
||||||
bottom: 0,
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
||||||
}),
|
Paragraph::new(self.get_text_area())
|
||||||
)
|
.alignment(Alignment::Center)
|
||||||
|
.block(self.get_block())
|
||||||
.render(area, buf);
|
.render(area, buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -33,11 +33,15 @@ impl KeybindingsWidget {
|
|||||||
/// # Parameters
|
/// # Parameters
|
||||||
///
|
///
|
||||||
/// * `actions` – A slice of actions for which keybindings should be shown.
|
/// * `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
|
/// # Returns
|
||||||
///
|
///
|
||||||
/// A fully‑initialised `KeybindingsWidget` ready for rendering.
|
/// 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>> =
|
let keybindings: Vec<Option<&'static KeyBinding>> =
|
||||||
actions.iter().map(|a| binding_for(*a)).collect();
|
actions.iter().map(|a| binding_for(*a)).collect();
|
||||||
|
|
||||||
@@ -47,7 +51,8 @@ impl KeybindingsWidget {
|
|||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let mut grouped_keybindings: Vec<Paragraph<'static>> = Vec::new();
|
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) {
|
if !used_groups.contains(group) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@@ -66,9 +71,18 @@ impl KeybindingsWidget {
|
|||||||
})
|
})
|
||||||
.collect();
|
.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);
|
block = block.borders(Borders::LEFT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user