From 8d3afcf276d07e1bac777847259392ca261cdec7 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 8 Dec 2019 21:49:15 +0100 Subject: [PATCH] made CanSelectContent be set automatically by the root through adding more events --- Demos/UiDemo.cs | 2 +- MLEM.Ui/Elements/Element.cs | 4 ++++ MLEM.Ui/Elements/Tooltip.cs | 2 +- MLEM.Ui/UiSystem.cs | 16 +++++++++++++++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index c60b5c8..0bacbda 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -140,7 +140,7 @@ namespace Demos { root.AddChild(new RadioButton(Anchor.AutoLeft, new Vector2(1, 10), "Radio button 2!") {PositionOffset = new Vector2(0, 1)}); var tooltip = new Tooltip(50, "This is a test tooltip to see the window bounding") {IsHidden = true}; - this.UiSystem.Add("TestTooltip", tooltip).CanSelectContent = false; + this.UiSystem.Add("TestTooltip", tooltip); root.AddChild(new VerticalSpace(3)); root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Toggle Test Tooltip") { OnPressed = element => tooltip.IsHidden = !tooltip.IsHidden diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index e14157a..c2be578 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -163,6 +163,8 @@ namespace MLEM.Ui.Elements { element.AndChildren(e => { e.Root = this.Root; e.System = this.System; + if (this.Root != null) + this.Root.OnElementAdded(e); }); this.SetSortedChildrenDirty(); this.SetAreaDirty(); @@ -175,6 +177,8 @@ namespace MLEM.Ui.Elements { element.AndChildren(e => { e.Root = null; e.System = null; + if (this.Root != null) + this.Root.OnElementRemoved(e); }); this.SetSortedChildrenDirty(); this.SetAreaDirty(); diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index f47dcf7..09d8e2d 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -16,7 +16,7 @@ namespace MLEM.Ui.Elements { this.CanBeSelected = false; if (elementToHover != null) { - elementToHover.OnMouseEnter += element => element.System.Add(element.GetType().Name + "Tooltip", this).CanSelectContent = false; + elementToHover.OnMouseEnter += element => element.System.Add(element.GetType().Name + "Tooltip", this); elementToHover.OnMouseExit += element => { if (this.System != null) this.System.Remove(element.GetType().Name + "Tooltip"); diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 3412443..67f6ebb 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -139,6 +139,7 @@ namespace MLEM.Ui { root.Element.AndChildren(e => { e.Root = root; e.System = this; + root.OnElementAdded(e); e.SetAreaDirty(); }); this.OnRootAdded?.Invoke(root); @@ -160,6 +161,7 @@ namespace MLEM.Ui { root.Element.AndChildren(e => { e.Root = null; e.System = null; + root.OnElementRemoved(e); e.SetAreaDirty(); }); this.OnRootRemoved?.Invoke(root); @@ -204,17 +206,29 @@ namespace MLEM.Ui { } } public float ActualScale => this.System.GlobalScale * this.Scale; - public bool CanSelectContent = true; public Matrix Transform = Matrix.Identity; public Matrix InvTransform => Matrix.Invert(this.Transform); public Element SelectedElement { get; private set; } + public bool CanSelectContent { get; private set; } + + public Element.GenericCallback OnElementAdded; + public Element.GenericCallback OnElementRemoved; public RootElement(string name, Element element, UiSystem system) { this.Name = name; this.Element = element; this.System = system; + + this.OnElementAdded += e => { + if (e.CanBeSelected) + this.CanSelectContent = true; + }; + this.OnElementRemoved += e => { + if (e.CanBeSelected && !this.Element.GetChildren(regardGrandchildren: true).Any(c => c.CanBeSelected)) + this.CanSelectContent = false; + }; } public void SelectElement(Element element) {