From 5a1b31e8a3b160fcdf9431dfc55bcdd98808bc3f Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 3 May 2022 19:07:53 +0200 Subject: [PATCH] Allow adding dropdown and tooltip elements at a specified index --- CHANGELOG.md | 1 + MLEM.Ui/Elements/Dropdown.cs | 15 +++++++++------ MLEM.Ui/Elements/Tooltip.cs | 17 ++++++++++------- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72aadc3..17549d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Improvements - Started using SpriteBatchContext for Draw and DrawTransformed methods - Make use of the new consuming variants in InputHandler and Keybind to consume UiControls inputs - Allow Tooltip to manage more than one paragraph and make it easier to add new lines +- Allow adding dropdown elements at a specified index Fixes - Fixed auto-nav tooltip displaying on the selected element even when not in auto-nav mode diff --git a/MLEM.Ui/Elements/Dropdown.cs b/MLEM.Ui/Elements/Dropdown.cs index 8f4f25e..3825c00 100644 --- a/MLEM.Ui/Elements/Dropdown.cs +++ b/MLEM.Ui/Elements/Dropdown.cs @@ -55,8 +55,9 @@ namespace MLEM.Ui.Elements { /// Adds an element to this dropdown's /// /// The element to add - public void AddElement(Element element) { - this.Panel.AddChild(element); + /// The index to add the child at, or -1 to add it to the end of the list + public void AddElement(Element element, int index = -1) { + this.Panel.AddChild(element, index); // Since the dropdown causes elements to be over each other, // usual gamepad code doesn't apply element.GetGamepadNextElement = (dir, usualNext) => { @@ -77,8 +78,9 @@ namespace MLEM.Ui.Elements { /// /// The text to display /// The resulting paragraph's event - public Element AddElement(string text, GenericCallback pressed = null) { - return this.AddElement(p => text, pressed); + /// The index to add the child at, or -1 to add it to the end of the list + public Element AddElement(string text, GenericCallback pressed = null, int index = -1) { + return this.AddElement(p => text, pressed, index); } /// @@ -87,7 +89,8 @@ namespace MLEM.Ui.Elements { /// /// The text to display /// The resulting paragraph's event - public Element AddElement(Paragraph.TextCallback text, GenericCallback pressed = null) { + /// The index to add the child at, or -1 to add it to the end of the list + public Element AddElement(Paragraph.TextCallback text, GenericCallback pressed = null, int index = -1) { var paragraph = new Paragraph(Anchor.AutoLeft, 1, text) { CanBeMoused = true, CanBeSelected = true, @@ -97,7 +100,7 @@ namespace MLEM.Ui.Elements { paragraph.OnPressed += pressed; paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray; paragraph.OnMouseExit += e => paragraph.TextColor = Color.White; - this.AddElement(paragraph); + this.AddElement(paragraph, index); return paragraph; } diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index 1f64fee..d69f34f 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -15,7 +15,7 @@ namespace MLEM.Ui.Elements { /// /// A list of objects that this tooltip automatically manages. /// A paragraph that is contained in this list will automatically have the and applied. - /// To add a paragraph to both this list and to , use . + /// To add a paragraph to both this list and to , use . /// public readonly List Paragraphs = new List(); @@ -132,9 +132,10 @@ namespace MLEM.Ui.Elements { /// /// The paragraph to add /// The added paragraph, for chaining - public Paragraph AddParagraph(Paragraph paragraph) { + /// The index to add the child at, or -1 to add it to the end of the list + public Paragraph AddParagraph(Paragraph paragraph, int index = -1) { this.Paragraphs.Add(paragraph); - this.AddChild(paragraph); + this.AddChild(paragraph, index); if (this.Style.HasValue()) SetParagraphStyle(paragraph, this.Style); return paragraph; @@ -146,8 +147,9 @@ namespace MLEM.Ui.Elements { /// /// The text that the paragraph should display /// The created paragraph, for chaining - public Paragraph AddParagraph(Paragraph.TextCallback text) { - return this.AddParagraph(new Paragraph(Anchor.AutoLeft, 0, text)); + /// The index to add the child at, or -1 to add it to the end of the list + public Paragraph AddParagraph(Paragraph.TextCallback text, int index = -1) { + return this.AddParagraph(new Paragraph(Anchor.AutoLeft, 0, text), index); } /// @@ -156,8 +158,9 @@ namespace MLEM.Ui.Elements { /// /// The text that the paragraph should display /// The created paragraph, for chaining - public Paragraph AddParagraph(string text) { - return this.AddParagraph(p => text); + /// The index to add the child at, or -1 to add it to the end of the list + public Paragraph AddParagraph(string text, int index = -1) { + return this.AddParagraph(p => text, index); } ///