1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-14 17:17:24 +08:00

Merge pull request #28745 from peppy/music-controller-nullability

Apply nullability to `MusicController`
This commit is contained in:
Dean Herbert 2024-07-05 15:04:28 +09:00 committed by GitHub
commit bab1216c6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,12 +1,9 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // 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. // See the LICENCE file in the repository root for full licence text.
#nullable disable
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using JetBrains.Annotations;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Audio; using osu.Framework.Audio;
using osu.Framework.Audio.Track; using osu.Framework.Audio.Track;
@ -28,7 +25,7 @@ namespace osu.Game.Overlays
public partial class MusicController : CompositeDrawable public partial class MusicController : CompositeDrawable
{ {
[Resolved] [Resolved]
private BeatmapManager beatmaps { get; set; } private BeatmapManager beatmaps { get; set; } = null!;
/// <summary> /// <summary>
/// Point in time after which the current track will be restarted on triggering a "previous track" action. /// Point in time after which the current track will be restarted on triggering a "previous track" action.
@ -49,25 +46,28 @@ namespace osu.Game.Overlays
/// Fired when the global <see cref="WorkingBeatmap"/> has changed. /// Fired when the global <see cref="WorkingBeatmap"/> has changed.
/// Includes direction information for display purposes. /// Includes direction information for display purposes.
/// </summary> /// </summary>
public event Action<WorkingBeatmap, TrackChangeDirection> TrackChanged; public event Action<WorkingBeatmap, TrackChangeDirection>? TrackChanged;
[Resolved] [Resolved]
private IBindable<WorkingBeatmap> beatmap { get; set; } private IBindable<WorkingBeatmap> beatmap { get; set; } = null!;
[Resolved] [Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; } private IBindable<IReadOnlyList<Mod>> mods { get; set; } = null!;
[NotNull]
public DrawableTrack CurrentTrack { get; private set; } = new DrawableTrack(new TrackVirtual(1000)); public DrawableTrack CurrentTrack { get; private set; } = new DrawableTrack(new TrackVirtual(1000));
[Resolved] [Resolved]
private RealmAccess realm { get; set; } private RealmAccess realm { get; set; } = null!;
protected override void LoadComplete() protected override void LoadComplete()
{ {
base.LoadComplete(); base.LoadComplete();
beatmap.BindValueChanged(b => changeBeatmap(b.NewValue), true); beatmap.BindValueChanged(b =>
{
if (b.NewValue != null)
changeBeatmap(b.NewValue);
}, true);
mods.BindValueChanged(_ => ResetTrackAdjustments(), true); mods.BindValueChanged(_ => ResetTrackAdjustments(), true);
} }
@ -76,6 +76,9 @@ namespace osu.Game.Overlays
/// </summary> /// </summary>
public void ReloadCurrentTrack() public void ReloadCurrentTrack()
{ {
if (current == null)
return;
changeTrack(); changeTrack();
TrackChanged?.Invoke(current, TrackChangeDirection.None); TrackChanged?.Invoke(current, TrackChangeDirection.None);
} }
@ -90,7 +93,7 @@ namespace osu.Game.Overlays
/// </summary> /// </summary>
public bool TrackLoaded => CurrentTrack.TrackLoaded; public bool TrackLoaded => CurrentTrack.TrackLoaded;
private ScheduledDelegate seekDelegate; private ScheduledDelegate? seekDelegate;
public void SeekTo(double position) public void SeekTo(double position)
{ {
@ -192,7 +195,7 @@ namespace osu.Game.Overlays
/// Play the previous track or restart the current track if it's current time below <see cref="restart_cutoff_point"/>. /// Play the previous track or restart the current track if it's current time below <see cref="restart_cutoff_point"/>.
/// </summary> /// </summary>
/// <param name="onSuccess">Invoked when the operation has been performed successfully.</param> /// <param name="onSuccess">Invoked when the operation has been performed successfully.</param>
public void PreviousTrack(Action<PreviousTrackResult> onSuccess = null) => Schedule(() => public void PreviousTrack(Action<PreviousTrackResult>? onSuccess = null) => Schedule(() =>
{ {
PreviousTrackResult res = prev(); PreviousTrackResult res = prev();
if (res != PreviousTrackResult.None) if (res != PreviousTrackResult.None)
@ -218,7 +221,7 @@ namespace osu.Game.Overlays
queuedDirection = TrackChangeDirection.Prev; queuedDirection = TrackChangeDirection.Prev;
var playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current.BeatmapSetInfo)).LastOrDefault() var playableSet = getBeatmapSets().AsEnumerable().TakeWhile(i => !i.Equals(current?.BeatmapSetInfo)).LastOrDefault()
?? getBeatmapSets().LastOrDefault(); ?? getBeatmapSets().LastOrDefault();
if (playableSet != null) if (playableSet != null)
@ -236,7 +239,7 @@ namespace osu.Game.Overlays
/// </summary> /// </summary>
/// <param name="onSuccess">Invoked when the operation has been performed successfully.</param> /// <param name="onSuccess">Invoked when the operation has been performed successfully.</param>
/// <returns>A <see cref="ScheduledDelegate"/> of the operation.</returns> /// <returns>A <see cref="ScheduledDelegate"/> of the operation.</returns>
public void NextTrack(Action onSuccess = null) => Schedule(() => public void NextTrack(Action? onSuccess = null) => Schedule(() =>
{ {
bool res = next(); bool res = next();
if (res) if (res)
@ -250,7 +253,7 @@ namespace osu.Game.Overlays
queuedDirection = TrackChangeDirection.Next; queuedDirection = TrackChangeDirection.Next;
var playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current.BeatmapSetInfo)).ElementAtOrDefault(1) var playableSet = getBeatmapSets().AsEnumerable().SkipWhile(i => !i.Equals(current?.BeatmapSetInfo)).ElementAtOrDefault(1)
?? getBeatmapSets().FirstOrDefault(); ?? getBeatmapSets().FirstOrDefault();
var playableBeatmap = playableSet?.Beatmaps.FirstOrDefault(); var playableBeatmap = playableSet?.Beatmaps.FirstOrDefault();
@ -272,7 +275,7 @@ namespace osu.Game.Overlays
Schedule(() => CurrentTrack.RestartAsync()); Schedule(() => CurrentTrack.RestartAsync());
} }
private WorkingBeatmap current; private WorkingBeatmap? current;
private TrackChangeDirection? queuedDirection; private TrackChangeDirection? queuedDirection;
@ -289,7 +292,7 @@ namespace osu.Game.Overlays
TrackChangeDirection direction = TrackChangeDirection.None; TrackChangeDirection direction = TrackChangeDirection.None;
bool audioEquals = newWorking?.BeatmapInfo?.AudioEquals(current?.BeatmapInfo) == true; bool audioEquals = newWorking.BeatmapInfo?.AudioEquals(current?.BeatmapInfo) == true;
if (current != null) if (current != null)
{ {
@ -304,7 +307,7 @@ namespace osu.Game.Overlays
{ {
// figure out the best direction based on order in playlist. // figure out the best direction based on order in playlist.
int last = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(current.BeatmapSetInfo)).Count(); int last = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(current.BeatmapSetInfo)).Count();
int next = newWorking == null ? -1 : getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count(); int next = getBeatmapSets().AsEnumerable().TakeWhile(b => !b.Equals(newWorking.BeatmapSetInfo)).Count();
direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next; direction = last > next ? TrackChangeDirection.Prev : TrackChangeDirection.Next;
} }
@ -361,7 +364,7 @@ namespace osu.Game.Overlays
{ {
// Important to keep this in its own method to avoid inadvertently capturing unnecessary variables in the callback. // Important to keep this in its own method to avoid inadvertently capturing unnecessary variables in the callback.
// Can lead to leaks. // Can lead to leaks.
var queuedTrack = new DrawableTrack(current.LoadTrack()); var queuedTrack = new DrawableTrack(current!.LoadTrack());
queuedTrack.Completed += onTrackCompleted; queuedTrack.Completed += onTrackCompleted;
return queuedTrack; return queuedTrack;
} }
@ -390,7 +393,7 @@ namespace osu.Game.Overlays
} }
} }
private AudioAdjustments modTrackAdjustments; private AudioAdjustments? modTrackAdjustments;
/// <summary> /// <summary>
/// Resets the adjustments currently applied on <see cref="CurrentTrack"/> and applies the mod adjustments if <see cref="ApplyModTrackAdjustments"/> is <c>true</c>. /// Resets the adjustments currently applied on <see cref="CurrentTrack"/> and applies the mod adjustments if <see cref="ApplyModTrackAdjustments"/> is <c>true</c>.