fix selection ui

This commit is contained in:
2025-11-10 13:30:37 -07:00
parent d9886fdea1
commit 691ef2b26b
2 changed files with 26 additions and 15 deletions

View File

@@ -53,7 +53,6 @@ impl<'a> SelectionUi<'a> {
loop { loop {
let key = get_key(); let key = get_key();
if key.state == KeyState::Pressed { if key.state == KeyState::Pressed {
print!("Got Key press: {:?}", key.key);
if let Some(s) = self.update(display, key.key)? { if let Some(s) = self.update(display, key.key)? {
selection = Some(s); selection = Some(s);
break; break;
@@ -70,16 +69,18 @@ impl<'a> SelectionUi<'a> {
display: &mut Display, display: &mut Display,
key: KeyCode, key: KeyCode,
) -> Result<Option<usize>, SelectionUiError<<Display as DrawTarget>::Error>> { ) -> Result<Option<usize>, SelectionUiError<<Display as DrawTarget>::Error>> {
print!("Got Key: {:?}", key);
match key { match key {
KeyCode::JoyDown => { KeyCode::Down => {
self.selection = (self.selection + 1).min(self.items.len() - 1); self.selection = (self.selection + 1).min(self.items.len() - 1);
} }
KeyCode::JoyUp => { KeyCode::Up => {
self.selection = self.selection.saturating_sub(1); self.selection = self.selection.saturating_sub(1);
} }
KeyCode::Enter | KeyCode::JoyRight => return Ok(Some(self.selection)), KeyCode::Enter | KeyCode::Right => return Ok(Some(self.selection)),
_ => return Ok(Some(self.selection)), _ => return Ok(None),
}; };
print!("new selection: {:?}", self.selection);
self.draw(display)?; self.draw(display)?;
Ok(None) Ok(None)
} }
@@ -95,6 +96,13 @@ impl<'a> SelectionUi<'a> {
return Err(SelectionUiError::SelectionListEmpty); return Err(SelectionUiError::SelectionListEmpty);
} }
if let Some(bounds) = self.last_bounds {
Rectangle::new(bounds.top_left, bounds.size)
.into_styled(PrimitiveStyle::with_fill(Rgb565::BLACK))
.draw(display)
.map_err(|e| SelectionUiError::DisplayError(e))?;
}
let mut views: Vec<Text<MonoTextStyle<Rgb565>>> = Vec::new(); let mut views: Vec<Text<MonoTextStyle<Rgb565>>> = Vec::new();
for i in self.items { for i in self.items {
@@ -109,6 +117,10 @@ impl<'a> SelectionUi<'a> {
.arrange() .arrange()
.align_to(&display_area, horizontal::Center, vertical::Center); .align_to(&display_area, horizontal::Center, vertical::Center);
layout
.draw(display)
.map_err(|e| SelectionUiError::DisplayError(e))?;
// draw selected box // draw selected box
if let Some(selected_bounds) = layout.inner().get(self.selection) { if let Some(selected_bounds) = layout.inner().get(self.selection) {
let selected_bounds = selected_bounds.bounding_box(); let selected_bounds = selected_bounds.bounding_box();
@@ -117,12 +129,10 @@ impl<'a> SelectionUi<'a> {
.draw(display) .draw(display)
.map_err(|e| SelectionUiError::DisplayError(e))?; .map_err(|e| SelectionUiError::DisplayError(e))?;
self.last_bounds = Some(layout.bounds()); self.last_bounds = Some(selected_bounds);
} }
layout Ok(())
.draw(display)
.map_err(|e| SelectionUiError::DisplayError(e))
} }
} }

View File

@@ -3,7 +3,7 @@
extern crate alloc; extern crate alloc;
use abi::{ use abi::{
display::Display, display::{Display, SCREEN_HEIGHT, SCREEN_WIDTH},
fs::{Entries, file_len, list_dir, read_file}, fs::{Entries, file_len, list_dir, read_file},
get_key, get_ms, get_key, get_ms,
keyboard::{KeyCode, KeyState}, keyboard::{KeyCode, KeyState},
@@ -70,17 +70,18 @@ pub fn main() {
assert!(read == size); assert!(read == size);
let gif = Gif::<Rgb565>::from_slice(&buf).expect("Failed to parse gif"); let gif = Gif::<Rgb565>::from_slice(&buf).expect("Failed to parse gif");
let height = gif.height();
let translation = Point::new(
(SCREEN_WIDTH as i32 - gif.width() as i32) / 2,
(SCREEN_HEIGHT as i32 - gif.height() as i32) / 2,
);
let mut frame_num = 0; let mut frame_num = 0;
loop { loop {
for mut frame in gif.frames() { for mut frame in gif.frames() {
let start = get_ms(); let start = get_ms();
frame frame.translate_mut(translation).draw(&mut display).unwrap();
.translate_mut(Point::new(0, (320 - height as i32) / 2))
.draw(&mut display)
.unwrap();
frame_num += 1; frame_num += 1;
if frame_num % 5 == 0 { if frame_num % 5 == 0 {