make the hex launcher grabbable

This commit is contained in:
Nicola Guerrera
2023-03-02 16:03:29 +01:00
committed by Nova
parent 2824e2dc2a
commit 1c90ed98cf
3 changed files with 53 additions and 58 deletions

View File

@@ -1,5 +1,4 @@
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use glam::Quat;
use manifest_dir_macros::directory_relative_path; use manifest_dir_macros::directory_relative_path;
use mint::Vector3; use mint::Vector3;
use protostar::{ use protostar::{
@@ -8,8 +7,6 @@ use protostar::{
}; };
use stardust_xr_fusion::{ use stardust_xr_fusion::{
client::{Client, FrameInfo, RootHandler}, client::{Client, FrameInfo, RootHandler},
core::values::Transform,
drawable::{Alignment, Bounds, Text, TextFit, TextStyle},
spatial::Spatial, spatial::Spatial,
}; };
@@ -72,7 +69,6 @@ impl RootHandler for AppGrid {
} }
} }
struct App { struct App {
_text: Text,
_desktop_file: DesktopFile, _desktop_file: DesktopFile,
protostar: ProtoStar, protostar: ProtoStar,
} }
@@ -82,33 +78,11 @@ impl App {
parent: &Spatial, parent: &Spatial,
position: impl Into<Vector3<f32>>, position: impl Into<Vector3<f32>>,
desktop_file: DesktopFile, desktop_file: DesktopFile,
//style: TextStyle,
) -> Option<Self> { ) -> Option<Self> {
let position = position.into(); let position = position.into();
let style = TextStyle {
character_height: APP_SIZE * 0.1,
bounds: Some(Bounds {
bounds: [APP_SIZE; 2].into(),
fit: TextFit::Wrap,
bounds_align: Alignment::XCenter | Alignment::YCenter,
}),
text_align: Alignment::XCenter | Alignment::YCenter,
..Default::default()
};
let protostar = let protostar =
ProtoStar::create_from_desktop_file(parent, position, desktop_file.clone()).ok()?; 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, APP_SIZE / 2.0],
Quat::from_rotation_y(3.14),
),
desktop_file.name.as_deref().unwrap_or("Unknown"),
style,
)
.unwrap();
Some(App { Some(App {
_text: text,
_desktop_file: desktop_file, _desktop_file: desktop_file,
protostar, protostar,
}) })

View File

@@ -10,10 +10,11 @@ use stardust_xr_fusion::{
client::{Client, FrameInfo, RootHandler}, client::{Client, FrameInfo, RootHandler},
core::values::Transform, core::values::Transform,
drawable::{MaterialParameter, Model, ResourceID}, drawable::{MaterialParameter, Model, ResourceID},
fields::BoxField,
node::NodeError, node::NodeError,
spatial::Spatial, spatial::Spatial,
}; };
use stardust_xr_molecules::touch_plane::TouchPlane; use stardust_xr_molecules::{touch_plane::TouchPlane, GrabData, Grabbable};
use std::f32::consts::PI; use std::f32::consts::PI;
use tween::TweenTime; use tween::TweenTime;
@@ -110,7 +111,7 @@ impl AppHexGrid {
}; };
apps.push( apps.push(
App::new( App::new(
client.get_root(), button.grabbable.content_parent(),
hex.get_coords(), hex.get_coords(),
desktop_files.pop().unwrap(), desktop_files.pop().unwrap(),
) )
@@ -126,7 +127,7 @@ impl AppHexGrid {
} }
impl RootHandler for AppHexGrid { impl RootHandler for AppHexGrid {
fn frame(&mut self, info: FrameInfo) { fn frame(&mut self, info: FrameInfo) {
self.button.touch_plane.update(); self.button.frame(info);
if self.button.touch_plane.touch_started() { if self.button.touch_plane.touch_started() {
let color = [0.0, 1.0, 0.0, 1.0]; let color = [0.0, 1.0, 0.0, 1.0];
self.button self.button
@@ -176,30 +177,48 @@ impl RootHandler for App {
struct Button { struct Button {
touch_plane: TouchPlane, touch_plane: TouchPlane,
grabbable: Grabbable,
model: Model, model: Model,
} }
impl Button { impl Button {
fn new(client: &Client) -> Result<Self, NodeError> { fn new(client: &Client) -> Result<Self, NodeError> {
let touch_plane = TouchPlane::new( let field = BoxField::create(client.get_root(), Transform::default(), [APP_SIZE; 3])?;
let grabbable = Grabbable::new(
client.get_root(), client.get_root(),
Transform::default(), Transform::default(),
&field,
GrabData {
max_distance: 0.01,
..Default::default()
},
)?;
field.set_spatial_parent(grabbable.content_parent())?;
let touch_plane = TouchPlane::new(
grabbable.content_parent(),
Transform::default(),
[(APP_SIZE + PADDING) / 2.0; 2], [(APP_SIZE + PADDING) / 2.0; 2],
(APP_SIZE + PADDING) / 2.0, (APP_SIZE + PADDING) / 2.0,
)?; )?;
let model = Model::create( let model = Model::create(
client.get_root(), grabbable.content_parent(),
Transform::from_rotation_scale( 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),
[0.03, 0.03, 0.03], [0.03, 0.03, 0.03],
), ),
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"), &ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?; )?;
model model.set_material_parameter(1, "color", MaterialParameter::Color([0.0, 0.0, 1.0, 1.0]))?;
.set_material_parameter(1, "color", MaterialParameter::Color([0.0, 0.0, 1.0, 1.0])) Ok(Button {
.unwrap(); touch_plane,
Ok(Button { touch_plane, model }) grabbable,
model,
})
} }
} }
impl RootHandler for Button { impl RootHandler for Button {
fn frame(&mut self, _info: FrameInfo) {} fn frame(&mut self, info: FrameInfo) {
self.touch_plane.update();
self.grabbable.update(&info);
}
} }

View File

@@ -5,18 +5,18 @@ use mint::Vector3;
use nix::unistd::setsid; use nix::unistd::setsid;
use regex::Regex; use regex::Regex;
use stardust_xr_fusion::{ use stardust_xr_fusion::{
client::{Client, FrameInfo, RootHandler}, client::{FrameInfo, RootHandler},
core::values::Transform, core::values::Transform,
drawable::{Alignment, MaterialParameter, Model, ResourceID, Text, TextStyle, Bounds, TextFit}, drawable::{Alignment, Bounds, MaterialParameter, Model, ResourceID, Text, TextFit, TextStyle},
fields::BoxField, fields::BoxField,
node::NodeType, node::NodeType,
spatial::Spatial, spatial::Spatial,
startup_settings::StartupSettings, startup_settings::StartupSettings,
}; };
use stardust_xr_molecules::{GrabData, Grabbable}; use stardust_xr_molecules::{GrabData, Grabbable};
use std::f32::consts::PI;
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio}; use std::process::{Command, Stdio};
use std::{f32::consts::PI, sync::Arc};
use tween::{QuartInOut, Tweener}; use tween::{QuartInOut, Tweener};
const MODEL_SCALE: f32 = 0.03; const MODEL_SCALE: f32 = 0.03;
@@ -57,7 +57,7 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
} }
pub struct ProtoStar { pub struct ProtoStar {
client: Arc<Client>, parent: Spatial,
position: Vector3<f32>, position: Vector3<f32>,
grabbable: Grabbable, grabbable: Grabbable,
field: BoxField, field: BoxField,
@@ -112,14 +112,7 @@ impl ProtoStar {
execute_command: String, execute_command: String,
) -> Result<Self> { ) -> Result<Self> {
let position = position.into(); let position = position.into();
let field = BoxField::create( let field = BoxField::create(parent, Transform::default(), [MODEL_SCALE * 2.0; 3])?;
parent,
Transform::default(),
match icon.as_ref() {
Some(_) => [0.05, 0.0665, 0.005],
_ => [0.05; 3],
},
)?;
let grabbable = Grabbable::new( let grabbable = Grabbable::new(
parent, parent,
Transform::from_position(position), Transform::from_position(position),
@@ -129,6 +122,7 @@ impl ProtoStar {
..Default::default() ..Default::default()
}, },
)?; )?;
grabbable.content_parent().set_spatial_parent(parent)?;
field.set_spatial_parent(grabbable.content_parent())?; field.set_spatial_parent(grabbable.content_parent())?;
let icon = icon let icon = icon
.map(|i| model_from_icon(grabbable.content_parent(), &i)) .map(|i| model_from_icon(grabbable.content_parent(), &i))
@@ -144,8 +138,8 @@ impl ProtoStar {
})?; })?;
let label_style = TextStyle { let label_style = TextStyle {
character_height: MODEL_SCALE*4.0 , character_height: MODEL_SCALE * 4.0,
bounds: Some(Bounds{ bounds: Some(Bounds {
bounds: [1.0; 2].into(), bounds: [1.0; 2].into(),
fit: TextFit::Wrap, fit: TextFit::Wrap,
bounds_align: Alignment::XCenter | Alignment::YCenter, bounds_align: Alignment::XCenter | Alignment::YCenter,
@@ -157,7 +151,7 @@ impl ProtoStar {
Text::create( Text::create(
&icon, &icon,
Transform::from_position_rotation( Transform::from_position_rotation(
[0.0, 0.1, -(MODEL_SCALE*8.0)], [0.0, 0.1, -(MODEL_SCALE * 8.0)],
Quat::from_rotation_x(PI * 0.5), Quat::from_rotation_x(PI * 0.5),
), ),
name, name,
@@ -166,7 +160,7 @@ impl ProtoStar {
.ok() .ok()
}); });
Ok(ProtoStar { Ok(ProtoStar {
client: parent.client()?, parent: parent.alias(),
position, position,
grabbable, grabbable,
field, field,
@@ -204,7 +198,7 @@ impl RootHandler for ProtoStar {
self.grabbable self.grabbable
.content_parent() .content_parent()
.set_position( .set_position(
Some(self.client.get_root()), Some(&self.parent),
[ [
self.position.x * scale, self.position.x * scale,
self.position.y * scale, self.position.y * scale,
@@ -225,9 +219,13 @@ impl RootHandler for ProtoStar {
let scale = grabbable_shrink.move_by(info.delta); let scale = grabbable_shrink.move_by(info.delta);
self.grabbable self.grabbable
.content_parent() .content_parent()
.set_scale(None, Vector3::from([scale; 3])) .set_scale(Some(&self.parent), Vector3::from([scale; 3]))
.unwrap(); .unwrap();
} else { } else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
if self.currently_shown { if self.currently_shown {
self.grabbable_grow = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25)); self.grabbable_grow = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25));
self.grabbable.cancel_angular_velocity(); self.grabbable.cancel_angular_velocity();
@@ -236,11 +234,11 @@ impl RootHandler for ProtoStar {
self.grabbable_shrink = None; self.grabbable_shrink = None;
self.grabbable self.grabbable
.content_parent() .content_parent()
.set_position(Some(self.client.get_root()), self.position) .set_position(Some(&self.parent), self.position)
.unwrap(); .unwrap();
self.grabbable self.grabbable
.content_parent() .content_parent()
.set_rotation(Some(self.client.get_root()), Quat::default()) .set_rotation(Some(&self.parent), Quat::default())
.unwrap(); .unwrap();
self.icon self.icon
.set_rotation( .set_rotation(
@@ -254,9 +252,13 @@ impl RootHandler for ProtoStar {
let scale = grabbable_grow.move_by(info.delta); let scale = grabbable_grow.move_by(info.delta);
self.grabbable self.grabbable
.content_parent() .content_parent()
.set_scale(None, Vector3::from([scale; 3])) .set_scale(Some(&self.parent), Vector3::from([scale; 3]))
.unwrap(); .unwrap();
} else { } else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
self.grabbable_grow = None; self.grabbable_grow = None;
} }
} else if self.grabbable.grab_action().actor_stopped() { } else if self.grabbable.grab_action().actor_stopped() {
@@ -268,7 +270,7 @@ impl RootHandler for ProtoStar {
let distance_future = self let distance_future = self
.grabbable .grabbable
.content_parent() .content_parent()
.get_position_rotation_scale(self.client.get_root()) .get_position_rotation_scale(&self.parent)
.unwrap(); .unwrap();
let executable = self.execute_command.clone(); let executable = self.execute_command.clone();