fix app_grid

This commit is contained in:
Nova
2023-05-14 16:46:28 -04:00
parent 963cdcbfde
commit 70f7e39f05
3 changed files with 51 additions and 70 deletions

Binary file not shown.

View File

@@ -1,10 +1,9 @@
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use glam::Quat; use glam::{Quat, Vec3};
use manifest_dir_macros::directory_relative_path; use manifest_dir_macros::directory_relative_path;
use mint::Vector3; use mint::Vector3;
use protostar::{ use protostar::{
application::Application, application::Application,
protostar::ProtoStar,
xdg::{get_desktop_files, parse_desktop_file, DesktopFile, Icon, IconType}, xdg::{get_desktop_files, parse_desktop_file, DesktopFile, Icon, IconType},
}; };
use stardust_xr_fusion::{ use stardust_xr_fusion::{
@@ -83,18 +82,18 @@ impl RootHandler for AppGrid {
fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> { fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
return match &icon.icon_type { return match &icon.icon_type {
IconType::Png => { IconType::Png => {
let t = Transform::from_rotation_scale( // let t = Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI), // Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[APP_SIZE * 0.5; 3], // [1.0; 3],
); // );
let model = Model::create( let model = Model::create(
parent, parent,
t, Transform::from_rotation(Quat::from_rotation_y(PI)),
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"), &ResourceID::new_namespaced("protostar", "cartridge"),
)?; )?;
model model
.model_part("Hex")? .model_part("Cartridge")?
.set_material_parameter("color", MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]))?; .set_material_parameter("color", MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]))?;
model.model_part("Icon")?.set_material_parameter( model.model_part("Icon")?.set_material_parameter(
"diffuse", "diffuse",
@@ -104,7 +103,7 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
} }
IconType::Gltf => Ok(Model::create( IconType::Gltf => Ok(Model::create(
parent, parent,
Transform::from_scale([0.05; 3]), Transform::none(),
&ResourceID::new_direct(icon.path.clone())?, &ResourceID::new_direct(icon.path.clone())?,
)?), )?),
_ => panic!("Invalid Icon Type"), _ => panic!("Invalid Icon Type"),
@@ -112,14 +111,12 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
} }
pub struct App { pub struct App {
root: Spatial,
application: Application, application: Application,
parent: Spatial,
position: Vector3<f32>,
grabbable: Grabbable, grabbable: Grabbable,
_field: BoxField, _field: BoxField,
icon: Model, _icon: Model,
label: Option<Text>, _label: Option<Text>,
grabbable_move: Option<Tweener<f32, f64, QuartInOut>>,
} }
impl App { impl App {
pub fn create_from_desktop_file( pub fn create_from_desktop_file(
@@ -127,13 +124,13 @@ impl App {
position: impl Into<Vector3<f32>>, position: impl Into<Vector3<f32>>,
desktop_file: DesktopFile, desktop_file: DesktopFile,
) -> Result<Self> { ) -> Result<Self> {
let position = position.into(); let root = Spatial::create(parent, Transform::from_position(position), false)?;
let field = BoxField::create(parent, Transform::default(), [APP_SIZE; 3])?; let field = BoxField::create(&root, Transform::default(), [APP_SIZE; 3])?;
let application = Application::create(&parent.client()?, desktop_file)?; let application = Application::create(&parent.client()?, desktop_file)?;
let icon = application.icon(128, false); let icon = application.icon(128, true);
let grabbable = Grabbable::create( let grabbable = Grabbable::create(
parent, &root,
Transform::from_position(position), Transform::identity(),
&field, &field,
GrabData { GrabData {
max_distance: 0.01, max_distance: 0.01,
@@ -148,18 +145,15 @@ impl App {
.unwrap_or_else(|| { .unwrap_or_else(|| {
Ok(Model::create( Ok(Model::create(
grabbable.content_parent(), grabbable.content_parent(),
Transform::from_rotation_scale( Transform::from_rotation(Quat::from_rotation_y(PI)),
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI), &ResourceID::new_namespaced("protostar", "cartridge"),
[APP_SIZE * 0.5; 3],
),
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?) )?)
})?; })?;
let label_style = TextStyle { let label_style = TextStyle {
character_height: APP_SIZE * 2.0, character_height: 0.005,
bounds: Some(Bounds { bounds: Some(Bounds {
bounds: [1.0; 2].into(), bounds: [0.047013, 0.01].into(),
fit: TextFit::Wrap, fit: TextFit::Wrap,
bounds_align: Alignment::XCenter | Alignment::YCenter, bounds_align: Alignment::XCenter | Alignment::YCenter,
}), }),
@@ -168,81 +162,68 @@ impl App {
}; };
let label = application.name().and_then(|name| { let label = application.name().and_then(|name| {
Text::create( Text::create(
&icon, &*icon.model_part("Label").ok()?,
Transform::from_position_rotation( Transform::none(),
[0.0, 0.1, -(APP_SIZE * 4.0)],
Quat::from_rotation_x(PI * 0.5),
),
name, name,
label_style, label_style,
) )
.ok() .ok()
}); });
Ok(App { Ok(App {
parent: parent.alias(), root,
position,
grabbable, grabbable,
_field: field, _field: field,
label, _label: label,
application, application,
icon, _icon: icon,
grabbable_move: None,
}) })
} }
pub fn content_parent(&self) -> &Spatial { pub fn content_parent(&self) -> &Spatial {
self.grabbable.content_parent() self.grabbable.content_parent()
} }
fn bring_back(&self) {
self.grabbable
.content_parent()
.set_transform(Some(&self.root), Transform::identity())
.unwrap();
}
} }
impl RootHandler for App { impl RootHandler for App {
fn frame(&mut self, info: FrameInfo) { fn frame(&mut self, info: FrameInfo) {
let _ = self.grabbable.update(&info); let _ = self.grabbable.update(&info);
if let Some(grabbable_move) = &mut self.grabbable_move { if self.grabbable.grab_action().actor_stopped() {
if !grabbable_move.is_finished() {
let scale = grabbable_move.move_by(info.delta);
self.grabbable
.content_parent()
.set_position(
Some(&self.parent),
[
self.position.x * scale,
self.position.y * scale,
self.position.z * scale,
],
)
.unwrap();
} else {
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.grabbable_move = None;
}
} else if self.grabbable.valid() && self.grabbable.grab_action().actor_stopped() {
self.grabbable.cancel_angular_velocity(); self.grabbable.cancel_angular_velocity();
self.grabbable.cancel_linear_velocity(); self.grabbable.cancel_linear_velocity();
self.grabbable
.content_parent()
.set_position(Some(&self.parent), self.position)
.unwrap();
if !self.grabbable.valid() {
self.bring_back();
return;
}
let Ok(distance_future) = self.grabbable let Ok(distance_future) = self.grabbable
.content_parent() .content_parent()
.get_position_rotation_scale(&self.parent) .get_position_rotation_scale(&self.root)
else {return}; else {return};
let application = self.application.clone(); let application = self.application.clone();
let space = self.content_parent().alias(); let space = self.content_parent().alias();
let root = self.root.alias();
//TODO: split the executable string for the args
tokio::task::spawn(async move { tokio::task::spawn(async move {
let distance_vector = distance_future.await.ok().unwrap().0; let Ok((distance, _rotation, _scale)) = distance_future.await else { space
let distance = ((distance_vector.x.powi(2) + distance_vector.y.powi(2)).sqrt() .set_transform(Some(&root), Transform::identity())
+ distance_vector.z.powi(2)) .unwrap(); return};
.sqrt(); let distance = Vec3::from(distance).length_squared();
if dbg!(distance) > ACTIVATION_DISTANCE { dbg!(distance.sqrt());
dbg!(ACTIVATION_DISTANCE);
if distance > ACTIVATION_DISTANCE.powi(2) {
let _ = application.launch(&space); let _ = application.launch(&space);
} }
space
.set_transform(Some(&root), Transform::identity())
.unwrap();
}); });
} }
} }

Binary file not shown.