refactor!: Transform events now send previous values of their changed fields

This commit is contained in:
Syntriax 2025-03-26 14:19:47 +03:00
parent 5fc8c012b3
commit d825bb55d3
3 changed files with 78 additions and 24 deletions

View File

@ -95,10 +95,46 @@ public interface ITransform : IAssignableGameObject, IEnumerable<ITransform>
/// <param name="transform">The child <see cref="ITransform"/> to remove.</param> /// <param name="transform">The child <see cref="ITransform"/> to remove.</param>
void RemoveChild(ITransform transform); void RemoveChild(ITransform transform);
delegate void OnPositionChangedEventHandler(ITransform sender); /// <summary>
delegate void OnScaleChangedEventHandler(ITransform sender); /// Delegate for the event triggered when the <see cref="ITransform"/>'s rotation changes.
delegate void OnRotationChangedEventHandler(ITransform sender); /// </summary>
/// <param name="sender">The <see cref="ITransform"/> that the parent has changed.</param>
/// <param name="previousPosition">The previous <see cref="Position"/> of the <see cref="ITransform"/>.</param>
delegate void OnPositionChangedEventHandler(ITransform sender, Vector2D previousPosition);
/// <summary>
/// Delegate for the event triggered when the <see cref="ITransform"/>'s rotation changes.
/// </summary>
/// <param name="sender">The <see cref="ITransform"/> that the parent has changed.</param>
/// <param name="previousScale">The previous <see cref="Scale"/> of the <see cref="ITransform"/>.</param>
delegate void OnScaleChangedEventHandler(ITransform sender, Vector2D previousScale);
/// <summary>
/// Delegate for the event triggered when the <see cref="ITransform"/>'s rotation changes.
/// </summary>
/// <param name="sender">The <see cref="ITransform"/> that the parent has changed.</param>
/// <param name="previousRotation">The previous <see cref="Rotation"/> of the <see cref="ITransform"/>.</param>
delegate void OnRotationChangedEventHandler(ITransform sender, float previousRotation);
/// <summary>
/// Delegate for the event triggered when the <see cref="ITransform"/>'s parent changes.
/// </summary>
/// <param name="sender">The <see cref="ITransform"/> that the parent has changed.</param>
/// <param name="previousParent">The previous <see cref="ITransform"/> the sender was a child of.</param>
/// <param name="newParent">The new and current <see cref="ITransform"/> the sender is a child of.</param>
delegate void OnParentChangedEventHandler(ITransform sender, ITransform? previousParent, ITransform? newParent); delegate void OnParentChangedEventHandler(ITransform sender, ITransform? previousParent, ITransform? newParent);
/// <summary>
/// Delegate for the event triggered when a new <see cref="ITransform"/> added as a child.
/// </summary>
/// <param name="sender">The parent <see cref="ITransform"/> this event is being called from.</param>
/// <param name="childrenAdded">The <see cref="ITransform"/> that got removed as a children of the sender <see cref="ITransform"/>.</param>
delegate void OnChildrenAddedEventHandler(ITransform sender, ITransform childrenAdded); delegate void OnChildrenAddedEventHandler(ITransform sender, ITransform childrenAdded);
/// <summary>
/// Delegate for the event triggered when a new <see cref="ITransform"/> removed from being a child.
/// </summary>
/// <param name="sender">The parent <see cref="ITransform"/> this event is being called from.</param>
/// <param name="childrenAdded">The <see cref="ITransform"/> that got removed as a children of the sender <see cref="ITransform"/>.</param>
delegate void OnChildrenRemovedEventHandler(ITransform sender, ITransform childrenRemoved); delegate void OnChildrenRemovedEventHandler(ITransform sender, ITransform childrenRemoved);
} }

View File

@ -43,9 +43,11 @@ public class Transform : ITransform
if (value == _position) if (value == _position)
return; return;
Vector2D previousPosition = _position;
_position = value; _position = value;
UpdateLocalPosition(); UpdateLocalPosition();
OnPositionChanged?.Invoke(this); OnPositionChanged?.Invoke(this, _position);
} }
} }
@ -57,9 +59,11 @@ public class Transform : ITransform
if (value == _scale) if (value == _scale)
return; return;
Vector2D previousScale = _scale;
_scale = value; _scale = value;
UpdateLocalScale(); UpdateLocalScale();
OnScaleChanged?.Invoke(this); OnScaleChanged?.Invoke(this, previousScale);
} }
} }
@ -71,9 +75,12 @@ public class Transform : ITransform
if (value == _rotation) if (value == _rotation)
return; return;
float previousRotation = _rotation;
_rotation = value; _rotation = value;
UpdateLocalPosition(); UpdateLocalPosition();
OnRotationChanged?.Invoke(this); OnRotationChanged?.Invoke(this, previousRotation);
} }
} }
@ -85,9 +92,11 @@ public class Transform : ITransform
if (value == _localPosition) if (value == _localPosition)
return; return;
Vector2D previousPosition = _position;
_localPosition = value; _localPosition = value;
UpdatePosition(); UpdatePosition();
OnPositionChanged?.Invoke(this); OnPositionChanged?.Invoke(this, previousPosition);
} }
} }
@ -99,9 +108,11 @@ public class Transform : ITransform
if (value == _localScale) if (value == _localScale)
return; return;
Vector2D previousScale = _scale;
_localScale = value; _localScale = value;
UpdateScale(); UpdateScale();
OnScaleChanged?.Invoke(this); OnScaleChanged?.Invoke(this, previousScale);
} }
} }
@ -113,9 +124,11 @@ public class Transform : ITransform
if (value == _localRotation) if (value == _localRotation)
return; return;
float previousRotation = _rotation;
_localRotation = value; _localRotation = value;
UpdateRotation(); UpdateRotation();
OnRotationChanged?.Invoke(this); OnRotationChanged?.Invoke(this, previousRotation);
} }
} }
@ -182,33 +195,36 @@ public class Transform : ITransform
child.SetParent(this); child.SetParent(this);
} }
private void RecalculatePosition(ITransform _) private void RecalculatePosition(ITransform _, Vector2D previousPosition)
{ {
if (Parent is null) if (Parent is null)
return; return;
UpdatePosition(); UpdatePosition();
OnPositionChanged?.Invoke(this);
OnPositionChanged?.Invoke(this, previousPosition);
} }
private void RecalculateScale(ITransform _) private void RecalculateScale(ITransform _, Vector2D previousScale)
{ {
if (Parent is null) if (Parent is null)
return; return;
UpdateScale(); UpdateScale();
OnScaleChanged?.Invoke(this);
OnScaleChanged?.Invoke(this, previousScale);
} }
private void RecalculateRotation(ITransform _) private void RecalculateRotation(ITransform _, float previousRotation)
{ {
if (Parent is null) if (Parent is null)
return; return;
UpdatePosition(); // UpdatePosition();
UpdateRotation(); UpdateRotation();
OnPositionChanged?.Invoke(this);
OnRotationChanged?.Invoke(this); // OnPositionChanged?.Invoke(this, previousPosition);
OnRotationChanged?.Invoke(this, previousRotation);
} }
private void UpdateLocalPosition() private void UpdateLocalPosition()

View File

@ -39,9 +39,9 @@ public abstract class Collider2DBehaviourBase : Behaviour, ICollider2D
BehaviourController.OnBehaviourAdded += OnBehaviourAddedToController; BehaviourController.OnBehaviourAdded += OnBehaviourAddedToController;
BehaviourController.OnBehaviourRemoved += OnBehaviourRemovedFromController; BehaviourController.OnBehaviourRemoved += OnBehaviourRemovedFromController;
Transform.OnPositionChanged += SetNeedsRecalculation; Transform.OnPositionChanged += SetNeedsRecalculationFromPosition;
Transform.OnRotationChanged += SetNeedsRecalculation; Transform.OnRotationChanged += SetNeedsRecalculationFromRotation;
Transform.OnScaleChanged += SetNeedsRecalculation; Transform.OnScaleChanged += SetNeedsRecalculationFromScale;
Transform.OnParentChanged += UpdateRigidBody2D; Transform.OnParentChanged += UpdateRigidBody2D;
} }
@ -62,16 +62,18 @@ public abstract class Collider2DBehaviourBase : Behaviour, ICollider2D
_rigidBody2D = null; _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() protected override void OnFinalize()
{ {
BehaviourController.OnBehaviourAdded -= OnBehaviourAddedToController; BehaviourController.OnBehaviourAdded -= OnBehaviourAddedToController;
BehaviourController.OnBehaviourRemoved -= OnBehaviourRemovedFromController; BehaviourController.OnBehaviourRemoved -= OnBehaviourRemovedFromController;
Transform.OnScaleChanged -= SetNeedsRecalculation; Transform.OnScaleChanged -= SetNeedsRecalculationFromScale;
Transform.OnPositionChanged -= SetNeedsRecalculation; Transform.OnPositionChanged -= SetNeedsRecalculationFromPosition;
Transform.OnRotationChanged -= SetNeedsRecalculation; Transform.OnRotationChanged -= SetNeedsRecalculationFromRotation;
} }
public void Detect(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionDetected?.Invoke(this, collisionDetectionInformation); public void Detect(CollisionDetectionInformation collisionDetectionInformation) => OnCollisionDetected?.Invoke(this, collisionDetectionInformation);