diff --git a/src/apps.rs b/src/apps.rs index 5621045..887a8f5 100644 --- a/src/apps.rs +++ b/src/apps.rs @@ -1,219 +1,31 @@ +use crate::spotify::Spotify; + pub struct BeoApps { - pub apps: Vec>, + pub apps: Vec>, } -pub struct AppBase { - name: String, - pub main_menu: Menu, +#[derive(Debug)] +pub struct Title { + pub title: String, + //pub image… } -impl AppBase { - pub fn name(&self) -> &str { - &self.name - } - - pub fn enter_submenu(&mut self, submenu_id: usize) { - let mut current_menu = &mut self.main_menu; - while let Some(ref mut selected_submenu) = current_menu.selected_submenu { - current_menu = selected_submenu - } - current_menu.set_submenu_id(submenu_id) - } - - pub fn exit_submenu(&mut self) { - let current_menu = &mut self.main_menu; - while let Some(ref mut selected_submenu) = current_menu.selected_submenu { - if selected_submenu.selected_submenu.is_none() { - current_menu.selected_submenu = None; - return; - } - } - } +#[derive(Debug)] +pub struct AppView { + pub title: String, + pub menus: Vec, } -#[derive(Debug, Clone)] -pub struct Menu { - pub submenus: Vec<Menu>, - current_submenu_id: usize, - pub selected_submenu: Option<Box<Menu>>, - pub name: String, - //pub image: Option<Image>, -} +pub trait BeoApp { + fn name(&self) -> &str; + fn enter_menu(&mut self, menu_id: usize); + fn exit_menu(&mut self); + fn go(&mut self, menu_id: usize); -impl Menu { - pub fn set_submenu_id(&mut self, id: usize) { - if id >= self.submenus.len() { - panic!("Invalid submenu id"); - } - self.current_submenu_id = id; - self.selected_submenu = Some(Box::new(self.submenus[self.current_submenu_id].clone())); - } - - 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 { - fn base(&self) -> &AppBase; - fn base_mut(&mut self) -> &mut AppBase; - // fn main_menu(&self) -> &MainMenu; -} - -struct Spotify { - base: AppBase, -} - -impl Spotify { - fn new() -> Self { - let spotify_menus = vec![ - Menu { - submenus: vec![ - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Liked Songs".to_string(), - }, - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Recently Played".to_string(), - }, - ], - current_submenu_id: 0, - selected_submenu: None, - name: "Playlists".to_string(), - }, - Menu { - submenus: vec![ - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Mike Oldfield".to_string(), - }, - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "JM. Jarre".to_string(), - }, - ], - current_submenu_id: 0, - selected_submenu: None, - name: "Artists".to_string(), - }, - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Albums".to_string(), - }, - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Songs".to_string(), - }, - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Genres".to_string(), - }, - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "New Releases".to_string(), - }, - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Charts".to_string(), - }, - ]; - Spotify { - base: AppBase { - name: "Spotify".to_string(), - main_menu: Menu { - submenus: spotify_menus, - current_submenu_id: 0, - selected_submenu: None, - name: "XXX first one unused".to_string(), - }, - }, - } - } -} - -impl App for Spotify { - fn base(&self) -> &AppBase { - &self.base - } - fn base_mut(&mut self) -> &mut AppBase { - &mut self.base - } -} - -// Similar implementations for other apps like Radio and Settings -struct Radio { - base: AppBase, -} - -impl Radio { - fn new() -> Self { - let radio_menus = vec![ - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Favorites".to_string(), - }, - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Local".to_string(), - }, - Menu { - submenus: vec![], - current_submenu_id: 0, - selected_submenu: None, - name: "Global".to_string(), - }, - ]; - Radio { - base: AppBase { - name: "Radio".to_string(), - main_menu: Menu { - submenus: radio_menus, - current_submenu_id: 0, - selected_submenu: None, - name: "XXX first one unused".to_string(), - }, - }, - } - } -} - -impl App for Radio { - fn base(&self) -> &AppBase { - &self.base - } - fn base_mut(&mut self) -> &mut AppBase { - &mut self.base - } + fn get_current_view(&self) -> AppView; } pub fn get_beo_apps() -> BeoApps { - let apps: Vec<Box<dyn App>> = vec![Box::new(Spotify::new()), Box::new(Radio::new())]; + let apps: Vec<Box<dyn BeoApp>> = vec![Box::new(Spotify::new())]; BeoApps { apps } } diff --git a/src/hid.rs b/src/hid.rs index b52022d..4a9156b 100644 --- a/src/hid.rs +++ b/src/hid.rs @@ -152,22 +152,15 @@ impl Beo5Device { } } } - /* - evdev::Switch::SW_GO => { + evdev::Key::BTN_SELECT => { if ev.value() == 1 { - return Beo5Event::GoButtonPressed; - } else { - return Beo5Event::GoButtonReleased; + if ev.value() == 1 { + return Some(Beo5Event::GoButtonPressed); + } else { + return Some(Beo5Event::GoButtonReleased); + } } } - evdev::Switch::SW_POWER => { - if ev.value() == 1 { - return Beo5Event::PowerButtonPressed; - } else { - return Beo5Event::PowerButtonReleased; - } - } - */ // Shouldn't be any other switches _ => { panic!("Unknown switch event: {ev:?}") diff --git a/src/main.rs b/src/main.rs index 73c46d0..c3619f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,6 +18,7 @@ use glutin::prelude::*; mod apps; mod hid; mod roundy_math; +mod spotify; mod ui; #[allow(dead_code)] diff --git a/src/spotify.rs b/src/spotify.rs new file mode 100644 index 0000000..157e257 --- /dev/null +++ b/src/spotify.rs @@ -0,0 +1,54 @@ +use crate::apps::BeoApp; + +enum SpotifyMenuState { + Root, + Genres, + Genre(String), // (name) + Playlists, + Playlist(String), // (name +} + +#[derive(Debug, Default)] +pub struct Spotify { + i: i32, +} + +impl Spotify { + pub fn new() -> Self { + Self::default() + } +} + +impl BeoApp for Spotify { + fn name(&self) -> &str { + "Spotify" + } + + fn enter_menu(&mut self, _menu_id: usize) { + println!("Spotify2 entered"); + self.i += 1; + } + + fn exit_menu(&mut self) { + println!("Spotify2 exited"); + self.i -= 1; + } + + fn go(&mut self, _menu_id: usize) { + println!("Spotify2 go"); + } + + fn get_current_view(&self) -> crate::apps::AppView { + crate::apps::AppView { + title: "Spotify".to_string(), + menus: vec![ + crate::apps::Title { + title: format!("Genre {}", self.i), + }, + crate::apps::Title { + title: format!("Playlist {}", self.i), + }, + ], + } + } +} diff --git a/src/ui.rs b/src/ui.rs index 3682ac1..37cc680 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -1,8 +1,10 @@ +use std::thread::current; + use femtovg::{Baseline, Canvas, Color, Paint, Path, Renderer}; use crate::{hid::Beo5Event, Fonts}; -use crate::apps::{App, BeoApps}; +use crate::apps::{BeoApp, BeoApps}; use crate::roundy_math; pub struct BeoUi { @@ -83,11 +85,8 @@ impl BeoUi { } if let Some(selected_app) = self.current_app() { - let selected_menu = selected_app.base().main_menu.get_deepest_selected_submenu(); - let menu_count = selected_menu.submenus.len(); - println!("helol menu_count: {}", menu_count); + let menu_count = selected_app.get_current_view().menus.len(); 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 { self.angle_shift = max_angle; @@ -99,8 +98,8 @@ impl BeoUi { println!("choose_app_wheel_angle"); let angle = self.angle_shift; if let Some(selected_app) = self.current_app() { - let actual_menu = selected_app.base().main_menu.get_deepest_selected_submenu(); - let menu_count = actual_menu.submenus.len(); + let actual_menu = selected_app.get_current_view().menus; + let menu_count = actual_menu.len(); let max_angle = (menu_count - 1) as f32 * ANGLE_DEG_BETWEEN_MENU_ITEMS - 10.; let angle = angle.min(max_angle); @@ -120,24 +119,24 @@ impl BeoUi { } Beo5Event::LeftButtonPressed => { println!("Left button pressed. Will select submenu"); - let submenu_id = self.get_selected_wheel_angle_menu_id(); - match submenu_id { - Some(submenu_id) => { - self.current_app_mut() - .unwrap() - .base_mut() - .enter_submenu(submenu_id); - self.angle_shift = 0.0; - } - None => { - println!("No submenu id found"); + let selected_menu_id = self.get_selected_wheel_angle_menu_id(); + if let Some(app) = self.current_app_mut() { + match selected_menu_id { + Some(menu_id) => { + app.enter_menu(menu_id); + self.angle_shift = 0.0; + } + None => { + println!("No menu id found"); + } } } } Beo5Event::RightButtonPressed => { // That means that we get out of a submenu - self.current_app_mut().unwrap().base_mut().exit_submenu(); - println!("Right button pressed"); + if let Some(app) = self.current_app_mut() { + app.exit_menu(); + } } Beo5Event::SelectionWheelRel(rel_angle_eps) => { self.time_without_wheel_spin = 0.0; @@ -149,13 +148,9 @@ impl BeoUi { } } println!( - "Current submenu: {:?}", - self.current_app_mut() - .unwrap() - .base_mut() - .main_menu - .get_deepest_selected_submenu() - .name + "Current app {:?} - VIEW: {:?}", + self.current_app().unwrap().name(), + self.current_app().unwrap().get_current_view() ); } @@ -188,10 +183,10 @@ impl BeoUi { self.current_app_id = None; } - fn current_app(&self) -> Option<&Box<dyn App>> { + fn current_app(&self) -> Option<&Box<dyn BeoApp>> { self.current_app_id.map(|id| &self.beo_apps.apps[id]) } - fn current_app_mut(&mut self) -> Option<&mut Box<dyn App>> { + fn current_app_mut(&mut self) -> Option<&mut Box<dyn BeoApp>> { self.current_app_id.map(|id| &mut self.beo_apps.apps[id]) } @@ -234,7 +229,7 @@ impl BeoUi { }; let app = &apps[appid]; - let _ = canvas.fill_text(pts[appid].x, pts[appid].y, app.base().name(), paint); + let _ = canvas.fill_text(pts[appid].x, pts[appid].y, app.name(), paint); } // draw the laser @@ -247,8 +242,8 @@ impl BeoUi { canvas.fill_path(&path, &ellipse_color); } - fn draw_app<T: Renderer>(&self, canvas: &mut Canvas<T>, fonts: &Fonts, app: &Box<dyn App>) { - let current_app_deepest_menu = &app.base().main_menu.get_deepest_selected_submenu(); + fn draw_app<T: Renderer>(&self, canvas: &mut Canvas<T>, fonts: &Fonts, app: &Box<dyn BeoApp>) { + let current_app_menu = app.get_current_view(); let menu_circle = roundy_math::VirtualCircle { center: roundy_math::Point { @@ -258,14 +253,11 @@ impl BeoUi { radius: MENU_CIRCLE_RADIUS, }; - for i in 0..current_app_deepest_menu.submenus.len() { + for i in 0..current_app_menu.menus.len() { let angle = i as f32 * -ANGLE_DEG_BETWEEN_MENU_ITEMS + self.angle_shift; if angle > -90. && angle < 90. { let pos = menu_circle.get_point_from_angle_bcircle(angle); - let n = format!( - "{}: {} ({}°)", - i, current_app_deepest_menu.submenus[i].name, angle - ); + let n = format!("{}: {} ({}°)", i, current_app_menu.menus[i].title, angle); let _ = canvas.fill_text( pos.x, pos.y, @@ -275,114 +267,5 @@ impl BeoUi { continue; } } - - /* - let menus_count = current_app_deepest_menu.submenus.len(); - - let menu_circle = roundy_math::VirtualCircle { - center: roundy_math::Point { - x: canvas_width as f32, - y: canvas_height as f32 / 2.0, - }, - radius: MENU_CIRCLE_RADIUS, - }; - - let canvas_size = roundy_math::Point { - x: canvas_width as f32, - y: canvas_height as f32, - }; - - // draw the main apps in the circle - let apps = &self.beo_apps.apps; - let pts = laser_menu_circle.get_equidistant_points(apps.len(), canvas_size); - - // XXX To be taken from global struct - let mut paint_normal = Paint::color(Color::hex("B7410E")); - paint_normal.set_font(&[fonts.sans]); - paint_normal.set_text_baseline(Baseline::Top); - - let mut paint_selected = Paint::color(Color::hex("D7612E")); - paint_selected.set_font(&[fonts.bold]); - paint_selected.set_text_baseline(Baseline::Top); - - for appid in 0..apps.len() { - let paint; - if self.current_app_id == Some(appid) { - paint = &paint_selected; - } else { - paint = &paint_normal; - } - - let app = &apps[appid]; - let _ = canvas.fill_text(pts[appid].x, pts[appid].y, app.base().name(), paint); - } - - // draw the laser - let ellipse_color = Paint::color(Color::hex("5C89D188")); - let mut path = Path::new(); - let ey = laser_pct_to_y_pos(self.laser_pct); - let ex = laser_menu_circle.get_x_on_circle(ey); - - path.ellipse(ex + 15., ey, 30., 10.); - canvas.fill_path(&path, &ellipse_color); - */ } - /* - - - - let num_menu_elements_before = 1; - let num_menu_elements_after = 2; - // That means max 1 + >1< + 2 = 4 elements in total - // Minimum aronud 3: 0 + >1< + 2 - - let current_menu_id = app.base().main_menu.selected_id; - let menu_elements = &app.base().main_menu.names; - - // Calculate indices for slices - let start_before = - (current_menu_id as isize - num_menu_elements_before as isize).max(0) as usize; - let end_before = current_menu_id; - let start_after = current_menu_id + 1; - let end_after = - ((current_menu_id + 1 + num_menu_elements_after).min(menu_elements.len())) as usize; - - // Safely getting slices using clamping - let menu_elems_before = &menu_elements[start_before..end_before]; - let menu_elem_selected = &menu_elements[current_menu_id]; - let menu_elems_after = &menu_elements[start_after..end_after]; - - let mut toti = 0.; - for i in 0..num_menu_elements_before { - // This is a special case: we reserve some space - //so that the current menu is always at the same place - if i < menu_elems_before.len() { - let _ = canvas.fill_text( - toti * 100. + 50., - CANVAS_HEIGHT - 80., - &menu_elems_before[i], - &fonts.app_menu, - ); - } - toti += 1.; - } - - let _ = canvas.fill_text( - toti * 100. + 50., - CANVAS_HEIGHT - 80., - &menu_elem_selected, - &fonts.app_menu_selected, - ); - toti += 1.; - - for i in 0..menu_elems_after.len() { - let _ = canvas.fill_text( - toti * 100. + 50., - CANVAS_HEIGHT - 80., - &menu_elems_after[i], - &fonts.app_menu, - ); - toti += 1.; - } - */ }