From b11315ca4699ce206e7b348134b31b8d3902b28f Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Thu, 20 Apr 2017 14:34:29 +0900 Subject: [PATCH] Remove unnecessary interfaces and simplify tooltip logic. --- .../Tests/TestCaseTooltip.cs | 13 ++-- osu.Game/Graphics/Cursor/IHasTooltip.cs | 22 ------ osu.Game/Graphics/Cursor/TooltipContainer.cs | 67 +++++++++++-------- 3 files changed, 42 insertions(+), 60 deletions(-) diff --git a/osu.Desktop.VisualTests/Tests/TestCaseTooltip.cs b/osu.Desktop.VisualTests/Tests/TestCaseTooltip.cs index e7615e63bf..3ade142fd4 100644 --- a/osu.Desktop.VisualTests/Tests/TestCaseTooltip.cs +++ b/osu.Desktop.VisualTests/Tests/TestCaseTooltip.cs @@ -31,11 +31,8 @@ namespace osu.Desktop.VisualTests.Tests Spacing = new Vector2(0,10), Children = new Drawable[] { - new TooltipTextContainer("Text with some tooltip"), - new TooltipTextContainer("and another one with a custom delay") - { - TooltipDelay = 1000, - }, + new TooltipTextContainer("text with a tooltip"), + new TooltipTextContainer("more text with another tooltip"), new TooltipTextbox { Text = "a box with a tooltip", @@ -56,14 +53,12 @@ namespace osu.Desktop.VisualTests.Tests }); } - private class TooltipTextContainer : Container, IHasTooltipWithCustomDelay + private class TooltipTextContainer : Container, IHasTooltip { private readonly OsuSpriteText text; public string TooltipText => text.Text; - public int TooltipDelay { get; set; } = TooltipContainer.DEFAULT_APPEAR_DELAY; - public TooltipTextContainer(string tooltipText) { AutoSizeAxes = Axes.Both; @@ -82,7 +77,7 @@ namespace osu.Desktop.VisualTests.Tests public string TooltipText => Text; } - private class TooltipSlider : OsuSliderBar, IHasDisappearingTooltip + private class TooltipSlider : OsuSliderBar, IHasTooltip { public string TooltipText => Current.Value.ToString(); diff --git a/osu.Game/Graphics/Cursor/IHasTooltip.cs b/osu.Game/Graphics/Cursor/IHasTooltip.cs index e7b6637f2a..2d166ca9dc 100644 --- a/osu.Game/Graphics/Cursor/IHasTooltip.cs +++ b/osu.Game/Graphics/Cursor/IHasTooltip.cs @@ -12,26 +12,4 @@ namespace osu.Game.Graphics.Cursor /// string TooltipText { get; } } - - /// - /// Tooltip with custom appear time - /// - public interface IHasTooltipWithCustomDelay : IHasTooltip - { - /// - /// Time until the tooltip appears (in milliseconds) - /// - int TooltipDelay { get; } - } - - /// - /// Tooltip which can decide when to disappear - /// - public interface IHasDisappearingTooltip : IHasTooltip - { - /// - /// Should the tooltip disappear? - /// - bool Disappear { get; } - } } diff --git a/osu.Game/Graphics/Cursor/TooltipContainer.cs b/osu.Game/Graphics/Cursor/TooltipContainer.cs index 562f604fca..63d914e013 100644 --- a/osu.Game/Graphics/Cursor/TooltipContainer.cs +++ b/osu.Game/Graphics/Cursor/TooltipContainer.cs @@ -23,55 +23,68 @@ namespace osu.Game.Graphics.Cursor private readonly CursorContainer cursor; private readonly Tooltip tooltip; - private ScheduledDelegate show; - private UserInputManager input; - private IHasDisappearingTooltip disappearingTooltip; - private IHasTooltip hasTooltip; + private ScheduledDelegate findTooltipTask; + private UserInputManager inputManager; - public const int DEFAULT_APPEAR_DELAY = 250; + private const int default_appear_delay = 250; + + private IHasTooltip currentlyDisplayed; + + private IMouseState lastState; public TooltipContainer(CursorContainer cursor) { this.cursor = cursor; AlwaysPresent = true; RelativeSizeAxes = Axes.Both; - Add(tooltip = new Tooltip()); + Add(tooltip = new Tooltip { Alpha = 0 }); } [BackgroundDependencyLoader] private void load(OsuColour colour, UserInputManager input) { - this.input = input; + this.inputManager = input; } protected override void Update() { - if (tooltip?.IsPresent == true) - tooltip.TooltipText = hasTooltip?.TooltipText; - else if (disappearingTooltip?.Disappear == true && show?.Completed == true) + if (tooltip.IsPresent && lastState != null) { - disappearingTooltip = null; - tooltip.TooltipText = string.Empty; + if (currentlyDisplayed != null) + tooltip.TooltipText = currentlyDisplayed.TooltipText; + + //update the position of the displayed tooltip. + tooltip.Position = new Vector2( + lastState.Position.X, + Math.Min(cursor.ActiveCursor.BoundingBox.Bottom, lastState.Position.Y + cursor.ActiveCursor.DrawHeight)); } } protected override bool OnMouseMove(InputState state) { - if (((hasTooltip as Drawable)?.Hovering != true && disappearingTooltip?.Disappear != false) || show?.Completed != true) + lastState = state.Mouse; + + if (currentlyDisplayed?.Hovering != true) { - show?.Cancel(); - tooltip.TooltipText = string.Empty; - hasTooltip = input.HoveredDrawables.OfType().FirstOrDefault(); - if (hasTooltip != null) + if (currentlyDisplayed != null) { - IHasTooltipWithCustomDelay delayedTooltip = hasTooltip as IHasTooltipWithCustomDelay; - disappearingTooltip = hasTooltip as IHasDisappearingTooltip; - show = Scheduler.AddDelayed(delegate - { - tooltip.TooltipText = hasTooltip.TooltipText; - tooltip.Position = new Vector2(state.Mouse.Position.X, Math.Min(cursor.ActiveCursor.BoundingBox.Bottom, state.Mouse.Position.Y + cursor.ActiveCursor.DrawHeight)); - }, delayedTooltip?.TooltipDelay ?? DEFAULT_APPEAR_DELAY); + tooltip.Delay(100); + tooltip.FadeOut(500, EasingTypes.OutQuint); + currentlyDisplayed = null; } + + findTooltipTask?.Cancel(); + findTooltipTask = Scheduler.AddDelayed(delegate + { + var tooltipTarget = inputManager.HoveredDrawables.OfType().FirstOrDefault(); + + if (tooltipTarget == null) return; + + tooltip.TooltipText = tooltipTarget.TooltipText; + tooltip.FadeIn(500, EasingTypes.OutQuint); + + currentlyDisplayed = tooltipTarget; + }, (1 - tooltip.Alpha) * default_appear_delay); } return base.OnMouseMove(state); @@ -87,10 +100,6 @@ namespace osu.Game.Graphics.Cursor set { text.Text = value; - if (string.IsNullOrEmpty(value) && !Hovering) - Hide(); - else - Show(); } } @@ -116,7 +125,7 @@ namespace osu.Game.Graphics.Cursor }, text = new OsuSpriteText { - Padding = new MarginPadding(3), + Padding = new MarginPadding(5), Font = @"Exo2.0-Regular", } };