1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-06 21:57:19 +08:00
osu-lazer/osu.Game/Graphics/Containers/OsuHoverContainer.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

87 lines
2.3 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 18:19:50 +09:00
2022-06-17 16:37:17 +09:00
#nullable disable
2017-08-08 23:11:46 +02:00
using osu.Framework.Allocation;
using osu.Framework.Extensions.IEnumerableExtensions;
2017-08-08 23:11:46 +02:00
using osu.Framework.Graphics;
2018-10-02 12:02:47 +09:00
using osu.Framework.Input.Events;
2019-02-05 14:48:35 +03:00
using osuTK.Graphics;
using System.Collections.Generic;
2021-06-18 16:08:14 +09:00
using osu.Game.Graphics.UserInterface;
2018-04-13 18:19:50 +09:00
2017-08-08 23:11:46 +02:00
namespace osu.Game.Graphics.Containers
{
public partial class OsuHoverContainer : OsuClickableContainer
{
2019-04-03 15:20:38 +09:00
protected const float FADE_DURATION = 500;
protected Color4 HoverColour;
2018-04-13 18:19:50 +09:00
protected Color4 IdleColour = Color4.White;
2018-04-13 18:19:50 +09:00
protected virtual IEnumerable<Drawable> EffectTargets => new[] { Content };
2018-04-13 18:19:50 +09:00
2021-06-18 16:08:14 +09:00
public OsuHoverContainer(HoverSampleSet sampleSet = HoverSampleSet.Default)
: base(sampleSet)
2019-05-21 15:02:31 +09:00
{
Enabled.ValueChanged += e =>
{
2019-06-07 11:45:58 +09:00
if (isHovered)
{
if (e.NewValue)
fadeIn();
else
fadeOut();
}
2019-05-21 15:02:31 +09:00
};
}
private bool isHovered;
2018-10-02 12:02:47 +09:00
protected override bool OnHover(HoverEvent e)
2017-08-08 23:11:46 +02:00
{
2019-06-07 11:45:58 +09:00
if (isHovered)
return false;
isHovered = true;
2019-05-21 15:02:31 +09:00
if (!Enabled.Value)
return false;
fadeIn();
2019-05-21 15:02:31 +09:00
2018-10-02 12:02:47 +09:00
return base.OnHover(e);
2017-08-08 23:11:46 +02:00
}
2018-04-13 18:19:50 +09:00
2018-10-02 12:02:47 +09:00
protected override void OnHoverLost(HoverLostEvent e)
2019-05-21 15:02:31 +09:00
{
2019-05-30 23:07:04 +03:00
if (!isHovered)
return;
2019-05-21 15:02:31 +09:00
isHovered = false;
2019-06-07 11:45:58 +09:00
fadeOut();
base.OnHoverLost(e);
2019-05-21 15:02:31 +09:00
}
2017-08-08 23:11:46 +02:00
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
2019-03-06 16:30:56 +09:00
if (HoverColour == default)
2018-12-22 16:51:24 +01:00
HoverColour = colours.Yellow;
}
2018-04-13 18:19:50 +09:00
protected override void LoadComplete()
{
base.LoadComplete();
EffectTargets.ForEach(d => d.FadeColour(IdleColour));
2017-08-08 23:11:46 +02:00
}
2019-06-07 14:11:21 +09:00
private void fadeIn() => EffectTargets.ForEach(d => d.FadeColour(HoverColour, FADE_DURATION, Easing.OutQuint));
2019-06-07 11:45:58 +09:00
2019-06-07 14:11:21 +09:00
private void fadeOut() => EffectTargets.ForEach(d => d.FadeColour(IdleColour, FADE_DURATION, Easing.OutQuint));
2017-08-08 23:11:46 +02:00
}
}