diff --git a/src/main.rs b/src/main.rs index 2caebc6..fbcc4d6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,99 @@ use manifest_dir_macros::directory_relative_path; use stardust_xr_fusion::{ - client::Client, core::values::ResourceID, drawable::Model, spatial::Transform + client::{Client, RootHandler}, core::values::ResourceID, drawable::Model, fields::BoxField, input::InputHandler, spatial::{SpatialAspect, Transform}, HandlerWrapper }; +use stardust_xr_molecules::{input_action::{BaseInputAction, InputActionHandler}, Grabbable, GrabbableSettings}; + +struct Root { + model: Model, + field: BoxField, + grabbable: Grabbable, + input_handler: HandlerWrapper>, + color_hover: BaseInputAction<()>, +} + +impl Root { + async fn create(client: &Client) -> Self { + // stardust_xr_fusion::drawable::Model + // pub fn create(spatial_parent: &impl SpatialAspect, transform: Transform, model: &ResourceID) -> NodeResult + let model = Model::create( + client.get_root(), + Transform::identity(), + &ResourceID::new_namespaced( + "color_cube", + "color_cube" + ), + ).unwrap(); + + let field = + BoxField::create(&model, Transform::none(), mint::Vector3 { x: 0.4, y: 0.4, z: 0.4 }).unwrap(); + + let grab_settings = GrabbableSettings { + zoneable: true, + ..Default::default() + }; + + let grabbable = + Grabbable::create(client.get_root(), Transform::none(), &field, grab_settings).unwrap(); + + model + .set_spatial_parent_in_place(grabbable.content_parent()) + .unwrap(); + + let input_handler = InputActionHandler::wrap( + InputHandler::create( + &model, + Transform::none(), + &field, + ).unwrap(), + (), + ).unwrap(); + + let color_hover = BaseInputAction::new( + false, + |d, _| d.distance < 0.0, + ); + + Root { + model, + field, + grabbable, + input_handler, + color_hover, + } + } +} + +impl RootHandler for Root { + fn frame(&mut self, info: stardust_xr_fusion::client::FrameInfo) { + self.grabbable.update(&info).unwrap(); + + self.input_handler.lock_wrapped().update_actions( + [&mut self.color_hover], + ); + + if self.color_hover.currently_acting.is_empty() { + self.model.set_local_transform( + Transform::from_scale([1.1; 3]) + ).unwrap(); + } else { + self.model.set_local_transform( + Transform::from_scale([1.0; 3]) + ).unwrap(); + } + } + + fn save_state(&mut self) -> stardust_xr_fusion::client::ClientState { + todo!("implement save state") + } +} #[tokio::main] async fn main() { let (client, event_loop) = Client::connect_with_async_loop().await.unwrap(); client.set_base_prefixes(&[directory_relative_path!("res")]); - // stardust_xr_fusion::drawable::Model - // pub fn create(spatial_parent: &impl SpatialAspect, transform: Transform, model: &ResourceID) -> NodeResult - let _model = Model::create( - client.get_root(), - Transform::identity(), - &ResourceID::new_namespaced( - "color_cube", - "color_cube" - ), - ); + let _root = client.wrap_root(Root::create(&client).await).unwrap(); tokio::select! { _ = tokio::signal::ctrl_c() => (),