refactor shit

Signed-off-by: Frank Villaro-Dixon <frank@villaro-dixon.eu>
This commit is contained in:
Frank Villaro-Dixon 2024-05-09 00:00:45 +02:00
parent 8341b9d54c
commit ff510a8692
3 changed files with 128 additions and 33 deletions

View file

@ -1,31 +1,122 @@
pub struct BeoApps {
pub apps: Vec<Box<dyn App>>,
}
struct MainMenu {
pub last_id: usize,
pub default_id: usize,
pub names: Vec<String>,
}
pub struct AppBase {
name: String,
pub main_menu: MainMenu,
}
impl AppBase {
pub fn name(&self) -> &str {
&self.name
}
}
pub trait App {
fn name(&self) -> &str;
fn base(&self) -> &AppBase;
// fn main_menu(&self) -> &MainMenu;
}
struct Spotify {
base: AppBase,
}
impl Spotify {
fn new() -> Self {
Spotify {
base: AppBase {
name: "Spotify".to_string(),
main_menu: MainMenu {
last_id: 0,
default_id: 0,
names: vec![
"Playlists".to_string(),
"Artists".to_string(),
"Albums".to_string(),
],
},
},
}
}
}
struct Spotify;
impl App for Spotify {
fn name(&self) -> &str {
"Spotify"
fn base(&self) -> &AppBase {
&self.base
}
}
struct Radio;
// Similar implementations for other apps like Radio and Settings
struct Radio {
base: AppBase,
}
impl Radio {
fn new() -> Self {
Radio {
base: AppBase {
name: "Radio".to_string(),
main_menu: MainMenu {
last_id: 0,
default_id: 0,
names: vec![
"Favorites".to_string(),
"Local".to_string(),
"Global".to_string(),
],
},
},
}
}
}
impl App for Radio {
fn name(&self) -> &str {
"Radio"
fn base(&self) -> &AppBase {
&self.base
}
}
struct Settings {
base: AppBase,
}
impl Settings {
fn new() -> Self {
Settings {
base: AppBase {
name: "Settings".to_string(),
main_menu: MainMenu {
last_id: 0,
default_id: 0,
names: vec![
"Display".to_string(),
"Sound".to_string(),
"Network".to_string(),
],
},
},
}
}
}
struct Settings;
impl App for Settings {
fn name(&self) -> &str {
"Settings"
fn base(&self) -> &AppBase {
&self.base
}
}
pub fn get_apps() -> Vec<Box<dyn App>> {
vec![
Box::new(Spotify {}),
Box::new(Radio {}),
Box::new(Settings {}),
]
pub fn get_beo_apps() -> BeoApps {
let apps: Vec<Box<dyn App>> = vec![
Box::new(Spotify::new()),
Box::new(Radio::new()),
Box::new(Settings::new()),
];
BeoApps { apps }
}

View file

@ -1,3 +1,4 @@
use apps::get_beo_apps;
use femtovg::{
renderer::OpenGl, Align, Baseline, Canvas, Color, FontId, ImageId, Paint, Path, Renderer,
};
@ -49,10 +50,7 @@ fn run(
.expect("Cannot add font"),
};
let mut beo = ui::Beo::new();
for app in &beo.apps {
println!(">> {}", app.name());
}
let mut beo = ui::BeoUi::new(get_beo_apps());
let start = Instant::now();
let mut prevt = start;

View file

@ -2,11 +2,11 @@ use femtovg::{Align, Baseline, Canvas, Color, Paint, Path, Renderer};
use crate::{hid::Beo5Event, Fonts};
use crate::apps::{get_apps, App};
use crate::apps::{App, BeoApps};
use crate::roundy_math;
pub struct Beo {
pub apps: Vec<Box<dyn App>>,
pub struct BeoUi {
pub beo_apps: BeoApps,
pub laser_pct: f32,
}
@ -20,10 +20,10 @@ fn laser_pct_to_y_pos(pct: f32) -> f32 {
(pct * 1.5 - 0.25) * CANVAS_HEIGHT
}
impl Beo {
pub fn new() -> Beo {
Beo {
apps: get_apps(),
impl BeoUi {
pub fn new(apps: BeoApps) -> BeoUi {
BeoUi {
beo_apps: apps,
laser_pct: 0.0,
}
}
@ -43,19 +43,24 @@ impl Beo {
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.apps.get(selected_app_id).unwrap();
let selected_app = self.beo_apps.apps.get(selected_app_id).unwrap();
let mut paint_title = Paint::color(Color::hex("FFFFFF"));
paint_title.set_font(&[fonts.bold]);
paint_title.set_text_baseline(Baseline::Top);
paint_title.set_text_align(Align::Center);
paint_title.set_font_size(20.);
let _ = canvas.fill_text(CANVAS_WIDTH / 2., 10., selected_app.name(), &paint_title);
let _ = canvas.fill_text(
CANVAS_WIDTH / 2.,
10.,
selected_app.base().name(),
&paint_title,
);
}
}
fn lasered_application_id(&self) -> Option<usize> {
let app_count = self.apps.len();
let app_count = self.beo_apps.apps.len();
if app_count == 0 {
return None;
}
@ -91,7 +96,7 @@ impl Beo {
y: canvas_height as f32,
};
let apps = &self.apps;
let apps = &self.beo_apps.apps;
// draw the main apps in the circle
let pts = main_menu_circle.get_equidistant_points(apps.len(), canvas_size);
@ -104,9 +109,10 @@ impl Beo {
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].name(), &paint_selected);
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].name(), &paint_normal);
let _ = canvas.fill_text(pts[i].x, pts[i].y, apps[i].base().name(), &paint_normal);
}
}