feat: save/restore state

This commit is contained in:
Nova
2024-02-20 23:12:23 -05:00
parent a85a9f56a6
commit c582c85a1a
10 changed files with 119 additions and 46 deletions

View File

@@ -14,5 +14,6 @@ mint = "0.5.9"
tween = "2.0.1"
tracing-subscriber = "0.3.17"
tokio = { workspace = true }
serde = { workspace = true }
stardust-xr-fusion = { workspace = true }
stardust-xr-molecules = { workspace = true }

View File

@@ -20,7 +20,7 @@ use stardust_xr_molecules::{Grabbable, GrabbableSettings};
use std::f32::consts::PI;
use tween::{QuartInOut, Tweener};
use crate::{ACTIVATION_DISTANCE, APP_SIZE, DEFAULT_HEX_COLOR};
use crate::{State, ACTIVATION_DISTANCE, APP_SIZE, DEFAULT_HEX_COLOR};
// Model handling
fn model_from_icon(parent: &Spatial, icon: &Icon) -> Result<Model> {
@@ -65,13 +65,13 @@ pub struct App {
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,
state: &State,
) -> Result<Self> {
let position = position.into();
let field = BoxField::create(parent, Transform::identity(), [APP_SIZE; 3])?;
@@ -87,6 +87,9 @@ impl App {
..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
@@ -101,6 +104,9 @@ impl App {
&ResourceID::new_namespaced("protostar", "hexagon/hexagon"),
)?)
})?;
if !state.unfurled {
icon.set_enabled(false)?;
}
let label_style = TextStyle {
character_height: APP_SIZE * 2.0,
@@ -127,6 +133,12 @@ impl App {
)
.ok()
});
if !state.unfurled {
if let Some(label) = label.as_ref() {
label.set_enabled(false)?;
}
}
Ok(App {
parent: parent.alias(),
position,
@@ -138,27 +150,25 @@ impl App {
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 {
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
}
self.currently_shown = !self.currently_shown;
}
pub fn frame(&mut self, info: FrameInfo) {
pub fn frame(&mut self, info: FrameInfo, state: &State) {
let _ = self.grabbable.update(&info);
if let Some(grabbable_move) = &mut self.grabbable_move {
@@ -193,7 +203,7 @@ impl App {
.content_parent()
.set_spatial_parent(&self.parent)
.unwrap();
if self.currently_shown {
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();

View File

@@ -8,16 +8,17 @@ 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 serde::{Deserialize, Serialize};
use stardust_xr_fusion::{
client::{Client, ClientState, FrameInfo, RootHandler},
core::values::ResourceID,
core::{schemas::flex::flexbuffers, values::ResourceID},
drawable::{MaterialParameter, Model, ModelPartAspect},
fields::BoxField,
node::NodeError,
spatial::{SpatialAspect, Transform},
node::{NodeError, NodeType},
spatial::{Spatial, SpatialAspect, Transform},
};
use stardust_xr_molecules::{touch_plane::TouchPlane, Grabbable, GrabbableSettings, PointerMode};
use std::f32::consts::PI;
use std::{f32::consts::PI, time::Duration};
const APP_SIZE: f32 = 0.06;
const PADDING: f32 = 0.005;
@@ -38,7 +39,7 @@ async fn main() -> Result<()> {
let (client, event_loop) = Client::connect_with_async_loop().await?;
client.set_base_prefixes(&[directory_relative_path!("res")]);
let _root = client.wrap_root(AppHexGrid::new(&client))?;
let _root = client.wrap_root(AppHexGrid::new(&client).await)?;
tokio::select! {
_ = tokio::signal::ctrl_c() => (),
@@ -47,13 +48,27 @@ async fn main() -> Result<()> {
Ok(())
}
#[derive(Debug, Default, Serialize, Deserialize)]
pub struct State {
unfurled: bool,
}
struct AppHexGrid {
movable_root: Spatial,
apps: Vec<App>,
button: Button,
state: State,
}
impl AppHexGrid {
fn new(client: &Client) -> Self {
let button = Button::new(client).unwrap();
async fn new(client: &Client) -> Self {
let state = flexbuffers::from_slice(&client.state().data).unwrap_or_default();
let movable_root =
Spatial::create(client.get_root(), Transform::identity(), false).unwrap();
let button = Button::new(client, &client.state()).unwrap();
tokio::time::sleep(Duration::from_millis(10)).await; // give it a bit of time to send the messages properly
let mut desktop_files: Vec<DesktopFile> = get_desktop_files()
.filter_map(|d| parse_desktop_file(d).ok())
.filter(|d| !d.no_display)
@@ -78,6 +93,7 @@ impl AppHexGrid {
button.grabbable.content_parent(),
hex.get_coords(),
desktop_files.pop().unwrap(),
&state,
)
.unwrap(),
);
@@ -86,7 +102,12 @@ impl AppHexGrid {
}
radius += 1;
}
AppHexGrid { apps, button }
AppHexGrid {
movable_root,
apps,
button,
state,
}
}
}
impl RootHandler for AppHexGrid {
@@ -99,8 +120,9 @@ impl RootHandler for AppHexGrid {
.unwrap()
.set_material_parameter("color", MaterialParameter::Color(BTN_SELECTED_COLOR))
.unwrap();
self.state.unfurled = !self.state.unfurled;
for app in &mut self.apps {
app.toggle();
app.apply_state(&self.state);
}
} else if self.button.touch_plane.touch_stopped() {
self.button
@@ -111,12 +133,27 @@ impl RootHandler for AppHexGrid {
.unwrap();
}
for app in &mut self.apps {
app.frame(info);
app.frame(info, &self.state);
}
}
fn save_state(&mut self) -> ClientState {
ClientState::default()
self.movable_root
.set_relative_transform(
self.button.grabbable.content_parent(),
Transform::from_translation([0.0; 3]),
)
.unwrap();
ClientState {
data: flexbuffers::to_vec(&self.state).unwrap(),
root: self.movable_root.alias(),
spatial_anchors: [(
"content_parent".to_string(),
self.button.grabbable.content_parent().alias(),
)]
.into_iter()
.collect(),
}
}
}
@@ -126,7 +163,7 @@ struct Button {
model: Model,
}
impl Button {
fn new(client: &Client) -> Result<Self, NodeError> {
fn new(client: &Client, state: &ClientState) -> Result<Self, NodeError> {
let field = BoxField::create(client.get_root(), Transform::identity(), [APP_SIZE; 3])?;
let grabbable = Grabbable::create(
client.get_root(),
@@ -161,6 +198,11 @@ impl Button {
model
.model_part("Hex")?
.set_material_parameter("color", MaterialParameter::Color(BTN_COLOR))?;
if let Some(content_parent) = state.spatial_anchors.get("content_parent") {
grabbable
.content_parent()
.set_relative_transform(content_parent, Transform::identity())?;
}
Ok(Button {
touch_plane,
grabbable,