1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-15 10:22:56 +08:00

move logic to DirectPanel and reuse stuff for the PreviewButton

This commit is contained in:
Jorolf 2017-09-29 23:08:30 +02:00
parent 355a7b6649
commit 647304c14b
7 changed files with 166 additions and 169 deletions

View File

@ -0,0 +1,35 @@
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Track;
using osu.Framework.Graphics;
using osu.Game.Beatmaps;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace osu.Game.Audio
{
public class AudioLoadWrapper : Drawable
{
private readonly string preview;
public Track Preview;
public AudioLoadWrapper(string preview)
{
this.preview = preview;
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
if (!string.IsNullOrEmpty(preview))
{
Preview = audio.Track.Get(preview);
Preview.Volume.Value = 0.5;
}
}
}
}

View File

@ -15,6 +15,9 @@ using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using OpenTK;
using OpenTK.Graphics;
using osu.Game.Audio;
using osu.Game.Overlays.Direct;
using osu.Framework.Configuration;
namespace osu.Game.Overlays.BeatmapSet
{
@ -24,27 +27,10 @@ namespace osu.Game.Overlays.BeatmapSet
private readonly Container audioWrapper;
private readonly Box bg, progress;
private readonly SpriteIcon icon;
private readonly LoadingAnimation loadingAnimation;
private readonly PlayButton playButton;
private Track preview;
private bool loading
{
set
{
if (value)
{
loadingAnimation.Show();
icon.FadeOut(transition_duration * 5, Easing.OutQuint);
}
else
{
loadingAnimation.Hide();
icon.FadeIn(transition_duration, Easing.OutQuint);
}
}
}
private readonly Bindable<bool> playing = new Bindable<bool>();
private BeatmapSetInfo beatmapSet;
public BeatmapSetInfo BeatmapSet
@ -55,42 +41,11 @@ namespace osu.Game.Overlays.BeatmapSet
if (value == beatmapSet) return;
beatmapSet = value;
Playing = false;
playing.Value = false;
preview = null;
}
}
private bool playing;
public bool Playing
{
get { return playing; }
set
{
if (value == playing) return;
playing = value;
if (preview == null)
{
loading = true;
audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper(BeatmapSet)
{
OnLoadComplete = d =>
{
loading = false;
preview = (d as AudioLoadWrapper)?.Preview;
Playing = Playing;
updatePlayingState();
},
});
return;
}
updatePlayingState();
}
}
public PreviewButton()
{
Height = 42;
@ -116,22 +71,16 @@ namespace osu.Game.Overlays.BeatmapSet
Alpha = 0f,
},
},
icon = new SpriteIcon
playButton = new PlayButton(playing)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
Icon = FontAwesome.fa_play,
Size = new Vector2(18),
Shadow = false,
},
loadingAnimation = new LoadingAnimation
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
};
Action = () => Playing = !Playing;
Action = () => playing.Value = !playing.Value;
playing.ValueChanged += updatePlayingState;
}
[BackgroundDependencyLoader]
@ -144,12 +93,12 @@ namespace osu.Game.Overlays.BeatmapSet
{
base.Update();
if (Playing && preview != null)
if (playing.Value && preview != null)
{
progress.Width = (float)(preview.CurrentTime / preview.Length);
if (preview.HasCompleted)
{
Playing = false;
playing.Value = false;
preview = null;
}
}
@ -157,7 +106,7 @@ namespace osu.Game.Overlays.BeatmapSet
protected override void Dispose(bool isDisposing)
{
Playing = false;
playing.Value = false;
base.Dispose(isDisposing);
}
@ -173,44 +122,35 @@ namespace osu.Game.Overlays.BeatmapSet
base.OnHoverLost(state);
}
private void updatePlayingState()
private void updatePlayingState(bool newValue)
{
if (preview == null) return;
if (Playing)
if (preview == null)
{
icon.Icon = FontAwesome.fa_stop;
progress.FadeIn(100);
playButton.Loading = true;
audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper(BeatmapSet.OnlineInfo.Preview)
{
OnLoadComplete = d =>
{
playButton.Loading = false;
preview.Seek(0);
preview.Start();
preview = (d as AudioLoadWrapper)?.Preview;
playing.TriggerChange();
},
});
}
else
{
icon.Icon = FontAwesome.fa_play;
progress.FadeOut(100);
preview.Stop();
}
}
private class AudioLoadWrapper : Drawable
{
private readonly string preview;
public Track Preview;
public AudioLoadWrapper(BeatmapSetInfo set)
{
preview = set.OnlineInfo.Preview;
}
[BackgroundDependencyLoader]
private void load(AudioManager audio)
{
if (!string.IsNullOrEmpty(preview))
if (newValue)
{
Preview = audio.Track.Get(preview);
Preview.Volume.Value = 0.5;
progress.FadeIn(100);
preview.Seek(0);
preview.Start();
}
else
{
progress.FadeOut(100);
preview.Stop();
}
}
}

View File

@ -26,6 +26,7 @@ namespace osu.Game.Overlays.Direct
private Box progressBar;
protected override PlayButton PlayButton => playButton;
protected override Box PreviewBar => progressBar;
public DirectGridPanel(BeatmapSetInfo beatmap) : base(beatmap)
{
@ -197,20 +198,8 @@ namespace osu.Game.Overlays.Direct
Margin = new MarginPadding { Top = 5, Left = 10 },
Size = new Vector2(30),
Alpha = 0,
TrackUrl = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3",
},
});
PreviewPlaying.ValueChanged += newValue => playButton.FadeTo(newValue || IsHovered ? 1 : 0, 120, Easing.InOutQuint);
PreviewPlaying.ValueChanged += newValue => progressBar.FadeTo(newValue ? 1 : 0, 120, Easing.InOutQuint);
}
protected override void Update()
{
base.Update();
if (PreviewPlaying && playButton.Track != null)
progressBar.Width = (float)(playButton.Track.CurrentTime / playButton.Track.Length);
}
}
}

View File

@ -14,6 +14,7 @@ using osu.Framework.Localisation;
using osu.Framework.Graphics.Shapes;
using osu.Game.Beatmaps;
using osu.Framework.Configuration;
using System;
namespace osu.Game.Overlays.Direct
{
@ -33,6 +34,7 @@ namespace osu.Game.Overlays.Direct
private Box progressBar;
protected override PlayButton PlayButton => playButton;
protected override Box PreviewBar => progressBar;
[BackgroundDependencyLoader]
private void load(LocalisationEngine localisation, OsuColour colours)
@ -70,7 +72,6 @@ namespace osu.Game.Overlays.Direct
Size = new Vector2(height / 2),
FillMode = FillMode.Fit,
Alpha = 0,
TrackUrl = "https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3",
},
new FillFlowContainer
{
@ -165,17 +166,6 @@ namespace osu.Game.Overlays.Direct
Colour = colours.Yellow,
},
});
PreviewPlaying.ValueChanged += newValue => playButton.FadeTo(newValue || IsHovered ? 1 : 0, 120, Easing.InOutQuint);
PreviewPlaying.ValueChanged += newValue => progressBar.FadeTo(newValue ? 1 : 0, 120, Easing.InOutQuint);
}
protected override void Update()
{
base.Update();
if (PreviewPlaying && playButton.Track != null)
progressBar.Width = (float)(playButton.Track.CurrentTime / playButton.Track.Length);
}
}
}

View File

@ -21,6 +21,8 @@ using osu.Framework.Logging;
using osu.Game.Overlays.Notifications;
using osu.Game.Online.API.Requests;
using osu.Framework.Configuration;
using osu.Framework.Audio.Track;
using osu.Game.Audio;
namespace osu.Game.Overlays.Direct
{
@ -39,9 +41,12 @@ namespace osu.Game.Overlays.Direct
private BeatmapManager beatmaps;
private NotificationOverlay notifications;
private BeatmapSetOverlay beatmapSetOverlay;
private Container audioWrapper;
protected Track Preview;
public readonly Bindable<bool> PreviewPlaying = new Bindable<bool>();
protected abstract PlayButton PlayButton { get; }
protected abstract Box PreviewBar { get; }
protected override Container<Drawable> Content => content;
@ -82,6 +87,7 @@ namespace osu.Game.Overlays.Direct
EdgeEffect = edgeEffectNormal,
Children = new[]
{
audioWrapper = new Container(),
// temporary blackness until the actual background loads.
BlackBackground = new Box
{
@ -106,6 +112,53 @@ namespace osu.Game.Overlays.Direct
if (downloadRequest != null)
attachDownload(downloadRequest);
PreviewPlaying.ValueChanged += newValue => PlayButton.FadeTo(newValue || IsHovered ? 1 : 0, 120, Easing.InOutQuint);
PreviewPlaying.ValueChanged += newValue => PreviewBar.FadeTo(newValue ? 1 : 0, 120, Easing.InOutQuint);
PreviewPlaying.ValueChanged += setPlaying;
}
private void setPlaying(bool newValue)
{
if (newValue)
{
if(Preview == null)
{
PlayButton.Loading = true;
audioWrapper.Child = new AsyncLoadWrapper(new AudioLoadWrapper("https://b.ppy.sh/preview/" + SetInfo.OnlineBeatmapSetID + ".mp3")
{
OnLoadComplete = d =>
{
PlayButton.Loading = false;
Preview = (d as AudioLoadWrapper)?.Preview;
Preview.Start();
},
});
}
else
{
Preview.Start();
}
}
else
{
Preview?.Stop();
}
}
protected override void Update()
{
base.Update();
if (PreviewPlaying && Preview != null)
{
PreviewBar.Width = (float)(Preview.CurrentTime / Preview.Length);
if (Preview.HasCompleted)
{
PreviewPlaying.Value = false;
Preview = null;
}
}
}
protected override bool OnHover(InputState state)

View File

@ -11,81 +11,70 @@ using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Game.Beatmaps;
using osu.Game.Graphics;
using osu.Game.Graphics.UserInterface;
using System.Threading.Tasks;
namespace osu.Game.Overlays.Direct
{
public class PlayButton : Container
{
public string TrackUrl;
public Bindable<bool> Playing;
public Track Track;
private Bindable<WorkingBeatmap> gameBeatmap;
private AudioManager audio;
private readonly Bindable<bool> playing;
private Color4 hoverColour;
private readonly SpriteIcon icon;
private readonly LoadingAnimation loadingAnimation;
private const float transition_duration = 500;
private bool loading;
public bool Loading
{
get { return loading; }
set
{
loading = value;
if (value)
{
loadingAnimation.Show();
icon.FadeOut(transition_duration * 5, Easing.OutQuint);
}
else
{
loadingAnimation.Hide();
icon.FadeIn(transition_duration, Easing.OutQuint);
}
}
}
public PlayButton(Bindable<bool> playing)
{
Playing = playing;
Add(icon = new SpriteIcon
this.playing = playing;
AddRange(new Drawable[]
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
FillMode = FillMode.Fit,
RelativeSizeAxes = Axes.Both,
Icon = FontAwesome.fa_play,
icon = new SpriteIcon
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
FillMode = FillMode.Fit,
RelativeSizeAxes = Axes.Both,
Icon = FontAwesome.fa_play,
},
loadingAnimation = new LoadingAnimation(),
});
Playing.ValueChanged += newValue => icon.Icon = newValue ? (Track == null ? FontAwesome.fa_spinner : FontAwesome.fa_pause) : FontAwesome.fa_play;
playing.ValueChanged += newValue => icon.Icon = newValue ? FontAwesome.fa_pause : FontAwesome.fa_play;
Playing.ValueChanged += newValue =>
{
if (newValue)
Track?.Start();
else
Track?.Stop();
};
Playing.ValueChanged += newValue => icon.FadeColour(newValue || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint);
playing.ValueChanged += newValue => icon.FadeColour(newValue || IsHovered ? hoverColour : Color4.White, 120, Easing.InOutQuint);
}
[BackgroundDependencyLoader]
private void load(OsuColour colour, OsuGameBase game, AudioManager audio)
private void load(OsuColour colour)
{
hoverColour = colour.Yellow;
gameBeatmap = game.Beatmap;
this.audio = audio;
}
private Task loadTask;
protected override bool OnClick(InputState state)
{
gameBeatmap.Value.Track.Stop();
Playing.Value = !Playing.Value;
if (loadTask == null)
{
icon.Spin(2000, RotationDirection.Clockwise);
loadTask = Task.Run(() =>
{
Track = audio.Track.Get(TrackUrl);
Track.Looping = true;
if (Playing)
Track.Start();
icon.ClearTransforms();
icon.Rotation = 0;
Playing.TriggerChange();
});
}
playing.Value = !playing.Value;
return true;
}
@ -97,7 +86,7 @@ namespace osu.Game.Overlays.Direct
protected override void OnHoverLost(InputState state)
{
if(!Playing)
if(!playing.Value)
icon.FadeColour(Color4.White, 120, Easing.InOutQuint);
base.OnHoverLost(state);
}

View File

@ -239,6 +239,7 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="Audio\AudioLoadWrapper.cs" />
<Compile Include="Audio\SampleInfo.cs" />
<Compile Include="Audio\SampleInfoList.cs" />
<Compile Include="Beatmaps\Beatmap.cs" />