From 7619ac0dcff24e7f99e703c40dc09c5e2d39c84e Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 18 Feb 2021 04:16:17 +0100 Subject: [PATCH] added an extension method for adding tooltip for elements --- MLEM.Ui/Elements/Button.cs | 2 +- MLEM.Ui/Elements/ElementHelper.cs | 29 +++++++++++++++++++++++++ MLEM.Ui/Elements/Tooltip.cs | 23 +++++++++++++------- MLEM/Misc/SoundEffectInstanceHandler.cs | 2 +- 4 files changed, 46 insertions(+), 10 deletions(-) diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index e0a6c76..aeea6ad 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -75,7 +75,7 @@ namespace MLEM.Ui.Elements { this.AddChild(this.Text); } if (tooltipText != null) - this.Tooltip = new Tooltip(tooltipWidth, tooltipText, this); + this.Tooltip = this.AddTooltip(tooltipWidth, tooltipText); } /// diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index 5c26c32..1ab92db 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -112,4 +112,33 @@ namespace MLEM.Ui.Elements { } } + + /// + /// This class contains a set of extensions for dealing with objects + /// + public static class ElementExtensions { + + /// + /// Adds a new to the given element using the constructor + /// + /// + /// The width of the tooltip + /// The text to display on the tooltip + /// The created tooltip instance + public static Tooltip AddTooltip(this Element element, float width, Paragraph.TextCallback textCallback) { + return new Tooltip(width, textCallback, element); + } + + /// + /// Adds a new to the given element using the constructor + /// + /// + /// The width of the tooltip + /// The text to display on the tooltip + /// The created tooltip instance + public static Tooltip AddTooltip(this Element element, float width, string text) { + return new Tooltip(width, text, element); + } + + } } \ No newline at end of file diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index e6e72b0..5b7eaca 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -122,6 +122,19 @@ namespace MLEM.Ui.Elements { this.System.Remove(this.Root.Name); } + /// + /// Adds this tooltip instance to the given , making it display when it is moused over + /// + /// The element that should automatically cause the tooltip to appear and disappear when hovered and not hovered, respectively + public void AddToElement(Element elementToHover) { + elementToHover.OnMouseEnter += element => { + // only display the tooltip if there is anything in it + if (this.Children.Any(c => !c.IsHidden)) + this.Display(element.System, element.GetType().Name + "Tooltip"); + }; + elementToHover.OnMouseExit += element => this.Remove(); + } + private void Init(Element elementToHover) { if (this.Paragraph != null) this.Paragraph.AutoAdjustWidth = true; @@ -131,14 +144,8 @@ namespace MLEM.Ui.Elements { this.ChildPadding = new Vector2(2); this.CanBeMoused = false; - if (elementToHover != null) { - elementToHover.OnMouseEnter += element => { - // only display the tooltip if there is anything in it - if (this.Children.Any(c => !c.IsHidden)) - this.Display(element.System, element.GetType().Name + "Tooltip"); - }; - elementToHover.OnMouseExit += element => this.Remove(); - } + if (elementToHover != null) + this.AddToElement(elementToHover); } } diff --git a/MLEM/Misc/SoundEffectInstanceHandler.cs b/MLEM/Misc/SoundEffectInstanceHandler.cs index a877cfb..61caaee 100644 --- a/MLEM/Misc/SoundEffectInstanceHandler.cs +++ b/MLEM/Misc/SoundEffectInstanceHandler.cs @@ -8,7 +8,7 @@ namespace MLEM.Misc { /// /// A simple class that handles automatically removing and disposing objects once they are done playing to free up the audio source for new sounds. /// Additionally, a callback can be registered that is invoked when the finishes playing. - /// Note that an object of this class can be added to a using . + /// Note that an object of this class can be added to a using its . /// public class SoundEffectInstanceHandler : GameComponent, IEnumerable {