1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-06 06:57:39 +08:00
osu-lazer/osu.Game/Overlays/BeatmapSet/PreviewButton.cs

109 lines
3.3 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-09-13 08:24:43 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using osu.Framework.Allocation;
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;
2017-09-25 17:26:27 +08:00
using OpenTK;
using OpenTK.Graphics;
using osu.Game.Overlays.Direct;
using osu.Framework.Configuration;
2017-09-13 08:24:43 +08:00
2017-09-25 17:26:27 +08:00
namespace osu.Game.Overlays.BeatmapSet
2017-09-13 08:24:43 +08:00
{
public class PreviewButton : OsuClickableContainer
{
2017-09-16 00:47:03 +08:00
private const float transition_duration = 500;
2017-09-13 08:24:43 +08:00
private readonly Box bg, progress;
private readonly PlayButton playButton;
2017-09-13 08:24:43 +08:00
private Track preview => playButton.Preview;
public Bindable<bool> Playing => playButton.Playing;
2017-09-16 00:47:03 +08:00
public BeatmapSetInfo BeatmapSet
{
get { return playButton.BeatmapSet; }
set { playButton.BeatmapSet = value; }
}
public PreviewButton()
2017-09-13 08:24:43 +08:00
{
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,
},
},
2017-10-07 03:06:37 +08:00
playButton = new PlayButton
2017-09-13 08:24:43 +08:00
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Size = new Vector2(18),
2017-09-16 00:47:03 +08:00
},
2017-09-13 08:24:43 +08:00
};
Action = () => Playing.Value = !Playing.Value;
Playing.ValueChanged += newValue => progress.FadeTo(newValue ? 1 : 0, 100);
2017-09-13 08:24:43 +08:00
}
[BackgroundDependencyLoader]
2017-09-16 00:47:03 +08:00
private void load(OsuColour colours)
2017-09-13 08:24:43 +08:00
{
progress.Colour = colours.Yellow;
}
protected override void Update()
{
base.Update();
if (Playing.Value && preview != null)
2017-09-13 08:24:43 +08:00
{
2018-01-18 03:35:28 +08:00
// prevent negative (potential infinite) width if a track without length was loaded
2018-01-18 22:53:53 +08:00
progress.Width = preview.Length > 0 ? (float)(preview.CurrentTime / preview.Length) : 0f;
2017-09-13 08:24:43 +08:00
}
}
protected override void Dispose(bool isDisposing)
{
Playing.Value = false;
2017-09-13 08:24:43 +08:00
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);
}
}
}