From 4888bb0fd7a2d948456a5c63abbb71b3f6081b6f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 18 Nov 2019 22:36:55 +0100 Subject: [PATCH] made dropdowns only have high priority when opened and fixed panels sometimes ignoring priority --- MLEM.Ui/Elements/Dropdown.cs | 8 +++++-- MLEM.Ui/Elements/Element.cs | 27 ++++++++++++----------- MLEM.Ui/Elements/Panel.cs | 42 ++++++++++++++++++++---------------- 3 files changed, 45 insertions(+), 32 deletions(-) diff --git a/MLEM.Ui/Elements/Dropdown.cs b/MLEM.Ui/Elements/Dropdown.cs index e7c73e4..59e80fd 100644 --- a/MLEM.Ui/Elements/Dropdown.cs +++ b/MLEM.Ui/Elements/Dropdown.cs @@ -6,15 +6,19 @@ namespace MLEM.Ui.Elements { public readonly Panel Panel; public bool IsOpen { get => !this.Panel.IsHidden; - set => this.Panel.IsHidden = !value; + set { + this.Panel.IsHidden = !value; + this.OnOpenedOrClosed?.Invoke(this); + } } + public GenericCallback OnOpenedOrClosed; public Dropdown(Anchor anchor, Vector2 size, string text = null, string tooltipText = null, float tooltipWidth = 50) : base(anchor, size, text, tooltipText, tooltipWidth) { this.Panel = this.AddChild(new Panel(Anchor.TopCenter, size, Vector2.Zero, true) { IsHidden = true }); this.OnAreaUpdated += e => this.Panel.PositionOffset = new Vector2(0, e.Area.Height / this.Scale); - this.Priority = 10000; + this.OnOpenedOrClosed += e => this.Priority = this.IsOpen ? 10000 : 0; this.OnPressed += e => this.IsOpen = !this.IsOpen; } diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 40f9b93..b8d058b 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -189,24 +189,27 @@ namespace MLEM.Ui.Elements { } } - public void SetAreaDirty() { - this.areaDirty = true; - if (this.Anchor >= Anchor.AutoLeft && this.Parent != null) - this.Parent.SetAreaDirty(); - } - public void SetSortedChildrenDirty() { this.sortedChildrenDirty = true; } public void UpdateSortedChildrenIfDirty() { - if (this.sortedChildrenDirty) { - this.sortedChildrenDirty = false; + if (this.sortedChildrenDirty) + this.ForceUpdateSortedChildren(); + } - this.sortedChildren.Clear(); - this.sortedChildren.AddRange(this.Children); - this.sortedChildren.Sort((e1, e2) => e1.Priority.CompareTo(e2.Priority)); - } + public virtual void ForceUpdateSortedChildren() { + this.sortedChildrenDirty = false; + + this.sortedChildren.Clear(); + this.sortedChildren.AddRange(this.Children); + this.sortedChildren.Sort((e1, e2) => e1.Priority.CompareTo(e2.Priority)); + } + + public void SetAreaDirty() { + this.areaDirty = true; + if (this.Anchor >= Anchor.AutoLeft && this.Parent != null) + this.Parent.SetAreaDirty(); } public void UpdateAreaIfDirty() { diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 48cb0c4..eed7462 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -97,30 +97,36 @@ namespace MLEM.Ui.Elements { this.relevantChildrenDirty = true; } - public override void Update(GameTime time) { - base.Update(time); - if (this.relevantChildrenDirty) { - this.relevantChildrenDirty = false; + public override void ForceUpdateSortedChildren() { + base.ForceUpdateSortedChildren(); + if (this.scrollOverflow) + this.relevantChildrenDirty = true; + } - var visible = this.GetRenderTargetArea(); - this.relevantChildren.Clear(); - foreach (var child in this.SortedChildren) { - if (child.Area.Intersects(visible)) { - this.relevantChildren.Add(child); - } else { - foreach (var c in child.GetChildren(regardGrandchildren: true)) { - if (c.Area.Intersects(visible)) { - this.relevantChildren.Add(child); - break; + protected override List GetRelevantChildren() { + var relevant = base.GetRelevantChildren(); + if (this.scrollOverflow) { + if (this.relevantChildrenDirty) { + this.relevantChildrenDirty = false; + + var visible = this.GetRenderTargetArea(); + this.relevantChildren.Clear(); + foreach (var child in this.SortedChildren) { + if (child.Area.Intersects(visible)) { + this.relevantChildren.Add(child); + } else { + foreach (var c in child.GetChildren(regardGrandchildren: true)) { + if (c.Area.Intersects(visible)) { + this.relevantChildren.Add(child); + break; + } } } } } + relevant = this.relevantChildren; } - } - - protected override List GetRelevantChildren() { - return this.scrollOverflow ? this.relevantChildren : base.GetRelevantChildren(); + return relevant; } public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {