From d825bb55d33ca8161904dcc8700734d88b2999a8 Mon Sep 17 00:00:00 2001 From: Syntriax Date: Wed, 26 Mar 2025 14:19:47 +0300 Subject: [PATCH] refactor!: Transform events now send previous values of their changed fields --- Engine.Core/Abstract/ITransform.cs | 42 ++++++++++++++++++-- Engine.Core/Transform.cs | 44 ++++++++++++++------- Engine.Physics2D/Collider2DBehaviourBase.cs | 16 ++++---- 3 files changed, 78 insertions(+), 24 deletions(-) diff --git a/Engine.Core/Abstract/ITransform.cs b/Engine.Core/Abstract/ITransform.cs index cd5e66f..a170f39 100644 --- a/Engine.Core/Abstract/ITransform.cs +++ b/Engine.Core/Abstract/ITransform.cs @@ -95,10 +95,46 @@ public interface ITransform : IAssignableGameObject, IEnumerable /// The child to remove. void RemoveChild(ITransform transform); - delegate void OnPositionChangedEventHandler(ITransform sender); - delegate void OnScaleChangedEventHandler(ITransform sender); - delegate void OnRotationChangedEventHandler(ITransform sender); + /// + /// Delegate for the event triggered when the 's rotation changes. + /// + /// The that the parent has changed. + /// The previous of the . + delegate void OnPositionChangedEventHandler(ITransform sender, Vector2D previousPosition); + + /// + /// Delegate for the event triggered when the 's rotation changes. + /// + /// The that the parent has changed. + /// The previous of the . + delegate void OnScaleChangedEventHandler(ITransform sender, Vector2D previousScale); + + /// + /// Delegate for the event triggered when the 's rotation changes. + /// + /// The that the parent has changed. + /// The previous of the . + delegate void OnRotationChangedEventHandler(ITransform sender, float previousRotation); + + /// + /// Delegate for the event triggered when the 's parent changes. + /// + /// The that the parent has changed. + /// The previous the sender was a child of. + /// The new and current the sender is a child of. delegate void OnParentChangedEventHandler(ITransform sender, ITransform? previousParent, ITransform? newParent); + + /// + /// Delegate for the event triggered when a new added as a child. + /// + /// The parent this event is being called from. + /// The that got removed as a children of the sender . delegate void OnChildrenAddedEventHandler(ITransform sender, ITransform childrenAdded); + + /// + /// Delegate for the event triggered when a new removed from being a child. + /// + /// The parent this event is being called from. + /// The that got removed as a children of the sender . delegate void OnChildrenRemovedEventHandler(ITransform sender, ITransform childrenRemoved); } diff --git a/Engine.Core/Transform.cs b/Engine.Core/Transform.cs index 3451e70..cddbfc5 100644 --- a/Engine.Core/Transform.cs +++ b/Engine.Core/Transform.cs @@ -43,9 +43,11 @@ public class Transform : ITransform if (value == _position) return; + Vector2D previousPosition = _position; _position = value; + UpdateLocalPosition(); - OnPositionChanged?.Invoke(this); + OnPositionChanged?.Invoke(this, _position); } } @@ -57,9 +59,11 @@ public class Transform : ITransform if (value == _scale) return; + Vector2D previousScale = _scale; _scale = value; + UpdateLocalScale(); - OnScaleChanged?.Invoke(this); + OnScaleChanged?.Invoke(this, previousScale); } } @@ -71,9 +75,12 @@ public class Transform : ITransform if (value == _rotation) return; + + float previousRotation = _rotation; _rotation = value; + UpdateLocalPosition(); - OnRotationChanged?.Invoke(this); + OnRotationChanged?.Invoke(this, previousRotation); } } @@ -85,9 +92,11 @@ public class Transform : ITransform if (value == _localPosition) return; + Vector2D previousPosition = _position; _localPosition = value; + UpdatePosition(); - OnPositionChanged?.Invoke(this); + OnPositionChanged?.Invoke(this, previousPosition); } } @@ -99,9 +108,11 @@ public class Transform : ITransform if (value == _localScale) return; + Vector2D previousScale = _scale; _localScale = value; + UpdateScale(); - OnScaleChanged?.Invoke(this); + OnScaleChanged?.Invoke(this, previousScale); } } @@ -113,9 +124,11 @@ public class Transform : ITransform if (value == _localRotation) return; + float previousRotation = _rotation; _localRotation = value; + UpdateRotation(); - OnRotationChanged?.Invoke(this); + OnRotationChanged?.Invoke(this, previousRotation); } } @@ -182,33 +195,36 @@ public class Transform : ITransform child.SetParent(this); } - private void RecalculatePosition(ITransform _) + private void RecalculatePosition(ITransform _, Vector2D previousPosition) { if (Parent is null) return; UpdatePosition(); - OnPositionChanged?.Invoke(this); + + OnPositionChanged?.Invoke(this, previousPosition); } - private void RecalculateScale(ITransform _) + private void RecalculateScale(ITransform _, Vector2D previousScale) { if (Parent is null) return; UpdateScale(); - OnScaleChanged?.Invoke(this); + + OnScaleChanged?.Invoke(this, previousScale); } - private void RecalculateRotation(ITransform _) + private void RecalculateRotation(ITransform _, float previousRotation) { if (Parent is null) return; - UpdatePosition(); + // UpdatePosition(); UpdateRotation(); - OnPositionChanged?.Invoke(this); - OnRotationChanged?.Invoke(this); + + // OnPositionChanged?.Invoke(this, previousPosition); + OnRotationChanged?.Invoke(this, previousRotation); } private void UpdateLocalPosition() diff --git a/Engine.Physics2D/Collider2DBehaviourBase.cs b/Engine.Physics2D/Collider2DBehaviourBase.cs index 5688655..2a06b4a 100644 --- a/Engine.Physics2D/Collider2DBehaviourBase.cs +++ b/Engine.Physics2D/Collider2DBehaviourBase.cs @@ -39,9 +39,9 @@ public abstract class Collider2DBehaviourBase : Behaviour, ICollider2D BehaviourController.OnBehaviourAdded += OnBehaviourAddedToController; BehaviourController.OnBehaviourRemoved += OnBehaviourRemovedFromController; - Transform.OnPositionChanged += SetNeedsRecalculation; - Transform.OnRotationChanged += SetNeedsRecalculation; - Transform.OnScaleChanged += SetNeedsRecalculation; + Transform.OnPositionChanged += SetNeedsRecalculationFromPosition; + Transform.OnRotationChanged += SetNeedsRecalculationFromRotation; + Transform.OnScaleChanged += SetNeedsRecalculationFromScale; Transform.OnParentChanged += UpdateRigidBody2D; } @@ -62,16 +62,18 @@ public abstract class Collider2DBehaviourBase : Behaviour, ICollider2D _rigidBody2D = null; } - private void SetNeedsRecalculation(ITransform transform) => NeedsRecalculation = true; + private void SetNeedsRecalculationFromScale(ITransform sender, Vector2D previousScale) => NeedsRecalculation = true; + private void SetNeedsRecalculationFromPosition(ITransform sender, Vector2D previousPosition) => NeedsRecalculation = true; + private void SetNeedsRecalculationFromRotation(ITransform sender, float previousRotation) => NeedsRecalculation = true; protected override void OnFinalize() { BehaviourController.OnBehaviourAdded -= OnBehaviourAddedToController; BehaviourController.OnBehaviourRemoved -= OnBehaviourRemovedFromController; - Transform.OnScaleChanged -= SetNeedsRecalculation; + Transform.OnScaleChanged -= SetNeedsRecalculationFromScale; - Transform.OnPositionChanged -= SetNeedsRecalculation; - Transform.OnRotationChanged -= SetNeedsRecalculation; + Transform.OnPositionChanged -= SetNeedsRecalculationFromPosition; + Transform.OnRotationChanged -= SetNeedsRecalculationFromRotation; } public void Detect(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionDetected?.Invoke(this, collisionDetectionInformation);