Add bounded focus and padding to skirmish UI

- Initialize FocusedCell with map dimensions and start near the map
  centre.
- Store max rows/cols in FocusedCell and clamp moves to these bounds.
- Scroll actions now adjust the corresponding offset and also move the
  focused cell, keeping navigation consistent.
- Zoom actions modify the vertical offset to keep the view centered
  after
  changing zoom levels.
- Cell widget now uses a Padding widget with dynamic top padding based
  on
  the area height.
This commit is contained in:
2026-03-30 16:32:28 +02:00
parent 363baa1c1a
commit 07e941eba1
4 changed files with 50 additions and 20 deletions
+23 -11
View File
@@ -9,35 +9,47 @@ pub fn skirmish_keybindings(app: &mut App, key_event: &KeyEvent) {
if let Some(action) = event_to_action(&key_event) {
common_keybindings(app, action);
match action {
Action::Up => {
app.states.skirmish.focused_cell.move_up();
// if app.states.skirmish.horizontal_offset.get_value() {
// }
}
Action::Up => app.states.skirmish.focused_cell.move_up(),
Action::Down => app.states.skirmish.focused_cell.move_down(),
Action::Left => app.states.skirmish.focused_cell.move_left(),
Action::Right => app.states.skirmish.focused_cell.move_right(),
Action::ScrollUp => app.states.skirmish.vertical_offset.prev(),
Action::ScrollDown => app.states.skirmish.vertical_offset.next(),
Action::ScrollLeft => app.states.skirmish.horizontal_offset.prev(),
Action::ScrollRight => app.states.skirmish.horizontal_offset.next(),
Action::ScrollUp => {
app.states.skirmish.vertical_offset.prev();
app.states.skirmish.focused_cell.move_up();
}
Action::ScrollDown => {
app.states.skirmish.vertical_offset.next();
app.states.skirmish.focused_cell.move_down();
}
Action::ScrollLeft => {
app.states.skirmish.horizontal_offset.prev();
app.states.skirmish.focused_cell.move_left();
}
Action::ScrollRight => {
app.states.skirmish.horizontal_offset.next();
app.states.skirmish.focused_cell.move_right();
}
Action::ZoomIn => match app.states.skirmish.zoom_level {
ZoomLevel::ZoomedIn => {}
ZoomLevel::Default => {
app.states.skirmish.zoom_level = ZoomLevel::ZoomedIn;
app.states.skirmish.vertical_offset.next();
}
ZoomLevel::ZoomedOut => {
app.states.skirmish.zoom_level = ZoomLevel::Default;
app.states.skirmish.vertical_offset.next();
app.states.skirmish.vertical_offset.next();
}
},
Action::ZoomOut => match app.states.skirmish.zoom_level {
ZoomLevel::ZoomedIn => {
app.states.skirmish.zoom_level = ZoomLevel::Default;
app.states.skirmish.vertical_offset.prev();
}
ZoomLevel::Default => {
app.states.skirmish.zoom_level = ZoomLevel::ZoomedOut;
app.states.skirmish.vertical_offset.prev();
app.states.skirmish.vertical_offset.prev();
}
ZoomLevel::ZoomedOut => {}
},
+6 -1
View File
@@ -32,7 +32,12 @@ impl GameStates {
map_height: args.map_height as usize,
board_cells: Vec::new(),
zoom_level: args.zoom_level,
focused_cell: FocusedCell::new(0, 0),
focused_cell: FocusedCell::new(
(args.map_height / 2) as usize - 1,
2,
args.map_height as usize,
args.map_width as usize,
),
},
perk_decks: PerkDecksState {
id: 2,
+13 -6
View File
@@ -54,27 +54,34 @@ impl Offset {
pub struct FocusedCell {
pub row: usize,
pub col: usize,
pub max_row: usize,
pub max_col: usize,
}
impl FocusedCell {
pub fn new(row: usize, col: usize) -> Self {
Self { row, col }
pub fn new(row: usize, col: usize, max_row: usize, max_col: usize) -> Self {
Self {
row,
col,
max_row,
max_col,
}
}
pub fn move_up(&mut self) {
self.row = self.row.saturating_sub(1);
self.row = self.row.saturating_sub(1).max(0);
}
pub fn move_down(&mut self) {
self.row = self.row.saturating_add(1);
self.row = self.row.saturating_add(1).min(self.max_row - 1);
}
pub fn move_left(&mut self) {
self.col = self.col.saturating_sub(1);
self.col = self.col.saturating_sub(1).max(0);
}
pub fn move_right(&mut self) {
self.col = self.col.saturating_add(1);
self.col = self.col.saturating_add(1).min(self.max_col - 1);
}
}
+8 -2
View File
@@ -2,7 +2,7 @@ use ratatui::{
buffer::Buffer,
layout::{Alignment, Rect},
style::{Color, Style, Stylize},
widgets::{Block, Borders, Paragraph, Widget},
widgets::{Block, Borders, Padding, Paragraph, Widget},
};
// pub enum CellTags {
@@ -59,7 +59,13 @@ impl Widget for CellWidget {
Block::default()
.borders(Borders::ALL)
.style(Style::default().fg(self.is_selected()))
.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 },
bottom: 0,
}),
)
.render(area, buf);
}