generated from GarandPLG/rust-flake-template
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:
@@ -9,35 +9,47 @@ pub fn skirmish_keybindings(app: &mut App, key_event: &KeyEvent) {
|
|||||||
if let Some(action) = event_to_action(&key_event) {
|
if let Some(action) = event_to_action(&key_event) {
|
||||||
common_keybindings(app, action);
|
common_keybindings(app, action);
|
||||||
match action {
|
match action {
|
||||||
Action::Up => {
|
Action::Up => app.states.skirmish.focused_cell.move_up(),
|
||||||
app.states.skirmish.focused_cell.move_up();
|
|
||||||
|
|
||||||
// if app.states.skirmish.horizontal_offset.get_value() {
|
|
||||||
|
|
||||||
// }
|
|
||||||
}
|
|
||||||
Action::Down => app.states.skirmish.focused_cell.move_down(),
|
Action::Down => app.states.skirmish.focused_cell.move_down(),
|
||||||
Action::Left => app.states.skirmish.focused_cell.move_left(),
|
Action::Left => app.states.skirmish.focused_cell.move_left(),
|
||||||
Action::Right => app.states.skirmish.focused_cell.move_right(),
|
Action::Right => app.states.skirmish.focused_cell.move_right(),
|
||||||
Action::ScrollUp => app.states.skirmish.vertical_offset.prev(),
|
Action::ScrollUp => {
|
||||||
Action::ScrollDown => app.states.skirmish.vertical_offset.next(),
|
app.states.skirmish.vertical_offset.prev();
|
||||||
Action::ScrollLeft => app.states.skirmish.horizontal_offset.prev(),
|
app.states.skirmish.focused_cell.move_up();
|
||||||
Action::ScrollRight => app.states.skirmish.horizontal_offset.next(),
|
}
|
||||||
|
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 {
|
Action::ZoomIn => match app.states.skirmish.zoom_level {
|
||||||
ZoomLevel::ZoomedIn => {}
|
ZoomLevel::ZoomedIn => {}
|
||||||
ZoomLevel::Default => {
|
ZoomLevel::Default => {
|
||||||
app.states.skirmish.zoom_level = ZoomLevel::ZoomedIn;
|
app.states.skirmish.zoom_level = ZoomLevel::ZoomedIn;
|
||||||
|
app.states.skirmish.vertical_offset.next();
|
||||||
}
|
}
|
||||||
ZoomLevel::ZoomedOut => {
|
ZoomLevel::ZoomedOut => {
|
||||||
app.states.skirmish.zoom_level = ZoomLevel::Default;
|
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 {
|
Action::ZoomOut => match app.states.skirmish.zoom_level {
|
||||||
ZoomLevel::ZoomedIn => {
|
ZoomLevel::ZoomedIn => {
|
||||||
app.states.skirmish.zoom_level = ZoomLevel::Default;
|
app.states.skirmish.zoom_level = ZoomLevel::Default;
|
||||||
|
app.states.skirmish.vertical_offset.prev();
|
||||||
}
|
}
|
||||||
ZoomLevel::Default => {
|
ZoomLevel::Default => {
|
||||||
app.states.skirmish.zoom_level = ZoomLevel::ZoomedOut;
|
app.states.skirmish.zoom_level = ZoomLevel::ZoomedOut;
|
||||||
|
app.states.skirmish.vertical_offset.prev();
|
||||||
|
app.states.skirmish.vertical_offset.prev();
|
||||||
}
|
}
|
||||||
ZoomLevel::ZoomedOut => {}
|
ZoomLevel::ZoomedOut => {}
|
||||||
},
|
},
|
||||||
|
|||||||
+6
-1
@@ -32,7 +32,12 @@ impl GameStates {
|
|||||||
map_height: args.map_height as usize,
|
map_height: args.map_height as usize,
|
||||||
board_cells: Vec::new(),
|
board_cells: Vec::new(),
|
||||||
zoom_level: args.zoom_level,
|
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 {
|
perk_decks: PerkDecksState {
|
||||||
id: 2,
|
id: 2,
|
||||||
|
|||||||
@@ -54,27 +54,34 @@ impl Offset {
|
|||||||
pub struct FocusedCell {
|
pub struct FocusedCell {
|
||||||
pub row: usize,
|
pub row: usize,
|
||||||
pub col: usize,
|
pub col: usize,
|
||||||
|
pub max_row: usize,
|
||||||
|
pub max_col: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FocusedCell {
|
impl FocusedCell {
|
||||||
pub fn new(row: usize, col: usize) -> Self {
|
pub fn new(row: usize, col: usize, max_row: usize, max_col: usize) -> Self {
|
||||||
Self { row, col }
|
Self {
|
||||||
|
row,
|
||||||
|
col,
|
||||||
|
max_row,
|
||||||
|
max_col,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn move_up(&mut self) {
|
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) {
|
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) {
|
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) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use ratatui::{
|
|||||||
buffer::Buffer,
|
buffer::Buffer,
|
||||||
layout::{Alignment, Rect},
|
layout::{Alignment, Rect},
|
||||||
style::{Color, Style, Stylize},
|
style::{Color, Style, Stylize},
|
||||||
widgets::{Block, Borders, Paragraph, Widget},
|
widgets::{Block, Borders, Padding, Paragraph, Widget},
|
||||||
};
|
};
|
||||||
|
|
||||||
// pub enum CellTags {
|
// pub enum CellTags {
|
||||||
@@ -59,7 +59,13 @@ impl Widget for CellWidget {
|
|||||||
Block::default()
|
Block::default()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.style(Style::default().fg(self.is_selected()))
|
.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);
|
.render(area, buf);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user