perf!: events refactored throughout all the project to use Event<> class

All delegate events are refactored to use the Event<TSender> and Event<TSender, TArgument> for performance issues regarding delegate events creating garbage, also this gives us better control on event invocation since C# Delegates did also create unnecessary garbage during Delegate.DynamicInvoke
This commit is contained in:
2025-05-31 00:14:43 +03:00
parent 996e61d0ad
commit 61e2761580
58 changed files with 637 additions and 485 deletions

View File

@@ -5,9 +5,9 @@ namespace Syntriax.Engine.Core;
[System.Diagnostics.DebuggerDisplay("Name: {UniverseObject.Name, nq} Position: {Position.ToString(), nq}, Scale: {Scale.ToString(), nq}, Rotation: {Rotation}")]
public class Transform2D : Behaviour, ITransform2D
{
public event ITransform2D.PositionChangedEventHandler? OnPositionChanged = null;
public event ITransform2D.ScaleChangedEventHandler? OnScaleChanged = null;
public event ITransform2D.RotationChangedEventHandler? OnRotationChanged = null;
public Event<ITransform2D, ITransform2D.PositionChangedArguments> OnPositionChanged { get; } = new();
public Event<ITransform2D, ITransform2D.ScaleChangedArguments> OnScaleChanged { get; } = new();
public Event<ITransform2D, ITransform2D.RotationChangedArguments> OnRotationChanged { get; } = new();
private Vector2D _position = Vector2D.Zero;
private Vector2D _scale = Vector2D.One;
@@ -31,7 +31,7 @@ public class Transform2D : Behaviour, ITransform2D
_position = value;
UpdateLocalPosition();
OnPositionChanged?.Invoke(this, previousPosition);
OnPositionChanged?.Invoke(this, new(previousPosition));
}
}
@@ -47,7 +47,7 @@ public class Transform2D : Behaviour, ITransform2D
_scale = value;
UpdateLocalScale();
OnScaleChanged?.Invoke(this, previousScale);
OnScaleChanged?.Invoke(this, new(previousScale));
}
}
@@ -63,7 +63,7 @@ public class Transform2D : Behaviour, ITransform2D
_rotation = value;
UpdateLocalRotation();
OnRotationChanged?.Invoke(this, previousRotation);
OnRotationChanged?.Invoke(this, new(previousRotation));
}
}
@@ -79,7 +79,7 @@ public class Transform2D : Behaviour, ITransform2D
_localPosition = value;
UpdatePosition();
OnPositionChanged?.Invoke(this, previousPosition);
OnPositionChanged?.Invoke(this, new(previousPosition));
}
}
@@ -97,8 +97,8 @@ public class Transform2D : Behaviour, ITransform2D
UpdateScale();
UpdatePosition();
OnScaleChanged?.Invoke(this, previousScale);
OnPositionChanged?.Invoke(this, previousPosition);
OnScaleChanged?.Invoke(this, new(previousScale));
OnPositionChanged?.Invoke(this, new(previousPosition));
}
}
@@ -114,21 +114,21 @@ public class Transform2D : Behaviour, ITransform2D
_localRotation = value;
UpdateRotation();
OnRotationChanged?.Invoke(this, previousRotation);
OnRotationChanged?.Invoke(this, new(previousRotation));
}
}
private void RecalculatePosition(ITransform2D _, Vector2D previousPosition)
private void RecalculatePosition(ITransform2D _, ITransform2D.PositionChangedArguments args)
{
if (parentTransform is null)
return;
UpdatePosition();
OnPositionChanged?.Invoke(this, previousPosition);
OnPositionChanged?.Invoke(this, args);
}
private void RecalculateScale(ITransform2D _, Vector2D previousScale)
private void RecalculateScale(ITransform2D _, ITransform2D.ScaleChangedArguments args)
{
if (parentTransform is null)
return;
@@ -138,11 +138,11 @@ public class Transform2D : Behaviour, ITransform2D
UpdateScale();
UpdatePosition();
OnScaleChanged?.Invoke(this, previousScale);
OnPositionChanged?.Invoke(this, previousPosition);
OnScaleChanged?.Invoke(this, args);
OnPositionChanged?.Invoke(this, new(previousPosition));
}
private void RecalculateRotation(ITransform2D _, float previousRotation)
private void RecalculateRotation(ITransform2D _, ITransform2D.RotationChangedArguments args)
{
if (parentTransform is null)
return;
@@ -152,8 +152,8 @@ public class Transform2D : Behaviour, ITransform2D
UpdateRotation();
UpdatePosition();
OnRotationChanged?.Invoke(this, previousRotation);
OnPositionChanged?.Invoke(this, previousPosition);
OnRotationChanged?.Invoke(this, args);
OnPositionChanged?.Invoke(this, new(previousPosition));
}
private void UpdateLocalPosition()
@@ -207,12 +207,12 @@ public class Transform2D : Behaviour, ITransform2D
protected override void InitializeInternal()
{
UpdateReferences(UniverseObject.Parent);
UniverseObject.OnParentChanged += OnParentChanged;
UniverseObject.OnParentChanged.AddListener(OnParentChanged);
}
protected override void FinalizeInternal()
{
UniverseObject.OnParentChanged -= OnParentChanged;
UniverseObject.OnParentChanged.RemoveListener(OnParentChanged);
}
private void UpdateReferences(IUniverseObject? parent)
@@ -220,48 +220,48 @@ public class Transform2D : Behaviour, ITransform2D
ITransform2D? previousParent = parentTransform;
if (previousParent is not null)
{
previousParent.OnPositionChanged -= RecalculatePosition;
previousParent.OnScaleChanged -= RecalculateScale;
previousParent.OnRotationChanged -= RecalculateRotation;
previousParent.BehaviourController.UniverseObject.OnParentChanged -= OnParentChanged;
previousParent.BehaviourController.OnBehaviourAdded -= LookForTransform2D;
previousParent.OnPositionChanged.RemoveListener(RecalculatePosition);
previousParent.OnScaleChanged.RemoveListener(RecalculateScale);
previousParent.OnRotationChanged.RemoveListener(RecalculateRotation);
previousParent.BehaviourController.UniverseObject.OnParentChanged.RemoveListener(OnParentChanged);
previousParent.BehaviourController.OnBehaviourAdded.RemoveListener(LookForTransform2D);
}
parentTransform = parent?.BehaviourController.GetBehaviour<ITransform2D>();
if (parentTransform is not null)
{
parentTransform.OnPositionChanged += RecalculatePosition;
parentTransform.OnScaleChanged += RecalculateScale;
parentTransform.OnRotationChanged += RecalculateRotation;
parentTransform.BehaviourController.UniverseObject.OnParentChanged += OnParentChanged;
parentTransform.OnPositionChanged.AddListener(RecalculatePosition);
parentTransform.OnScaleChanged.AddListener(RecalculateScale);
parentTransform.OnRotationChanged.AddListener(RecalculateRotation);
parentTransform.BehaviourController.UniverseObject.OnParentChanged.AddListener(OnParentChanged);
UpdatePosition();
UpdateScale();
UpdateRotation();
}
else if (UniverseObject.Parent is not null)
UniverseObject.Parent.BehaviourController.OnBehaviourAdded += LookForTransform2D;
UniverseObject.Parent.BehaviourController.OnBehaviourAdded.AddListener(LookForTransform2D);
UpdateLocalPosition();
UpdateLocalScale();
UpdateLocalRotation();
OnPositionChanged?.Invoke(this, Position);
OnScaleChanged?.Invoke(this, Scale);
OnRotationChanged?.Invoke(this, Rotation);
OnPositionChanged?.Invoke(this, new(Position));
OnScaleChanged?.Invoke(this, new(Scale));
OnRotationChanged?.Invoke(this, new(Rotation));
}
private void LookForTransform2D(IBehaviourController sender, IBehaviour behaviourAdded)
private void LookForTransform2D(IBehaviourController sender, IBehaviourController.BehaviourAddedArguments args)
{
if (behaviourAdded is not ITransform2D transform2D)
if (args.BehaviourAdded is not ITransform2D)
return;
UpdateReferences(UniverseObject.Parent);
}
private void OnParentChanged(IUniverseObject sender, IUniverseObject? previousParent, IUniverseObject? newParent)
private void OnParentChanged(IUniverseObject sender, IUniverseObject.ParentChangedArguments args)
{
UpdateReferences(newParent);
UpdateReferences(args.CurrentParent);
}
}