wheel works

Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
Frank Villaro-Dixon 2024-05-16 21:55:24 +02:00
parent dd7fa0ecce
commit 376ddf53ac
2 changed files with 83 additions and 40 deletions

View file

@ -23,25 +23,29 @@ pub struct Menu {
} }
impl Menu { impl Menu {
pub fn go_left(&mut self) { pub fn set_submenu_id(&mut self, id: usize) {
if self.current_submenu_id > 0 { if id >= self.submenus.len() {
self.current_submenu_id -= 1; panic!("Invalid submenu id");
} }
} self.current_submenu_id = id;
pub fn go_right(&mut self) {
if self.current_submenu_id < self.submenus.len() - 1 {
self.current_submenu_id += 1;
}
}
fn select_submenu(&mut self) {
self.selected_submenu = Some(self.submenus[self.current_submenu_id].clone()); self.selected_submenu = Some(self.submenus[self.current_submenu_id].clone());
} }
pub fn get_deepest_selected_submenu(&self) -> &Menu { pub fn get_deepest_selected_submenu_mut(&mut self) -> &mut Menu {
match &self.selected_submenu { // Use a loop to traverse to the deepest submenu
Some(selected_submenu) => selected_submenu.get_deepest_selected_submenu(), let mut current_menu = self;
None => self, while let Some(ref mut selected_submenu) = current_menu.selected_submenu {
current_menu = selected_submenu;
} }
current_menu
}
pub fn get_deepest_selected_submenu(&self) -> &Menu {
// Use a loop to traverse to the deepest submenu
let mut current_menu = self;
while let Some(ref selected_submenu) = current_menu.selected_submenu {
current_menu = selected_submenu;
}
current_menu
} }
} }
pub trait App { pub trait App {
@ -58,13 +62,39 @@ impl Spotify {
fn new() -> Self { fn new() -> Self {
let spotify_menus = vec![ let spotify_menus = vec![
Box::new(Menu { Box::new(Menu {
submenus: vec![], submenus: vec![
Box::new(Menu {
submenus: vec![],
current_submenu_id: 0,
selected_submenu: None,
name: "Liked Songs".to_string(),
}),
Box::new(Menu {
submenus: vec![],
current_submenu_id: 0,
selected_submenu: None,
name: "Recently Played".to_string(),
}),
],
current_submenu_id: 0, current_submenu_id: 0,
selected_submenu: None, selected_submenu: None,
name: "Playlists".to_string(), name: "Playlists".to_string(),
}), }),
Box::new(Menu { Box::new(Menu {
submenus: vec![], submenus: vec![
Box::new(Menu {
submenus: vec![],
current_submenu_id: 0,
selected_submenu: None,
name: "Mike Oldfield".to_string(),
}),
Box::new(Menu {
submenus: vec![],
current_submenu_id: 0,
selected_submenu: None,
name: "JM. Jarre".to_string(),
}),
],
current_submenu_id: 0, current_submenu_id: 0,
selected_submenu: None, selected_submenu: None,
name: "Artists".to_string(), name: "Artists".to_string(),

View file

@ -2,7 +2,7 @@ use femtovg::{Align, Baseline, Canvas, Color, Paint, Path, Renderer};
use crate::{hid::Beo5Event, Fonts}; use crate::{hid::Beo5Event, Fonts};
use crate::apps::{App, BeoApps}; use crate::apps::{App, BeoApps, Menu};
use crate::roundy_math; use crate::roundy_math;
pub struct BeoUi { pub struct BeoUi {
@ -85,36 +85,31 @@ impl BeoUi {
if let Some(selected_app) = self.current_app() { if let Some(selected_app) = self.current_app() {
let selected_menu = selected_app.base().main_menu.get_deepest_selected_submenu(); let selected_menu = selected_app.base().main_menu.get_deepest_selected_submenu();
let menu_count = selected_menu.submenus.len(); let menu_count = selected_menu.submenus.len();
println!("helol menu_count: {}", menu_count);
let max_angle = (menu_count - 1) as f32 * ANGLE_DEG_BETWEEN_MENU_ITEMS + 10.; let max_angle = (menu_count - 1) as f32 * ANGLE_DEG_BETWEEN_MENU_ITEMS + 10.;
println!("max_angle: {}", max_angle);
if self.angle_shift > max_angle { if self.angle_shift > max_angle {
self.angle_shift = max_angle; self.angle_shift = max_angle;
} }
} }
self.choose_app_wheel_angle();
} }
fn choose_app_wheel_angle(&mut self) { pub fn get_selected_wheel_angle_menu_id(&self) -> Option<usize> {
println!("choose_app_wheel_angle");
let angle = self.angle_shift;
if let Some(selected_app) = self.current_app() { if let Some(selected_app) = self.current_app() {
let selected_menu = selected_app.base().main_menu.get_deepest_selected_submenu(); let actual_menu = selected_app.base().main_menu.get_deepest_selected_submenu();
let menu_count = selected_menu.submenus.len(); let menu_count = actual_menu.submenus.len();
let max_angle = (menu_count - 1) as f32 * -ANGLE_DEG_BETWEEN_MENU_ITEMS - 10.; let max_angle = (menu_count - 1) as f32 * ANGLE_DEG_BETWEEN_MENU_ITEMS - 10.;
let angle = self.angle_shift; let angle = angle.min(max_angle);
println!("Angle before: {}", angle); let angle = angle.max(0.);
let angle = angle.max(max_angle);
let angle = angle.min(0.);
println!("Angle: {}", angle);
let menu_id_unrounded = (angle - max_angle) / ANGLE_DEG_BETWEEN_MENU_ITEMS;
println!("Menu id unrounded: {}", menu_id_unrounded);
let menu_id_unrounded = angle / ANGLE_DEG_BETWEEN_MENU_ITEMS;
println!("Menu id unrounded bis : {}", menu_id_unrounded);
let menu_id = (angle / ANGLE_DEG_BETWEEN_MENU_ITEMS).round() as usize; let menu_id = (angle / ANGLE_DEG_BETWEEN_MENU_ITEMS).round() as usize;
println!("Menu id: {}", menu_id); return Some(menu_id);
//selected_app.base_mut().main_menu.selected_id = menu_id;
} }
None
} }
pub fn accept_event(&mut self, event: Beo5Event) { pub fn accept_event(&mut self, event: Beo5Event) {
@ -124,16 +119,25 @@ impl BeoUi {
self.choose_app_from_laser_pct(); self.choose_app_from_laser_pct();
} }
Beo5Event::LeftButtonPressed => { Beo5Event::LeftButtonPressed => {
println!("Left button pressed"); println!("Left button pressed. Will select submenu");
if let Some(selected_app) = self.current_app_mut() { let submenu_id = self.get_selected_wheel_angle_menu_id();
selected_app.base_mut().main_menu.go_left(); match submenu_id {
Some(submenu_id) => {
self.current_app_mut()
.unwrap()
.base_mut()
.main_menu
.get_deepest_selected_submenu_mut()
.set_submenu_id(submenu_id);
self.angle_shift = 0.0;
}
None => {
println!("No submenu id found");
}
} }
} }
Beo5Event::RightButtonPressed => { Beo5Event::RightButtonPressed => {
println!("Right button pressed"); println!("Right button pressed");
if let Some(selected_app) = self.current_app_mut() {
selected_app.base_mut().main_menu.go_right();
}
} }
Beo5Event::SelectionWheelRel(rel_angle_eps) => { Beo5Event::SelectionWheelRel(rel_angle_eps) => {
self.time_without_wheel_spin = 0.0; self.time_without_wheel_spin = 0.0;
@ -144,6 +148,15 @@ impl BeoUi {
// TODO: pass event to current app // TODO: pass event to current app
} }
} }
println!(
"Current submenu: {:?}",
self.current_app_mut()
.unwrap()
.base_mut()
.main_menu
.get_deepest_selected_submenu()
.name
);
} }
pub fn draw<T: Renderer>(&self, canvas: &mut Canvas<T>, fonts: &Fonts) { pub fn draw<T: Renderer>(&self, canvas: &mut Canvas<T>, fonts: &Fonts) {