1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-21 07:53:38 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/DrawableCarouselItem.cs

155 lines
4.5 KiB
C#
Raw Normal View History

2018-04-13 17:19:50 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
2018-10-02 11:02:47 +08:00
using osu.Framework.Input.Events;
2018-04-13 17:19:50 +08:00
using osu.Framework.MathUtils;
using osu.Game.Graphics;
2018-11-20 15:51:59 +08:00
using osuTK;
using osuTK.Graphics;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Screens.Select.Carousel
{
public abstract class DrawableCarouselItem : Container
{
public const float MAX_HEIGHT = 80;
public override bool RemoveWhenNotAlive => false;
public override bool IsPresent => base.IsPresent || Item.Visible;
public readonly CarouselItem Item;
private Container nestedContainer;
private Container borderContainer;
private Box hoverLayer;
protected override Container<Drawable> Content => nestedContainer;
protected DrawableCarouselItem(CarouselItem item)
{
Item = item;
Height = MAX_HEIGHT;
RelativeSizeAxes = Axes.X;
Alpha = 0;
}
private SampleChannel sampleHover;
[BackgroundDependencyLoader]
2018-08-31 06:04:40 +08:00
private void load(AudioManager audio, OsuColour colours)
2018-04-13 17:19:50 +08:00
{
InternalChild = borderContainer = new Container
{
RelativeSizeAxes = Axes.Both,
Masking = true,
CornerRadius = 10,
BorderColour = new Color4(221, 255, 255, 255),
Children = new Drawable[]
{
nestedContainer = new Container
{
RelativeSizeAxes = Axes.Both,
},
hoverLayer = new Box
{
RelativeSizeAxes = Axes.Both,
Alpha = 0,
Blending = BlendingMode.Additive,
},
}
};
2018-08-31 06:04:40 +08:00
sampleHover = audio.Sample.Get($@"SongSelect/song-ping-variation-{RNG.Next(1, 5)}");
2018-04-13 17:19:50 +08:00
hoverLayer.Colour = colours.Blue.Opacity(0.1f);
}
2018-10-02 11:02:47 +08:00
protected override bool OnHover(HoverEvent e)
2018-04-13 17:19:50 +08:00
{
sampleHover?.Play();
hoverLayer.FadeIn(100, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
return base.OnHover(e);
2018-04-13 17:19:50 +08:00
}
2018-10-02 11:02:47 +08:00
protected override void OnHoverLost(HoverLostEvent e)
2018-04-13 17:19:50 +08:00
{
hoverLayer.FadeOut(1000, Easing.OutQuint);
2018-10-02 11:02:47 +08:00
base.OnHoverLost(e);
2018-04-13 17:19:50 +08:00
}
public void SetMultiplicativeAlpha(float alpha) => borderContainer.Alpha = alpha;
protected override void LoadComplete()
{
base.LoadComplete();
ApplyState();
Item.Filtered.ValueChanged += _ => Schedule(ApplyState);
Item.State.ValueChanged += _ => Schedule(ApplyState);
}
protected virtual void ApplyState()
{
if (!IsLoaded) return;
switch (Item.State.Value)
{
case CarouselItemState.NotSelected:
Deselected();
break;
case CarouselItemState.Selected:
Selected();
break;
}
if (!Item.Visible)
this.FadeOut(300, Easing.OutQuint);
else
this.FadeIn(250);
}
protected virtual void Selected()
{
Item.State.Value = CarouselItemState.Selected;
borderContainer.BorderThickness = 2.5f;
borderContainer.EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Glow,
Colour = new Color4(130, 204, 255, 150),
Radius = 20,
Roundness = 10,
};
}
protected virtual void Deselected()
{
Item.State.Value = CarouselItemState.NotSelected;
borderContainer.BorderThickness = 0;
borderContainer.EdgeEffect = new EdgeEffectParameters
{
Type = EdgeEffectType.Shadow,
Offset = new Vector2(1),
Radius = 10,
Colour = Color4.Black.Opacity(100),
};
}
2018-10-02 11:02:47 +08:00
protected override bool OnClick(ClickEvent e)
2018-04-13 17:19:50 +08:00
{
Item.State.Value = CarouselItemState.Selected;
return true;
}
}
}