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>
void RemoveChild(ITransform transform);
delegate void OnPositionChangedEventHandler(ITransform sender);
delegate void OnScaleChangedEventHandler(ITransform sender);
delegate void OnRotationChangedEventHandler(ITransform sender);
/// <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="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);
/// <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);
/// <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);
}

View File

@ -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()

View File

@ -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);