refactor(input): switch to manual handler order
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use super::{DistanceLink, Finger, Hand, InputDataTrait, Joint, Thumb};
|
||||
use super::{Finger, Hand, InputDataTrait, InputLink, Joint, Thumb};
|
||||
use crate::nodes::fields::Field;
|
||||
use crate::nodes::spatial::Spatial;
|
||||
use glam::{vec3a, Mat4, Quat};
|
||||
@@ -52,10 +52,7 @@ impl Default for Hand {
|
||||
}
|
||||
|
||||
impl InputDataTrait for Hand {
|
||||
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
self.true_distance(space, field).abs()
|
||||
}
|
||||
fn true_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
let mut min_distance = f32::MAX;
|
||||
|
||||
for tip in [
|
||||
@@ -70,7 +67,7 @@ impl InputDataTrait for Hand {
|
||||
|
||||
min_distance
|
||||
}
|
||||
fn update_to(&mut self, distance_link: &DistanceLink, local_to_handler_matrix: Mat4) {
|
||||
fn update_to(&mut self, input_link: &InputLink, local_to_handler_matrix: Mat4) {
|
||||
let mut joints: Vec<&mut Joint> = Vec::new();
|
||||
|
||||
joints.extend([&mut self.palm, &mut self.wrist]);
|
||||
@@ -104,10 +101,10 @@ impl InputDataTrait for Hand {
|
||||
let (_, rotation, position) = joint_matrix.to_scale_rotation_translation();
|
||||
joint.position = position.into();
|
||||
joint.rotation = rotation.into();
|
||||
joint.distance = distance_link
|
||||
joint.distance = input_link
|
||||
.handler
|
||||
.field
|
||||
.distance(&distance_link.handler.spatial, position.into());
|
||||
.distance(&input_link.handler.spatial, position.into());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use super::{
|
||||
input_handler_client, DistanceLink, InputHandlerAspect, INPUT_HANDLER_REGISTRY,
|
||||
input_handler_client, InputHandlerAspect, InputLink, INPUT_HANDLER_REGISTRY,
|
||||
INPUT_METHOD_REGISTRY,
|
||||
};
|
||||
use crate::{
|
||||
@@ -7,13 +7,11 @@ use crate::{
|
||||
nodes::{fields::Field, spatial::Spatial, Aspect, Node},
|
||||
};
|
||||
use color_eyre::eyre::Result;
|
||||
use portable_atomic::AtomicBool;
|
||||
use stardust_xr::values::Datamap;
|
||||
use std::sync::{Arc, Weak};
|
||||
use tracing::instrument;
|
||||
|
||||
pub struct InputHandler {
|
||||
pub enabled: Arc<AtomicBool>,
|
||||
pub uid: String,
|
||||
pub node: Weak<Node>,
|
||||
pub spatial: Arc<Spatial>,
|
||||
@@ -23,7 +21,6 @@ pub struct InputHandler {
|
||||
impl InputHandler {
|
||||
pub fn add_to(node: &Arc<Node>, field: &Arc<Field>) -> Result<()> {
|
||||
let handler = InputHandler {
|
||||
enabled: node.enabled.clone(),
|
||||
uid: node.uid.clone(),
|
||||
node: Arc::downgrade(node),
|
||||
spatial: node.get_aspect::<Spatial>().unwrap().clone(),
|
||||
@@ -39,28 +36,28 @@ impl InputHandler {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[instrument(level = "debug", skip(self, distance_link))]
|
||||
#[instrument(level = "debug", skip(self, input_link))]
|
||||
pub(super) fn send_input(
|
||||
&self,
|
||||
order: u32,
|
||||
captured: bool,
|
||||
distance_link: &DistanceLink,
|
||||
input_link: &InputLink,
|
||||
datamap: Datamap,
|
||||
) {
|
||||
let Some(node) = self.node.upgrade() else {
|
||||
return;
|
||||
};
|
||||
let Some(method_alias) = distance_link
|
||||
let Some(method_alias) = input_link
|
||||
.handler
|
||||
.method_aliases
|
||||
.get(&(Arc::as_ptr(&distance_link.method) as usize))
|
||||
.get(&(Arc::as_ptr(&input_link.method) as usize))
|
||||
else {
|
||||
return;
|
||||
};
|
||||
let _ = input_handler_client::input(
|
||||
&node,
|
||||
&method_alias,
|
||||
&distance_link.serialize(order, captured, datamap),
|
||||
&input_link.serialize(order, captured, datamap),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,9 +25,9 @@ pub struct InputMethod {
|
||||
pub data: Mutex<InputDataType>,
|
||||
pub datamap: Mutex<Datamap>,
|
||||
|
||||
pub(super) captures: Registry<InputHandler>,
|
||||
pub captures: Registry<InputHandler>,
|
||||
pub(super) handler_aliases: LifeLinkedNodeMap<String>,
|
||||
pub(super) handler_order: Mutex<Option<Vec<Weak<InputHandler>>>>,
|
||||
pub(super) handler_order: Mutex<Vec<Weak<InputHandler>>>,
|
||||
}
|
||||
impl InputMethod {
|
||||
pub fn add_to(
|
||||
@@ -44,7 +44,7 @@ impl InputMethod {
|
||||
captures: Registry::new(),
|
||||
datamap: Mutex::new(datamap),
|
||||
handler_aliases: LifeLinkedNodeMap::default(),
|
||||
handler_order: Mutex::new(None),
|
||||
handler_order: Mutex::new(Vec::new()),
|
||||
};
|
||||
for handler in INPUT_HANDLER_REGISTRY.get_valid_contents() {
|
||||
method.handle_new_handler(&handler);
|
||||
@@ -84,16 +84,12 @@ impl InputMethod {
|
||||
.add(self as *const InputMethod as usize, &method_alias);
|
||||
}
|
||||
|
||||
pub fn compare_distance(&self, to: &InputHandler) -> f32 {
|
||||
let distance = self.data.lock().compare_distance(&self.spatial, &to.field);
|
||||
if self.captures.contains(to) {
|
||||
distance * 0.5
|
||||
} else {
|
||||
distance
|
||||
}
|
||||
pub fn distance(&self, to: &Field) -> f32 {
|
||||
self.data.lock().distance(&self.spatial, to)
|
||||
}
|
||||
pub fn true_distance(&self, to: &Field) -> f32 {
|
||||
self.data.lock().true_distance(&self.spatial, to)
|
||||
|
||||
pub fn set_handler_order<'a>(&self, handlers: impl Iterator<Item = &'a Arc<InputHandler>>) {
|
||||
*self.handler_order.lock() = handlers.map(Arc::downgrade).collect();
|
||||
}
|
||||
|
||||
pub(super) fn handle_new_handler(&self, handler: &InputHandler) {
|
||||
@@ -137,7 +133,7 @@ impl InputMethod {
|
||||
.add(handler.uid.clone() + "-field", &rx_field_alias);
|
||||
}
|
||||
|
||||
let _ = input_method_client::new_handler(&method_node, &handler.uid, &handler_node);
|
||||
let _ = input_method_client::create_handler(&method_node, &handler.uid, &handler_node);
|
||||
}
|
||||
pub(super) fn handle_drop_handler(&self, handler: &InputHandler) {
|
||||
let uid = handler.uid.as_str();
|
||||
@@ -147,7 +143,7 @@ impl InputMethod {
|
||||
return;
|
||||
};
|
||||
|
||||
let _ = input_method_client::drop_handler(&tx_node, &uid);
|
||||
let _ = input_method_client::destroy_handler(&tx_node, &uid);
|
||||
}
|
||||
}
|
||||
impl Aspect for InputMethod {
|
||||
@@ -176,25 +172,25 @@ impl InputMethodAspect for InputMethod {
|
||||
fn set_handler_order(
|
||||
node: Arc<Node>,
|
||||
_calling_client: Arc<Client>,
|
||||
handlers: Option<Vec<Arc<Node>>>,
|
||||
handlers: Vec<Arc<Node>>,
|
||||
) -> Result<()> {
|
||||
let input_method = node.get_aspect::<InputMethod>()?;
|
||||
let Some(handlers) = handlers else {
|
||||
*input_method.handler_order.lock() = None;
|
||||
return Ok(());
|
||||
};
|
||||
let handlers = handlers
|
||||
.into_iter()
|
||||
.filter_map(|p| p.get_aspect::<InputHandler>().ok())
|
||||
.map(|i| Arc::downgrade(&i))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
*input_method.handler_order.lock() = Some(handlers);
|
||||
*input_method.handler_order.lock() = handlers;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[doc = "Have the input handler that this method reference came from capture the method for the next frame."]
|
||||
fn capture(node: Arc<Node>, _calling_client: Arc<Client>, handler: Arc<Node>) -> Result<()> {
|
||||
fn request_capture(
|
||||
node: Arc<Node>,
|
||||
_calling_client: Arc<Client>,
|
||||
handler: Arc<Node>,
|
||||
) -> Result<()> {
|
||||
let input_method = node.get_aspect::<InputMethod>()?;
|
||||
let input_handler = handler.get_aspect::<InputHandler>()?;
|
||||
|
||||
|
||||
@@ -21,22 +21,17 @@ use std::sync::{Arc, Weak};
|
||||
use tracing::{debug_span, instrument};
|
||||
|
||||
static INPUT_METHOD_REGISTRY: Registry<InputMethod> = Registry::new();
|
||||
static INPUT_HANDLER_REGISTRY: Registry<InputHandler> = Registry::new();
|
||||
pub static INPUT_HANDLER_REGISTRY: Registry<InputHandler> = Registry::new();
|
||||
|
||||
stardust_xr_server_codegen::codegen_input_protocol!();
|
||||
|
||||
pub struct DistanceLink {
|
||||
distance: f32,
|
||||
pub struct InputLink {
|
||||
method: Arc<InputMethod>,
|
||||
handler: Arc<InputHandler>,
|
||||
}
|
||||
impl DistanceLink {
|
||||
impl InputLink {
|
||||
fn from(method: Arc<InputMethod>, handler: Arc<InputHandler>) -> Self {
|
||||
DistanceLink {
|
||||
distance: method.compare_distance(&handler),
|
||||
method,
|
||||
handler,
|
||||
}
|
||||
InputLink { method, handler }
|
||||
}
|
||||
|
||||
fn send_input(&self, order: u32, captured: bool, datamap: Datamap) {
|
||||
@@ -53,7 +48,7 @@ impl DistanceLink {
|
||||
InputData {
|
||||
uid: self.method.uid.clone(),
|
||||
input,
|
||||
distance: self.method.true_distance(&self.handler.field),
|
||||
distance: self.method.distance(&self.handler.field),
|
||||
datamap,
|
||||
order,
|
||||
captured,
|
||||
@@ -61,32 +56,23 @@ impl DistanceLink {
|
||||
}
|
||||
}
|
||||
pub trait InputDataTrait {
|
||||
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32;
|
||||
fn true_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32;
|
||||
fn update_to(&mut self, distance_link: &DistanceLink, local_to_handler_matrix: Mat4);
|
||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32;
|
||||
fn update_to(&mut self, input_link: &InputLink, local_to_handler_matrix: Mat4);
|
||||
}
|
||||
impl InputDataTrait for InputDataType {
|
||||
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
match self {
|
||||
InputDataType::Pointer(i) => i.compare_distance(space, field),
|
||||
InputDataType::Hand(i) => i.compare_distance(space, field),
|
||||
InputDataType::Tip(i) => i.compare_distance(space, field),
|
||||
InputDataType::Pointer(i) => i.distance(space, field),
|
||||
InputDataType::Hand(i) => i.distance(space, field),
|
||||
InputDataType::Tip(i) => i.distance(space, field),
|
||||
}
|
||||
}
|
||||
|
||||
fn true_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
fn update_to(&mut self, input_link: &InputLink, local_to_handler_matrix: Mat4) {
|
||||
match self {
|
||||
InputDataType::Pointer(i) => i.true_distance(space, field),
|
||||
InputDataType::Hand(i) => i.true_distance(space, field),
|
||||
InputDataType::Tip(i) => i.true_distance(space, field),
|
||||
}
|
||||
}
|
||||
|
||||
fn update_to(&mut self, distance_link: &DistanceLink, local_to_handler_matrix: Mat4) {
|
||||
match self {
|
||||
InputDataType::Pointer(i) => i.update_to(distance_link, local_to_handler_matrix),
|
||||
InputDataType::Hand(i) => i.update_to(distance_link, local_to_handler_matrix),
|
||||
InputDataType::Tip(i) => i.update_to(distance_link, local_to_handler_matrix),
|
||||
InputDataType::Pointer(i) => i.update_to(input_link, local_to_handler_matrix),
|
||||
InputDataType::Hand(i) => i.update_to(input_link, local_to_handler_matrix),
|
||||
InputDataType::Tip(i) => i.update_to(input_link, local_to_handler_matrix),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -144,66 +130,42 @@ pub fn process_input() {
|
||||
.into_iter()
|
||||
.filter(|method| *method.enabled.lock())
|
||||
});
|
||||
let handlers = INPUT_HANDLER_REGISTRY.get_valid_contents();
|
||||
const LIMIT: usize = 50;
|
||||
// const LIMIT: usize = 50;
|
||||
for method in methods {
|
||||
for alias in method.node.upgrade().unwrap().aliases.get_valid_contents() {
|
||||
alias.enabled.store(false, Ordering::Release);
|
||||
}
|
||||
|
||||
debug_span!("Process input method").in_scope(|| {
|
||||
// Get all valid input handlers and convert them to DistanceLink objects
|
||||
let distance_links: Vec<DistanceLink> = debug_span!("Generate distance links")
|
||||
.in_scope(|| {
|
||||
if let Some(handler_order) = &*method.handler_order.lock() {
|
||||
handler_order
|
||||
.iter()
|
||||
.filter_map(Weak::upgrade)
|
||||
.filter(|handler| handler.enabled.load(Ordering::Relaxed))
|
||||
.map(|handler| DistanceLink::from(method.clone(), handler))
|
||||
.collect()
|
||||
} else {
|
||||
let mut distance_links: Vec<_> = handlers
|
||||
.iter()
|
||||
.filter(|handler| handler.enabled.load(Ordering::Relaxed))
|
||||
.map(|handler| {
|
||||
debug_span!("Create distance link").in_scope(|| {
|
||||
DistanceLink::from(method.clone(), handler.clone())
|
||||
})
|
||||
})
|
||||
.collect();
|
||||
// Get all valid input handlers and convert them to InputLink objects
|
||||
let input_links: Vec<InputLink> = debug_span!("Generate input links").in_scope(|| {
|
||||
method
|
||||
.handler_order
|
||||
.lock()
|
||||
.iter()
|
||||
.filter_map(Weak::upgrade)
|
||||
.filter(|handler| {
|
||||
let Some(node) = handler.node.upgrade() else {
|
||||
return false;
|
||||
};
|
||||
node.enabled()
|
||||
})
|
||||
.map(|handler| InputLink::from(method.clone(), handler))
|
||||
.collect()
|
||||
});
|
||||
|
||||
// Sort the distance links by their distance in ascending order
|
||||
debug_span!("Sort distance links").in_scope(|| {
|
||||
distance_links.sort_unstable_by(|a, b| {
|
||||
a.distance.abs().partial_cmp(&b.distance.abs()).unwrap()
|
||||
});
|
||||
});
|
||||
|
||||
distance_links.truncate(LIMIT);
|
||||
distance_links
|
||||
}
|
||||
});
|
||||
|
||||
let captures = method.captures.take_valid_contents();
|
||||
method.captures.clear();
|
||||
// Iterate over the distance links and send input to them
|
||||
for (i, distance_link) in distance_links.into_iter().enumerate() {
|
||||
if let Some(method_alias) = distance_link
|
||||
for (i, input_link) in input_links.into_iter().enumerate() {
|
||||
if let Some(method_alias) = input_link
|
||||
.handler
|
||||
.method_aliases
|
||||
.get(&(Arc::as_ptr(&distance_link.method) as usize))
|
||||
.get(&(Arc::as_ptr(&input_link.method) as usize))
|
||||
.and_then(|a| a.get_aspect::<Alias>().ok())
|
||||
{
|
||||
method_alias.enabled.store(true, Ordering::Release);
|
||||
}
|
||||
let captured = captures.contains(&distance_link.handler);
|
||||
distance_link.send_input(i as u32, captured, method.datamap.lock().clone());
|
||||
|
||||
// If the current distance link is in the list of captured input handlers,
|
||||
// break out of the loop to avoid sending input to the remaining distance links
|
||||
if captured {
|
||||
break;
|
||||
}
|
||||
input_link.send_input(i as u32, true, method.datamap.lock().clone());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{DistanceLink, InputDataTrait, Pointer};
|
||||
use super::{InputDataTrait, InputLink, Pointer};
|
||||
use crate::nodes::{
|
||||
fields::{Field, Ray, RayMarchResult},
|
||||
spatial::Spatial,
|
||||
@@ -29,27 +29,17 @@ impl Pointer {
|
||||
}
|
||||
}
|
||||
impl InputDataTrait for Pointer {
|
||||
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
let ray_info = self.ray_march(space, field);
|
||||
if ray_info.min_distance > 0.0 {
|
||||
ray_info.deepest_point_distance + 1000.0
|
||||
} else {
|
||||
ray_info
|
||||
.deepest_point_distance
|
||||
.hypot(0.001 / ray_info.min_distance)
|
||||
}
|
||||
}
|
||||
fn true_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
let ray_info = self.ray_march(space, field);
|
||||
ray_info.min_distance
|
||||
}
|
||||
fn update_to(&mut self, distance_link: &DistanceLink, mut local_to_handler_matrix: Mat4) {
|
||||
fn update_to(&mut self, input_link: &InputLink, mut local_to_handler_matrix: Mat4) {
|
||||
local_to_handler_matrix =
|
||||
Mat4::from_rotation_translation(self.orientation.into(), self.origin.into())
|
||||
* local_to_handler_matrix;
|
||||
let (_, orientation, origin) = local_to_handler_matrix.to_scale_rotation_translation();
|
||||
|
||||
let ray_march = self.ray_march(&distance_link.method.spatial, &distance_link.handler.field);
|
||||
let ray_march = self.ray_march(&input_link.method.spatial, &input_link.handler.field);
|
||||
let direction = local_to_handler_matrix
|
||||
.transform_vector3(vec3(0.0, 0.0, -1.0))
|
||||
.normalize();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use super::{DistanceLink, InputDataTrait, Tip};
|
||||
use super::{InputDataTrait, InputLink, Tip};
|
||||
use crate::nodes::{fields::Field, spatial::Spatial};
|
||||
use glam::{Mat4, Quat};
|
||||
use std::sync::Arc;
|
||||
@@ -12,13 +12,10 @@ impl Default for Tip {
|
||||
}
|
||||
}
|
||||
impl InputDataTrait for Tip {
|
||||
fn compare_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
field.distance(space, self.origin.into()).abs()
|
||||
}
|
||||
fn true_distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
fn distance(&self, space: &Arc<Spatial>, field: &Field) -> f32 {
|
||||
field.distance(space, self.origin.into())
|
||||
}
|
||||
fn update_to(&mut self, _distance_link: &DistanceLink, mut local_to_handler_matrix: Mat4) {
|
||||
fn update_to(&mut self, _input_link: &InputLink, mut local_to_handler_matrix: Mat4) {
|
||||
local_to_handler_matrix *=
|
||||
Mat4::from_rotation_translation(self.orientation.into(), self.origin.into());
|
||||
let (_, orientation, origin) = local_to_handler_matrix.to_scale_rotation_translation();
|
||||
|
||||
@@ -55,7 +55,7 @@ pub type Method = fn(Arc<Node>, Arc<Client>, Message, MethodResponseSender);
|
||||
stardust_xr_server_codegen::codegen_node_protocol!();
|
||||
|
||||
pub struct Node {
|
||||
pub enabled: Arc<AtomicBool>,
|
||||
enabled: Arc<AtomicBool>,
|
||||
pub(super) uid: String,
|
||||
path: String,
|
||||
client: Weak<Client>,
|
||||
@@ -113,6 +113,9 @@ impl Node {
|
||||
.scenegraph
|
||||
.add_node(self))
|
||||
}
|
||||
pub fn enabled(&self) -> bool {
|
||||
self.enabled.load(Ordering::Relaxed)
|
||||
}
|
||||
pub fn destroy(&self) {
|
||||
if let Some(client) = self.get_client() {
|
||||
client.scenegraph.remove_node(self.get_path());
|
||||
|
||||
@@ -74,7 +74,7 @@ impl Root {
|
||||
message: Message,
|
||||
) -> Result<()> {
|
||||
let prefixes: Vec<PathBuf> = deserialize(message.as_ref())?;
|
||||
info!(?client, ?prefixes, "Set base prefixes");
|
||||
info!(?calling_client, ?prefixes, "Set base prefixes");
|
||||
*calling_client.base_resource_prefixes.lock() = prefixes;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user