feat: Parent Change Propagation to Children

This commit is contained in:
Syntriax 2024-02-06 12:15:45 +03:00
parent 14e3337daa
commit 3902f1caca
1 changed files with 10 additions and 0 deletions

View File

@ -133,6 +133,7 @@ public class Transform : ITransform
Parent.OnPositionChanged -= RecalculatePosition; Parent.OnPositionChanged -= RecalculatePosition;
Parent.OnScaleChanged -= RecalculateScale; Parent.OnScaleChanged -= RecalculateScale;
Parent.OnRotationChanged -= RecalculateRotation; Parent.OnRotationChanged -= RecalculateRotation;
Parent.OnParentChanged -= NotifyChildrenOnParentChange;
} }
Parent = transform; Parent = transform;
@ -143,6 +144,7 @@ public class Transform : ITransform
transform.OnPositionChanged += RecalculatePosition; transform.OnPositionChanged += RecalculatePosition;
transform.OnScaleChanged += RecalculateScale; transform.OnScaleChanged += RecalculateScale;
transform.OnRotationChanged += RecalculateRotation; transform.OnRotationChanged += RecalculateRotation;
transform.OnParentChanged += NotifyChildrenOnParentChange;
} }
UpdateLocalPosition(); UpdateLocalPosition();
@ -174,6 +176,14 @@ public class Transform : ITransform
public IEnumerator<ITransform> GetEnumerator() => _children.GetEnumerator(); public IEnumerator<ITransform> GetEnumerator() => _children.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _children.GetEnumerator(); IEnumerator IEnumerable.GetEnumerator() => _children.GetEnumerator();
private void NotifyChildrenOnParentChange(ITransform transform, ITransform? previousParent)
{
// TODO No idea how logical this is to propagate this to the children the way I'm doing right now.
// I was originally gonna just call `child.OnParentChanged?.Invoke(child, child.Parent);` but seems an unnecessary call too?
foreach (var child in Children)
child.OnParentChanged?.Invoke(transform, previousParent);
}
private void RecalculatePosition(ITransform _) private void RecalculatePosition(ITransform _)
{ {
if (Parent is null) if (Parent is null)