cargo fmt #23
@@ -13,9 +13,7 @@ impl<T: Send + Sync + ?Sized> Registry<T> {
|
|||||||
Registry(const_mutex(None))
|
Registry(const_mutex(None))
|
||||||
}
|
}
|
||||||
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Weak<T>>> {
|
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Weak<T>>> {
|
||||||
MutexGuard::map(self.0.lock(), |r| {
|
MutexGuard::map(self.0.lock(), |r| r.get_or_insert_with(FxHashMap::default))
|
||||||
r.get_or_insert_with(FxHashMap::default)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
pub fn add(&self, t: T) -> Arc<T>
|
pub fn add(&self, t: T) -> Arc<T>
|
||||||
where
|
where
|
||||||
@@ -118,9 +116,7 @@ impl<T: Send + Sync + ?Sized> OwnedRegistry<T> {
|
|||||||
OwnedRegistry(const_mutex(None))
|
OwnedRegistry(const_mutex(None))
|
||||||
}
|
}
|
||||||
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Arc<T>>> {
|
fn lock(&self) -> MappedMutexGuard<FxHashMap<usize, Arc<T>>> {
|
||||||
MutexGuard::map(self.0.lock(), |r| {
|
MutexGuard::map(self.0.lock(), |r| r.get_or_insert_with(FxHashMap::default))
|
||||||
r.get_or_insert_with(FxHashMap::default)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
pub fn add(&self, t: T) -> Arc<T>
|
pub fn add(&self, t: T) -> Arc<T>
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ pub mod sk_controller;
|
|||||||
pub mod sk_hand;
|
pub mod sk_hand;
|
||||||
|
|
||||||
use crate::nodes::{
|
use crate::nodes::{
|
||||||
fields::{Field, FieldTrait, Ray}, input::{InputDataTrait, InputDataType, InputHandler, InputMethod, INPUT_HANDLER_REGISTRY}, spatial::Spatial
|
fields::{Field, FieldTrait, Ray},
|
||||||
|
input::{InputDataTrait, InputDataType, InputHandler, InputMethod, INPUT_HANDLER_REGISTRY},
|
||||||
|
spatial::Spatial,
|
||||||
};
|
};
|
||||||
use glam::vec3;
|
use glam::vec3;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct CaptureManager {
|
pub struct CaptureManager {
|
||||||
pub capture: Option<Arc<InputHandler>>,
|
pub capture: Option<Arc<InputHandler>>,
|
||||||
@@ -17,12 +18,20 @@ pub struct CaptureManager {
|
|||||||
impl CaptureManager {
|
impl CaptureManager {
|
||||||
pub fn update_capture(&mut self, pointer: &InputMethod) {
|
pub fn update_capture(&mut self, pointer: &InputMethod) {
|
||||||
if let Some(capture) = &self.capture {
|
if let Some(capture) = &self.capture {
|
||||||
if !pointer.internal_capture_requests.get_valid_contents().contains(capture) {
|
if !pointer
|
||||||
|
.internal_capture_requests
|
||||||
|
.get_valid_contents()
|
||||||
|
.contains(capture)
|
||||||
|
{
|
||||||
self.capture.take();
|
self.capture.take();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn set_new_capture(&mut self, pointer: &InputMethod, distance_calculator: DistanceCalculator) {
|
pub fn set_new_capture(
|
||||||
|
&mut self,
|
||||||
|
pointer: &InputMethod,
|
||||||
|
distance_calculator: DistanceCalculator,
|
||||||
|
) {
|
||||||
if self.capture.is_none() {
|
if self.capture.is_none() {
|
||||||
self.capture = find_closest_capture(pointer, distance_calculator);
|
self.capture = find_closest_capture(pointer, distance_calculator);
|
||||||
}
|
}
|
||||||
@@ -36,25 +45,39 @@ impl CaptureManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
type DistanceCalculator = fn(&Arc<Spatial>, &InputDataType, &Field) -> Option<f32>;
|
type DistanceCalculator = fn(&Arc<Spatial>, &InputDataType, &Field) -> Option<f32>;
|
||||||
|
|
||||||
pub fn find_closest_capture(method: &InputMethod, distance_calculator: DistanceCalculator) -> Option<Arc<InputHandler>> {
|
pub fn find_closest_capture(
|
||||||
|
method: &InputMethod,
|
||||||
|
distance_calculator: DistanceCalculator,
|
||||||
|
) -> Option<Arc<InputHandler>> {
|
||||||
method
|
method
|
||||||
.internal_capture_requests
|
.internal_capture_requests
|
||||||
.get_valid_contents()
|
.get_valid_contents()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|h| distance_calculator(&method.spatial, &method.data.lock(), &h.field).map(|dist| (h.clone(), dist)))
|
.filter_map(|h| {
|
||||||
|
distance_calculator(&method.spatial, &method.data.lock(), &h.field)
|
||||||
|
.map(|dist| (h.clone(), dist))
|
||||||
|
})
|
||||||
.min_by(|(_, dist_a), (_, dist_b)| dist_a.partial_cmp(dist_b).unwrap())
|
.min_by(|(_, dist_a), (_, dist_b)| dist_a.partial_cmp(dist_b).unwrap())
|
||||||
.map(|(handler, _)| handler)
|
.map(|(handler, _)| handler)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_sorted_handlers(method: &InputMethod, distance_calculator: DistanceCalculator) -> Vec<Arc<InputHandler>> {
|
pub fn get_sorted_handlers(
|
||||||
|
method: &InputMethod,
|
||||||
|
distance_calculator: DistanceCalculator,
|
||||||
|
) -> Vec<Arc<InputHandler>> {
|
||||||
INPUT_HANDLER_REGISTRY
|
INPUT_HANDLER_REGISTRY
|
||||||
.get_valid_contents()
|
.get_valid_contents()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|handler| handler.spatial.node().map_or(false, |node| node.enabled()))
|
.filter(|handler| handler.spatial.node().map_or(false, |node| node.enabled()))
|
||||||
.filter(|handler| handler.field.spatial.node().map_or(false, |node| node.enabled()))
|
.filter(|handler| {
|
||||||
|
handler
|
||||||
|
.field
|
||||||
|
.spatial
|
||||||
|
.node()
|
||||||
|
.map_or(false, |node| node.enabled())
|
||||||
|
})
|
||||||
.filter_map(|handler| {
|
.filter_map(|handler| {
|
||||||
distance_calculator(&method.spatial, &method.data.lock(), &handler.field)
|
distance_calculator(&method.spatial, &method.data.lock(), &handler.field)
|
||||||
.map(|distance| (vec![handler], distance))
|
.map(|distance| (vec![handler], distance))
|
||||||
|
|||||||
Reference in New Issue
Block a user