From 32e050ca262555f120caa3a38c0d69861a9f5eb3 Mon Sep 17 00:00:00 2001 From: Nicola Guerrera Date: Thu, 2 Mar 2023 13:55:30 +0100 Subject: [PATCH] Improvements by Nova --- examples/hexagon_launcher.rs | 21 +------------- src/main.rs | 2 +- src/protostar.rs | 56 ++++++++++++++++++++++++------------ src/xdg.rs | 13 +++++---- 4 files changed, 47 insertions(+), 45 deletions(-) diff --git a/examples/hexagon_launcher.rs b/examples/hexagon_launcher.rs index c337868..9fd04ae 100644 --- a/examples/hexagon_launcher.rs +++ b/examples/hexagon_launcher.rs @@ -9,7 +9,7 @@ use protostar::{ use stardust_xr_fusion::{ client::{Client, FrameInfo, RootHandler}, core::values::Transform, - drawable::{Alignment, Bounds, MaterialParameter, Model, ResourceID, Text, TextFit, TextStyle}, + drawable::{MaterialParameter, Model, ResourceID}, node::NodeError, spatial::Spatial, }; @@ -149,7 +149,6 @@ impl RootHandler for AppHexGrid { } } struct App { - _text: Text, _desktop_file: DesktopFile, protostar: ProtoStar, } @@ -161,27 +160,9 @@ impl App { desktop_file: DesktopFile, ) -> Option { let position = position.into(); - let style = TextStyle { - character_height: (APP_SIZE + PADDING) * 0.1, - bounds: Some(Bounds { - bounds: [(APP_SIZE + PADDING); 2].into(), - fit: TextFit::Wrap, - bounds_align: Alignment::XCenter | Alignment::YCenter, - }), - text_align: Alignment::XCenter | Alignment::YCenter, - ..Default::default() - }; let protostar = ProtoStar::create_from_desktop_file(parent, position, desktop_file.clone()).ok()?; - let text = Text::create( - protostar.content_parent(), - Transform::from_position_rotation([0.0, 0.0, 0.004], Quat::from_rotation_y(PI)), - desktop_file.name.as_deref().unwrap_or("Unknown"), - style, - ) - .unwrap(); Some(App { - _text: text, _desktop_file: desktop_file, protostar, }) diff --git a/src/main.rs b/src/main.rs index f59b05a..a41c780 100644 --- a/src/main.rs +++ b/src/main.rs @@ -42,7 +42,7 @@ async fn main() -> Result<()> { parse_desktop_file(desktop_file).map_err(|e| Report::msg(e))?, )? } else if let Some(command) = args.command { - ProtoStar::new_raw(client.get_root(), [0.0, 0.0, 0.0], None, command)? + ProtoStar::new_raw(client.get_root(), [0.0, 0.0, 0.0], None, None, command)? } else { bail!("No command or desktop file, nothing to launch."); }; diff --git a/src/protostar.rs b/src/protostar.rs index 199c452..a279684 100644 --- a/src/protostar.rs +++ b/src/protostar.rs @@ -7,7 +7,7 @@ use regex::Regex; use stardust_xr_fusion::{ client::{Client, FrameInfo, RootHandler}, core::values::Transform, - drawable::{MaterialParameter, Model, ResourceID}, + drawable::{Alignment, MaterialParameter, Model, ResourceID, Text, TextStyle}, fields::BoxField, node::NodeType, spatial::Spatial, @@ -62,9 +62,10 @@ pub struct ProtoStar { grabbable: Grabbable, field: BoxField, icon: Model, + label: Option, grabbable_shrink: Option>, grabbable_grow: Option>, - grabbabe_move: Option>, + grabbable_move: Option>, execute_command: String, currently_shown: bool, } @@ -98,6 +99,7 @@ impl ProtoStar { Self::new_raw( parent, position, + desktop_file.name.as_deref(), icon, desktop_file.command.ok_or_else(|| eyre!("No command"))?, ) @@ -105,6 +107,7 @@ impl ProtoStar { pub fn new_raw( parent: &Spatial, position: impl Into>, + name: Option<&str>, icon: Option, execute_command: String, ) -> Result { @@ -139,31 +142,50 @@ impl ProtoStar { &ResourceID::new_namespaced("protostar", "hexagon/hexagon"), )?) })?; + + let label_style = TextStyle { + character_height: 0.15, + bounds: None, + text_align: Alignment::Center.into(), + ..Default::default() + }; + let label = name.and_then(|name| { + Text::create( + &icon, + Transform::from_position_rotation( + [0.0, 0.05, -0.6], + Quat::from_rotation_x(PI * 0.5), + ), + name, + label_style, + ) + .ok() + }); Ok(ProtoStar { client: parent.client()?, position, grabbable, field, + label, icon, grabbable_shrink: None, grabbable_grow: None, execute_command, currently_shown: true, - grabbabe_move: None, + grabbable_move: None, }) } pub fn content_parent(&self) -> &Spatial { self.grabbable.content_parent() } pub fn toggle(&mut self) { + self.grabbable.set_enabled(!self.currently_shown).unwrap(); if self.currently_shown { - self.grabbabe_move = Some(Tweener::quart_in_out(1.0, 0.0001, 0.25)); //TODO make the scale a parameter + self.grabbable_move = Some(Tweener::quart_in_out(1.0, 0.0001, 0.25)); //TODO make the scale a parameter } else { - self.grabbable - .content_parent() - .set_scale(None, Vector3::from([1.0; 3])) - .unwrap(); - self.grabbabe_move = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25)); + self.icon.set_enabled(true).unwrap(); + self.label.as_ref().map(|l| l.set_enabled(true).unwrap()); + self.grabbable_move = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25)); } self.currently_shown = !self.currently_shown; } @@ -172,9 +194,9 @@ impl RootHandler for ProtoStar { fn frame(&mut self, info: FrameInfo) { self.grabbable.update(&info); - if let Some(grabbabe_move) = &mut self.grabbabe_move { - if !grabbabe_move.is_finished() { - let scale = grabbabe_move.move_by(info.delta); + if let Some(grabbable_move) = &mut self.grabbable_move { + if !grabbable_move.is_finished() { + let scale = grabbable_move.move_by(info.delta); self.grabbable .content_parent() .set_position( @@ -187,13 +209,11 @@ impl RootHandler for ProtoStar { ) .unwrap(); } else { - if grabbabe_move.final_value() == 0.0001 { - self.grabbable - .content_parent() - .set_scale(None, Vector3::from([0.001; 3])) - .unwrap(); + if grabbable_move.final_value() == 0.0001 { + self.icon.set_enabled(false).unwrap(); + self.label.as_ref().map(|l| l.set_enabled(false).unwrap()); } - self.grabbabe_move = None; + self.grabbable_move = None; } } if let Some(grabbable_shrink) = &mut self.grabbable_shrink { diff --git a/src/xdg.rs b/src/xdg.rs index e1b9f1d..9390232 100644 --- a/src/xdg.rs +++ b/src/xdg.rs @@ -195,7 +195,9 @@ impl DesktopFile { let cache_icon_path = get_image_cache_dir().join(icon_name).canonicalize(); if cache_icon_path.is_ok() { - return vec![Icon::from_path(cache_icon_path.unwrap(), 128).unwrap()]; + if let Some(icon) = Icon::from_path(cache_icon_path.unwrap(), 128) { + return vec![icon]; + } } let mut icons_iter = linicon::lookup_icon(icon_name) @@ -232,12 +234,11 @@ pub enum IconType { impl Icon { pub fn from_path(path: PathBuf, size: u16) -> Option { let icon_type = match path.extension().and_then(|ext| ext.to_str()) { - Some("png") => Some(IconType::Png), - Some("svg") => Some(IconType::Svg), - Some("glb") | Some("gltf") => Some(IconType::Gltf), + Some("png") => IconType::Png, + Some("svg") => IconType::Svg, + Some("glb") | Some("gltf") => IconType::Gltf, _ => return None, - } - .unwrap(); + }; return Some(Icon { icon_type, path,