1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 20:22:55 +08:00

Play control.

This commit is contained in:
Huo Yaoyuan 2016-10-24 15:34:44 +08:00
parent d2495e34fb
commit a32f9eed51

View File

@ -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<BeatmapSetInfo> playList;
private BeatmapSetInfo currentPlay;
private AudioTrack currentTrack;
public override void Load(BaseGame game)
{
base.Load(game);
playList = (game as OsuGameBase).Beatmaps.Query<BeatmapSetInfo>().ToList();
osuGame = game as OsuGameBase;
playList = osuGame.Beatmaps.Query<BeatmapSetInfo>().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<ClickableTextAwesome> Action;
public Action Action;
protected override bool OnClick(InputState state)
{
Action?.Invoke(this);
Action?.Invoke();
return true;
}
}