From 435042e1f5e2e191b715e4ddbcbab430755c74b4 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 3 May 2022 18:58:18 +0200 Subject: [PATCH] Allow Tooltip to manage more than one paragraph and make it easier to add new lines --- CHANGELOG.md | 2 + MLEM.Ui/Elements/Tooltip.cs | 75 ++++++++++++++++++++++++++++++++----- 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f6e7e9c..72aadc3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ Improvements - Ensure that Element.IsMouseOver is always accurate by making it an auto-property - 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 Fixes - Fixed auto-nav tooltip displaying on the selected element even when not in auto-nav mode @@ -31,6 +32,7 @@ Fixes Removals - Marked old Draw and DrawTransformed overloads as obsolete in favor of SpriteBatchContext ones +- Marked Tooltip.Paragraph as obsolete in favor of new Paragraphs collection ## 5.3.0 ### MLEM diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index c8e885a..1f64fee 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Microsoft.Xna.Framework; using MLEM.Input; using MLEM.Ui.Style; @@ -11,6 +12,13 @@ namespace MLEM.Ui.Elements { /// public class Tooltip : Panel { + /// + /// 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 . + /// + public readonly List Paragraphs = new List(); + /// /// The offset that this tooltip's top left corner should have from the mouse position /// @@ -26,6 +34,7 @@ namespace MLEM.Ui.Elements { /// /// The paragraph of text that this tooltip displays /// + [Obsolete("Use Paragraphs instead, which allows for multiple paragraphs to be managed by one tooltip")] public Paragraph Paragraph; /// /// Determines whether this tooltip should display when is true, which is when the UI is being controlled using a keyboard or gamepad. @@ -53,8 +62,11 @@ namespace MLEM.Ui.Elements { /// The element that should automatically cause the tooltip to appear and disappear when hovered and not hovered, respectively public Tooltip(string text = null, Element elementToHover = null) : base(Anchor.TopLeft, Vector2.One, Vector2.Zero) { - if (text != null) - this.Paragraph = this.AddChild(new Paragraph(Anchor.TopLeft, 0, text)); + if (text != null) { + #pragma warning disable CS0618 + this.Paragraph = this.AddParagraph(text); + #pragma warning restore CS0618 + } this.Init(elementToHover); } @@ -65,7 +77,9 @@ namespace MLEM.Ui.Elements { /// The element that should automatically cause the tooltip to appear and disappear when hovered and not hovered, respectively public Tooltip(Paragraph.TextCallback textCallback, Element elementToHover = null) : base(Anchor.TopLeft, Vector2.One, Vector2.Zero) { - this.Paragraph = this.AddChild(new Paragraph(Anchor.TopLeft, 0, textCallback)); + #pragma warning disable CS0618 + this.Paragraph = this.AddParagraph(textCallback); + #pragma warning restore CS0618 this.Init(elementToHover); } @@ -102,10 +116,48 @@ namespace MLEM.Ui.Elements { this.AutoNavOffset = this.AutoNavOffset.OrStyle(style.TooltipAutoNavOffset); this.Delay = this.Delay.OrStyle(style.TooltipDelay); this.ChildPadding = this.ChildPadding.OrStyle(style.TooltipChildPadding); - if (this.Paragraph != null) { - this.Paragraph.TextColor = this.Paragraph.TextColor.OrStyle(style.TooltipTextColor, 1); - this.Paragraph.Size = new Vector2(style.TooltipTextWidth, 0); - } + foreach (var paragraph in this.Paragraphs) + SetParagraphStyle(paragraph, style); + + #pragma warning disable CS0618 + // still set style here in case someone changed the paragraph field manually + if (this.Paragraph != null) + SetParagraphStyle(this.Paragraph, style); + #pragma warning restore CS0618 + } + + /// + /// Adds the given paragraph to this tooltip's managed list, as well as to its children using . + /// A paragraph that is contained in the list will automatically have the and applied. + /// + /// The paragraph to add + /// The added paragraph, for chaining + public Paragraph AddParagraph(Paragraph paragraph) { + this.Paragraphs.Add(paragraph); + this.AddChild(paragraph); + if (this.Style.HasValue()) + SetParagraphStyle(paragraph, this.Style); + return paragraph; + } + + /// + /// Adds a new paragraph with the given text callback to this tooltip's managed list, as well as to its children using . + /// A paragraph that is contained in the list will automatically have the and applied. + /// + /// 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)); + } + + /// + /// Adds a new paragraph with the given text to this tooltip's managed list, as well as to its children using . + /// A paragraph that is contained in the list will automatically have the and applied. + /// + /// The text that the paragraph should display + /// The created paragraph, for chaining + public Paragraph AddParagraph(string text) { + return this.AddParagraph(p => text); } /// @@ -185,9 +237,6 @@ namespace MLEM.Ui.Elements { } private void Init(Element elementToHover) { - if (this.Paragraph != null) - this.Paragraph.AutoAdjustWidth = true; - this.SetWidthBasedOnChildren = true; this.SetHeightBasedOnChildren = true; this.CanBeMoused = false; @@ -210,5 +259,11 @@ namespace MLEM.Ui.Elements { } } + private static void SetParagraphStyle(Paragraph paragraph, UiStyle style) { + paragraph.TextColor = paragraph.TextColor.OrStyle(style.TooltipTextColor, 1); + paragraph.Size = new Vector2(style.TooltipTextWidth, 0); + paragraph.AutoAdjustWidth = true; + } + } } \ No newline at end of file