osu/osu.Game/Screens/Select/SongSelect.cs

350 lines
11 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-09-29 11:13:58 +00:00
using System;
2016-11-14 08:23:33 +00:00
using System.Linq;
using System.Threading;
using OpenTK;
using OpenTK.Input;
2016-11-14 08:23:33 +00:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2016-11-14 08:23:33 +00:00
using osu.Framework.Audio.Track;
2016-10-08 10:12:31 +00:00
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
2016-11-14 08:23:33 +00:00
using osu.Framework.Graphics.Primitives;
using osu.Framework.Graphics.Transforms;
using osu.Framework.Input;
using osu.Framework.Screens;
2016-11-14 08:23:33 +00:00
using osu.Game.Beatmaps;
using osu.Game.Beatmaps.Drawables;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2016-11-14 09:03:20 +00:00
using osu.Game.Modes;
using osu.Game.Overlays;
2016-11-14 08:23:33 +00:00
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Select.Options;
2016-09-29 11:13:58 +00:00
namespace osu.Game.Screens.Select
2016-09-29 11:13:58 +00:00
{
public abstract class SongSelect : OsuScreen
2016-09-29 11:13:58 +00:00
{
private Bindable<PlayMode> playMode = new Bindable<PlayMode>();
private BeatmapDatabase database;
2017-02-17 09:59:30 +00:00
protected override BackgroundScreen CreateBackground() => new BackgroundScreenBeatmap(Beatmap);
2017-03-17 10:12:54 +00:00
private BeatmapCarousel carousel;
2016-10-28 10:55:48 +00:00
private TrackManager trackManager;
private DialogOverlay dialogOverlay;
private static readonly Vector2 wedged_container_size = new Vector2(0.5f, 225);
2016-11-24 04:48:48 +00:00
private BeatmapInfoWedge beatmapInfoWedge;
2017-02-07 07:15:45 +00:00
private static readonly Vector2 background_blur = new Vector2(20);
private CancellationTokenSource initialAddSetsTask;
private SampleChannel sampleChangeDifficulty;
private SampleChannel sampleChangeBeatmap;
2017-02-08 11:30:05 +00:00
2017-03-15 02:10:59 +00:00
protected virtual bool ShowFooter => true;
2017-03-14 13:51:26 +00:00
/// <summary>
2017-03-16 05:08:37 +00:00
/// Can be null if <see cref="ShowFooter"/> is false.
2017-03-14 13:51:26 +00:00
/// </summary>
2017-03-15 02:10:59 +00:00
protected readonly BeatmapOptionsOverlay BeatmapOptions;
2017-03-14 13:51:26 +00:00
/// <summary>
2017-03-16 05:08:37 +00:00
/// Can be null if <see cref="ShowFooter"/> is false.
2017-03-14 13:51:26 +00:00
/// </summary>
2017-03-15 02:10:59 +00:00
protected readonly Footer Footer;
public readonly FilterControl FilterControl;
2017-03-15 02:10:59 +00:00
protected SongSelect()
{
2017-02-07 07:15:45 +00:00
const float carousel_width = 640;
2017-02-08 11:30:05 +00:00
const float filter_height = 100;
2017-03-14 13:51:26 +00:00
Add(new ParallaxContainer
{
2017-03-14 13:51:26 +00:00
Padding = new MarginPadding { Top = filter_height },
ParallaxAmount = 0.005f,
RelativeSizeAxes = Axes.Both,
Children = new[]
2016-10-13 20:55:15 +00:00
{
2017-03-14 13:51:26 +00:00
new WedgeBackground
2016-10-19 18:02:03 +00:00
{
2017-03-14 13:51:26 +00:00
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = carousel_width * 0.76f },
2016-10-19 18:02:03 +00:00
}
2017-03-14 13:51:26 +00:00
}
});
2017-03-17 10:12:54 +00:00
Add(carousel = new BeatmapCarousel
2017-03-14 13:51:26 +00:00
{
RelativeSizeAxes = Axes.Y,
Size = new Vector2(carousel_width, 1),
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
SelectionChanged = selectionChanged,
StartRequested = raiseSelect
2017-03-14 13:51:26 +00:00
});
Add(FilterControl = new FilterControl
2017-03-14 13:51:26 +00:00
{
RelativeSizeAxes = Axes.X,
Height = filter_height,
FilterChanged = criteria => filterChanged(criteria),
2017-03-14 13:51:26 +00:00
Exit = Exit,
});
Add(beatmapInfoWedge = new BeatmapInfoWedge
{
Alpha = 0,
Size = wedged_container_size,
RelativeSizeAxes = Axes.X,
Margin = new MarginPadding
{
2017-03-14 13:51:26 +00:00
Top = 20,
Right = 20,
},
2017-03-14 13:51:26 +00:00
X = -50,
});
2017-03-15 02:10:59 +00:00
if (ShowFooter)
2017-03-14 13:51:26 +00:00
{
Add(BeatmapOptions = new BeatmapOptionsOverlay
2017-02-16 20:05:03 +00:00
{
Margin = new MarginPadding
{
Bottom = 50,
},
2017-03-14 13:51:26 +00:00
});
Add(Footer = new Footer
2016-10-21 14:52:52 +00:00
{
OnBack = Exit,
2017-03-14 13:51:26 +00:00
OnStart = raiseSelect,
});
}
}
2017-03-14 13:51:26 +00:00
[BackgroundDependencyLoader(permitNulls: true)]
private void load(BeatmapDatabase beatmaps, AudioManager audio, DialogOverlay dialog, OsuGame osu, OsuColour colours)
2017-03-14 13:51:26 +00:00
{
if (Footer != null)
{
Footer.AddButton(@"random", colours.Green, SelectRandom, Key.F2);
Footer.AddButton(@"options", colours.Blue, BeatmapOptions.ToggleVisibility, Key.F3);
2017-03-14 12:18:14 +00:00
2017-03-14 13:51:26 +00:00
BeatmapOptions.AddButton(@"Delete", @"Beatmap", FontAwesome.fa_trash, colours.Pink, promptDelete, Key.Number4, float.MaxValue);
}
2017-03-14 12:18:14 +00:00
if (osu != null)
playMode.BindTo(osu.PlayMode);
playMode.ValueChanged += playMode_ValueChanged;
if (database == null)
2016-11-08 23:13:20 +00:00
database = beatmaps;
2017-02-24 08:21:14 +00:00
database.BeatmapSetAdded += onBeatmapSetAdded;
database.BeatmapSetRemoved += onBeatmapSetRemoved;
2016-11-08 23:13:20 +00:00
trackManager = audio.Track;
dialogOverlay = dialog;
2016-10-28 10:55:48 +00:00
2016-12-05 11:09:56 +00:00
sampleChangeDifficulty = audio.Sample.Get(@"SongSelect/select-difficulty");
sampleChangeBeatmap = audio.Sample.Get(@"SongSelect/select-expand");
initialAddSetsTask = new CancellationTokenSource();
carousel.BeatmapsChanged = beatmapsLoaded;
carousel.Beatmaps = database.Query<BeatmapSetInfo>().Where(b => !b.DeletePending);
}
private void beatmapsLoaded()
{
if (Beatmap != null)
carousel.SelectBeatmap(Beatmap.BeatmapInfo, false);
else
carousel.SelectNext();
}
2017-03-14 13:51:26 +00:00
private void raiseSelect()
{
if (Beatmap == null) return;
Beatmap.PreferredPlayMode = playMode.Value;
2017-03-14 14:22:23 +00:00
OnSelected();
2017-03-14 13:51:26 +00:00
}
2017-03-14 10:38:06 +00:00
public void SelectRandom() => carousel.SelectRandom();
2017-03-14 14:22:23 +00:00
protected abstract void OnSelected();
private void filterChanged(FilterCriteria criteria, bool debounce = true)
2017-01-18 00:18:15 +00:00
{
carousel.Filter(criteria, debounce);
2017-01-18 00:18:15 +00:00
}
private void onBeatmapSetAdded(BeatmapSetInfo s) => carousel.AddBeatmap(s);
2016-10-08 10:12:31 +00:00
2017-02-24 08:21:14 +00:00
private void onBeatmapSetRemoved(BeatmapSetInfo s) => Schedule(() => removeBeatmapSet(s));
2016-10-08 10:12:31 +00:00
2017-02-17 09:59:30 +00:00
protected override void OnEntering(Screen last)
2016-10-28 10:55:48 +00:00
{
base.OnEntering(last);
ensurePlayingSelected();
changeBackground(Beatmap);
Content.FadeInFromZero(250);
beatmapInfoWedge.State = Visibility.Visible;
2017-02-08 11:30:05 +00:00
FilterControl.Activate();
2016-10-28 10:55:48 +00:00
}
2017-02-17 09:59:30 +00:00
protected override void OnResuming(Screen last)
2016-10-28 10:55:48 +00:00
{
changeBackground(Beatmap);
2016-10-28 10:55:48 +00:00
ensurePlayingSelected();
base.OnResuming(last);
Content.FadeIn(250);
Content.ScaleTo(1, 250, EasingTypes.OutSine);
FilterControl.Activate();
}
2017-02-17 09:59:30 +00:00
protected override void OnSuspending(Screen next)
{
Content.ScaleTo(1.1f, 250, EasingTypes.InSine);
Content.FadeOut(250);
2017-02-08 11:30:05 +00:00
FilterControl.Deactivate();
base.OnSuspending(next);
}
2017-02-17 09:59:30 +00:00
protected override bool OnExiting(Screen next)
{
beatmapInfoWedge.State = Visibility.Hidden;
Content.FadeOut(100);
FilterControl.Deactivate();
return base.OnExiting(next);
2016-10-28 10:55:48 +00:00
}
2016-10-08 10:12:31 +00:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
2017-02-24 08:21:14 +00:00
database.BeatmapSetAdded -= onBeatmapSetAdded;
database.BeatmapSetRemoved -= onBeatmapSetRemoved;
initialAddSetsTask.Cancel();
2016-10-06 14:33:28 +00:00
}
private void playMode_ValueChanged(object sender, EventArgs e) => carousel.Filter();
private void changeBackground(WorkingBeatmap beatmap)
2016-10-26 14:52:04 +00:00
{
2017-02-17 09:59:30 +00:00
var backgroundModeBeatmap = Background as BackgroundScreenBeatmap;
if (backgroundModeBeatmap != null)
{
backgroundModeBeatmap.Beatmap = beatmap;
2017-02-07 07:15:45 +00:00
backgroundModeBeatmap.BlurTo(background_blur, 1000);
2017-02-22 12:43:29 +00:00
backgroundModeBeatmap.FadeTo(1, 250);
}
2017-03-13 20:59:44 +00:00
beatmapInfoWedge.UpdateBeatmap(beatmap);
}
/// <summary>
/// The global Beatmap was changed.
/// </summary>
protected override void OnBeatmapChanged(WorkingBeatmap beatmap)
{
base.OnBeatmapChanged(beatmap);
//todo: change background in selectionChanged instead; support per-difficulty backgrounds.
changeBackground(beatmap);
2017-03-13 20:59:44 +00:00
carousel.SelectBeatmap(beatmap?.BeatmapInfo);
2016-10-28 10:55:48 +00:00
}
/// <summary>
/// selection has been changed as the result of interaction with the carousel.
/// </summary>
private void selectionChanged(BeatmapGroup group, BeatmapInfo beatmap)
{
2016-12-15 23:11:48 +00:00
bool beatmapSetChange = false;
2016-10-28 12:08:32 +00:00
if (!beatmap.Equals(Beatmap?.BeatmapInfo))
2016-12-05 11:09:56 +00:00
{
2016-12-21 06:47:56 +00:00
if (beatmap.BeatmapSetInfoID == Beatmap?.BeatmapInfo.BeatmapSetInfoID)
2016-12-05 11:09:56 +00:00
sampleChangeDifficulty.Play();
else
2016-12-15 23:11:48 +00:00
{
2016-12-05 11:09:56 +00:00
sampleChangeBeatmap.Play();
2016-12-15 23:11:48 +00:00
beatmapSetChange = true;
}
Beatmap = database.GetWorkingBeatmap(beatmap, Beatmap);
2016-12-05 11:09:56 +00:00
}
2016-12-15 23:11:48 +00:00
ensurePlayingSelected(beatmapSetChange);
2016-10-26 14:52:04 +00:00
}
private void ensurePlayingSelected(bool preview = false)
2016-10-28 10:55:48 +00:00
{
Track track = Beatmap?.Track;
2016-10-28 10:55:48 +00:00
if (track != null)
2016-10-28 10:55:48 +00:00
{
trackManager.SetExclusive(track);
if (preview)
track.Seek(Beatmap.Beatmap.Metadata.PreviewTime);
track.Start();
}
2016-10-28 10:55:48 +00:00
}
private void selectBeatmap(BeatmapSetInfo beatmapSet = null)
{
carousel.SelectBeatmap(beatmapSet != null ? beatmapSet.Beatmaps.First() : Beatmap?.BeatmapInfo);
}
private void removeBeatmapSet(BeatmapSetInfo beatmapSet)
{
carousel.RemoveBeatmap(beatmapSet);
if (carousel.SelectedBeatmap == null)
Beatmap = null;
}
2017-03-14 12:18:14 +00:00
private void promptDelete()
2017-03-02 12:40:55 +00:00
{
if (Beatmap != null)
dialogOverlay?.Push(new BeatmapDeleteDialog(Beatmap));
}
2016-12-15 11:55:37 +00:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
2017-03-14 13:51:26 +00:00
if (args.Repeat) return false;
switch (args.Key)
2016-12-15 11:55:37 +00:00
{
2017-03-14 13:51:26 +00:00
case Key.Enter:
raiseSelect();
return true;
case Key.Delete:
if (state.Keyboard.ShiftPressed)
{
promptDelete();
return true;
}
break;
2016-12-15 11:55:37 +00:00
}
return base.OnKeyDown(state, args);
}
2016-09-29 11:13:58 +00:00
}
}