1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:17:23 +08:00
osu-lazer/osu.Game/Graphics/Cursor/TooltipContainer.cs

158 lines
5.0 KiB
C#
Raw Normal View History

2017-04-19 21:01:05 +08:00
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Cursor;
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input;
using osu.Framework.Threading;
using osu.Game.Graphics.Sprites;
using System.Linq;
namespace osu.Game.Graphics.Cursor
{
public class TooltipContainer : Container
{
private readonly CursorContainer cursor;
private readonly Tooltip tooltip;
private ScheduledDelegate findTooltipTask;
private UserInputManager inputManager;
2017-04-19 21:01:05 +08:00
2017-04-20 15:28:55 +08:00
private const int default_appear_delay = 220;
private IHasTooltip currentlyDisplayed;
2017-04-19 21:01:05 +08:00
public TooltipContainer(CursorContainer cursor)
{
this.cursor = cursor;
AlwaysPresent = true;
2017-04-20 06:42:40 +08:00
RelativeSizeAxes = Axes.Both;
Add(tooltip = new Tooltip { Alpha = 0 });
2017-04-19 21:01:05 +08:00
}
[BackgroundDependencyLoader]
2017-04-20 14:29:34 +08:00
private void load(UserInputManager input)
2017-04-19 21:01:05 +08:00
{
2017-04-20 14:29:34 +08:00
inputManager = input;
2017-04-19 21:01:05 +08:00
}
protected override void Update()
{
if (tooltip.IsPresent)
2017-04-19 21:01:05 +08:00
{
if (currentlyDisplayed != null)
tooltip.TooltipText = currentlyDisplayed.TooltipText;
//update the position of the displayed tooltip.
tooltip.Position = ToLocalSpace(cursor.ActiveCursor.ScreenSpaceDrawQuad.Centre) + new Vector2(10);
2017-04-19 21:01:05 +08:00
}
}
2017-04-20 17:17:12 +08:00
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
{
updateTooltipState(state);
return base.OnMouseUp(state, args);
}
2017-04-19 21:01:05 +08:00
protected override bool OnMouseMove(InputState state)
{
2017-04-20 17:17:12 +08:00
updateTooltipState(state);
return base.OnMouseMove(state);
}
2017-04-20 17:17:12 +08:00
private void updateTooltipState(InputState state)
{
if (currentlyDisplayed?.Hovering != true)
2017-04-20 06:42:40 +08:00
{
if (currentlyDisplayed != null && !state.Mouse.HasMainButtonPressed)
2017-04-20 06:42:40 +08:00
{
2017-04-20 15:05:25 +08:00
tooltip.Delay(150);
tooltip.FadeOut(500, EasingTypes.OutQuint);
currentlyDisplayed = null;
2017-04-20 06:42:40 +08:00
}
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);
2017-04-20 06:42:40 +08:00
}
2017-04-19 21:01:05 +08:00
}
public class Tooltip : Container
{
2017-04-20 15:05:25 +08:00
private readonly Box background;
2017-04-19 21:01:05 +08:00
private readonly OsuSpriteText text;
public string TooltipText
{
set
{
2017-04-20 15:05:25 +08:00
if (value == text.Text) return;
2017-04-19 21:01:05 +08:00
text.Text = value;
2017-04-20 15:05:25 +08:00
if (Alpha > 0)
{
AutoSizeDuration = 250;
background.FlashColour(OsuColour.Gray(0.4f), 1000, EasingTypes.OutQuint);
}
else
AutoSizeDuration = 0;
2017-04-19 21:01:05 +08:00
}
}
2017-04-20 06:42:40 +08:00
public override bool HandleInput => false;
2017-04-20 15:05:25 +08:00
private const float text_size = 16;
2017-04-19 21:01:05 +08:00
public Tooltip()
{
2017-04-20 15:05:25 +08:00
AutoSizeEasing = EasingTypes.OutQuint;
2017-04-19 21:01:05 +08:00
AutoSizeAxes = Axes.Both;
2017-04-20 15:05:25 +08:00
2017-04-19 21:01:05 +08:00
CornerRadius = 5;
Masking = true;
EdgeEffect = new EdgeEffect
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(40),
Radius = 5,
};
Children = new Drawable[]
{
2017-04-20 15:05:25 +08:00
background = new Box
2017-04-19 21:01:05 +08:00
{
2017-04-20 15:29:05 +08:00
RelativeSizeAxes = Axes.Both,
Alpha = 0.9f,
2017-04-19 21:01:05 +08:00
},
text = new OsuSpriteText
{
2017-04-20 15:05:25 +08:00
TextSize = text_size,
Padding = new MarginPadding(5),
2017-04-19 21:01:05 +08:00
Font = @"Exo2.0-Regular",
}
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colour)
{
2017-04-20 15:05:25 +08:00
background.Colour = colour.Gray3;
2017-04-19 21:01:05 +08:00
}
}
}
}