// Copyright (c) 2007-2018 ppy Pty Ltd . // 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.Configuration; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Input; using osu.Game.Audio; using osu.Game.Beatmaps; using osu.Game.Graphics; using osu.Game.Graphics.UserInterface; using OpenTK.Graphics; namespace osu.Game.Overlays.Direct { public class PlayButton : Container { public readonly Bindable Playing = new Bindable(); public Track Preview { get; private set; } private BeatmapSetInfo beatmapSet; public BeatmapSetInfo BeatmapSet { get { return beatmapSet; } set { if (value == beatmapSet) return; beatmapSet = value; Playing.Value = false; Preview = null; } } private PreviewTrackManager previewTrackManager; private Color4 hoverColour; private readonly SpriteIcon icon; private readonly LoadingAnimation loadingAnimation; private const float transition_duration = 500; private bool loading { set { if (value) { loadingAnimation.Show(); icon.FadeOut(transition_duration * 5, Easing.OutQuint); } else { loadingAnimation.Hide(); icon.FadeIn(transition_duration, Easing.OutQuint); } } } public PlayButton(BeatmapSetInfo setInfo = null) { BeatmapSet = setInfo; AddRange(new Drawable[] { icon = new SpriteIcon { Anchor = Anchor.Centre, Origin = Anchor.Centre, FillMode = FillMode.Fit, RelativeSizeAxes = Axes.Both, Icon = FontAwesome.fa_play, }, loadingAnimation = new LoadingAnimation(), }); Playing.ValueChanged += playingStateChanged; } [BackgroundDependencyLoader] private void load(OsuColour colour, PreviewTrackManager previewTrackManager) { hoverColour = colour.Yellow; this.previewTrackManager = previewTrackManager; previewTrackManager.PlaybackStopped += () => { if (Preview == previewTrackManager.CurrentTrack) Playing.Value = false; }; } protected override bool OnClick(InputState state) { Playing.Value = !Playing.Value; return true; } protected override bool OnHover(InputState state) { icon.FadeColour(hoverColour, 120, Easing.InOutQuint); return base.OnHover(state); } protected override void OnHoverLost(InputState state) { if (!Playing.Value) icon.FadeColour(Color4.White, 120, Easing.InOutQuint); base.OnHoverLost(state); } private void playingStateChanged(bool playing) { if (playing && BeatmapSet == null) { Playing.Value = false; return; } icon.Icon = playing ? FontAwesome.fa_pause : FontAwesome.fa_play; icon.FadeColour(playing || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint); if (playing) { if (Preview == null) { loading = true; Preview = previewTrackManager.Get(beatmapSet); loading = false; } previewTrackManager.Play(Preview); } else { previewTrackManager.Stop(); loading = false; } } protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); Playing.Value = false; } } }