diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 3975ea8..b9a12ab 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -174,7 +174,7 @@ namespace MLEM.Ui.Elements { index = this.Children.Count; this.Children.Insert(index, element); element.Parent = this; - element.Propagate(e => { + element.AndChildren(e => { e.Root = this.Root; e.System = this.System; }); @@ -186,7 +186,7 @@ namespace MLEM.Ui.Elements { public void RemoveChild(Element element) { this.Children.Remove(element); element.Parent = null; - element.Propagate(e => { + element.AndChildren(e => { e.Root = null; e.System = null; }); @@ -454,6 +454,12 @@ namespace MLEM.Ui.Elements { return this.CanBeMoused && this.Area.Contains(position) ? this : null; } + public void AndChildren(Action action) { + action(this); + foreach (var child in this.Children) + child.AndChildren(action); + } + protected virtual void InitStyle(UiStyle style) { this.SelectionIndicator = style.SelectionIndicator; } @@ -466,11 +472,5 @@ namespace MLEM.Ui.Elements { public delegate void DrawCallback(Element element, GameTime time, SpriteBatch batch, float alpha, Point offset); - internal void Propagate(Action action) { - action(this); - foreach (var child in this.Children) - child.Propagate(action); - } - } } \ No newline at end of file diff --git a/MLEM.Ui/UiControls.cs b/MLEM.Ui/UiControls.cs index 25eb2a7..ade1a82 100644 --- a/MLEM.Ui/UiControls.cs +++ b/MLEM.Ui/UiControls.cs @@ -38,7 +38,7 @@ namespace MLEM.Ui { if (mousedNow != null) mousedNow.OnMouseEnter?.Invoke(mousedNow); this.MousedElement = mousedNow; - this.system.Propagate(e => e.OnMousedElementChanged?.Invoke(e, mousedNow)); + this.system.ApplyToAll(e => e.OnMousedElementChanged?.Invoke(e, mousedNow)); } if (this.Input.IsMouseButtonPressed(MouseButton.Left)) { @@ -91,7 +91,7 @@ namespace MLEM.Ui { element.OnSelected?.Invoke(element); this.SelectedElement = element; this.SelectedLastElementWithMouse = mouse; - this.system.Propagate(e => e.OnSelectedElementChanged?.Invoke(e, element)); + this.system.ApplyToAll(e => e.OnSelectedElementChanged?.Invoke(e, element)); } public Element GetElementUnderPos(Point position) { diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 2eaecec..2bcb1e3 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -42,7 +42,7 @@ namespace MLEM.Ui { set { this.style = value; foreach (var root in this.rootElements) { - root.Element.Propagate(e => e.System = this); + root.Element.AndChildren(e => e.System = this); root.Element.SetAreaDirty(); } } @@ -71,7 +71,7 @@ namespace MLEM.Ui { window.AddTextInputListener((sender, key, character) => { foreach (var root in this.rootElements) - root.Element.Propagate(e => e.OnTextInput?.Invoke(e, key, character)); + root.Element.AndChildren(e => e.OnTextInput?.Invoke(e, key, character)); }); this.OnSelectedElementDrawn = (element, time, batch, alpha, offset) => { @@ -116,7 +116,7 @@ namespace MLEM.Ui { if (index < 0 || index > this.rootElements.Count) index = this.rootElements.Count; this.rootElements.Insert(index, root); - root.Element.Propagate(e => { + root.Element.AndChildren(e => { e.Root = root; e.System = this; }); @@ -128,7 +128,7 @@ namespace MLEM.Ui { if (root == null) return; this.rootElements.Remove(root); - root.Element.Propagate(e => { + root.Element.AndChildren(e => { e.Root = null; e.System = null; }); @@ -148,9 +148,9 @@ namespace MLEM.Ui { yield return this.rootElements[i]; } - internal void Propagate(Action action) { + public void ApplyToAll(Action action) { foreach (var root in this.rootElements) - root.Element.Propagate(action); + root.Element.AndChildren(action); } }