From a32f9eed516e49254b369722f046d9075e2838b6 Mon Sep 17 00:00:00 2001 From: Huo Yaoyuan Date: Mon, 24 Oct 2016 15:34:44 +0800 Subject: [PATCH] Play control. --- osu.Game/Overlays/MusicController.cs | 93 +++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 10 deletions(-) diff --git a/osu.Game/Overlays/MusicController.cs b/osu.Game/Overlays/MusicController.cs index 5bb93289a4..d7ee3ca8fc 100644 --- a/osu.Game/Overlays/MusicController.cs +++ b/osu.Game/Overlays/MusicController.cs @@ -7,11 +7,14 @@ using System.Linq; using OpenTK; using OpenTK.Graphics; using osu.Framework; +using osu.Framework.Audio.Track; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Sprites; using osu.Framework.Graphics.Textures; +using osu.Framework.Graphics.Transformations; using osu.Framework.Input; +using osu.Game.Beatmaps.IO; using osu.Game.Database; using osu.Game.Graphics; @@ -21,18 +24,23 @@ namespace osu.Game.Overlays { private Sprite backgroundSprite; private Box progress; + private ClickableTextAwesome playButton, listButton; private SpriteText title, artist; + private OsuGameBase osuGame; private List playList; private BeatmapSetInfo currentPlay; + private AudioTrack currentTrack; public override void Load(BaseGame game) { base.Load(game); - playList = (game as OsuGameBase).Beatmaps.Query().ToList(); + osuGame = game as OsuGameBase; + playList = osuGame.Beatmaps.Query().ToList(); currentPlay = playList.FirstOrDefault(); Width = 400; Height = 130; CornerRadius = 5; Masking = true; + Children = new Drawable[] { backgroundSprite = getScaledSprite(game.Textures.Get(@"Backgrounds/bg4")),//placeholder @@ -48,7 +56,7 @@ namespace osu.Game.Overlays Position = new Vector2(0, 40), TextSize = 20, Colour = Color4.White, - Text = currentPlay?.Metadata.TitleUnicode ?? currentPlay?.Metadata.Title ?? @"Nothing to play" + Text = @"Nothing to play" }, artist = new SpriteText { @@ -57,7 +65,7 @@ namespace osu.Game.Overlays Position = new Vector2(0, 45), TextSize = 12, Colour = Color4.White, - Text = currentPlay?.Metadata.ArtistUnicode ?? currentPlay?.Metadata.Artist ?? @"Nothing to play" + Text = @"Nothing to play" }, new Box { @@ -67,13 +75,27 @@ namespace osu.Game.Overlays Anchor = Anchor.BottomCentre, Colour = new Color4(0, 0, 0, 127) }, - new ClickableTextAwesome + playButton = new ClickableTextAwesome { TextSize = 30, Icon = FontAwesome.play_circle_o, Origin = Anchor.Centre, Anchor = Anchor.BottomCentre, - Position = new Vector2(0, 30) + Position = new Vector2(0, 30), + Action = () => + { + if (currentTrack == null) return; + if (currentTrack.IsRunning) + { + currentTrack.Stop(); + playButton.Icon = FontAwesome.play_circle_o; + } + else + { + currentTrack.Start(); + playButton.Icon = FontAwesome.pause; + } + } }, new ClickableTextAwesome { @@ -81,7 +103,8 @@ namespace osu.Game.Overlays Icon = FontAwesome.step_backward, Origin = Anchor.Centre, Anchor = Anchor.BottomCentre, - Position = new Vector2(-30, 30) + Position = new Vector2(-30, 30), + Action = prev }, new ClickableTextAwesome { @@ -89,9 +112,10 @@ namespace osu.Game.Overlays Icon = FontAwesome.step_forward, Origin = Anchor.Centre, Anchor = Anchor.BottomCentre, - Position = new Vector2(30, 30) + Position = new Vector2(30, 30), + Action = next }, - new ClickableTextAwesome + listButton = new ClickableTextAwesome { TextSize = 15, Icon = FontAwesome.bars, @@ -109,6 +133,55 @@ namespace osu.Game.Overlays Colour = Color4.Orange } }; + if (currentPlay != null) play(currentPlay, null); + } + + private void prev() + { + int i = playList.IndexOf(currentPlay); + if (i == -1) return; + i = (i - 1 + playList.Count) % playList.Count; + currentPlay = playList[i]; + play(currentPlay, false); + } + + private void next() + { + int i = playList.IndexOf(currentPlay); + if (i == -1) return; + i = (i + 1) % playList.Count; + currentPlay = playList[i]; + play(currentPlay, true); + } + + private void play(BeatmapSetInfo beatmap, bool? isNext) + { + title.Text = beatmap.Metadata.TitleUnicode ?? beatmap.Metadata.Title; + artist.Text = beatmap.Metadata.ArtistUnicode ?? beatmap.Metadata.Artist; + ArchiveReader reader = osuGame.Beatmaps.GetReader(currentPlay); + currentTrack?.Stop(); + currentTrack = new AudioTrackBass(reader.ReadFile(beatmap.Metadata.AudioFile)); + currentTrack.Start(); + Sprite newBackground = getScaledSprite(TextureLoader.FromStream(reader.ReadFile(beatmap.Metadata.BackgroundFile))); + Add(newBackground); + if (isNext == true) + { + newBackground.Position = new Vector2(400, 0); + newBackground.MoveToX(0, 200, EasingTypes.Out); + backgroundSprite.MoveToX(-400, 200, EasingTypes.Out); + backgroundSprite.Expire(); + } + else if (isNext == false) + { + newBackground.Position = new Vector2(-400, 0); + newBackground.MoveToX(0, 200, EasingTypes.Out); + backgroundSprite.MoveToX(400, 200, EasingTypes.Out); + backgroundSprite.Expire(); + } + else + { + Remove(backgroundSprite); + } } private Sprite getScaledSprite(Texture background) @@ -132,11 +205,11 @@ namespace osu.Game.Overlays public class ClickableTextAwesome : TextAwesome { - public Action Action; + public Action Action; protected override bool OnClick(InputState state) { - Action?.Invoke(this); + Action?.Invoke(); return true; } }