Move logic out of OsuGame

This commit is contained in:
Dean Herbert 2019-08-13 14:38:49 +09:00
parent 8d23cda51a
commit 9aac5efa4e
4 changed files with 70 additions and 48 deletions

View File

@ -68,8 +68,6 @@ namespace osu.Game
private BeatmapSetOverlay beatmapSetOverlay;
private OnScreenDisplay osd;
[Cached]
private readonly ScreenshotManager screenshotManager = new ScreenshotManager();
@ -469,7 +467,7 @@ namespace osu.Game
});
loadComponentSingleFile(volume = new VolumeOverlay(), leftFloatingOverlayContent.Add);
loadComponentSingleFile(osd = new OnScreenDisplay(), Add, true);
loadComponentSingleFile(new OnScreenDisplay(), Add, true);
loadComponentSingleFile(musicController = new MusicController(), Add, true);
@ -734,27 +732,6 @@ namespace osu.Game
case GlobalAction.ToggleGameplayMouseButtons:
LocalConfig.Set(OsuSetting.MouseDisableButtons, !LocalConfig.Get<bool>(OsuSetting.MouseDisableButtons));
return true;
case GlobalAction.MusicPlay:
if (!musicController.IsLoaded) return true;
if (musicController.PlayTrack())
osd.Display(new Overlays.OSD.MusicControllerToast(musicController.IsPlaying ? "Play track" : "Pause track"));
return true;
case GlobalAction.MusicNext:
if (!musicController.IsLoaded) return true;
if (musicController.NextTrack())
osd.Display(new Overlays.OSD.MusicControllerToast("Next track"));
return true;
case GlobalAction.MusicPrev:
if (!musicController.IsLoaded) return true;
if (musicController.PreviousTrack())
osd.Display(new Overlays.OSD.MusicControllerToast("Previous track"));
return true;
}
return false;

View File

@ -7,8 +7,11 @@ using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Input.Bindings;
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
@ -16,7 +19,7 @@ namespace osu.Game.Overlays
/// <summary>
/// Handles playback of the global music track.
/// </summary>
public class MusicController : Component
public class MusicController : Component, IKeyBindingHandler<GlobalAction>
{
[Resolved]
private BeatmapManager beatmaps { get; set; }
@ -37,6 +40,9 @@ namespace osu.Game.Overlays
[Resolved]
private IBindable<IReadOnlyList<Mod>> mods { get; set; }
[Resolved(canBeNull: true)]
private OnScreenDisplay onScreenDisplay { get; set; }
[BackgroundDependencyLoader]
private void load()
{
@ -58,11 +64,17 @@ namespace osu.Game.Overlays
/// <param name="beatmapSetInfo">The beatmap to move.</param>
/// <param name="index">The new position.</param>
public void ChangeBeatmapSetPosition(BeatmapSetInfo beatmapSetInfo, int index)
{
beatmapSets.Remove(beatmapSetInfo);
beatmapSets.Insert(index, beatmapSetInfo);
}
/// <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));
@ -84,15 +96,17 @@ namespace osu.Game.Overlays
/// <summary>
/// Toggle pause / play.
/// </summary>
public void TogglePause()
public bool TogglePause()
{
var track = current?.Track;
if (track == null)
{
if (!beatmap.Disabled)
next(true);
return;
if (beatmap.Disabled)
return false;
next(true);
return true;
}
if (track.IsRunning)
@ -105,12 +119,14 @@ namespace osu.Game.Overlays
track.Start();
IsUserPaused = false;
}
return true;
}
/// <summary>
/// Play the previous track.
/// </summary>
public void PrevTrack()
public bool PrevTrack()
{
queuedDirection = TrackChangeDirection.Prev;
@ -121,15 +137,19 @@ namespace osu.Game.Overlays
if (beatmap is Bindable<WorkingBeatmap> working)
working.Value = beatmaps.GetWorkingBeatmap(playable.Beatmaps.First(), beatmap.Value);
beatmap.Value.Track.Restart();
return true;
}
return false;
}
/// <summary>
/// Play the next random or playlist track.
/// </summary>
public void NextTrack() => next();
public bool NextTrack() => next();
private void next(bool instant = false)
private bool next(bool instant = false)
{
if (!instant)
queuedDirection = TrackChangeDirection.Next;
@ -141,7 +161,10 @@ namespace osu.Game.Overlays
if (beatmap is Bindable<WorkingBeatmap> working)
working.Value = beatmaps.GetWorkingBeatmap(playable.Beatmaps.First(), beatmap.Value);
beatmap.Value.Track.Restart();
return true;
}
return false;
}
private WorkingBeatmap current;
@ -200,6 +223,41 @@ namespace osu.Game.Overlays
beatmaps.ItemAdded -= handleBeatmapAdded;
beatmaps.ItemRemoved -= handleBeatmapRemoved;
}
public bool OnPressed(GlobalAction action)
{
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

View File

@ -138,7 +138,7 @@ namespace osu.Game.Overlays
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Action = musicController.PrevTrack,
Action = () => musicController.PrevTrack(),
Icon = FontAwesome.Solid.StepBackward,
},
playButton = new MusicIconButton
@ -147,14 +147,14 @@ namespace osu.Game.Overlays
Origin = Anchor.Centre,
Scale = new Vector2(1.4f),
IconScale = new Vector2(1.4f),
Action = musicController.TogglePause,
Action = () => musicController.TogglePause(),
Icon = FontAwesome.Regular.PlayCircle,
},
nextButton = new MusicIconButton
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Action = musicController.NextTrack,
Action = () => musicController.NextTrack(),
Icon = FontAwesome.Solid.StepForward,
},
}

View File

@ -1,13 +0,0 @@
// 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.
namespace osu.Game.Overlays.OSD
{
public class MusicControllerToast : Toast
{
public MusicControllerToast(string value)
: base("Music Playback", value, "")
{
}
}
}