refactor(input): switch to manual handler order
This commit is contained in:
@@ -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(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user