osu/osu.Game/Graphics/Containers/OsuHoverContainer.cs

72 lines
1.9 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 09:19:50 +00:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics;
2018-10-02 03:02:47 +00:00
using osu.Framework.Input.Events;
2019-02-05 11:48:35 +00:00
using osuTK.Graphics;
using System.Collections.Generic;
2018-04-13 09:19:50 +00:00
namespace osu.Game.Graphics.Containers
{
public class OsuHoverContainer : OsuClickableContainer
{
2019-04-03 06:20:38 +00:00
protected const float FADE_DURATION = 500;
2018-04-13 09:19:50 +00:00
protected Color4 HoverColour;
protected Color4 IdleColour = Color4.White;
protected virtual IEnumerable<Drawable> EffectTargets => new[] { Content };
2019-05-21 06:02:31 +00:00
public OsuHoverContainer()
{
Enabled.ValueChanged += e =>
{
if (!e.NewValue) unhover();
};
}
private bool isHovered;
2018-10-02 03:02:47 +00:00
protected override bool OnHover(HoverEvent e)
2018-04-13 09:19:50 +00:00
{
2019-05-21 06:02:31 +00:00
if (!Enabled.Value)
return false;
EffectTargets.ForEach(d => d.FadeColour(HoverColour, FADE_DURATION, Easing.OutQuint));
isHovered = true;
2018-10-02 03:02:47 +00:00
return base.OnHover(e);
2018-04-13 09:19:50 +00:00
}
2018-10-02 03:02:47 +00:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 09:19:50 +00:00
{
2019-05-21 06:02:31 +00:00
unhover();
2018-10-02 03:02:47 +00:00
base.OnHoverLost(e);
2018-04-13 09:19:50 +00:00
}
2019-05-21 06:02:31 +00:00
private void unhover()
{
if (!isHovered) return;
isHovered = false;
EffectTargets.ForEach(d => d.FadeColour(IdleColour, FADE_DURATION, Easing.OutQuint));
}
2018-04-13 09:19:50 +00:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-03-06 07:30:56 +00:00
if (HoverColour == default)
2018-12-22 15:51:24 +00:00
HoverColour = colours.Yellow;
2018-04-13 09:19:50 +00:00
}
protected override void LoadComplete()
{
base.LoadComplete();
EffectTargets.ForEach(d => d.FadeColour(IdleColour));
}
}
}