1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 20:37:52 +08:00
osu-lazer/osu.Game/Screens/Select/Carousel/DrawableCarouselItem.cs

155 lines
4.6 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2017-12-12 16:48:38 +08:00
using osu.Framework.Allocation;
2017-11-26 03:16:36 +08:00
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2017-12-12 16:48:38 +08:00
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2017-12-12 16:48:38 +08:00
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
2017-11-26 03:16:36 +08:00
using osu.Framework.MathUtils;
using osu.Game.Graphics;
2017-12-12 16:48:38 +08:00
using OpenTK;
using OpenTK.Graphics;
2017-12-12 16:48:38 +08:00
namespace osu.Game.Screens.Select.Carousel
{
2017-12-12 16:48:38 +08:00
public abstract class DrawableCarouselItem : Container
{
public const float MAX_HEIGHT = 80;
2016-11-25 17:14:56 +08:00
public override bool RemoveWhenNotAlive => false;
2017-12-12 16:48:38 +08:00
public override bool IsPresent => base.IsPresent || Item.Visible;
public readonly CarouselItem Item;
2017-12-18 06:58:48 +08:00
private Container nestedContainer;
private Container borderContainer;
2017-12-18 06:58:48 +08:00
private Box hoverLayer;
2016-11-23 10:59:50 +08:00
protected override Container<Drawable> Content => nestedContainer;
2017-12-12 16:48:38 +08:00
protected DrawableCarouselItem(CarouselItem item)
{
2017-12-12 16:48:38 +08:00
Item = item;
Height = MAX_HEIGHT;
RelativeSizeAxes = Axes.X;
2017-12-13 18:56:16 +08:00
Alpha = 0;
2017-12-18 06:58:48 +08:00
}
2017-12-13 18:56:16 +08:00
2017-12-18 06:58:48 +08:00
private SampleChannel sampleHover;
[BackgroundDependencyLoader]
private void load(AudioManager audio, OsuColour colours)
{
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,
2017-11-27 09:01:44 +08:00
Blending = BlendingMode.Additive,
},
}
2017-12-18 06:58:48 +08:00
};
2017-11-26 03:16:36 +08:00
sampleHover = audio.Sample.Get($@"SongSelect/song-ping-variation-{RNG.Next(1, 5)}");
hoverLayer.Colour = colours.Blue.Opacity(0.1f);
}
2017-11-26 03:16:36 +08:00
protected override bool OnHover(InputState state)
{
sampleHover?.Play();
hoverLayer.FadeIn(100, Easing.OutQuint);
2017-11-26 03:16:36 +08:00
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
2017-11-26 03:16:36 +08:00
{
hoverLayer.FadeOut(1000, Easing.OutQuint);
base.OnHoverLost(state);
2017-11-26 03:16:36 +08:00
}
2017-12-12 16:48:38 +08:00
public void SetMultiplicativeAlpha(float alpha) => borderContainer.Alpha = alpha;
protected override void LoadComplete()
{
base.LoadComplete();
2017-12-18 06:58:48 +08:00
ApplyState();
2017-12-18 06:58:48 +08:00
Item.Filtered.ValueChanged += _ => Schedule(ApplyState);
Item.State.ValueChanged += _ => Schedule(ApplyState);
}
2017-12-12 16:48:38 +08:00
protected virtual void ApplyState()
{
if (!IsLoaded) return;
2017-12-12 16:48:38 +08:00
switch (Item.State.Value)
{
2017-12-12 16:48:38 +08:00
case CarouselItemState.NotSelected:
Deselected();
break;
2017-12-12 16:48:38 +08:00
case CarouselItemState.Selected:
Selected();
break;
}
2017-12-12 16:48:38 +08:00
if (!Item.Visible)
2017-07-23 02:50:25 +08:00
this.FadeOut(300, Easing.OutQuint);
else
this.FadeIn(250);
}
protected virtual void Selected()
{
2017-12-12 16:48:38 +08:00
Item.State.Value = CarouselItemState.Selected;
borderContainer.BorderThickness = 2.5f;
borderContainer.EdgeEffect = new EdgeEffectParameters
2016-11-02 05:57:11 +08:00
{
Type = EdgeEffectType.Glow,
Colour = new Color4(130, 204, 255, 150),
2016-11-02 05:57:11 +08:00
Radius = 20,
Roundness = 10,
};
}
protected virtual void Deselected()
{
2017-12-12 16:48:38 +08:00
Item.State.Value = CarouselItemState.NotSelected;
borderContainer.BorderThickness = 0;
borderContainer.EdgeEffect = new EdgeEffectParameters
2016-11-05 19:00:14 +08:00
{
Type = EdgeEffectType.Shadow,
Offset = new Vector2(1),
Radius = 10,
Colour = Color4.Black.Opacity(100),
2016-11-05 19:00:14 +08:00
};
}
2016-11-02 05:57:11 +08:00
protected override bool OnClick(InputState state)
{
2017-12-12 16:48:38 +08:00
Item.State.Value = CarouselItemState.Selected;
2016-11-02 05:57:11 +08:00
return true;
}
}
}