refactor(spatial): get/set parent methods
This commit is contained in:
@@ -81,7 +81,7 @@ impl Spatial {
|
|||||||
*self.transform.lock()
|
*self.transform.lock()
|
||||||
}
|
}
|
||||||
pub fn global_transform(&self) -> Mat4 {
|
pub fn global_transform(&self) -> Mat4 {
|
||||||
match self.parent.lock().clone() {
|
match self.get_parent() {
|
||||||
Some(value) => value.global_transform() * *self.transform.lock(),
|
Some(value) => value.global_transform() * *self.transform.lock(),
|
||||||
None => *self.transform.lock(),
|
None => *self.transform.lock(),
|
||||||
}
|
}
|
||||||
@@ -109,10 +109,7 @@ impl Spatial {
|
|||||||
}
|
}
|
||||||
let reference_to_parent_transform = reference_space
|
let reference_to_parent_transform = reference_space
|
||||||
.map(|reference_space| {
|
.map(|reference_space| {
|
||||||
Spatial::space_to_space_matrix(
|
Spatial::space_to_space_matrix(Some(reference_space), self.get_parent().as_deref())
|
||||||
Some(reference_space),
|
|
||||||
self.parent.lock().clone().as_deref(),
|
|
||||||
)
|
|
||||||
})
|
})
|
||||||
.unwrap_or(Mat4::IDENTITY);
|
.unwrap_or(Mat4::IDENTITY);
|
||||||
let mut local_transform_in_reference_space =
|
let mut local_transform_in_reference_space =
|
||||||
@@ -150,8 +147,7 @@ impl Spatial {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
let current_ancestor_parent = current_ancestor.parent.lock().clone();
|
if let Some(parent) = current_ancestor.get_parent() {
|
||||||
if let Some(parent) = current_ancestor_parent {
|
|
||||||
current_ancestor = parent;
|
current_ancestor = parent;
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
@@ -159,23 +155,26 @@ impl Spatial {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_parent(&self, new_parent: Option<&Arc<Spatial>>) {
|
fn get_parent(&self) -> Option<Arc<Spatial>> {
|
||||||
let mut parent = self.parent.lock();
|
self.parent.lock().clone()
|
||||||
if let Some(parent) = &*parent {
|
}
|
||||||
|
fn set_parent(&self, new_parent: Option<Arc<Spatial>>) {
|
||||||
|
if let Some(parent) = self.get_parent() {
|
||||||
parent.children.remove(self);
|
parent.children.remove(self);
|
||||||
}
|
}
|
||||||
if let Some(new_parent) = new_parent {
|
if let Some(new_parent) = &new_parent {
|
||||||
new_parent
|
new_parent
|
||||||
.children
|
.children
|
||||||
.add_raw(&self.self_ref.upgrade().unwrap());
|
.add_raw(&self.self_ref.upgrade().unwrap());
|
||||||
}
|
}
|
||||||
|
|
||||||
*parent = new_parent.cloned();
|
*self.parent.lock() = new_parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(level = "debug", skip_all)]
|
#[instrument(level = "debug", skip_all)]
|
||||||
pub fn set_spatial_parent(&self, parent: Option<&Arc<Spatial>>) -> Result<()> {
|
pub fn set_spatial_parent(&self, parent: Option<Arc<Spatial>>) -> Result<()> {
|
||||||
let is_ancestor = parent
|
let is_ancestor = parent
|
||||||
|
.as_ref()
|
||||||
.map(|parent| self.is_ancestor_of(parent.clone()))
|
.map(|parent| self.is_ancestor_of(parent.clone()))
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
if is_ancestor {
|
if is_ancestor {
|
||||||
@@ -187,8 +186,9 @@ impl Spatial {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[instrument(level = "debug", skip_all)]
|
#[instrument(level = "debug", skip_all)]
|
||||||
pub fn set_spatial_parent_in_place(&self, parent: Option<&Arc<Spatial>>) -> Result<()> {
|
pub fn set_spatial_parent_in_place(&self, parent: Option<Arc<Spatial>>) -> Result<()> {
|
||||||
let is_ancestor = parent
|
let is_ancestor = parent
|
||||||
|
.as_ref()
|
||||||
.map(|parent| self.is_ancestor_of(parent.clone()))
|
.map(|parent| self.is_ancestor_of(parent.clone()))
|
||||||
.unwrap_or(false);
|
.unwrap_or(false);
|
||||||
if is_ancestor {
|
if is_ancestor {
|
||||||
@@ -197,7 +197,7 @@ impl Spatial {
|
|||||||
|
|
||||||
self.set_local_transform(Spatial::space_to_space_matrix(
|
self.set_local_transform(Spatial::space_to_space_matrix(
|
||||||
Some(self),
|
Some(self),
|
||||||
parent.cloned().as_deref(),
|
parent.as_deref(),
|
||||||
));
|
));
|
||||||
self.set_parent(parent);
|
self.set_parent(parent);
|
||||||
|
|
||||||
@@ -252,10 +252,7 @@ impl Spatial {
|
|||||||
data: &[u8],
|
data: &[u8],
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let parent = find_spatial_parent(&calling_client, deserialize(data)?)?;
|
let parent = find_spatial_parent(&calling_client, deserialize(data)?)?;
|
||||||
node.spatial
|
node.spatial.get().unwrap().set_spatial_parent(Some(parent))
|
||||||
.get()
|
|
||||||
.unwrap()
|
|
||||||
.set_spatial_parent(Some(&parent))
|
|
||||||
}
|
}
|
||||||
pub fn set_spatial_parent_in_place_flex(
|
pub fn set_spatial_parent_in_place_flex(
|
||||||
node: &Node,
|
node: &Node,
|
||||||
@@ -266,7 +263,7 @@ impl Spatial {
|
|||||||
node.spatial
|
node.spatial
|
||||||
.get()
|
.get()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.set_spatial_parent_in_place(Some(&parent))?;
|
.set_spatial_parent_in_place(Some(parent))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn set_zoneable(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
|
pub fn set_zoneable(node: &Node, _calling_client: Arc<Client>, data: &[u8]) -> Result<()> {
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ pub fn capture(spatial: &Arc<Spatial>, zone: &Arc<Zone>) {
|
|||||||
.unwrap_or(f32::MAX);
|
.unwrap_or(f32::MAX);
|
||||||
if new_distance.abs() < old_distance.abs() {
|
if new_distance.abs() < old_distance.abs() {
|
||||||
release(spatial);
|
release(spatial);
|
||||||
*spatial.old_parent.lock() = spatial.parent.lock().clone();
|
*spatial.old_parent.lock() = spatial.get_parent();
|
||||||
*spatial.zone.lock() = Arc::downgrade(zone);
|
*spatial.zone.lock() = Arc::downgrade(zone);
|
||||||
zone.captured.add_raw(spatial);
|
zone.captured.add_raw(spatial);
|
||||||
let node = zone.spatial.node.upgrade().unwrap();
|
let node = zone.spatial.node.upgrade().unwrap();
|
||||||
@@ -36,7 +36,7 @@ pub fn capture(spatial: &Arc<Spatial>, zone: &Arc<Zone>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn release(spatial: &Spatial) {
|
pub fn release(spatial: &Spatial) {
|
||||||
let _ = spatial.set_spatial_parent_in_place(spatial.old_parent.lock().take().as_ref());
|
let _ = spatial.set_spatial_parent_in_place(spatial.old_parent.lock().take());
|
||||||
let mut spatial_zone = spatial.zone.lock();
|
let mut spatial_zone = spatial.zone.lock();
|
||||||
if let Some(spatial_zone) = spatial_zone.upgrade() {
|
if let Some(spatial_zone) = spatial_zone.upgrade() {
|
||||||
let node = spatial_zone.spatial.node.upgrade().unwrap();
|
let node = spatial_zone.spatial.node.upgrade().unwrap();
|
||||||
|
|||||||
Reference in New Issue
Block a user