1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-20 12:09:10 +02:00

added an extension method for adding tooltip for elements

This commit is contained in:
Ell 2021-02-18 04:16:17 +01:00
parent 81d4fb2dd4
commit 7619ac0dcf
4 changed files with 46 additions and 10 deletions

View file

@ -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);
}
/// <inheritdoc />

View file

@ -112,4 +112,33 @@ namespace MLEM.Ui.Elements {
}
}
/// <summary>
/// This class contains a set of extensions for dealing with <see cref="Element"/> objects
/// </summary>
public static class ElementExtensions {
/// <summary>
/// Adds a new <see cref="Tooltip"/> to the given element using the <see cref="Tooltip(float,Paragraph.TextCallback,Element)"/> constructor
/// </summary>
/// <param name="element"></param>
/// <param name="width">The width of the tooltip</param>
/// <param name="textCallback">The text to display on the tooltip</param>
/// <returns>The created tooltip instance</returns>
public static Tooltip AddTooltip(this Element element, float width, Paragraph.TextCallback textCallback) {
return new Tooltip(width, textCallback, element);
}
/// <summary>
/// Adds a new <see cref="Tooltip"/> to the given element using the <see cref="Tooltip(float,string,Element)"/> constructor
/// </summary>
/// <param name="element"></param>
/// <param name="width">The width of the tooltip</param>
/// <param name="text">The text to display on the tooltip</param>
/// <returns>The created tooltip instance</returns>
public static Tooltip AddTooltip(this Element element, float width, string text) {
return new Tooltip(width, text, element);
}
}
}

View file

@ -122,6 +122,19 @@ namespace MLEM.Ui.Elements {
this.System.Remove(this.Root.Name);
}
/// <summary>
/// Adds this tooltip instance to the given <see cref="Element"/>, making it display when it is moused over
/// </summary>
/// <param name="elementToHover">The element that should automatically cause the tooltip to appear and disappear when hovered and not hovered, respectively</param>
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);
}
}

View file

@ -8,7 +8,7 @@ namespace MLEM.Misc {
/// <summary>
/// A simple class that handles automatically removing and disposing <see cref="SoundEffectInstance"/> 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 <see cref="SoundEffectInstance"/> finishes playing.
/// Note that an object of this class can be added to a <see cref="Game"/> using <see cref="GameComponentCollection.Add"/>.
/// Note that an object of this class can be added to a <see cref="Game"/> using its <see cref="GameComponentCollection"/>.
/// </summary>
public class SoundEffectInstanceHandler : GameComponent, IEnumerable<SoundEffectInstance> {