modular protostar

This commit is contained in:
Nova
2023-05-11 00:17:13 -04:00
parent 307df9829f
commit d41e34d746
12 changed files with 2345 additions and 458 deletions

34
Cargo.lock generated
View File

@@ -878,15 +878,6 @@ version = "0.27.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad0a93d233ebf96623465aad4046a8d3aa4da22d4f4beba5388838c8a434bbb4"
[[package]]
name = "glam"
version = "0.22.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12f597d56c1bd55a811a1be189459e8fad2bbc272616375602443bdfb37fa774"
dependencies = [
"mint",
]
[[package]]
name = "glam"
version = "0.24.0"
@@ -1636,7 +1627,7 @@ dependencies = [
"directories",
"dirs",
"ez-pixmap",
"glam 0.24.0",
"glam",
"image",
"lazy_static",
"linicon",
@@ -2065,9 +2056,7 @@ dependencies = [
[[package]]
name = "stardust-xr"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52ce36e7f3285a6b1391a298c01bbbb92c9f3d77967a78d1beaac21c0043d23b"
version = "0.11.4"
dependencies = [
"chrono",
"cluFlock",
@@ -2085,9 +2074,7 @@ dependencies = [
[[package]]
name = "stardust-xr-fusion"
version = "0.39.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75214564f52e031f3873963b855cbd9a8253502ef18912480e8e84ad98a30c8"
version = "0.40.2"
dependencies = [
"color-eyre",
"color-rs",
@@ -2103,17 +2090,16 @@ dependencies = [
"thiserror",
"tokio",
"tracing",
"xkbcommon",
]
[[package]]
name = "stardust-xr-molecules"
version = "0.24.0"
version = "0.24.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "185291b7cf2368a9ef0deed9e5ebf2c49954e862d2747aeee96cc08a57796c1c"
checksum = "724a92e67765300651d165950b70ec4800d4ad6450f473882f6024d5a559ea9d"
dependencies = [
"color-rs",
"glam 0.24.0",
"glam",
"lazy_static",
"map-range",
"mint",
@@ -2127,16 +2113,16 @@ dependencies = [
[[package]]
name = "stardust-xr-schemas"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bebfbddbcba7db7ce8180bb373c9ad5a7d58ff60ee213ca07562820d7e8aab0"
version = "1.5.0"
dependencies = [
"flatbuffers",
"flexbuffers",
"glam 0.22.0",
"glam",
"manifest-dir-macros",
"mint",
"ouroboros",
"serde",
"serde_repr",
"thiserror",
]

View File

@@ -20,8 +20,8 @@ nix = "0.26.1"
regex = "1.7.1"
resvg = "0.29.0"
rustc-hash = "1.1.0"
stardust-xr-fusion = "0.39.2"
stardust-xr-molecules = "0.24.0"
stardust-xr-fusion = "0.40.2"
stardust-xr-molecules = "0.24.3"
tokio = { version = "1.24.1", features = ["full"] }
tracing-subscriber = { version = "0.3.16", features = ["env-filter"] }
tween = "2.0.0"
@@ -29,4 +29,7 @@ ustr = "0.9.0"
walkdir = "2.3.2"
[dev-dependencies]
tempdir = "0.3.7"
tempdir = "0.3.7"
[patch.crates-io.stardust-xr-fusion]
path = "../../core/fusion"

Binary file not shown.

View File

@@ -3,23 +3,26 @@ use glam::Quat;
use manifest_dir_macros::directory_relative_path;
use mint::Vector3;
use protostar::{
protostar::ProtoStar,
xdg::{get_desktop_files, parse_desktop_file, DesktopFile},
application::Application,
xdg::{get_desktop_files, parse_desktop_file, DesktopFile, Icon, IconType},
};
use stardust_xr_fusion::{
client::{Client, FrameInfo, RootHandler},
core::values::Transform,
drawable::{MaterialParameter, Model, ResourceID},
drawable::{Alignment, Bounds, MaterialParameter, Model, ResourceID, Text, TextFit, TextStyle},
fields::BoxField,
node::NodeError,
node::NodeType,
spatial::Spatial,
};
use stardust_xr_molecules::{touch_plane::TouchPlane, GrabData, Grabbable};
use std::f32::consts::PI;
use tween::TweenTime;
use tween::{QuartInOut, Tweener};
const APP_SIZE: f32 = 0.06;
const PADDING: f32 = 0.005;
const ACTIVATION_DISTANCE: f32 = 0.5;
#[derive(Clone)]
struct Hex {
@@ -110,7 +113,7 @@ impl AppHexGrid {
break;
};
apps.push(
App::new(
App::create_from_desktop_file(
button.grabbable.content_parent(),
hex.get_coords(),
desktop_files.pop().unwrap(),
@@ -132,16 +135,20 @@ impl RootHandler for AppHexGrid {
let color = [0.0, 1.0, 0.0, 1.0];
self.button
.model
.set_material_parameter(1, "color", MaterialParameter::Color(color))
.model_part("Hex")
.unwrap()
.set_material_parameter("color", MaterialParameter::Color(color))
.unwrap();
for app in &mut self.apps {
app.protostar.toggle();
app.toggle();
}
} else if self.button.touch_plane.touch_stopped() {
let color = [0.0, 0.0, 1.0, 1.0];
self.button
.model
.set_material_parameter(1, "color", MaterialParameter::Color(color))
.model_part("Hex")
.unwrap()
.set_material_parameter("color", MaterialParameter::Color(color))
.unwrap();
}
for app in &mut self.apps {
@@ -149,31 +156,6 @@ impl RootHandler for AppHexGrid {
}
}
}
struct App {
_desktop_file: DesktopFile,
protostar: ProtoStar,
}
impl App {
fn new(
parent: &Spatial,
position: impl Into<Vector3<f32>>,
desktop_file: DesktopFile,
) -> Option<Self> {
let position = position.into();
let protostar =
ProtoStar::create_from_desktop_file(parent, position, desktop_file.clone()).ok()?;
Some(App {
_desktop_file: desktop_file,
protostar,
})
}
}
impl RootHandler for App {
fn frame(&mut self, info: FrameInfo) {
self.protostar.frame(info);
}
}
struct Button {
touch_plane: TouchPlane,
@@ -210,7 +192,9 @@ impl Button {
),
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?;
model.set_material_parameter(1, "color", MaterialParameter::Color([0.0, 0.0, 1.0, 1.0]))?;
model
.model_part("Hex")?
.set_material_parameter("color", MaterialParameter::Color([0.0, 0.0, 1.0, 1.0]))?;
Ok(Button {
touch_plane,
grabbable,
@@ -230,3 +214,230 @@ impl RootHandler for Button {
self.touch_plane.update();
}
}
fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
return match &icon.icon_type {
IconType::Png => {
let t = Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[APP_SIZE * 0.5; 3],
);
let model = Model::create(
parent,
t,
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?;
model
.model_part("Hex")?
.set_material_parameter("color", MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]))?;
model.model_part("Icon")?.set_material_parameter(
"diffuse",
MaterialParameter::Texture(ResourceID::Direct(icon.path.clone())),
)?;
Ok(model)
}
IconType::Gltf => Ok(Model::create(
parent,
Transform::from_scale([0.05; 3]),
&ResourceID::new_direct(icon.path.clone())?,
)?),
_ => panic!("Invalid Icon Type"),
};
}
pub struct App {
application: Application,
parent: Spatial,
position: Vector3<f32>,
grabbable: Grabbable,
_field: BoxField,
icon: Model,
label: Option<Text>,
grabbable_shrink: Option<Tweener<f32, f64, QuartInOut>>,
grabbable_grow: Option<Tweener<f32, f64, QuartInOut>>,
grabbable_move: Option<Tweener<f32, f64, QuartInOut>>,
currently_shown: bool,
}
impl App {
pub fn create_from_desktop_file(
parent: &Spatial,
position: impl Into<Vector3<f32>>,
desktop_file: DesktopFile,
) -> Result<Self> {
let position = position.into();
let field = BoxField::create(parent, Transform::default(), [APP_SIZE; 3])?;
let application = Application::create(&parent.client()?, desktop_file)?;
let icon = application.icon(128, false);
let grabbable = Grabbable::create(
parent,
Transform::from_position(position),
&field,
GrabData {
max_distance: 0.01,
frame_cancel_threshold: 50,
..Default::default()
},
)?;
grabbable.content_parent().set_spatial_parent(parent)?;
field.set_spatial_parent(grabbable.content_parent())?;
let icon = icon
.map(|i| model_from_icon(grabbable.content_parent(), &i))
.unwrap_or_else(|| {
Ok(Model::create(
grabbable.content_parent(),
Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[APP_SIZE * 0.5; 3],
),
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?)
})?;
let label_style = TextStyle {
character_height: APP_SIZE * 2.0,
bounds: Some(Bounds {
bounds: [1.0; 2].into(),
fit: TextFit::Wrap,
bounds_align: Alignment::XCenter | Alignment::YCenter,
}),
text_align: Alignment::Center.into(),
..Default::default()
};
let label = application.name().and_then(|name| {
Text::create(
&icon,
Transform::from_position_rotation(
[0.0, 0.1, -(APP_SIZE * 4.0)],
Quat::from_rotation_x(PI * 0.5),
),
name,
label_style,
)
.ok()
});
Ok(App {
parent: parent.alias(),
position,
grabbable,
_field: field,
label,
application,
icon,
grabbable_shrink: None,
grabbable_grow: None,
grabbable_move: None,
currently_shown: true,
})
}
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.grabbable_move = Some(Tweener::quart_in_out(1.0, 0.0001, 0.25)); //TODO make the scale a parameter
} else {
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;
}
}
impl RootHandler for App {
fn frame(&mut self, info: FrameInfo) {
let _ = self.grabbable.update(&info);
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(
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;
}
}
if let Some(grabbable_shrink) = &mut self.grabbable_shrink {
if !grabbable_shrink.is_finished() {
let scale = grabbable_shrink.move_by(info.delta);
self.grabbable
.content_parent()
.set_scale(Some(&self.parent), Vector3::from([scale; 3]))
.unwrap();
} else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
if self.currently_shown {
self.grabbable_grow = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25));
self.grabbable.cancel_angular_velocity();
self.grabbable.cancel_linear_velocity();
}
self.grabbable_shrink = None;
self.grabbable
.content_parent()
.set_position(Some(&self.parent), self.position)
.unwrap();
self.grabbable
.content_parent()
.set_rotation(Some(&self.parent), Quat::default())
.unwrap();
self.icon
.set_rotation(
None,
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
)
.unwrap();
}
} else if let Some(grabbable_grow) = &mut self.grabbable_grow {
if !grabbable_grow.is_finished() {
let scale = grabbable_grow.move_by(info.delta);
self.grabbable
.content_parent()
.set_scale(Some(&self.parent), Vector3::from([scale; 3]))
.unwrap();
} else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
self.grabbable_grow = None;
}
} else if self.grabbable.valid() && self.grabbable.grab_action().actor_stopped() {
self.grabbable_shrink = Some(Tweener::quart_in_out(APP_SIZE * 0.5, 0.0001, 0.25));
let Ok(distance_future) = self.grabbable
.content_parent()
.get_position_rotation_scale(&self.parent)
else {return};
let application = self.application.clone();
let space = self.content_parent().alias();
//TODO: split the executable string for the args
tokio::task::spawn(async move {
let distance_vector = distance_future.await.ok().unwrap().0;
let distance = ((distance_vector.x.powi(2) + distance_vector.y.powi(2)).sqrt()
+ distance_vector.z.powi(2))
.sqrt();
if dbg!(distance) > ACTIVATION_DISTANCE {
let _ = application.launch(&space);
}
});
}
}
}

1665
mono_crash.0.0.json Normal file

File diff suppressed because it is too large Load Diff

Binary file not shown.

View File

@@ -1,276 +1,286 @@
{
"asset" : {
"generator" : "Khronos glTF Blender I/O v3.4.50",
"version" : "2.0"
},
"extensionsUsed" : [
"KHR_materials_unlit"
],
"scene" : 0,
"scenes" : [
{
"name" : "Scene",
"nodes" : [
0
]
}
],
"nodes" : [
{
"mesh" : 0,
"name" : "Hex"
}
],
"materials" : [
{
"alphaCutoff" : 0.5,
"alphaMode" : "MASK",
"extensions" : {
"KHR_materials_unlit" : {}
},
"name" : "Icon",
"pbrMetallicRoughness" : {
"baseColorTexture" : {
"index" : 0,
"texCoord" : 0
},
"metallicFactor" : 0,
"roughnessFactor" : 0.9
}
},
{
"alphaMode" : "BLEND",
"extensions" : {
"KHR_materials_unlit" : {}
},
"name" : "Hex",
"pbrMetallicRoughness" : {
"baseColorFactor" : [
1,
0,
0,
1
],
"baseColorTexture" : {
"index" : 1,
"texCoord" : 0
},
"metallicFactor" : 0,
"roughnessFactor" : 0.9
}
}
],
"meshes" : [
{
"name" : "Circle",
"primitives" : [
{
"attributes" : {
"COLOR_0" : 0,
"POSITION" : 1,
"TEXCOORD_0" : 2,
"NORMAL" : 3
},
"indices" : 4,
"material" : 0
},
{
"attributes" : {
"COLOR_0" : 5,
"POSITION" : 6,
"TEXCOORD_0" : 7,
"NORMAL" : 8
},
"indices" : 9,
"material" : 1
}
]
}
],
"textures" : [
{
"sampler" : 0,
"source" : 0
},
{
"sampler" : 1,
"source" : 1
}
],
"images" : [
{
"mimeType" : "image/png",
"name" : "icon_test",
"uri" : "icon_test.png"
},
{
"mimeType" : "image/png",
"name" : "hex_atlas",
"uri" : "hex_atlas.png"
}
],
"accessors" : [
{
"bufferView" : 0,
"componentType" : 5123,
"count" : 4,
"normalized" : true,
"type" : "VEC4"
},
{
"bufferView" : 1,
"componentType" : 5126,
"count" : 4,
"max" : [
0.5853440165519714,
0.05000000074505806,
0.5853440165519714
],
"min" : [
-0.5853440165519714,
0.05000000074505806,
-0.5853440165519714
],
"type" : "VEC3"
},
{
"bufferView" : 2,
"componentType" : 5126,
"count" : 4,
"type" : "VEC2"
},
{
"bufferView" : 3,
"componentType" : 5126,
"count" : 4,
"type" : "VEC3"
},
{
"bufferView" : 4,
"componentType" : 5123,
"count" : 6,
"type" : "SCALAR"
},
{
"bufferView" : 5,
"componentType" : 5123,
"count" : 54,
"normalized" : true,
"type" : "VEC4"
},
{
"bufferView" : 6,
"componentType" : 5126,
"count" : 54,
"max" : [
1.0441828966140747,
0.05000000074505806,
0.9042890667915344
],
"min" : [
-1.0441828966140747,
0,
-0.9042890667915344
],
"type" : "VEC3"
},
{
"bufferView" : 7,
"componentType" : 5126,
"count" : 54,
"type" : "VEC2"
},
{
"bufferView" : 8,
"componentType" : 5126,
"count" : 54,
"type" : "VEC3"
},
{
"bufferView" : 9,
"componentType" : 5123,
"count" : 84,
"type" : "SCALAR"
}
],
"bufferViews" : [
{
"buffer" : 0,
"byteLength" : 32,
"byteOffset" : 0,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 48,
"byteOffset" : 32,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 32,
"byteOffset" : 80,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 48,
"byteOffset" : 112,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 12,
"byteOffset" : 160,
"target" : 34963
},
{
"buffer" : 0,
"byteLength" : 432,
"byteOffset" : 172,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 648,
"byteOffset" : 604,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 432,
"byteOffset" : 1252,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 648,
"byteOffset" : 1684,
"target" : 34962
},
{
"buffer" : 0,
"byteLength" : 168,
"byteOffset" : 2332,
"target" : 34963
}
],
"samplers" : [
{
"magFilter" : 9729,
"minFilter" : 9987,
"wrapS" : 33071,
"wrapT" : 33071
},
{
"magFilter" : 9729,
"minFilter" : 9987
}
],
"buffers" : [
{
"byteLength" : 2500,
"uri" : "hexagon.bin"
}
]
"asset":{
"generator":"Khronos glTF Blender I/O v3.5.30",
"version":"2.0"
},
"extensionsUsed":[
"KHR_materials_unlit"
],
"scene":0,
"scenes":[
{
"name":"Scene",
"nodes":[
0,
1
]
}
],
"nodes":[
{
"mesh":0,
"name":"Icon"
},
{
"mesh":1,
"name":"Hex"
}
],
"materials":[
{
"alphaCutoff":0.5,
"alphaMode":"MASK",
"extensions":{
"KHR_materials_unlit":{}
},
"name":"Icon",
"pbrMetallicRoughness":{
"baseColorTexture":{
"index":0,
"texCoord":0
},
"metallicFactor":0,
"roughnessFactor":0.9
}
},
{
"alphaMode":"BLEND",
"extensions":{
"KHR_materials_unlit":{}
},
"name":"Hex",
"pbrMetallicRoughness":{
"baseColorFactor":[
1,
0,
0,
1
],
"baseColorTexture":{
"index":1,
"texCoord":0
},
"metallicFactor":0,
"roughnessFactor":0.9
}
}
],
"meshes":[
{
"name":"Circle",
"primitives":[
{
"attributes":{
"COLOR_0":0,
"POSITION":1,
"TEXCOORD_0":2,
"NORMAL":3
},
"indices":4,
"material":0
}
]
},
{
"name":"Circle.001",
"primitives":[
{
"attributes":{
"COLOR_0":5,
"POSITION":6,
"TEXCOORD_0":7,
"NORMAL":8
},
"indices":9,
"material":1
}
]
}
],
"textures":[
{
"sampler":0,
"source":0
},
{
"sampler":1,
"source":1
}
],
"images":[
{
"mimeType":"image/png",
"name":"icon_test",
"uri":"icon_test.png"
},
{
"mimeType":"image/png",
"name":"hex_atlas",
"uri":"hex_atlas.png"
}
],
"accessors":[
{
"bufferView":0,
"componentType":5123,
"count":4,
"normalized":true,
"type":"VEC4"
},
{
"bufferView":1,
"componentType":5126,
"count":4,
"max":[
0.5853440165519714,
0.05000000074505806,
0.5853440165519714
],
"min":[
-0.5853440165519714,
0.05000000074505806,
-0.5853440165519714
],
"type":"VEC3"
},
{
"bufferView":2,
"componentType":5126,
"count":4,
"type":"VEC2"
},
{
"bufferView":3,
"componentType":5126,
"count":4,
"type":"VEC3"
},
{
"bufferView":4,
"componentType":5123,
"count":6,
"type":"SCALAR"
},
{
"bufferView":5,
"componentType":5123,
"count":54,
"normalized":true,
"type":"VEC4"
},
{
"bufferView":6,
"componentType":5126,
"count":54,
"max":[
1.0441828966140747,
0.05000000074505806,
0.9042890667915344
],
"min":[
-1.0441828966140747,
0,
-0.9042890667915344
],
"type":"VEC3"
},
{
"bufferView":7,
"componentType":5126,
"count":54,
"type":"VEC2"
},
{
"bufferView":8,
"componentType":5126,
"count":54,
"type":"VEC3"
},
{
"bufferView":9,
"componentType":5123,
"count":84,
"type":"SCALAR"
}
],
"bufferViews":[
{
"buffer":0,
"byteLength":32,
"byteOffset":0,
"target":34962
},
{
"buffer":0,
"byteLength":48,
"byteOffset":32,
"target":34962
},
{
"buffer":0,
"byteLength":32,
"byteOffset":80,
"target":34962
},
{
"buffer":0,
"byteLength":48,
"byteOffset":112,
"target":34962
},
{
"buffer":0,
"byteLength":12,
"byteOffset":160,
"target":34963
},
{
"buffer":0,
"byteLength":432,
"byteOffset":172,
"target":34962
},
{
"buffer":0,
"byteLength":648,
"byteOffset":604,
"target":34962
},
{
"buffer":0,
"byteLength":432,
"byteOffset":1252,
"target":34962
},
{
"buffer":0,
"byteLength":648,
"byteOffset":1684,
"target":34962
},
{
"buffer":0,
"byteLength":168,
"byteOffset":2332,
"target":34963
}
],
"samplers":[
{
"magFilter":9729,
"minFilter":9987,
"wrapS":33071,
"wrapT":33071
},
{
"magFilter":9729,
"minFilter":9987
}
],
"buffers":[
{
"byteLength":2500,
"uri":"hexagon.bin"
}
]
}

100
src/application.rs Normal file
View File

@@ -0,0 +1,100 @@
use crate::xdg::{DesktopFile, Icon, IconType};
use nix::unistd::setsid;
use regex::Regex;
use stardust_xr_fusion::{
client::Client,
node::{NodeError, NodeType},
spatial::Spatial,
startup_settings::StartupSettings,
};
use std::{
os::unix::process::CommandExt,
process::{Command, Stdio},
sync::Arc,
};
#[derive(Debug, Clone)]
pub struct Application {
desktop_file: DesktopFile,
startup_settings: Arc<StartupSettings>,
}
impl Application {
pub fn create(client: &Arc<Client>, desktop_file: DesktopFile) -> Result<Self, NodeError> {
if desktop_file.no_display {
return Err(NodeError::DoesNotExist);
}
let startup_settings = Arc::new(StartupSettings::create(client)?);
Ok(Application {
desktop_file,
startup_settings,
})
}
pub fn name(&self) -> Option<&str> {
self.desktop_file.name.as_deref()
}
pub fn categories(&self) -> &[String] {
self.desktop_file.categories.as_slice()
}
pub fn icon(&self, preferred_px_size: u16, prefer_3d: bool) -> Option<Icon> {
let raw_icons = self.desktop_file.get_raw_icons(preferred_px_size);
let mut icon = raw_icons.iter().max_by_key(|i| i.size).cloned();
if prefer_3d {
icon = raw_icons
.into_iter()
.find(|i| match i.icon_type {
IconType::Gltf => true,
_ => false,
})
.or(icon);
}
icon.and_then(|i| i.cached_process(preferred_px_size).ok())
}
pub fn launch(&self, launch_space: &Spatial) -> Result<(), NodeError> {
self.startup_settings.set_root(launch_space)?;
let future_startup_token = self.startup_settings.generate_startup_token()?;
let future_connection_env = self
.startup_settings
.node()
.client()?
.get_connection_environment()?;
let executable = self
.desktop_file
.command
.clone()
.ok_or(NodeError::DoesNotExist)?;
tokio::task::spawn(async move {
let Ok(startup_token) = future_startup_token.await else {return};
let Ok(connection_env) = future_connection_env.await else {return};
for (k, v) in connection_env.into_iter() {
std::env::set_var(k, v);
}
std::env::set_var("STARDUST_STARTUP_TOKEN", startup_token);
let re = Regex::new(r"%[fFuUdDnNickvm]").unwrap();
let exec = re.replace_all(&executable, "");
unsafe {
Command::new("sh")
.arg("-c")
.arg(exec.to_string())
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.pre_exec(|| {
_ = setsid();
Ok(())
})
.spawn()
.expect("Failed to start child process");
}
});
Ok(())
}
}

View File

@@ -1,2 +1,3 @@
pub mod application;
pub mod protostar;
pub mod xdg;

View File

@@ -1,8 +1,5 @@
use clap::Parser;
use color_eyre::{
eyre::{bail, Result},
Report,
};
use color_eyre::{eyre::Result, Report};
use manifest_dir_macros::directory_relative_path;
use protostar::{protostar::ProtoStar, xdg::parse_desktop_file};
use stardust_xr_fusion::client::Client;
@@ -11,21 +8,8 @@ use std::path::PathBuf;
#[derive(Debug, Parser)]
#[clap(author, version, about, long_about = None)]
struct Args {
#[clap(short, long, default_value_t = 0.1)]
size: f32,
#[clap(
short,
long,
conflicts_with = "command",
required_unless_present = "command",
conflicts_with = "icon",
required_unless_present = "icon"
)]
desktop_file: Option<PathBuf>,
#[clap(short, long, conflicts_with = "desktop_file", requires = "command")]
icon: Option<PathBuf>,
#[clap(short, long, conflicts_with = "desktop_file", requires = "icon")]
command: Option<String>,
// #[clap(short, long)]
desktop_file: PathBuf,
}
#[tokio::main(flavor = "current_thread")]
@@ -35,17 +19,11 @@ async fn main() -> Result<()> {
let (client, event_loop) = Client::connect_with_async_loop().await?;
client.set_base_prefixes(&[directory_relative_path!("res")]);
let protostar = if let Some(desktop_file) = args.desktop_file {
ProtoStar::create_from_desktop_file(
client.get_root(),
[0.0, 0.0, 0.0],
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, None, command)?
} else {
bail!("No command or desktop file, nothing to launch.");
};
let protostar = ProtoStar::create_from_desktop_file(
client.get_root(),
[0.0, 0.0, 0.0],
parse_desktop_file(args.desktop_file).map_err(|e| Report::msg(e))?,
)?;
let _root = client.wrap_root(protostar);

View File

@@ -1,9 +1,10 @@
use crate::xdg::{DesktopFile, Icon, IconType};
use color_eyre::eyre::{eyre, Result};
use crate::{
application::Application,
xdg::{DesktopFile, Icon, IconType},
};
use color_eyre::eyre::Result;
use glam::Quat;
use mint::Vector3;
use nix::unistd::setsid;
use regex::Regex;
use stardust_xr_fusion::{
client::{FrameInfo, RootHandler},
core::values::Transform,
@@ -11,12 +12,9 @@ use stardust_xr_fusion::{
fields::BoxField,
node::NodeType,
spatial::Spatial,
startup_settings::StartupSettings,
};
use stardust_xr_molecules::{GrabData, Grabbable};
use std::f32::consts::PI;
use std::os::unix::process::CommandExt;
use std::process::{Command, Stdio};
use tween::{QuartInOut, Tweener};
const MODEL_SCALE: f32 = 0.03;
@@ -35,13 +33,10 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
t,
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?;
model.set_material_parameter(
1,
"color",
MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]),
)?;
model.set_material_parameter(
0,
model
.model_part("Hex")?
.set_material_parameter("color", MaterialParameter::Color([0.0, 1.0, 1.0, 1.0]))?;
model.model_part("Icon")?.set_material_parameter(
"diffuse",
MaterialParameter::Texture(ResourceID::Direct(icon.path.clone())),
)?;
@@ -57,16 +52,16 @@ fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
}
pub struct ProtoStar {
application: Application,
parent: Spatial,
position: Vector3<f32>,
grabbable: Grabbable,
field: BoxField,
_field: BoxField,
icon: Model,
label: Option<Text>,
grabbable_shrink: Option<Tweener<f32, f64, QuartInOut>>,
grabbable_grow: Option<Tweener<f32, f64, QuartInOut>>,
grabbable_move: Option<Tweener<f32, f64, QuartInOut>>,
execute_command: String,
currently_shown: bool,
}
impl ProtoStar {
@@ -74,45 +69,11 @@ impl ProtoStar {
parent: &Spatial,
position: impl Into<Vector3<f32>>,
desktop_file: DesktopFile,
) -> Result<Self> {
// dbg!(&desktop_file);
let raw_icons = desktop_file.get_raw_icons();
let mut icon = raw_icons
.clone()
.into_iter()
.find(|i| match i.icon_type {
IconType::Gltf => true,
_ => false,
})
.or(raw_icons.into_iter().max_by_key(|i| i.size));
match icon {
Some(i) => {
icon = match i.cached_process(128) {
Ok(i) => Some(i),
_ => None,
}
}
None => {}
}
Self::new_raw(
parent,
position,
desktop_file.name.as_deref(),
icon,
desktop_file.command.ok_or_else(|| eyre!("No command"))?,
)
}
pub fn new_raw(
parent: &Spatial,
position: impl Into<Vector3<f32>>,
name: Option<&str>,
icon: Option<Icon>,
execute_command: String,
) -> Result<Self> {
let position = position.into();
let field = BoxField::create(parent, Transform::default(), [MODEL_SCALE * 2.0; 3])?;
let application = Application::create(&parent.client()?, desktop_file)?;
let icon = application.icon(128, false);
let grabbable = Grabbable::create(
parent,
Transform::from_position(position),
@@ -147,7 +108,7 @@ impl ProtoStar {
text_align: Alignment::Center.into(),
..Default::default()
};
let label = name.and_then(|name| {
let label = application.name().and_then(|name| {
Text::create(
&icon,
Transform::from_position_rotation(
@@ -163,14 +124,14 @@ impl ProtoStar {
parent: parent.alias(),
position,
grabbable,
field,
_field: field,
label,
application,
icon,
grabbable_shrink: None,
grabbable_grow: None,
execute_command,
currently_shown: true,
grabbable_move: None,
currently_shown: true,
})
}
pub fn content_parent(&self) -> &Spatial {
@@ -261,20 +222,15 @@ impl RootHandler for ProtoStar {
.unwrap();
self.grabbable_grow = None;
}
} else if self.grabbable.grab_action().actor_stopped() {
let startup_settings = StartupSettings::create(&self.field.client().unwrap()).unwrap();
startup_settings
.set_root(self.grabbable.content_parent())
.unwrap();
} else if self.grabbable.valid() && self.grabbable.grab_action().actor_stopped() {
self.grabbable_shrink = Some(Tweener::quart_in_out(MODEL_SCALE, 0.0001, 0.25));
let distance_future = self
.grabbable
let Ok(distance_future) = self.grabbable
.content_parent()
.get_position_rotation_scale(&self.parent)
.unwrap();
else {return};
let executable = self.execute_command.clone();
let client = self.content_parent().client().unwrap();
let application = self.application.clone();
let space = self.content_parent().alias();
//TODO: split the executable string for the args
tokio::task::spawn(async move {
@@ -283,30 +239,7 @@ impl RootHandler for ProtoStar {
+ distance_vector.z.powi(2))
.sqrt();
if dbg!(distance) > ACTIVATION_DISTANCE {
let future = startup_settings.generate_startup_token().unwrap();
let env = client.get_connection_environment().unwrap().await.unwrap();
for (k, v) in env.into_iter() {
std::env::set_var(k, v);
}
std::env::set_var("STARDUST_STARTUP_TOKEN", future.await.unwrap());
let re = Regex::new(r"%[fFuUdDnNickvm]").unwrap();
let exec = re.replace_all(&executable, "");
unsafe {
Command::new("sh")
.arg("-c")
.arg(exec.to_string())
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.pre_exec(|| {
_ = setsid();
Ok(())
})
.spawn()
.expect("Failed to start child process");
}
let _ = application.launch(&space);
}
});
}

View File

@@ -183,19 +183,19 @@ pub struct DesktopFile {
pub no_display: bool,
}
impl DesktopFile {
pub fn get_raw_icons(&self) -> Vec<Icon> {
pub fn get_raw_icons(&self, preferred_px_size: u16) -> Vec<Icon> {
// Get the name of the icon from the DesktopFile struct
let Some(icon_name) = self.icon.as_ref() else { return Vec::new(); };
let test_icon_path = self.path.join(Path::new(icon_name));
if test_icon_path.exists() {
if let Some(icon) = Icon::from_path(test_icon_path, 128) {
if let Some(icon) = Icon::from_path(test_icon_path, preferred_px_size) {
return vec![icon];
}
}
let cache_icon_path = get_image_cache_dir().join(icon_name).canonicalize();
if cache_icon_path.is_ok() {
if let Some(icon) = Icon::from_path(cache_icon_path.unwrap(), 128) {
if let Some(icon) = Icon::from_path(cache_icon_path.unwrap(), preferred_px_size) {
return vec![icon];
}
}
@@ -272,7 +272,7 @@ fn test_get_icon_path() {
};
// Call the get_icon_path() function with a size argument and store the result
let icon_paths = desktop_file.get_raw_icons();
let icon_paths = desktop_file.get_raw_icons(128);
dbg!(&icon_paths);
// Assert that the get_icon_path() function returns the expected result