Skip to content

Commit

Permalink
fixed transform not being updated when changing it from inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
mrDIMAS committed Oct 28, 2024
1 parent cff3a9c commit a6e9037
Showing 1 changed file with 44 additions and 7 deletions.
51 changes: 44 additions & 7 deletions fyrox-impl/src/scene/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,39 @@ impl NodeMessage {
}
}

#[derive(Clone, Debug)]
struct TransformWrapper {
transform: Transform,
node_handle: Handle<Node>,
sender: Option<Sender<NodeMessage>>,
}

impl Visit for TransformWrapper {
fn visit(&mut self, name: &str, visitor: &mut Visitor) -> VisitResult {
self.transform.visit(name, visitor)
}
}

impl Deref for TransformWrapper {
type Target = Transform;

fn deref(&self) -> &Self::Target {
&self.transform
}
}

impl DerefMut for TransformWrapper {
fn deref_mut(&mut self) -> &mut Self::Target {
if let Some(sender) = self.sender.as_ref() {
Log::verify(sender.send(NodeMessage {
node: self.node_handle,
kind: NodeMessageKind::TransformChanged,
}))
}
&mut self.transform
}
}

/// Base scene graph node is a simplest possible node, it is used to build more complex ones using composition.
/// It contains all fundamental properties for each scene graph nodes, like local and global transforms, name,
/// lifetime, etc. Base node is a building block for all complex node hierarchies - it contains list of children
Expand Down Expand Up @@ -403,8 +436,8 @@ pub struct Base {
#[reflect(setter = "set_name_internal")]
pub(crate) name: ImmutableString,

#[reflect(setter = "set_local_transform")]
local_transform: Transform,
#[reflect(setter = "set_local_transform", deref)]
local_transform: TransformWrapper,

#[reflect(setter = "set_visibility")]
visibility: InheritableVariable<bool>,
Expand Down Expand Up @@ -539,8 +572,10 @@ impl Base {
script_message_sender: Sender<NodeScriptMessage>,
) {
self.self_handle = self_handle;
self.message_sender = Some(message_sender);
self.message_sender = Some(message_sender.clone());
self.script_message_sender = Some(script_message_sender);
self.local_transform.sender = Some(message_sender);
self.local_transform.node_handle = self_handle;
// Kick off initial hierarchical property propagation.
self.notify(self.self_handle, NodeMessageKind::TransformChanged);
self.notify(self.self_handle, NodeMessageKind::VisibilityChanged);
Expand All @@ -558,15 +593,13 @@ impl Base {
/// some local spatial properties, such as position, rotation, scale, etc.
#[inline]
pub fn local_transform_mut(&mut self) -> &mut Transform {
self.transform_modified.set(true);
self.notify(self.self_handle, NodeMessageKind::TransformChanged);
&mut self.local_transform
}

/// Sets new local transform of a node.
#[inline]
pub fn set_local_transform(&mut self, transform: Transform) {
self.local_transform = transform;
self.local_transform.transform = transform;
self.notify(self.self_handle, NodeMessageKind::TransformChanged);
}

Expand Down Expand Up @@ -1345,7 +1378,11 @@ impl BaseBuilder {
message_sender: None,
name: self.name.into(),
children: self.children,
local_transform: self.local_transform,
local_transform: TransformWrapper {
transform: self.local_transform,
node_handle: Default::default(),
sender: None,
},
lifetime: self.lifetime.into(),
visibility: self.visibility.into(),
global_visibility: Cell::new(true),
Expand Down

0 comments on commit a6e9037

Please sign in to comment.