From 9890c4895ce04b26ebc1a2aada59cff01fc2c855 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 9 Sep 2021 16:53:12 +0200 Subject: [PATCH] Fixed tooltips not displaying correctly with auto-hiding paragraphs --- CHANGELOG.md | 1 + Demos/UiDemo.cs | 5 +++++ MLEM.Ui/Elements/Element.cs | 4 ++-- MLEM.Ui/Elements/Tooltip.cs | 36 +++++++++++++++++++++++++----------- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 45893a0..c12703b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ Improvements Fixes - 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 not displaying correctly with auto-hiding paragraphs ### MLEM.Extended Improvements diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 09ad761..aee9d41 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -8,6 +8,7 @@ using MLEM.Extensions; using MLEM.Font; using MLEM.Formatting; using MLEM.Formatting.Codes; +using MLEM.Input; using MLEM.Misc; using MLEM.Startup; using MLEM.Textures; @@ -141,6 +142,10 @@ namespace Demos { this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Toggle Mouse Tooltip") { 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) { StepPerScroll = 0.01F diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index 689c75e..ac01556 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -649,7 +649,7 @@ namespace MLEM.Ui.Elements { autoSize.Y = lowest.UnscrolledArea.Bottom - pos.Y + this.ScaledChildPadding.Bottom; foundChild = lowest; } 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))})"); autoSize.Y = 0; } @@ -661,7 +661,7 @@ namespace MLEM.Ui.Elements { autoSize.X = rightmost.UnscrolledArea.Right - pos.X + this.ScaledChildPadding.Right; foundChild = rightmost; } 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))})"); autoSize.X = 0; } diff --git a/MLEM.Ui/Elements/Tooltip.cs b/MLEM.Ui/Elements/Tooltip.cs index 229c6af..3f26adc 100644 --- a/MLEM.Ui/Elements/Tooltip.cs +++ b/MLEM.Ui/Elements/Tooltip.cs @@ -24,6 +24,7 @@ namespace MLEM.Ui.Elements { public Paragraph Paragraph; private TimeSpan delayCountdown; + private bool autoHidden; /// /// Creates a new tooltip with the given settings @@ -55,10 +56,14 @@ namespace MLEM.Ui.Elements { base.Update(time); this.SnapPositionToMouse(); - if (this.IsHidden && this.delayCountdown > TimeSpan.Zero) { + if (this.delayCountdown > TimeSpan.Zero) { this.delayCountdown -= time.ElapsedGameTime; - if (this.delayCountdown <= TimeSpan.Zero) + if (this.delayCountdown <= TimeSpan.Zero) { this.IsHidden = false; + this.UpdateAutoHidden(); + } + } else { + this.UpdateAutoHidden(); } } @@ -111,6 +116,7 @@ namespace MLEM.Ui.Elements { this.IsHidden = true; this.delayCountdown = this.Delay; } + this.autoHidden = false; } /// @@ -127,15 +133,7 @@ namespace MLEM.Ui.Elements { /// /// 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 - foreach (var c in this.Children) { - if (!c.IsHidden) { - this.Display(element.System, element.GetType().Name + "Tooltip"); - break; - } - } - }; + elementToHover.OnMouseEnter += element => this.Display(element.System, element.GetType().Name + "Tooltip"); elementToHover.OnMouseExit += element => this.Remove(); } @@ -152,5 +150,21 @@ namespace MLEM.Ui.Elements { 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; + } + } + } } \ No newline at end of file