1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-29 02:45:37 +08:00
osu-lazer/osu.Game/Beatmaps/Drawables/Cards/Buttons/BeatmapCardIconButton.cs

135 lines
3.7 KiB
C#
Raw Normal View History

2023-06-23 00:37:25 +08:00
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2021-10-18 03:34:31 +08:00
// See the LICENCE file in the repository root for full licence text.
using osu.Framework.Allocation;
using osu.Framework.Extensions.Color4Extensions;
2021-10-18 03:34:31 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2021-10-18 03:34:31 +08:00
using osu.Framework.Graphics.Sprites;
using osu.Framework.Input.Events;
2021-10-18 03:34:31 +08:00
using osu.Game.Graphics.Containers;
using osu.Game.Overlays;
using osuTK;
using osuTK.Graphics;
2021-10-18 03:34:31 +08:00
namespace osu.Game.Beatmaps.Drawables.Cards.Buttons
{
2022-11-24 13:32:20 +08:00
public abstract partial class BeatmapCardIconButton : OsuClickableContainer
2021-10-18 03:34:31 +08:00
{
private Colour4 idleColour;
public Colour4 IdleColour
{
get => idleColour;
set
{
idleColour = value;
if (IsLoaded)
updateState();
}
}
private Colour4 hoverColour;
public Colour4 HoverColour
{
get => hoverColour;
set
{
hoverColour = value;
if (IsLoaded)
updateState();
}
}
2021-10-18 03:34:31 +08:00
private float iconSize;
2021-10-18 03:34:31 +08:00
public float IconSize
2021-10-18 03:34:31 +08:00
{
get => iconSize;
2021-10-18 03:34:31 +08:00
set
{
iconSize = value;
Icon.Size = new Vector2(iconSize);
2021-10-18 03:34:31 +08:00
}
}
protected readonly SpriteIcon Icon;
protected override Container<Drawable> Content => content;
2021-11-19 14:27:51 +08:00
private readonly Container content;
private readonly Box hover;
2021-11-19 14:27:51 +08:00
2021-10-18 03:34:31 +08:00
protected BeatmapCardIconButton()
{
2021-11-19 14:27:51 +08:00
Origin = Anchor.Centre;
Anchor = Anchor.Centre;
base.Content.Add(content = new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true,
CornerRadius = BeatmapCard.CORNER_RADIUS,
Scale = new Vector2(0.8f),
2021-11-19 14:27:51 +08:00
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Children = new Drawable[]
{
hover = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.White.Opacity(0.1f),
Blending = BlendingParameters.Additive,
},
Icon = new SpriteIcon
{
Origin = Anchor.Centre,
Anchor = Anchor.Centre,
Scale = new Vector2(1.2f),
},
}
});
2021-10-18 03:34:31 +08:00
IconSize = 12;
2021-10-18 03:34:31 +08:00
}
[BackgroundDependencyLoader]
private void load(OverlayColourProvider colourProvider)
{
IdleColour = colourProvider.Light1;
HoverColour = colourProvider.Content1;
}
protected override void LoadComplete()
{
base.LoadComplete();
Enabled.BindValueChanged(_ => updateState(), true);
FinishTransforms(true);
}
2021-10-18 03:34:31 +08:00
protected override bool OnHover(HoverEvent e)
{
updateState();
return base.OnHover(e);
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
updateState();
}
private void updateState()
{
2021-11-19 14:27:51 +08:00
bool isHovered = IsHovered && Enabled.Value;
hover.FadeTo(isHovered ? 1f : 0f, 500, Easing.OutQuint);
content.ScaleTo(isHovered ? 1 : 0.8f, 500, Easing.OutQuint);
Icon.FadeColour(isHovered ? HoverColour : IdleColour, BeatmapCard.TRANSITION_DURATION, Easing.OutQuint);
2021-10-18 03:34:31 +08:00
}
}
}