refactor(input): switch to manual handler order

This commit is contained in:
Nova
2024-04-16 08:00:07 -04:00
parent 226554fadc
commit be2f5b8e37
14 changed files with 414 additions and 150 deletions

View File

@@ -1,13 +1,14 @@
use crate::{
core::client::INTERNAL_CLIENT,
nodes::{
input::{InputDataType, InputMethod, Pointer},
fields::Ray,
input::{InputDataType, InputMethod, Pointer, INPUT_HANDLER_REGISTRY},
spatial::Spatial,
Node,
},
};
use color_eyre::eyre::Result;
use glam::Mat4;
use glam::{vec3, Mat4};
use nanoid::nanoid;
use serde::{Deserialize, Serialize};
use stardust_xr::values::Datamap;
@@ -56,5 +57,47 @@ impl EyePointer {
// Set pointer input datamap
*self.pointer.datamap.lock() = Datamap::from_typed(EyeDatamap { eye: 2 }).unwrap();
}
// send input to all the input handlers that are the closest to the ray as possible
let rx = INPUT_HANDLER_REGISTRY
.get_valid_contents()
.into_iter()
// filter out all the disabled handlers
.filter(|handler| {
let Some(node) = handler.node.upgrade() else {
return false;
};
node.enabled()
})
// ray march to all the enabled handlers' fields
.map(|handler| {
let result = handler.field.ray_march(Ray {
origin: vec3(0.0, 0.0, 0.0),
direction: vec3(0.0, 0.0, -1.0),
space: self.spatial.clone(),
});
(vec![handler], result)
})
// make sure the field isn't at the pointer origin and that it's being hit
.filter(|(_, result)| result.deepest_point_distance > 0.01 && result.min_distance < 0.0)
// .inspect(|(_, result)| {
// dbg!(result);
// })
// now collect all handlers that are same distance if they're the closest
.reduce(|(mut handlers_a, result_a), (handlers_b, result_b)| {
if (result_a.deepest_point_distance - result_b.deepest_point_distance).abs() < 0.001
{
// distance is basically the same
handlers_a.extend(handlers_b);
(handlers_a, result_a)
} else if result_a.deepest_point_distance < result_b.deepest_point_distance {
(handlers_a, result_a)
} else {
(handlers_b, result_b)
}
})
.map(|(rx, _)| rx)
.unwrap_or_default();
self.pointer.set_handler_order(rx.iter());
}
}

View File

@@ -5,7 +5,7 @@ use crate::{
mask_matches, pulse_receiver_client, PulseSender, KEYMAPS, PULSE_RECEIVER_REGISTRY,
},
fields::{Field, Ray},
input::{InputDataType, InputMethod, Pointer},
input::{InputDataType, InputHandler, InputMethod, Pointer, INPUT_HANDLER_REGISTRY},
spatial::Spatial,
Node,
},
@@ -52,6 +52,7 @@ pub struct MousePointer {
node: Arc<Node>,
spatial: Arc<Spatial>,
pointer: Arc<InputMethod>,
capture: Option<Arc<InputHandler>>,
mouse_datamap: MouseEvent,
keyboard_datamap: KeyboardEvent,
keyboard_sender: Arc<PulseSender>,
@@ -84,6 +85,7 @@ impl MousePointer {
node,
spatial,
pointer,
capture: None,
mouse_datamap: Default::default(),
keyboard_datamap: Default::default(),
keyboard_sender,
@@ -134,9 +136,106 @@ impl MousePointer {
self.mouse_datamap.scroll_discrete = vec2(0.0, mouse.scroll_change / 120.0);
*self.pointer.datamap.lock() = Datamap::from_typed(&self.mouse_datamap).unwrap();
}
self.target_pointer_input();
self.send_keyboard_input(sk);
}
fn target_pointer_input(&mut self) {
// remove the capture when it's removed from captures list
if let Some(capture) = &self.capture {
if !self
.pointer
.captures
.get_valid_contents()
.contains(&capture)
{
self.capture.take();
}
}
// add the capture that's the closest if we don't have one
if self.capture.is_none() {
if let Some(new_capture) = self
.pointer
.captures
.get_valid_contents()
.into_iter()
.map(|h| {
(
h.clone(),
h.field.ray_march(Ray {
origin: vec3(0.0, 0.0, 0.0),
direction: vec3(0.0, 0.0, -1.0),
space: self.spatial.clone(),
}),
)
})
.reduce(|(handlers_a, result_a), (handlers_b, result_b)| {
if result_a.min_distance < result_b.min_distance {
(handlers_a, result_a)
} else {
(handlers_b, result_b)
}
})
.map(|(rx, _)| rx)
{
self.capture = Some(new_capture.clone());
}
}
// make sure that if something is captured only send input to it
if let Some(capture) = &self.capture {
self.pointer.set_handler_order([capture].into_iter());
return;
}
// send input to all the input handlers that are the closest to the ray as possible
self.pointer.set_handler_order(
INPUT_HANDLER_REGISTRY
.get_valid_contents()
.into_iter()
// filter out all the disabled handlers
.filter(|handler| {
let Some(node) = handler.node.upgrade() else {
return false;
};
node.enabled()
})
// ray march to all the enabled handlers' fields
.map(|handler| {
let result = handler.field.ray_march(Ray {
origin: vec3(0.0, 0.0, 0.0),
direction: vec3(0.0, 0.0, -1.0),
space: self.spatial.clone(),
});
(vec![handler], result)
})
// make sure the field isn't at the pointer origin and that it's being hit
.filter(|(_, result)| {
result.deepest_point_distance > 0.01 && result.min_distance < 0.0
})
// .inspect(|(_, result)| {
// dbg!(result);
// })
// now collect all handlers that are same distance if they're the closest
.reduce(|(mut handlers_a, result_a), (handlers_b, result_b)| {
if (result_a.deepest_point_distance - result_b.deepest_point_distance).abs()
< 0.001
{
// distance is basically the same
handlers_a.extend(handlers_b);
(handlers_a, result_a)
} else if result_a.deepest_point_distance < result_b.deepest_point_distance {
(handlers_a, result_a)
} else {
(handlers_b, result_b)
}
})
.map(|(rx, _)| rx)
.unwrap_or_default()
.iter(),
);
}
fn send_keyboard_input(&mut self, sk: &impl StereoKitMultiThread) {
let rx = PULSE_RECEIVER_REGISTRY
.get_valid_contents()

View File

@@ -1,7 +1,7 @@
use crate::{
core::client::INTERNAL_CLIENT,
nodes::{
input::{InputDataType, InputMethod, Tip},
input::{InputDataType, InputHandler, InputMethod, Tip, INPUT_HANDLER_REGISTRY},
spatial::Spatial,
Node,
},
@@ -26,8 +26,9 @@ struct ControllerDatamap {
pub struct SkController {
_node: Arc<Node>,
input: Arc<InputMethod>,
model: Model,
handed: Handed,
model: Model,
capture: Option<Arc<InputHandler>>,
datamap: ControllerDatamap,
}
impl SkController {
@@ -56,6 +57,7 @@ impl SkController {
input,
handed,
model,
capture: None,
datamap: Default::default(),
})
}
@@ -79,5 +81,85 @@ impl SkController {
self.datamap.grab = controller.grip;
self.datamap.scroll = controller.stick;
*self.input.datamap.lock() = Datamap::from_typed(&self.datamap).unwrap();
// remove the capture when it's removed from captures list
if let Some(capture) = &self.capture {
if !self.input.captures.get_valid_contents().contains(&capture) {
self.capture.take();
}
}
// add the capture that's the closest if we don't have one
if self.capture.is_none() {
self.capture = self
.input
.captures
.get_valid_contents()
.into_iter()
.map(|handler| {
(
handler.clone(),
handler
.field
.distance(&self.input.spatial, [0.0; 3].into())
.abs(),
)
})
.reduce(|(handlers_a, distance_a), (handlers_b, distance_b)| {
if distance_a < distance_b {
(handlers_a, distance_a)
} else {
(handlers_b, distance_b)
}
})
.map(|(rx, _)| rx);
}
// make sure that if something is captured only send input to it
if let Some(capture) = &self.capture {
self.input.set_handler_order([capture].into_iter());
return;
}
// send input to all the input handlers that are the closest to the ray as possible
self.input.set_handler_order(
INPUT_HANDLER_REGISTRY
.get_valid_contents()
.into_iter()
// filter out all the disabled handlers
.filter(|handler| {
let Some(node) = handler.node.upgrade() else {
return false;
};
node.enabled()
})
// get the unsigned distance to the handler's field (unsigned so giant fields won't always eat input)
.map(|handler| {
(
vec![handler.clone()],
handler
.field
.distance(&self.input.spatial, [0.0; 3].into())
.abs(),
)
})
// .inspect(|(_, result)| {
// dbg!(result);
// })
// now collect all handlers that are same distance if they're the closest
.reduce(|(mut handlers_a, distance_a), (handlers_b, distance_b)| {
if (distance_a - distance_b).abs() < 0.001 {
// distance is basically the same (within 1mm)
handlers_a.extend(handlers_b);
(handlers_a, distance_a)
} else if distance_a < distance_b {
(handlers_a, distance_a)
} else {
(handlers_b, distance_b)
}
})
.map(|(rx, _)| rx)
.unwrap_or_default()
.iter(),
);
}
}

View File

@@ -1,5 +1,6 @@
use crate::core::client::INTERNAL_CLIENT;
use crate::nodes::input::InputDataType;
use crate::nodes::fields::Field;
use crate::nodes::input::{InputDataType, InputHandler, INPUT_HANDLER_REGISTRY};
use crate::nodes::{
input::{Hand, InputMethod, Joint},
spatial::Spatial,
@@ -10,8 +11,9 @@ use glam::Mat4;
use nanoid::nanoid;
use serde::{Deserialize, Serialize};
use stardust_xr::values::Datamap;
use std::f32::INFINITY;
use std::sync::Arc;
use stereokit::{ButtonState, HandJoint, Handed, StereoKitMultiThread};
use stereokit::{ButtonState, Color128, HandJoint, Handed, Material, StereoKitMultiThread};
fn convert_joint(joint: HandJoint) -> Joint {
Joint {
@@ -30,12 +32,14 @@ struct HandDatamap {
pub struct SkHand {
_node: Arc<Node>,
input: Arc<InputMethod>,
handed: Handed,
material: Material,
input: Arc<InputMethod>,
capture: Option<Arc<InputHandler>>,
datamap: HandDatamap,
}
impl SkHand {
pub fn new(handed: Handed) -> Result<Self> {
pub fn new(handed: Handed, sk: &impl StereoKitMultiThread) -> Result<Self> {
let _node = Node::create_parent_name(&INTERNAL_CLIENT, "", &nanoid!(), false)
.add_to_scenegraph()?;
Spatial::add_to(&_node, None, Mat4::IDENTITY, false);
@@ -45,10 +49,15 @@ impl SkHand {
});
let datamap = Datamap::from_typed(HandDatamap::default())?;
let input = InputMethod::add_to(&_node, hand, datamap)?;
let material = sk.material_copy(Material::HAND);
sk.input_hand_material(handed, Material(material.0));
Ok(SkHand {
_node,
input,
handed,
material,
input,
capture: None,
datamap: Default::default(),
})
}
@@ -98,5 +107,94 @@ impl SkHand {
self.datamap.pinch_strength = sk_hand.pinch_activation;
self.datamap.grab_strength = sk_hand.grip_activation;
*self.input.datamap.lock() = Datamap::from_typed(&self.datamap).unwrap();
// remove the capture when it's removed from captures list
if let Some(capture) = &self.capture {
if !self.input.captures.get_valid_contents().contains(&capture) {
self.capture.take();
sk.material_set_color(&self.material, "color", Color128::new_rgb(1.0, 1.0, 1.0));
}
}
// add the capture that's the closest if we don't have one
if self.capture.is_none() {
self.capture = self
.input
.captures
.get_valid_contents()
.into_iter()
.map(|handler| (handler.clone(), self.compare_distance(&handler.field).abs()))
.reduce(|(handlers_a, distance_a), (handlers_b, distance_b)| {
if distance_a < distance_b {
(handlers_a, distance_a)
} else {
(handlers_b, distance_b)
}
})
.map(|(rx, _)| rx);
if self.capture.is_some() {
sk.material_set_color(&self.material, "color", Color128::new_rgb(0.0, 1.0, 0.75));
}
}
// make sure that if something is captured only send input to it
if let Some(capture) = &self.capture {
self.input.set_handler_order([capture].into_iter());
return;
}
// send input to all the input handlers that are the closest to the ray as possible
self.input.set_handler_order(
INPUT_HANDLER_REGISTRY
.get_valid_contents()
.into_iter()
// filter out all the disabled handlers
.filter(|handler| {
let Some(node) = handler.node.upgrade() else {
return false;
};
node.enabled()
})
// get the unsigned distance to the handler's field (unsigned so giant fields won't always eat input)
.map(|handler| {
(
vec![handler.clone()],
self.compare_distance(&handler.field).abs(),
)
})
// .inspect(|(_, result)| {
// dbg!(result);
// })
// now collect all handlers that are same distance if they're the closest
.reduce(|(mut handlers_a, distance_a), (handlers_b, distance_b)| {
if (distance_a - distance_b).abs() < 0.001 {
// distance is basically the same (within 1mm)
handlers_a.extend(handlers_b);
(handlers_a, distance_a)
} else if distance_a < distance_b {
(handlers_a, distance_a)
} else {
(handlers_b, distance_b)
}
})
.map(|(rx, _)| rx)
.unwrap_or_default()
.iter(),
);
}
fn compare_distance(&self, field: &Field) -> f32 {
let InputDataType::Hand(hand) = &*self.input.data.lock() else {
return INFINITY;
};
let spatial = &self.input.spatial;
let thumb_tip_distance = field.distance(spatial, hand.thumb.tip.position.into());
let index_tip_distance = field.distance(spatial, hand.index.tip.position.into());
let middle_tip_distance = field.distance(spatial, hand.middle.tip.position.into());
let ring_tip_distance = field.distance(spatial, hand.ring.tip.position.into());
(thumb_tip_distance * 0.3)
+ (index_tip_distance * 0.4)
+ (middle_tip_distance * 0.15)
+ (ring_tip_distance * 0.15)
}
}