refactor: make everything asteroids

fix: hexagon spiral

upgrade: asteroids for hotpatch

refactor: better icon handling

feat: cache icons

fix(hexagon): refinement

fix(hexagon): field transform

refactor: broken mess ahaha

fix(single): it woooorks!!!

fix: hexagon launcher!!!

refactor(hexagon_launcher): skip instrumentation dbg

fix: sirius

refactor: make it FAST on asteroids
This commit is contained in:
Nova
2025-05-30 18:08:19 -07:00
parent f5008ff5b5
commit b03a345bf3
19 changed files with 2307 additions and 1908 deletions

View File

@@ -1,301 +0,0 @@
use color_eyre::eyre::Result;
use glam::{EulerRot, Quat, Vec3};
use protostar::{
application::Application,
xdg::{DesktopFile, Icon, IconType},
};
use stardust_xr_fusion::{
core::values::{ResourceID, Vector3},
drawable::{
MaterialParameter, Model, ModelPartAspect, Text, TextBounds, TextFit, TextStyle, XAlign,
YAlign,
},
fields::{CylinderShape, Field, Shape},
node::NodeType,
root::FrameInfo,
spatial::{Spatial, SpatialAspect, SpatialRefAspect, Transform},
};
use stardust_xr_molecules::{FrameSensitive as _, Grabbable, GrabbableSettings, UIElement};
use std::f32::consts::PI;
use tween::{QuartInOut, Tweener};
use crate::{State, ACTIVATION_DISTANCE, APP_SIZE, DEFAULT_HEX_COLOR};
// Model handling
fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
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 / 2.0; 3],
);
let model = Model::create(
parent,
t,
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?;
model
.part("Hex")?
.set_material_parameter("color", MaterialParameter::Color(DEFAULT_HEX_COLOR))?;
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: Field,
// field_lines: Lines,
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>>,
}
impl App {
pub fn create_from_desktop_file(
parent: &Spatial,
position: impl Into<Vector3<f32>>,
desktop_file: DesktopFile,
state: &State,
) -> Result<Self> {
let position = position.into();
let field = Field::create(
parent,
Transform::identity(),
Shape::Cylinder(CylinderShape {
length: 0.01,
radius: APP_SIZE / 2.0,
}),
)?;
// let circle = circle(32, 0.0, APP_SIZE / 2.0).thickness(0.001);
// let field_lines = Lines::create(
// &field,
// Transform::identity(),
// &[
// circle
// .clone()
// .transform(Mat4::from_translation([0.0, 0.0, 0.005].into())),
// circle
// .clone()
// .transform(Mat4::from_translation([0.0, 0.0, -0.005].into())),
// ],
// )?;
let application = Application::create(desktop_file)?;
let icon = application.icon(128, false);
let grabbable = Grabbable::create(
parent,
Transform::from_translation(position),
&field,
GrabbableSettings {
max_distance: 0.05,
zoneable: false,
..Default::default()
},
)?;
if !state.unfurled {
grabbable.set_enabled(false)?;
}
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"),
)?)
})?;
if !state.unfurled {
icon.set_enabled(false)?;
}
let label_style = TextStyle {
character_height: APP_SIZE * 2.0,
bounds: Some(TextBounds {
bounds: [1.0; 2].into(),
fit: TextFit::Wrap,
anchor_align_x: XAlign::Center,
anchor_align_y: YAlign::Center,
}),
text_align_x: XAlign::Center,
text_align_y: YAlign::Center,
..Default::default()
};
let label = application.name().and_then(|name| {
Text::create(
&icon,
Transform::from_translation_rotation(
[0.0, 0.1, -(APP_SIZE * 4.0)],
Quat::from_rotation_x(PI * 0.5),
),
name,
label_style,
)
.ok()
});
if !state.unfurled {
if let Some(label) = label.as_ref() {
label.set_enabled(false)?;
}
}
Ok(App {
parent: parent.clone(),
position,
grabbable,
_field: field,
// field_lines,
label,
application,
icon,
grabbable_shrink: None,
grabbable_grow: None,
grabbable_move: None,
})
}
pub fn content_parent(&self) -> &Spatial {
self.grabbable.content_parent()
}
pub fn apply_state(&mut self, state: &State) {
self.grabbable.set_enabled(state.unfurled).unwrap();
if state.unfurled {
self.icon.set_enabled(true).unwrap();
if let Some(label) = self.label.as_ref() {
label.set_enabled(true).unwrap()
}
self.grabbable_move = Some(Tweener::quart_in_out(0.0001, 1.0, 0.25));
} else {
self.grabbable_move = Some(Tweener::quart_in_out(1.0, 0.0001, 0.25)); //TODO make the scale a parameter
}
}
pub fn frame(&mut self, info: &FrameInfo, state: &State) {
if self.grabbable.handle_events() {
self.grabbable.frame(info);
}
if let Some(grabbable_move) = &mut self.grabbable_move {
if !grabbable_move.is_finished() {
let scale = grabbable_move.move_by(info.delta.into());
self.grabbable
.content_parent()
.set_relative_transform(
&self.parent,
Transform::from_translation(Vec3::from(self.position) * scale),
)
.unwrap();
} else {
if grabbable_move.final_value() == 0.0001 {
self.icon.set_enabled(false).unwrap();
if let Some(label) = self.label.as_ref() {
label.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.into());
self.grabbable
.content_parent()
.set_relative_transform(&self.parent, Transform::from_scale([scale; 3]))
.unwrap();
} else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
if state.unfurled {
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_relative_transform(
&self.parent,
Transform::from_translation(self.position),
)
.unwrap();
self.grabbable
.content_parent()
.set_relative_transform(&self.parent, Transform::from_rotation(Quat::default()))
.unwrap();
self.icon
.set_local_transform(Transform::from_rotation(
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.into());
self.grabbable
.content_parent()
.set_relative_transform(&self.parent, Transform::from_scale([scale; 3]))
.unwrap();
} else {
self.grabbable
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
self.grabbable_grow = None;
}
} else if self.grabbable.grab_action().actor_stopped() {
self.grabbable_shrink = Some(Tweener::quart_in_out(APP_SIZE * 0.5, 0.0001, 0.25));
let application = self.application.clone();
let space = self.content_parent().clone();
let parent = self.parent.clone();
//TODO: split the executable string for the args
tokio::task::spawn(async move {
let distance_vector = space
.get_transform(&parent)
.await
.unwrap()
.translation
.unwrap();
let distance = Vec3::from(distance_vector).length_squared();
if distance > ACTIVATION_DISTANCE {
let client = space.node().client().unwrap();
let space_rot = space
.get_transform(client.get_root())
.await
.unwrap()
.rotation
.unwrap();
let (_, y_rot, _) = Quat::from(space_rot).to_euler(EulerRot::XYZ);
let _ = space.set_relative_transform(
client.get_root(),
Transform::from_rotation_scale(Quat::from_rotation_y(y_rot), [1.0; 3]),
);
let _ = application.launch(&space);
}
});
}
}
}

View File

@@ -1,7 +1,8 @@
#![allow(dead_code)]
use std::ops::Add;
use crate::{APP_SIZE, PADDING};
use tween::TweenTime;
use single::{APP_SIZE, PADDING};
#[derive(Clone, Copy, Debug, Default)]
pub struct Hex {
@@ -26,9 +27,9 @@ impl Hex {
}
pub fn get_coords(&self) -> [f32; 3] {
let x = 3.0 / 2.0 * (APP_SIZE + PADDING) / 2.0 * (-self.q - self.s).to_f32();
let x = 3.0 / 2.0 * (APP_SIZE + PADDING) / 2.0 * (-self.q - self.s) as f32;
let y = 3.0_f32.sqrt() * (APP_SIZE + PADDING) / 2.0
* ((-self.q - self.s).to_f32() / 2.0 + self.s.to_f32());
* ((-self.q - self.s) as f32 / 2.0 + self.s as f32);
[x, y, 0.0]
}
@@ -39,6 +40,42 @@ impl Hex {
pub fn scale(self, factor: isize) -> Self {
Hex::new(self.q * factor, self.r * factor, self.s * factor)
}
/// outputs a hexagon at an outward spiral at position i, where i=0 is the center.
pub fn spiral(i: usize) -> Self {
if i == 0 {
return HEX_CENTER;
}
// Find which ring we're in and position within ring
let mut cells_before = 1; // Count center
let mut radius = 1;
while cells_before + (radius * 6) <= i {
cells_before += radius * 6;
radius += 1;
}
// Calculate steps needed within current ring
let pos_in_ring = i - cells_before;
// Start at top of ring (same as original code)
let mut hex = HEX_CENTER + HEX_DIRECTION_VECTORS[4].scale(radius as isize);
// Walk around sides just like original code
let mut steps_taken = 0;
for side in 0..6 {
for _ in 0..radius {
if steps_taken == pos_in_ring {
return hex;
}
hex = hex.neighbor(side);
steps_taken += 1;
}
}
hex
}
}
impl Add for Hex {
type Output = Hex;

View File

@@ -1,248 +1,150 @@
pub mod app;
pub mod hex;
mod hex;
use app::App;
use color_eyre::eyre::Result;
use asteroids::{
ClientState, CustomElement, Element, Migrate, Reify, Transformable, client,
elements::{Button, Grabbable, Model, ModelPart, PointerMode, Spatial},
};
use glam::Quat;
use hex::{HEX_CENTER, HEX_DIRECTION_VECTORS};
use manifest_dir_macros::directory_relative_path;
use protostar::xdg::{get_desktop_files, parse_desktop_file, DesktopFile};
use hex::Hex;
use mint::{Quaternion, Vector3};
use protostar::xdg::{DesktopFile, get_desktop_files};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use single::{APP_SIZE, App, BTN_COLOR, BTN_SELECTED_COLOR, MODEL_SCALE};
use stardust_xr_fusion::{
client::Client,
core::values::{
color::{color_space::LinearRgb, rgba_linear, Rgba},
ResourceID,
},
drawable::{MaterialParameter, Model, ModelPartAspect},
node::NodeError,
root::{ClientState, FrameInfo, RootAspect, RootEvent},
spatial::{Spatial, SpatialAspect, Transform},
ClientHandle,
drawable::MaterialParameter,
fields::{CylinderShape, Shape},
project_local_resources,
spatial::Transform,
};
use stardust_xr_molecules::{
button::{Button, ButtonSettings},
FrameSensitive, Grabbable, GrabbableSettings, PointerMode, UIElement,
};
use std::{f32::consts::PI, sync::Arc, time::Duration};
const APP_SIZE: f32 = 0.06;
const PADDING: f32 = 0.005;
const MODEL_SCALE: f32 = 0.03;
const ACTIVATION_DISTANCE: f32 = 0.05;
const DEFAULT_HEX_COLOR: Rgba<f32, LinearRgb> = rgba_linear!(0.211, 0.937, 0.588, 1.0);
const BTN_SELECTED_COLOR: Rgba<f32, LinearRgb> = rgba_linear!(0.0, 1.0, 0.0, 1.0);
const BTN_COLOR: Rgba<f32, LinearRgb> = rgba_linear!(1.0, 1.0, 0.0, 1.0);
use std::f32::consts::{FRAC_PI_2, PI};
use tracing_subscriber::{EnvFilter, Layer, layer::SubscriberExt, util::SubscriberInitExt};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
async fn main() {
color_eyre::install().unwrap();
tracing_subscriber::fmt()
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.pretty()
.init();
let owned_client = Client::connect().await?;
let client = owned_client.handle();
let async_loop = owned_client.async_event_loop();
client
.get_root()
.set_base_prefixes(&[directory_relative_path!("../res").to_string()])
.unwrap();
let mut grid = AppHexGrid::new(&client).await;
let mut owned_client = async_loop.stop().await.unwrap();
let event_loop = owned_client.sync_event_loop(|handle, _| {
let Some(event) = handle.get_root().recv_root_event() else {
return;
};
match event {
RootEvent::Ping { response } => response.send(Ok(())),
RootEvent::Frame { info } => {
grid.frame(info);
}
RootEvent::SaveState { response } => {
response.send(grid.save_state());
}
}
let registry = tracing_subscriber::registry();
#[cfg(feature = "tracy")]
let registry = registry.with({
use tracing_subscriber::Layer;
tracing_tracy::TracyLayer::new(tracing_tracy::DefaultConfig::default())
.with_filter(tracing::level_filters::LevelFilter::DEBUG)
});
let log_layer = tracing_subscriber::fmt::Layer::new()
.with_thread_names(true)
.with_ansi(true)
.with_line_number(true)
.with_filter(EnvFilter::from_default_env());
registry.with(log_layer).init();
tokio::select! {
_ = tokio::signal::ctrl_c() => (),
e = event_loop => e?,
}
Ok(())
client::run::<HexagonLauncher>(&[&project_local_resources!("../res")]).await
}
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct State {
unfurled: bool,
}
struct AppHexGrid {
movable_root: Spatial,
#[derive(Debug, Serialize, Deserialize)]
pub struct HexagonLauncher {
/// if the hexagon launcher is expanded
open: bool,
pos: Vector3<f32>,
rot: Quaternion<f32>,
#[serde(skip)]
/// position in the vector is mapped to hex coordinates
apps: Vec<App>,
button: CenterButton,
state: State,
}
impl AppHexGrid {
async fn new(client: &Arc<ClientHandle>) -> Self {
let client_state = client.get_root().get_state().await.unwrap();
let state = client_state.data().unwrap_or_default();
let movable_root =
Spatial::create(client.get_root(), Transform::identity(), false).unwrap();
impl Default for HexagonLauncher {
fn default() -> Self {
Self {
open: false,
pos: [0.0; 3].into(),
rot: Quat::IDENTITY.into(),
apps: Vec::new(),
}
}
}
impl Migrate for HexagonLauncher {
type Old = Self;
}
let button = CenterButton::new(client, &client_state).unwrap();
tokio::time::sleep(Duration::from_millis(10)).await; // give it a bit of time to send the messages properly
impl ClientState for HexagonLauncher {
const APP_ID: &'static str = "org.protostar.hexagon_launcher";
let mut desktop_files: Vec<DesktopFile> = get_desktop_files()
.filter_map(|d| parse_desktop_file(d).ok())
fn initial_state_update(&mut self) {
// Load desktop files
self.apps = get_desktop_files()
.filter_map(|d| DesktopFile::parse(d).ok())
.filter(|d| !d.no_display)
.filter_map(|d| App::new(d).ok())
.collect();
desktop_files.sort_by_key(|d| d.clone().name.unwrap_or_default());
self.apps.par_iter().for_each(|app| {
app.load_icon();
});
let mut apps = Vec::new();
let mut radius = 1;
while !desktop_files.is_empty() {
let mut hex = HEX_CENTER + HEX_DIRECTION_VECTORS[4].scale(radius);
for i in 0..6 {
if desktop_files.is_empty() {
break;
};
for _ in 0..radius {
if desktop_files.is_empty() {
break;
};
apps.push(
App::create_from_desktop_file(
button.grabbable.content_parent(),
hex.get_coords(),
desktop_files.pop().unwrap(),
&state,
)
.unwrap(),
);
hex = hex.neighbor(i);
}
}
radius += 1;
}
AppHexGrid {
movable_root,
apps,
button,
state,
}
// Sort by name
self.apps
.sort_by_key(|app| app.app.name().unwrap_or_default().to_string());
}
}
impl AppHexGrid {
fn frame(&mut self, info: FrameInfo) {
self.button.frame(&info);
if self.button.button.pressed() {
self.button
.model
.part("Hex")
.unwrap()
.set_material_parameter("color", MaterialParameter::Color(BTN_SELECTED_COLOR))
.unwrap();
self.state.unfurled = !self.state.unfurled;
for app in &mut self.apps {
app.apply_state(&self.state);
}
} else if self.button.button.released() {
self.button
.model
.part("Hex")
.unwrap()
.set_material_parameter("color", MaterialParameter::Color(BTN_COLOR))
.unwrap();
}
for app in &mut self.apps {
app.frame(&info, &self.state);
}
}
fn save_state(&mut self) -> Result<ClientState> {
self.movable_root
.set_relative_transform(
self.button.grabbable.content_parent(),
Transform::from_translation([0.0; 3]),
)
.unwrap();
ClientState::new(
Some(self.state.clone()),
&self.movable_root,
[(
"content_parent".to_string(),
self.button.grabbable.content_parent(),
)]
.into_iter()
.collect(),
impl Reify for HexagonLauncher {
#[tracing::instrument(skip_all)]
fn reify(&self) -> impl Element<Self> {
// Build UI based on current state
Grabbable::new(
Shape::Cylinder(CylinderShape {
radius: APP_SIZE / 2.0,
length: 0.01,
}),
self.pos,
self.rot,
|state: &mut Self, pos, rot| {
state.pos = pos;
state.rot = rot;
},
)
.field_transform(Transform::from_rotation(Quat::from_rotation_x(FRAC_PI_2)))
.pointer_mode(PointerMode::Align)
.zoneable(false)
.build()
.child(
Button::new(|state: &mut HexagonLauncher| {
state.open = !state.open;
})
.pos([0.0, 0.0, 0.005])
.size([APP_SIZE / 2.0; 2])
.build(),
)
.child(
Model::namespaced("protostar", "hexagon/hexagon")
.transform(Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[MODEL_SCALE; 3],
))
.part(ModelPart::new("Hex").mat_param(
"color",
MaterialParameter::Color(if self.open {
BTN_SELECTED_COLOR
} else {
BTN_COLOR
}),
))
.build(),
)
.children(
self.open
.then(|| {
self.apps.iter().enumerate().map(|(i, app)| {
Spatial::default()
.pos(Hex::spiral(i + 1).get_coords())
.build()
.identify(&app.app.name())
.child(app.reify_substate(move |state: &mut HexagonLauncher| {
state.apps.get_mut(i)
}))
})
})
.into_iter()
.flatten(),
)
}
}
struct CenterButton {
button: Button,
grabbable: Grabbable,
model: Model,
}
impl CenterButton {
fn new(client: &Arc<ClientHandle>, state: &ClientState) -> Result<Self, NodeError> {
// (APP_SIZE + PADDING) / 2.0,
let button = Button::create(
client.get_root(),
Transform::identity(),
[(APP_SIZE + PADDING) / 2.0; 2],
ButtonSettings {
visuals: None,
..Default::default()
},
)?;
let grabbable = Grabbable::create(
client.get_root(),
Transform::none(),
button.touch_plane().field(),
GrabbableSettings {
max_distance: 0.025,
pointer_mode: PointerMode::Align,
magnet: false,
..Default::default()
},
)?;
button
.touch_plane()
.root()
.set_spatial_parent(grabbable.content_parent())?;
let model = Model::create(
grabbable.content_parent(),
Transform::from_rotation_scale(
Quat::from_rotation_x(PI / 2.0) * Quat::from_rotation_y(PI),
[MODEL_SCALE; 3],
),
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?;
model
.part("Hex")?
.set_material_parameter("color", MaterialParameter::Color(BTN_COLOR))?;
if let Some(content_parent) = state.spatial_anchors(client).get("content_parent") {
grabbable
.content_parent()
.set_relative_transform(content_parent, Transform::identity())?;
}
Ok(CenterButton {
button,
grabbable,
model,
})
}
fn frame(&mut self, info: &FrameInfo) {
if self.grabbable.handle_events() {
self.grabbable.frame(info);
}
self.button.handle_events();
}
}