1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-20 03:47:28 +08:00

Remove unnecessary interfaces and simplify tooltip logic.

This commit is contained in:
Dean Herbert 2017-04-20 14:34:29 +09:00
parent ea67b41683
commit b11315ca46
No known key found for this signature in database
GPG Key ID: 46D71BF4958ABB49
3 changed files with 42 additions and 60 deletions

View File

@ -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<int>, IHasDisappearingTooltip
private class TooltipSlider : OsuSliderBar<int>, IHasTooltip
{
public string TooltipText => Current.Value.ToString();

View File

@ -12,26 +12,4 @@ namespace osu.Game.Graphics.Cursor
/// </summary>
string TooltipText { get; }
}
/// <summary>
/// Tooltip with custom appear time
/// </summary>
public interface IHasTooltipWithCustomDelay : IHasTooltip
{
/// <summary>
/// Time until the tooltip appears (in milliseconds)
/// </summary>
int TooltipDelay { get; }
}
/// <summary>
/// Tooltip which can decide when to disappear
/// </summary>
public interface IHasDisappearingTooltip : IHasTooltip
{
/// <summary>
/// Should the tooltip disappear?
/// </summary>
bool Disappear { get; }
}
}

View File

@ -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<IHasTooltip>().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<IHasTooltip>().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",
}
};