1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

Fixed tooltips not displaying correctly with auto-hiding paragraphs

This commit is contained in:
Ell 2021-09-09 16:53:12 +02:00
parent 05e320d4f4
commit 9890c4895c
4 changed files with 33 additions and 13 deletions

View file

@ -36,6 +36,7 @@ Improvements
Fixes Fixes
- Fixed a crash if a paragraph has a link formatting code, but no font - Fixed a crash if a paragraph has a link formatting code, but no font
- Fixed tooltips with custom text scale not snapping to the mouse correctly in their first displayed frame - Fixed tooltips with custom text scale not snapping to the mouse correctly in their first displayed frame
- Fixed tooltips not displaying correctly with auto-hiding paragraphs
### MLEM.Extended ### MLEM.Extended
Improvements Improvements

View file

@ -8,6 +8,7 @@ using MLEM.Extensions;
using MLEM.Font; using MLEM.Font;
using MLEM.Formatting; using MLEM.Formatting;
using MLEM.Formatting.Codes; using MLEM.Formatting.Codes;
using MLEM.Input;
using MLEM.Misc; using MLEM.Misc;
using MLEM.Startup; using MLEM.Startup;
using MLEM.Textures; using MLEM.Textures;
@ -141,6 +142,10 @@ namespace Demos {
this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Toggle Mouse Tooltip") { this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Toggle Mouse Tooltip") {
OnPressed = element => tooltip.IsHidden = !tooltip.IsHidden OnPressed = element => tooltip.IsHidden = !tooltip.IsHidden
}); });
var delayed = this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Delayed Tooltip") {PositionOffset = new Vector2(0, 1)});
delayed.AddTooltip(50, "This tooltip appears with a half second delay!").Delay = TimeSpan.FromSeconds(0.5);
var condition = this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Hold Ctrl for Tooltip") {PositionOffset = new Vector2(0, 1)});
condition.AddTooltip(50, p => this.InputHandler.IsModifierKeyDown(ModifierKey.Control) ? "This tooltip only appears when holding control!" : string.Empty);
var slider = new Slider(Anchor.AutoLeft, new Vector2(1, 10), 5, 1) { var slider = new Slider(Anchor.AutoLeft, new Vector2(1, 10), 5, 1) {
StepPerScroll = 0.01F StepPerScroll = 0.01F

View file

@ -649,7 +649,7 @@ namespace MLEM.Ui.Elements {
autoSize.Y = lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Bottom; autoSize.Y = lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Bottom;
foundChild = lowest; foundChild = lowest;
} else { } else {
if (this.Children.Count > 0) if (this.Children.Any(e => !e.IsHidden))
throw new InvalidOperationException($"{this} with root {this.Root.Name} sets its height based on children but it only has children anchored too low ({string.Join(", ", this.Children.Select(c => c.Anchor))})"); throw new InvalidOperationException($"{this} with root {this.Root.Name} sets its height based on children but it only has children anchored too low ({string.Join(", ", this.Children.Select(c => c.Anchor))})");
autoSize.Y = 0; autoSize.Y = 0;
} }
@ -661,7 +661,7 @@ namespace MLEM.Ui.Elements {
autoSize.X = rightmost.UnscrolledArea.Right - pos.X + this.ScaledChildPadding.Right; autoSize.X = rightmost.UnscrolledArea.Right - pos.X + this.ScaledChildPadding.Right;
foundChild = rightmost; foundChild = rightmost;
} else { } else {
if (this.Children.Count > 0) if (this.Children.Any(e => !e.IsHidden))
throw new InvalidOperationException($"{this} with root {this.Root.Name} sets its width based on children but it only has children anchored too far right ({string.Join(", ", this.Children.Select(c => c.Anchor))})"); throw new InvalidOperationException($"{this} with root {this.Root.Name} sets its width based on children but it only has children anchored too far right ({string.Join(", ", this.Children.Select(c => c.Anchor))})");
autoSize.X = 0; autoSize.X = 0;
} }

View file

@ -24,6 +24,7 @@ namespace MLEM.Ui.Elements {
public Paragraph Paragraph; public Paragraph Paragraph;
private TimeSpan delayCountdown; private TimeSpan delayCountdown;
private bool autoHidden;
/// <summary> /// <summary>
/// Creates a new tooltip with the given settings /// Creates a new tooltip with the given settings
@ -55,10 +56,14 @@ namespace MLEM.Ui.Elements {
base.Update(time); base.Update(time);
this.SnapPositionToMouse(); this.SnapPositionToMouse();
if (this.IsHidden && this.delayCountdown > TimeSpan.Zero) { if (this.delayCountdown > TimeSpan.Zero) {
this.delayCountdown -= time.ElapsedGameTime; this.delayCountdown -= time.ElapsedGameTime;
if (this.delayCountdown <= TimeSpan.Zero) if (this.delayCountdown <= TimeSpan.Zero) {
this.IsHidden = false; this.IsHidden = false;
this.UpdateAutoHidden();
}
} else {
this.UpdateAutoHidden();
} }
} }
@ -111,6 +116,7 @@ namespace MLEM.Ui.Elements {
this.IsHidden = true; this.IsHidden = true;
this.delayCountdown = this.Delay; this.delayCountdown = this.Delay;
} }
this.autoHidden = false;
} }
/// <summary> /// <summary>
@ -127,15 +133,7 @@ namespace MLEM.Ui.Elements {
/// </summary> /// </summary>
/// <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 void AddToElement(Element elementToHover) { public void AddToElement(Element elementToHover) {
elementToHover.OnMouseEnter += element => { elementToHover.OnMouseEnter += element => this.Display(element.System, element.GetType().Name + "Tooltip");
// only display the tooltip if there is anything in it
foreach (var c in this.Children) {
if (!c.IsHidden) {
this.Display(element.System, element.GetType().Name + "Tooltip");
break;
}
}
};
elementToHover.OnMouseExit += element => this.Remove(); elementToHover.OnMouseExit += element => this.Remove();
} }
@ -152,5 +150,21 @@ namespace MLEM.Ui.Elements {
this.AddToElement(elementToHover); this.AddToElement(elementToHover);
} }
private void UpdateAutoHidden() {
var shouldBeHidden = true;
foreach (var child in this.Children) {
if (!child.IsHidden) {
shouldBeHidden = false;
break;
}
}
if (this.autoHidden != shouldBeHidden) {
// only auto-hide if IsHidden wasn't changed manually
if (this.IsHidden == this.autoHidden)
this.IsHidden = shouldBeHidden;
this.autoHidden = shouldBeHidden;
}
}
} }
} }