From 04d325f38bd5bb9d6b23eb642da2ab8c191ef18e Mon Sep 17 00:00:00 2001 From: Syntriax Date: Sun, 6 Apr 2025 11:21:59 +0300 Subject: [PATCH] fix: Transform2D fields & parental transform references not working properly fixed --- Engine.Core/Transform2D.cs | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/Engine.Core/Transform2D.cs b/Engine.Core/Transform2D.cs index 05a13d4..176efd3 100644 --- a/Engine.Core/Transform2D.cs +++ b/Engine.Core/Transform2D.cs @@ -92,10 +92,13 @@ public class Transform2D : Behaviour, ITransform2D return; Vector2D previousScale = _scale; + Vector2D previousPosition = _position; _localScale = value; UpdateScale(); + UpdatePosition(); OnScaleChanged?.Invoke(this, previousScale); + OnPositionChanged?.Invoke(this, previousPosition); } } @@ -120,13 +123,9 @@ public class Transform2D : Behaviour, ITransform2D if (parentTransform is null) return; - float previousRotation = Rotation; - UpdatePosition(); - UpdateRotation(); OnPositionChanged?.Invoke(this, previousPosition); - OnRotationChanged?.Invoke(this, previousRotation); } private void RecalculateScale(ITransform2D _, Vector2D previousScale) @@ -134,9 +133,13 @@ public class Transform2D : Behaviour, ITransform2D if (parentTransform is null) return; + Vector2D previousPosition = _position; + UpdateScale(); + UpdatePosition(); OnScaleChanged?.Invoke(this, previousScale); + OnPositionChanged?.Invoke(this, previousPosition); } private void RecalculateRotation(ITransform2D _, float previousRotation) @@ -146,11 +149,11 @@ public class Transform2D : Behaviour, ITransform2D Vector2D previousPosition = Position; - UpdatePosition(); UpdateRotation(); + UpdatePosition(); - OnPositionChanged?.Invoke(this, previousPosition); OnRotationChanged?.Invoke(this, previousRotation); + OnPositionChanged?.Invoke(this, previousPosition); } private void UpdateLocalPosition() @@ -203,16 +206,16 @@ public class Transform2D : Behaviour, ITransform2D protected override void InitializeInternal() { - UpdateParent(BehaviourController.HierarchyObject.Parent); - BehaviourController.HierarchyObject.OnParentChanged += OnParentChanged; + UpdateReferences(HierarchyObject.Parent); + HierarchyObject.OnParentChanged += OnParentChanged; } protected override void FinalizeInternal() { - BehaviourController.HierarchyObject.OnParentChanged -= OnParentChanged; + HierarchyObject.OnParentChanged -= OnParentChanged; } - private void UpdateParent(IHierarchyObject? parent) + private void UpdateReferences(IHierarchyObject? parent) { ITransform2D? previousParent = parentTransform; if (previousParent is not null) @@ -222,6 +225,7 @@ public class Transform2D : Behaviour, ITransform2D previousParent.OnScaleChanged -= RecalculateScale; previousParent.OnRotationChanged -= RecalculateRotation; previousParent.BehaviourController.HierarchyObject.OnParentChanged -= OnParentChanged; + previousParent.BehaviourController.OnBehaviourAdded -= LookForTransform2D; } parentTransform = parent?.BehaviourController.GetBehaviour(); @@ -238,6 +242,8 @@ public class Transform2D : Behaviour, ITransform2D UpdateScale(); UpdateRotation(); } + else if (HierarchyObject.Parent is not null) + HierarchyObject.Parent.BehaviourController.OnBehaviourAdded += LookForTransform2D; UpdateLocalPosition(); UpdateLocalScale(); @@ -248,8 +254,16 @@ public class Transform2D : Behaviour, ITransform2D OnRotationChanged?.Invoke(this, Rotation); } + private void LookForTransform2D(IBehaviourController sender, IBehaviour behaviourAdded) + { + if (behaviourAdded is not ITransform2D transform2D) + return; + + UpdateReferences(HierarchyObject.Parent); + } + private void OnParentChanged(IHierarchyObject sender, IHierarchyObject? previousParent, IHierarchyObject? newParent) { - UpdateParent(newParent); + UpdateReferences(newParent); } }