1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 20:07:26 +08:00
osu-lazer/osu.Game/Overlays/OnlineBeatmapSet/PreviewButton.cs

158 lines
4.4 KiB
C#

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
using OpenTK.Graphics;
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Extensions.Color4Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Framework.Input;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
namespace osu.Game.Overlays.OnlineBeatmapSet
{
public class PreviewButton : OsuClickableContainer
{
private readonly Box bg, progress;
private readonly SpriteIcon icon;
private AudioManager audio;
private Track preview;
private BeatmapSetInfo beatmapSet;
public BeatmapSetInfo BeatmapSet
{
get { return beatmapSet; }
set
{
if (value == beatmapSet) return;
beatmapSet = value;
Playing = false;
preview = null;
loadPreview();
}
}
private bool playing;
public bool Playing
{
get { return playing; }
set
{
if (value == playing) return;
playing = value;
if (progress == null) return;
if (Playing)
{
icon.Icon = FontAwesome.fa_stop;
progress.FadeIn(100);
loadPreview();
preview.Start();
}
else
{
icon.Icon = FontAwesome.fa_play;
progress.FadeOut(100);
preview.Stop();
}
}
}
public PreviewButton()
{
Height = 42;
Children = new Drawable[]
{
bg = new Box
{
RelativeSizeAxes = Axes.Both,
Colour = Color4.Black.Opacity(0.25f),
},
new Container
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
Height = 3,
Child = progress = new Box
{
RelativeSizeAxes = Axes.Both,
Width = 0f,
Alpha = 0f,
},
},
icon = new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Icon = FontAwesome.fa_play,
Size = new Vector2(18),
Shadow = false,
},
};
Action = () => Playing = !Playing;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours, AudioManager audio)
{
this.audio = audio;
progress.Colour = colours.Yellow;
}
protected override void Update()
{
base.Update();
if (Playing)
{
progress.Width = (float)(preview.CurrentTime / preview.Length);
if (preview.HasCompleted) Playing = false;
}
}
protected override void Dispose(bool isDisposing)
{
Playing = false;
base.Dispose(isDisposing);
}
protected override bool OnHover(InputState state)
{
bg.FadeColour(Color4.Black.Opacity(0.5f), 100);
return base.OnHover(state);
}
protected override void OnHoverLost(InputState state)
{
bg.FadeColour(Color4.Black.Opacity(0.25f), 100);
base.OnHoverLost(state);
}
private void loadPreview()
{
if (preview == null || (preview?.HasCompleted ?? true) && BeatmapSet != null)
{
preview = audio.Track.Get(BeatmapSet.OnlineInfo.Preview);
preview.Volume.Value = 0.5;
}
else
{
preview.Seek(0);
}
}
}
}