refactor(input): make all inputs have nodes

This commit is contained in:
Nova
2023-02-24 11:43:06 -05:00
parent 931f857458
commit b5fc321e36
5 changed files with 84 additions and 77 deletions

View File

@@ -1,36 +1,45 @@
use crate::nodes::{
input::{tip::Tip, InputMethod, InputType},
spatial::Spatial,
use crate::{
core::client::INTERNAL_CLIENT,
nodes::{
input::{tip::Tip, InputMethod, InputType},
spatial::Spatial,
Node,
},
};
use color_eyre::eyre::Result;
use glam::Mat4;
use nanoid::nanoid;
use stardust_xr::{
schemas::{flat::Datamap, flex::flexbuffers},
values::Transform,
};
use std::sync::{Arc, Weak};
use std::sync::Arc;
use stereokit::input::{ButtonState, Handed, StereoKitInput};
use tracing::instrument;
pub struct SkController {
tip: Arc<InputMethod>,
_node: Arc<Node>,
input: Arc<InputMethod>,
handed: Handed,
}
impl SkController {
pub fn new(handed: Handed) -> Self {
SkController {
tip: InputMethod::new(
Spatial::new(Weak::new(), None, Mat4::IDENTITY),
InputType::Tip(Tip::default()),
),
pub fn new(handed: Handed) -> Result<Self> {
let _node = Node::create(&INTERNAL_CLIENT, "", &nanoid!(), false).add_to_scenegraph()?;
Spatial::add_to(&_node, None, Mat4::IDENTITY, false)?;
let tip = InputType::Tip(Tip::default());
let input = InputMethod::add_to(&_node, tip, None)?;
Ok(SkController {
_node,
input,
handed,
}
})
}
#[instrument(level = "debug", name = "Update StereoKit Tip Input Method", skip_all)]
pub fn update(&mut self, sk: &impl StereoKitInput) {
let controller = sk.input_controller(self.handed);
*self.tip.enabled.lock() = controller.tracked.contains(ButtonState::Active);
if *self.tip.enabled.lock() {
self.tip.spatial.set_local_transform_components(
*self.input.enabled.lock() = controller.tracked.contains(ButtonState::Active);
if *self.input.enabled.lock() {
self.input.spatial.set_local_transform_components(
None,
Transform::from_position_rotation(
controller.pose.position,
@@ -43,6 +52,6 @@ impl SkController {
map.push("select", controller.trigger);
map.push("grab", controller.grip);
map.end_map();
*self.tip.datamap.lock() = Datamap::new(fbb.take_buffer()).ok();
*self.input.datamap.lock() = Datamap::new(fbb.take_buffer()).ok();
}
}