osu/osu.Game/Graphics/Cursor/TooltipContainer.cs

159 lines
5.0 KiB
C#
Raw Normal View History

2017-04-19 13:01:05 +00: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.Primitives;
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 13:01:05 +00:00
2017-04-20 07:28:55 +00:00
private const int default_appear_delay = 220;
private IHasTooltip currentlyDisplayed;
2017-04-19 13:01:05 +00:00
public TooltipContainer(CursorContainer cursor)
{
this.cursor = cursor;
AlwaysPresent = true;
2017-04-19 22:42:40 +00:00
RelativeSizeAxes = Axes.Both;
Add(tooltip = new Tooltip { Alpha = 0 });
2017-04-19 13:01:05 +00:00
}
[BackgroundDependencyLoader]
2017-04-20 06:29:34 +00:00
private void load(UserInputManager input)
2017-04-19 13:01:05 +00:00
{
2017-04-20 06:29:34 +00:00
inputManager = input;
2017-04-19 13:01:05 +00:00
}
protected override void Update()
{
if (tooltip.IsPresent)
2017-04-19 13:01:05 +00: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 13:01:05 +00:00
}
}
2017-04-20 09:17:12 +00:00
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
{
updateTooltipState(state);
return base.OnMouseUp(state, args);
}
2017-04-19 13:01:05 +00:00
protected override bool OnMouseMove(InputState state)
{
2017-04-20 09:17:12 +00:00
updateTooltipState(state);
return base.OnMouseMove(state);
}
2017-04-20 09:17:12 +00:00
private void updateTooltipState(InputState state)
{
if (currentlyDisplayed?.Hovering != true)
2017-04-19 22:42:40 +00:00
{
if (currentlyDisplayed != null && !state.Mouse.HasMainButtonPressed)
2017-04-19 22:42:40 +00:00
{
2017-04-20 07:05:25 +00:00
tooltip.Delay(150);
tooltip.FadeOut(500, EasingTypes.OutQuint);
currentlyDisplayed = null;
2017-04-19 22:42:40 +00: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-19 22:42:40 +00:00
}
2017-04-19 13:01:05 +00:00
}
public class Tooltip : Container
{
2017-04-20 07:05:25 +00:00
private readonly Box background;
2017-04-19 13:01:05 +00:00
private readonly OsuSpriteText text;
public string TooltipText
{
set
{
2017-04-20 07:05:25 +00:00
if (value == text.Text) return;
2017-04-19 13:01:05 +00:00
text.Text = value;
2017-04-20 07:05:25 +00:00
if (Alpha > 0)
{
AutoSizeDuration = 250;
background.FlashColour(OsuColour.Gray(0.4f), 1000, EasingTypes.OutQuint);
}
else
AutoSizeDuration = 0;
2017-04-19 13:01:05 +00:00
}
}
2017-04-19 22:42:40 +00:00
public override bool HandleInput => false;
2017-04-20 07:05:25 +00:00
private const float text_size = 16;
2017-04-19 13:01:05 +00:00
public Tooltip()
{
2017-04-20 07:05:25 +00:00
AutoSizeEasing = EasingTypes.OutQuint;
2017-04-19 13:01:05 +00:00
AutoSizeAxes = Axes.Both;
2017-04-20 07:05:25 +00:00
2017-04-19 13:01:05 +00:00
CornerRadius = 5;
Masking = true;
EdgeEffect = new EdgeEffect
{
Type = EdgeEffectType.Shadow,
Colour = Color4.Black.Opacity(40),
Radius = 5,
};
Children = new Drawable[]
{
2017-04-20 07:05:25 +00:00
background = new Box
2017-04-19 13:01:05 +00:00
{
2017-04-20 07:29:05 +00:00
RelativeSizeAxes = Axes.Both,
Alpha = 0.9f,
2017-04-19 13:01:05 +00:00
},
text = new OsuSpriteText
{
2017-04-20 07:05:25 +00:00
TextSize = text_size,
Padding = new MarginPadding(5),
2017-04-19 13:01:05 +00:00
Font = @"Exo2.0-Regular",
}
};
}
[BackgroundDependencyLoader]
private void load(OsuColour colour)
{
2017-04-20 07:05:25 +00:00
background.Colour = colour.Gray3;
2017-04-19 13:01:05 +00:00
}
}
}
}