rework app logic

Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
Frank Villaro-Dixon 2024-05-09 00:17:11 +02:00
parent ff510a8692
commit fa132e2230
2 changed files with 27 additions and 16 deletions

View file

@ -50,7 +50,8 @@ fn run(
.expect("Cannot add font"),
};
let mut beo = ui::BeoUi::new(get_beo_apps());
let apps = get_beo_apps();
let mut beo = ui::BeoUi::new(apps);
let start = Instant::now();
let mut prevt = start;

View file

@ -7,6 +7,7 @@ use crate::roundy_math;
pub struct BeoUi {
pub beo_apps: BeoApps,
pub current_app_id: Option<usize>,
pub laser_pct: f32,
}
@ -24,6 +25,7 @@ impl BeoUi {
pub fn new(apps: BeoApps) -> BeoUi {
BeoUi {
beo_apps: apps,
current_app_id: None,
laser_pct: 0.0,
}
}
@ -32,7 +34,7 @@ impl BeoUi {
match event {
Beo5Event::LaserPosition(pct) => {
self.laser_pct = pct;
println!("Laser moved to {}%", pct * 100.);
self.choose_app_from_laser_pct();
}
_ => {
// TODO: pass event to current app
@ -42,9 +44,7 @@ impl BeoUi {
pub fn draw<T: Renderer>(&self, canvas: &mut Canvas<T>, fonts: &Fonts) {
self.draw_main_menu(canvas, fonts);
if let Some(selected_app_id) = self.lasered_application_id() {
let selected_app = self.beo_apps.apps.get(selected_app_id).unwrap();
if let Some(selected_app) = self.current_app() {
let mut paint_title = Paint::color(Color::hex("FFFFFF"));
paint_title.set_font(&[fonts.bold]);
paint_title.set_text_baseline(Baseline::Top);
@ -59,10 +59,11 @@ impl BeoUi {
}
}
fn lasered_application_id(&self) -> Option<usize> {
fn choose_app_from_laser_pct(&mut self) {
let app_count = self.beo_apps.apps.len();
if app_count == 0 {
return None;
self.current_app_id = None;
return;
}
let dx = 100.0 / (app_count as f32 * 2.);
@ -72,10 +73,16 @@ impl BeoUi {
let evt_app_pos = dx * (i * 2 + 1) as f32 * CANVAS_HEIGHT / 100.0;
let delta = (laser_y - evt_app_pos).abs();
if delta < LASER_EPS_MATCH {
return Some(i);
self.current_app_id = Some(i);
return;
//Some(&self.beo_apps.apps[i]);
}
}
None
self.current_app_id = None;
}
fn current_app(&self) -> Option<&Box<dyn App>> {
self.current_app_id.map(|id| &self.beo_apps.apps[id])
}
fn draw_main_menu<T: Renderer>(&self, canvas: &mut Canvas<T>, fonts: &Fonts) {
@ -107,13 +114,16 @@ impl BeoUi {
let mut paint_selected = Paint::color(Color::hex("D7612E"));
paint_selected.set_font(&[fonts.bold]);
paint_selected.set_text_baseline(Baseline::Top);
for i in 0..apps.len() {
if self.lasered_application_id() == Some(i) {
let _ =
canvas.fill_text(pts[i].x, pts[i].y, apps[i].base().name(), &paint_selected);
} else {
let _ = canvas.fill_text(pts[i].x, pts[i].y, apps[i].base().name(), &paint_normal);
}
let mut i = 0;
for app in apps {
//if self.current_app == Some(app) {
// let _ =
// canvas.fill_text(pts[i].x, pts[i].y, apps[i].base().name(), &paint_selected);
//} else {
let _ = canvas.fill_text(pts[i].x, pts[i].y, app.base().name(), &paint_normal);
// }
i += 1;
}
// draw the laser