1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-25 14:08:34 +01:00

Compare commits

...

5 commits

5 changed files with 140 additions and 25 deletions

View file

@ -23,6 +23,11 @@ Improvements
- Ensure that Element.IsMouseOver is always accurate by making it an auto-property - Ensure that Element.IsMouseOver is always accurate by making it an auto-property
- Started using SpriteBatchContext for Draw and DrawTransformed methods - Started using SpriteBatchContext for Draw and DrawTransformed methods
- Make use of the new consuming variants in InputHandler and Keybind to consume UiControls inputs - 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
- Turned Tooltip paragraph styling into style properties
- Improved ElementHelper.AddTooltip overloads
- Don't query a paragraph's text callback in the constructor
Fixes Fixes
- Fixed auto-nav tooltip displaying on the selected element even when not in auto-nav mode - Fixed auto-nav tooltip displaying on the selected element even when not in auto-nav mode
@ -31,6 +36,7 @@ Fixes
Removals Removals
- Marked old Draw and DrawTransformed overloads as obsolete in favor of SpriteBatchContext ones - 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 ## 5.3.0
### MLEM ### MLEM

View file

@ -55,8 +55,9 @@ namespace MLEM.Ui.Elements {
/// Adds an element to this dropdown's <see cref="Panel"/> /// Adds an element to this dropdown's <see cref="Panel"/>
/// </summary> /// </summary>
/// <param name="element">The element to add</param> /// <param name="element">The element to add</param>
public void AddElement(Element element) { /// <param name="index">The index to add the child at, or -1 to add it to the end of the <see cref="Element.Children"/> list</param>
this.Panel.AddChild(element); public void AddElement(Element element, int index = -1) {
this.Panel.AddChild(element, index);
// Since the dropdown causes elements to be over each other, // Since the dropdown causes elements to be over each other,
// usual gamepad code doesn't apply // usual gamepad code doesn't apply
element.GetGamepadNextElement = (dir, usualNext) => { element.GetGamepadNextElement = (dir, usualNext) => {
@ -77,8 +78,9 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
/// <param name="text">The text to display</param> /// <param name="text">The text to display</param>
/// <param name="pressed">The resulting paragraph's <see cref="Element.OnPressed"/> event</param> /// <param name="pressed">The resulting paragraph's <see cref="Element.OnPressed"/> event</param>
public Element AddElement(string text, GenericCallback pressed = null) { /// <param name="index">The index to add the child at, or -1 to add it to the end of the <see cref="Element.Children"/> list</param>
return this.AddElement(p => text, pressed); public Element AddElement(string text, GenericCallback pressed = null, int index = -1) {
return this.AddElement(p => text, pressed, index);
} }
/// <summary> /// <summary>
@ -87,7 +89,8 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
/// <param name="text">The text to display</param> /// <param name="text">The text to display</param>
/// <param name="pressed">The resulting paragraph's <see cref="Element.OnPressed"/> event</param> /// <param name="pressed">The resulting paragraph's <see cref="Element.OnPressed"/> event</param>
public Element AddElement(Paragraph.TextCallback text, GenericCallback pressed = null) { /// <param name="index">The index to add the child at, or -1 to add it to the end of the <see cref="Element.Children"/> list</param>
public Element AddElement(Paragraph.TextCallback text, GenericCallback pressed = null, int index = -1) {
var paragraph = new Paragraph(Anchor.AutoLeft, 1, text) { var paragraph = new Paragraph(Anchor.AutoLeft, 1, text) {
CanBeMoused = true, CanBeMoused = true,
CanBeSelected = true, CanBeSelected = true,
@ -97,7 +100,7 @@ namespace MLEM.Ui.Elements {
paragraph.OnPressed += pressed; paragraph.OnPressed += pressed;
paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray; paragraph.OnMouseEnter += e => paragraph.TextColor = Color.LightGray;
paragraph.OnMouseExit += e => paragraph.TextColor = Color.White; paragraph.OnMouseExit += e => paragraph.TextColor = Color.White;
this.AddElement(paragraph); this.AddElement(paragraph, index);
return paragraph; return paragraph;
} }

View file

@ -184,7 +184,7 @@ namespace MLEM.Ui.Elements {
/// <param name="textCallback">The text to display on the tooltip</param> /// <param name="textCallback">The text to display on the tooltip</param>
/// <returns>The created tooltip instance</returns> /// <returns>The created tooltip instance</returns>
public static Tooltip AddTooltip(this Element element, Paragraph.TextCallback textCallback) { public static Tooltip AddTooltip(this Element element, Paragraph.TextCallback textCallback) {
return new Tooltip(textCallback, element); return element.AddTooltip(new Tooltip(textCallback));
} }
/// <summary> /// <summary>
@ -194,7 +194,18 @@ namespace MLEM.Ui.Elements {
/// <param name="text">The text to display on the tooltip</param> /// <param name="text">The text to display on the tooltip</param>
/// <returns>The created tooltip instance</returns> /// <returns>The created tooltip instance</returns>
public static Tooltip AddTooltip(this Element element, string text) { public static Tooltip AddTooltip(this Element element, string text) {
return new Tooltip(text, element); return element.AddTooltip(new Tooltip(text));
}
/// <summary>
/// Adds the given <see cref="Tooltip"/> to the given element
/// </summary>
/// <param name="element">The element to add the tooltip to</param>
/// <param name="tooltip">The tooltip to add</param>
/// <returns>The passed tooltip, for chaining</returns>
public static Tooltip AddTooltip(this Element element, Tooltip tooltip) {
tooltip.AddToElement(element);
return tooltip;
} }
} }

View file

@ -110,19 +110,15 @@ namespace MLEM.Ui.Elements {
/// <param name="width">The paragraph's width. Note that its height is automatically calculated.</param> /// <param name="width">The paragraph's width. Note that its height is automatically calculated.</param>
/// <param name="textCallback">The paragraph's text</param> /// <param name="textCallback">The paragraph's text</param>
/// <param name="autoAdjustWidth">Whether the paragraph's width should automatically be calculated based on the text within it.</param> /// <param name="autoAdjustWidth">Whether the paragraph's width should automatically be calculated based on the text within it.</param>
public Paragraph(Anchor anchor, float width, TextCallback textCallback, bool autoAdjustWidth = false) public Paragraph(Anchor anchor, float width, TextCallback textCallback, bool autoAdjustWidth = false) : this(anchor, width, "", autoAdjustWidth) {
: this(anchor, width, "", autoAdjustWidth) {
this.GetTextCallback = textCallback;
this.Text = textCallback(this);
if (this.Text == null)
this.IsHidden = true; this.IsHidden = true;
this.GetTextCallback = textCallback;
} }
/// <inheritdoc cref="Paragraph(Anchor,float,TextCallback,bool)"/> /// <inheritdoc cref="Paragraph(Anchor,float,TextCallback,bool)"/>
public Paragraph(Anchor anchor, float width, string text, bool autoAdjustWidth = false) : base(anchor, new Vector2(width, 0)) { public Paragraph(Anchor anchor, float width, string text, bool autoAdjustWidth = false) : base(anchor, new Vector2(width, 0)) {
this.Text = text;
if (this.Text == null)
this.IsHidden = true; this.IsHidden = true;
this.Text = text;
this.AutoAdjustWidth = autoAdjustWidth; this.AutoAdjustWidth = autoAdjustWidth;
this.CanBeSelected = false; this.CanBeSelected = false;
this.CanBeMoused = false; this.CanBeMoused = false;

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Input; using MLEM.Input;
using MLEM.Ui.Style; using MLEM.Ui.Style;
@ -11,6 +12,13 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
public class Tooltip : Panel { public class Tooltip : Panel {
/// <summary>
/// A list of <see cref="Elements.Paragraph"/> objects that this tooltip automatically manages.
/// A paragraph that is contained in this list will automatically have the <see cref="UiStyle.TooltipTextWidth"/> and <see cref="UiStyle.TooltipTextColor"/> applied.
/// To add a paragraph to both this list and to <see cref="Element.Children"/>, use <see cref="AddParagraph(Elements.Paragraph,int)"/>.
/// </summary>
public readonly List<Paragraph> Paragraphs = new List<Paragraph>();
/// <summary> /// <summary>
/// The offset that this tooltip's top left corner should have from the mouse position /// The offset that this tooltip's top left corner should have from the mouse position
/// </summary> /// </summary>
@ -23,9 +31,41 @@ namespace MLEM.Ui.Elements {
/// The amount of time that the mouse has to be over an element before it appears /// The amount of time that the mouse has to be over an element before it appears
/// </summary> /// </summary>
public StyleProp<TimeSpan> Delay; public StyleProp<TimeSpan> Delay;
/// <summary>
/// The <see cref="Elements.Paragraph.TextColor"/> that this tooltip's <see cref="Paragraphs"/> should have
/// </summary>
public StyleProp<Color> ParagraphTextColor {
get => this.paragraphTextColor;
set {
this.paragraphTextColor = value;
this.UpdateParagraphsStyles();
}
}
/// <summary>
/// The <see cref="Elements.Paragraph.TextScale"/> that this tooltip's <see cref="Paragraphs"/> should have
/// </summary>
public StyleProp<float> ParagraphTextScale {
get => this.paragraphTextScale;
set {
this.paragraphTextScale = value;
this.UpdateParagraphsStyles();
}
}
/// <summary>
/// The width that this tooltip's <see cref="Paragraphs"/> should have
/// </summary>
public StyleProp<float> ParagraphWidth {
get => this.paragraphWidth;
set {
this.paragraphWidth = value;
this.UpdateParagraphsStyles();
}
}
/// <summary> /// <summary>
/// The paragraph of text that this tooltip displays /// The paragraph of text that this tooltip displays
/// </summary> /// </summary>
[Obsolete("Use Paragraphs instead, which allows for multiple paragraphs to be managed by one tooltip")]
public Paragraph Paragraph; public Paragraph Paragraph;
/// <summary> /// <summary>
/// Determines whether this tooltip should display when <see cref="UiControls.IsAutoNavMode"/> is true, which is when the UI is being controlled using a keyboard or gamepad. /// Determines whether this tooltip should display when <see cref="UiControls.IsAutoNavMode"/> is true, which is when the UI is being controlled using a keyboard or gamepad.
@ -45,6 +85,9 @@ namespace MLEM.Ui.Elements {
private TimeSpan delayCountdown; private TimeSpan delayCountdown;
private bool autoHidden; private bool autoHidden;
private Element snapElement; private Element snapElement;
private StyleProp<float> paragraphWidth;
private StyleProp<float> paragraphTextScale;
private StyleProp<Color> paragraphTextColor;
/// <summary> /// <summary>
/// Creates a new tooltip with the given settings /// Creates a new tooltip with the given settings
@ -53,8 +96,11 @@ namespace MLEM.Ui.Elements {
/// <param name="elementToHover">The element that should automatically cause the tooltip to appear and disappear when hovered and not hovered, respectively</param> /// <param name="elementToHover">The element that should automatically cause the tooltip to appear and disappear when hovered and not hovered, respectively</param>
public Tooltip(string text = null, Element elementToHover = null) : public Tooltip(string text = null, Element elementToHover = null) :
base(Anchor.TopLeft, Vector2.One, Vector2.Zero) { base(Anchor.TopLeft, Vector2.One, Vector2.Zero) {
if (text != null) if (text != null) {
this.Paragraph = this.AddChild(new Paragraph(Anchor.TopLeft, 0, text)); #pragma warning disable CS0618
this.Paragraph = this.AddParagraph(text);
#pragma warning restore CS0618
}
this.Init(elementToHover); this.Init(elementToHover);
} }
@ -65,7 +111,9 @@ namespace MLEM.Ui.Elements {
/// <param name="elementToHover">The element that should automatically cause the tooltip to appear and disappear when hovered and not hovered, respectively</param> /// <param name="elementToHover">The element that should automatically cause the tooltip to appear and disappear when hovered and not hovered, respectively</param>
public Tooltip(Paragraph.TextCallback textCallback, Element elementToHover = null) : public Tooltip(Paragraph.TextCallback textCallback, Element elementToHover = null) :
base(Anchor.TopLeft, Vector2.One, Vector2.Zero) { 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); this.Init(elementToHover);
} }
@ -101,11 +149,47 @@ namespace MLEM.Ui.Elements {
this.MouseOffset = this.MouseOffset.OrStyle(style.TooltipOffset); this.MouseOffset = this.MouseOffset.OrStyle(style.TooltipOffset);
this.AutoNavOffset = this.AutoNavOffset.OrStyle(style.TooltipAutoNavOffset); this.AutoNavOffset = this.AutoNavOffset.OrStyle(style.TooltipAutoNavOffset);
this.Delay = this.Delay.OrStyle(style.TooltipDelay); this.Delay = this.Delay.OrStyle(style.TooltipDelay);
this.ParagraphTextColor = this.ParagraphTextColor.OrStyle(style.TooltipTextColor);
this.ParagraphTextScale = this.ParagraphTextScale.OrStyle(style.TextScale);
this.ParagraphWidth = this.ParagraphWidth.OrStyle(style.TooltipTextWidth);
this.ChildPadding = this.ChildPadding.OrStyle(style.TooltipChildPadding); this.ChildPadding = this.ChildPadding.OrStyle(style.TooltipChildPadding);
if (this.Paragraph != null) { this.UpdateParagraphsStyles();
this.Paragraph.TextColor = this.Paragraph.TextColor.OrStyle(style.TooltipTextColor, 1);
this.Paragraph.Size = new Vector2(style.TooltipTextWidth, 0);
} }
/// <summary>
/// Adds the given paragraph to this tooltip's managed <see cref="Paragraphs"/> list, as well as to its children using <see cref="Element.AddChild{T}"/>.
/// A paragraph that is contained in the <see cref="Paragraphs"/> list will automatically have the <see cref="UiStyle.TooltipTextWidth"/> and <see cref="UiStyle.TooltipTextColor"/> applied.
/// </summary>
/// <param name="paragraph">The paragraph to add</param>
/// <returns>The added paragraph, for chaining</returns>
/// <param name="index">The index to add the child at, or -1 to add it to the end of the <see cref="Element.Children"/> list</param>
public Paragraph AddParagraph(Paragraph paragraph, int index = -1) {
this.Paragraphs.Add(paragraph);
this.AddChild(paragraph, index);
this.UpdateParagraphStyle(paragraph);
return paragraph;
}
/// <summary>
/// Adds a new paragraph with the given text callback to this tooltip's managed <see cref="Paragraphs"/> list, as well as to its children using <see cref="Element.AddChild{T}"/>.
/// A paragraph that is contained in the <see cref="Paragraphs"/> list will automatically have the <see cref="UiStyle.TooltipTextWidth"/> and <see cref="UiStyle.TooltipTextColor"/> applied.
/// </summary>
/// <param name="text">The text that the paragraph should display</param>
/// <returns>The created paragraph, for chaining</returns>
/// <param name="index">The index to add the child at, or -1 to add it to the end of the <see cref="Element.Children"/> list</param>
public Paragraph AddParagraph(Paragraph.TextCallback text, int index = -1) {
return this.AddParagraph(new Paragraph(Anchor.AutoLeft, 0, text), index);
}
/// <summary>
/// Adds a new paragraph with the given text to this tooltip's managed <see cref="Paragraphs"/> list, as well as to its children using <see cref="Element.AddChild{T}"/>.
/// A paragraph that is contained in the <see cref="Paragraphs"/> list will automatically have the <see cref="UiStyle.TooltipTextWidth"/> and <see cref="UiStyle.TooltipTextColor"/> applied.
/// </summary>
/// <param name="text">The text that the paragraph should display</param>
/// <returns>The created paragraph, for chaining</returns>
/// <param name="index">The index to add the child at, or -1 to add it to the end of the <see cref="Element.Children"/> list</param>
public Paragraph AddParagraph(string text, int index = -1) {
return this.AddParagraph(p => text, index);
} }
/// <summary> /// <summary>
@ -185,9 +269,6 @@ namespace MLEM.Ui.Elements {
} }
private void Init(Element elementToHover) { private void Init(Element elementToHover) {
if (this.Paragraph != null)
this.Paragraph.AutoAdjustWidth = true;
this.SetWidthBasedOnChildren = true; this.SetWidthBasedOnChildren = true;
this.SetHeightBasedOnChildren = true; this.SetHeightBasedOnChildren = true;
this.CanBeMoused = false; this.CanBeMoused = false;
@ -210,5 +291,23 @@ namespace MLEM.Ui.Elements {
} }
} }
private void UpdateParagraphsStyles() {
foreach (var paragraph in this.Paragraphs)
this.UpdateParagraphStyle(paragraph);
#pragma warning disable CS0618
// still set style here in case someone changed the paragraph field manually
if (this.Paragraph != null)
this.UpdateParagraphStyle(this.Paragraph);
#pragma warning restore CS0618
}
private void UpdateParagraphStyle(Paragraph paragraph) {
paragraph.TextColor = paragraph.TextColor.OrStyle(this.ParagraphTextColor, 1);
paragraph.TextScale = paragraph.TextScale.OrStyle(this.ParagraphTextScale, 1);
paragraph.Size = new Vector2(this.ParagraphWidth, 0);
paragraph.AutoAdjustWidth = true;
}
} }
} }