1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-22 02:07:34 +08:00
osu-lazer/osu.Game/Overlays/MusicController.cs

287 lines
8.7 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
2018-04-13 17:19:50 +08:00
using System;
using System.Collections.Generic;
2018-04-13 17:19:50 +08:00
using System.Linq;
using osu.Framework.Allocation;
2019-02-21 18:04:31 +08:00
using osu.Framework.Bindables;
2018-04-13 17:19:50 +08:00
using osu.Framework.Graphics;
2019-08-13 13:38:49 +08:00
using osu.Framework.Input.Bindings;
2018-04-13 17:19:50 +08:00
using osu.Framework.Threading;
using osu.Game.Beatmaps;
2019-08-13 13:38:49 +08:00
using osu.Game.Input.Bindings;
using osu.Game.Overlays.OSD;
using osu.Game.Rulesets.Mods;
2018-04-13 17:19:50 +08:00
namespace osu.Game.Overlays
{
/// <summary>
/// Handles playback of the global music track.
/// </summary>
2019-08-13 13:38:49 +08:00
public class MusicController : Component, IKeyBindingHandler<GlobalAction>
2018-04-13 17:19:50 +08:00
{
[Resolved]
private BeatmapManager beatmaps { get; set; }
2018-04-13 17:19:50 +08:00
private List<BeatmapSetInfo> beatmapSets;
2018-04-13 17:19:50 +08:00
2019-07-10 23:18:19 +08:00
public bool IsUserPaused { get; private set; }
/// <summary>
/// Fired when the global <see cref="WorkingBeatmap"/> has changed.
/// Includes direction information for display purposes.
/// </summary>
public event Action<WorkingBeatmap, TrackChangeDirection> TrackChanged;
[Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; }
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
2019-08-13 13:38:49 +08:00
[Resolved(canBeNull: true)]
private OnScreenDisplay onScreenDisplay { get; set; }
2018-04-13 17:19:50 +08:00
[BackgroundDependencyLoader]
private void load()
2018-04-13 17:19:50 +08:00
{
beatmapSets = beatmaps.GetAllUsableBeatmapSets();
beatmaps.ItemAdded += handleBeatmapAdded;
beatmaps.ItemRemoved += handleBeatmapRemoved;
}
2018-04-13 17:19:50 +08:00
protected override void LoadComplete()
{
2018-06-07 15:46:54 +08:00
beatmap.BindValueChanged(beatmapChanged, true);
2019-04-10 11:03:57 +08:00
mods.BindValueChanged(_ => updateAudioAdjustments(), true);
2018-04-13 17:19:50 +08:00
base.LoadComplete();
}
/// <summary>
/// Change the position of a <see cref="BeatmapSetInfo"/> in the current playlist.
/// </summary>
/// <param name="beatmapSetInfo">The beatmap to move.</param>
/// <param name="index">The new position.</param>
public void ChangeBeatmapSetPosition(BeatmapSetInfo beatmapSetInfo, int index)
2018-04-13 17:19:50 +08:00
{
beatmapSets.Remove(beatmapSetInfo);
beatmapSets.Insert(index, beatmapSetInfo);
2018-04-13 17:19:50 +08:00
}
2019-08-13 13:38:49 +08:00
/// <summary>
/// Returns whether the current beatmap track is playing.
/// </summary>
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));
2018-04-13 17:19:50 +08:00
private ScheduledDelegate seekDelegate;
2018-04-13 17:19:50 +08:00
public void SeekTo(double position)
{
seekDelegate?.Cancel();
seekDelegate = Schedule(() =>
2018-11-03 07:04:30 +08:00
{
if (!beatmap.Disabled)
current?.Track.Seek(position);
});
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Start playing the current track (if not already playing).
/// </summary>
public void Play()
{
if (!IsPlaying)
TogglePause();
}
/// <summary>
/// Toggle pause / play.
/// </summary>
/// <returns>Whether the operation was successful.</returns>
2019-08-13 13:38:49 +08:00
public bool TogglePause()
2018-04-13 17:19:50 +08:00
{
var track = current?.Track;
if (track == null)
{
2019-08-13 13:38:49 +08:00
if (beatmap.Disabled)
return false;
next(true);
return true;
2018-04-13 17:19:50 +08:00
}
if (track.IsRunning)
2019-07-10 23:18:19 +08:00
{
IsUserPaused = true;
2018-04-13 17:19:50 +08:00
track.Stop();
2019-07-10 23:18:19 +08:00
}
2018-04-13 17:19:50 +08:00
else
2019-07-10 23:18:19 +08:00
{
2018-04-13 17:19:50 +08:00
track.Start();
2019-07-10 23:18:19 +08:00
IsUserPaused = false;
}
2019-08-13 13:38:49 +08:00
return true;
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Play the previous track.
/// </summary>
/// <returns>Whether the operation was successful.</returns>
2019-08-13 13:38:49 +08:00
public bool PrevTrack()
2018-04-13 17:19:50 +08:00
{
queuedDirection = TrackChangeDirection.Prev;
var playable = beatmapSets.TakeWhile(i => i.ID != current.BeatmapSetInfo.ID).LastOrDefault() ?? beatmapSets.LastOrDefault();
2019-04-01 11:16:05 +08:00
if (playable != null)
2018-05-14 16:45:11 +08:00
{
if (beatmap is Bindable<WorkingBeatmap> working)
working.Value = beatmaps.GetWorkingBeatmap(playable.Beatmaps.First(), beatmap.Value);
beatmap.Value.Track.Restart();
2019-08-13 13:38:49 +08:00
return true;
2018-05-14 16:45:11 +08:00
}
2019-08-13 13:38:49 +08:00
return false;
2018-04-13 17:19:50 +08:00
}
/// <summary>
/// Play the next random or playlist track.
/// </summary>
/// <returns>Whether the operation was successful.</returns>
2019-08-13 13:38:49 +08:00
public bool NextTrack() => next();
2019-08-13 13:38:49 +08:00
private bool next(bool instant = false)
2018-04-13 17:19:50 +08:00
{
if (!instant)
queuedDirection = TrackChangeDirection.Next;
var playable = beatmapSets.SkipWhile(i => i.ID != current.BeatmapSetInfo.ID).Skip(1).FirstOrDefault() ?? beatmapSets.FirstOrDefault();
2019-04-01 11:16:05 +08:00
if (playable != null)
2018-05-14 16:45:11 +08:00
{
if (beatmap is Bindable<WorkingBeatmap> working)
working.Value = beatmaps.GetWorkingBeatmap(playable.Beatmaps.First(), beatmap.Value);
beatmap.Value.Track.Restart();
2019-08-13 13:38:49 +08:00
return true;
2018-05-14 16:45:11 +08:00
}
2019-08-13 13:38:49 +08:00
return false;
2018-04-13 17:19:50 +08:00
}
private WorkingBeatmap current;
private TrackChangeDirection? queuedDirection;
2018-04-13 17:19:50 +08:00
2019-02-25 18:29:09 +08:00
private void beatmapChanged(ValueChangedEvent<WorkingBeatmap> beatmap)
2018-04-13 17:19:50 +08:00
{
TrackChangeDirection direction = TrackChangeDirection.None;
2018-04-13 17:19:50 +08:00
if (current != null)
{
2019-02-25 18:29:09 +08:00
bool audioEquals = beatmap.NewValue?.BeatmapInfo?.AudioEquals(current.BeatmapInfo) ?? false;
2018-04-13 17:19:50 +08:00
if (audioEquals)
direction = TrackChangeDirection.None;
2018-04-13 17:19:50 +08:00
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();
2019-02-25 18:34:03 +08:00
var next = beatmap.NewValue == null ? -1 : beatmapSets.TakeWhile(b => b.ID != beatmap.NewValue.BeatmapSetInfo?.ID).Count();
2018-04-13 17:19:50 +08:00
direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next;
2018-04-13 17:19:50 +08:00
}
}
current = beatmap.NewValue;
TrackChanged?.Invoke(current, direction);
2018-04-13 17:19:50 +08:00
updateAudioAdjustments();
2018-04-13 17:19:50 +08:00
queuedDirection = null;
}
private void updateAudioAdjustments()
{
var track = current?.Track;
if (track == null)
return;
track.ResetSpeedAdjustments();
2019-04-10 11:03:57 +08:00
foreach (var mod in mods.Value.OfType<IApplicableToClock>())
mod.ApplyToClock(track);
}
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2019-08-13 13:45:27 +08:00
if (beatmaps != null)
{
beatmaps.ItemAdded -= handleBeatmapAdded;
beatmaps.ItemRemoved -= handleBeatmapRemoved;
}
}
2019-08-13 13:38:49 +08:00
public bool OnPressed(GlobalAction action)
{
if (beatmap.Disabled)
return false;
2019-08-13 13:38:49 +08:00
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
2018-04-13 17:19:50 +08:00
}
}