// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using System; using System.Collections.Generic; using System.Linq; using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Input.Bindings; using osu.Framework.MathUtils; using osu.Framework.Threading; using osu.Game.Beatmaps; using osu.Game.Input.Bindings; using osu.Game.Overlays.OSD; using osu.Game.Rulesets.Mods; namespace osu.Game.Overlays { /// /// Handles playback of the global music track. /// public class MusicController : Component, IKeyBindingHandler { [Resolved] private BeatmapManager beatmaps { get; set; } public IBindableList BeatmapSets => beatmapSets; private readonly BindableList beatmapSets = new BindableList(); public bool IsUserPaused { get; private set; } /// /// Fired when the global has changed. /// Includes direction information for display purposes. /// public event Action TrackChanged; [Resolved] private IBindable beatmap { get; set; } [Resolved] private IBindable> mods { get; set; } [Resolved(canBeNull: true)] private OnScreenDisplay onScreenDisplay { get; set; } [BackgroundDependencyLoader] private void load() { beatmapSets.AddRange(beatmaps.GetAllUsableBeatmapSets().OrderBy(_ => RNG.Next())); beatmaps.ItemAdded += handleBeatmapAdded; beatmaps.ItemRemoved += handleBeatmapRemoved; } protected override void LoadComplete() { beatmap.BindValueChanged(beatmapChanged, true); mods.BindValueChanged(_ => ResetTrackAdjustments(), true); base.LoadComplete(); } /// /// Change the position of a in the current playlist. /// /// The beatmap to move. /// The new position. public void ChangeBeatmapSetPosition(BeatmapSetInfo beatmapSetInfo, int index) { beatmapSets.Remove(beatmapSetInfo); beatmapSets.Insert(index, beatmapSetInfo); } /// /// Returns whether the current beatmap track is playing. /// public bool IsPlaying => beatmap.Value.Track.IsRunning; private void handleBeatmapAdded(BeatmapSetInfo set) => Schedule(() => beatmapSets.Add(set)); private void handleBeatmapRemoved(BeatmapSetInfo set) => Schedule(() => beatmapSets.RemoveAll(s => s.ID == set.ID)); private ScheduledDelegate seekDelegate; public void SeekTo(double position) { seekDelegate?.Cancel(); seekDelegate = Schedule(() => { if (!beatmap.Disabled) current?.Track.Seek(position); }); } /// /// Start playing the current track (if not already playing). /// public void Play() { if (!IsPlaying) TogglePause(); } /// /// Toggle pause / play. /// /// Whether the operation was successful. public bool TogglePause() { var track = current?.Track; if (track == null) { if (beatmap.Disabled) return false; next(true); return true; } if (track.IsRunning) { IsUserPaused = true; track.Stop(); } else { track.Start(); IsUserPaused = false; } return true; } /// /// Play the previous track. /// /// Whether the operation was successful. public bool PrevTrack() { queuedDirection = TrackChangeDirection.Prev; var playable = BeatmapSets.TakeWhile(i => i.ID != current.BeatmapSetInfo.ID).LastOrDefault() ?? BeatmapSets.LastOrDefault(); if (playable != null) { if (beatmap is Bindable working) working.Value = beatmaps.GetWorkingBeatmap(playable.Beatmaps.First(), beatmap.Value); beatmap.Value.Track.Restart(); return true; } return false; } /// /// Play the next random or playlist track. /// /// Whether the operation was successful. public bool NextTrack() => next(); private bool next(bool instant = false) { if (!instant) queuedDirection = TrackChangeDirection.Next; var playable = BeatmapSets.SkipWhile(i => i.ID != current.BeatmapSetInfo.ID).Skip(1).FirstOrDefault() ?? BeatmapSets.FirstOrDefault(); if (playable != null) { if (beatmap is Bindable working) working.Value = beatmaps.GetWorkingBeatmap(playable.Beatmaps.First(), beatmap.Value); beatmap.Value.Track.Restart(); return true; } return false; } private WorkingBeatmap current; private TrackChangeDirection? queuedDirection; private void beatmapChanged(ValueChangedEvent beatmap) { TrackChangeDirection direction = TrackChangeDirection.None; if (current != null) { bool audioEquals = beatmap.NewValue?.BeatmapInfo?.AudioEquals(current.BeatmapInfo) ?? false; if (audioEquals) direction = TrackChangeDirection.None; else if (queuedDirection.HasValue) { direction = queuedDirection.Value; queuedDirection = null; } else { //figure out the best direction based on order in playlist. var last = BeatmapSets.TakeWhile(b => b.ID != current.BeatmapSetInfo?.ID).Count(); var next = beatmap.NewValue == null ? -1 : BeatmapSets.TakeWhile(b => b.ID != beatmap.NewValue.BeatmapSetInfo?.ID).Count(); direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next; } } current = beatmap.NewValue; TrackChanged?.Invoke(current, direction); ResetTrackAdjustments(); queuedDirection = null; } public void ResetTrackAdjustments() { var track = current?.Track; if (track == null) return; track.ResetSpeedAdjustments(); foreach (var mod in mods.Value.OfType()) mod.ApplyToClock(track); } protected override void Dispose(bool isDisposing) { base.Dispose(isDisposing); if (beatmaps != null) { beatmaps.ItemAdded -= handleBeatmapAdded; beatmaps.ItemRemoved -= handleBeatmapRemoved; } } public bool OnPressed(GlobalAction action) { if (beatmap.Disabled) return false; switch (action) { case GlobalAction.MusicPlay: if (TogglePause()) onScreenDisplay?.Display(new MusicControllerToast(IsPlaying ? "Play track" : "Pause track")); return true; case GlobalAction.MusicNext: if (NextTrack()) onScreenDisplay?.Display(new MusicControllerToast("Next track")); return true; case GlobalAction.MusicPrev: if (PrevTrack()) onScreenDisplay?.Display(new MusicControllerToast("Previous track")); return true; } return false; } public bool OnReleased(GlobalAction action) => false; public class MusicControllerToast : Toast { public MusicControllerToast(string action) : base("Music Playback", action, string.Empty) { } } } public enum TrackChangeDirection { None, Next, Prev } }