1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 21:47:25 +08:00
osu-lazer/osu.Game/Screens/Select/SongSelect.cs

408 lines
14 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 19:13:58 +08:00
2017-07-19 12:32:16 +08:00
using System;
using System.Threading;
using OpenTK;
using OpenTK.Input;
2016-11-14 16:23:33 +08:00
using osu.Framework.Allocation;
using osu.Framework.Audio;
using osu.Framework.Audio.Sample;
2016-11-14 16:23:33 +08:00
using osu.Framework.Audio.Track;
2016-10-08 18:12:31 +08:00
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
using osu.Framework.Screens;
using osu.Framework.Threading;
2016-11-14 16:23:33 +08:00
using osu.Game.Beatmaps;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Overlays;
2016-11-14 16:23:33 +08:00
using osu.Game.Screens.Backgrounds;
using osu.Game.Screens.Select.Options;
2016-09-29 19:13:58 +08:00
namespace osu.Game.Screens.Select
2016-09-29 19:13:58 +08:00
{
public abstract class SongSelect : OsuScreen
2016-09-29 19:13:58 +08:00
{
2017-04-17 16:43:48 +08:00
private readonly Bindable<RulesetInfo> ruleset = new Bindable<RulesetInfo>();
private BeatmapDatabase database;
protected override BackgroundScreen CreateBackground() => new BackgroundScreenBeatmap();
private readonly BeatmapCarousel carousel;
2016-10-28 18:55:48 +08:00
private TrackManager trackManager;
private DialogOverlay dialogOverlay;
private static readonly Vector2 wedged_container_size = new Vector2(0.5f, 245);
private const float left_area_padding = 20;
private readonly BeatmapInfoWedge beatmapInfoWedge;
protected Container LeftContent;
2017-02-07 15:15:45 +08:00
private static readonly Vector2 background_blur = new Vector2(20);
private CancellationTokenSource initialAddSetsTask;
private SampleChannel sampleChangeDifficulty;
private SampleChannel sampleChangeBeatmap;
2017-02-08 19:30:05 +08:00
2017-03-15 10:10:59 +08:00
protected virtual bool ShowFooter => true;
2017-03-14 21:51:26 +08:00
/// <summary>
2017-03-16 13:08:37 +08:00
/// Can be null if <see cref="ShowFooter"/> is false.
2017-03-14 21:51:26 +08:00
/// </summary>
2017-03-15 10:10:59 +08:00
protected readonly BeatmapOptionsOverlay BeatmapOptions;
2017-03-14 21:51:26 +08:00
/// <summary>
2017-03-16 13:08:37 +08:00
/// Can be null if <see cref="ShowFooter"/> is false.
2017-03-14 21:51:26 +08:00
/// </summary>
2017-03-15 10:10:59 +08:00
protected readonly Footer Footer;
/// <summary>
/// Contains any panel which is triggered by a footer button.
/// Helps keep them located beneath the footer itself.
/// </summary>
protected readonly Container FooterPanels;
public readonly FilterControl FilterControl;
2017-03-15 10:10:59 +08:00
protected SongSelect()
{
2017-02-07 15:15:45 +08:00
const float carousel_width = 640;
2017-02-08 19:30:05 +08:00
const float filter_height = 100;
2017-03-14 21:51:26 +08:00
Add(new ParallaxContainer
{
2017-03-14 21:51:26 +08:00
Padding = new MarginPadding { Top = filter_height },
ParallaxAmount = 0.005f,
RelativeSizeAxes = Axes.Both,
Children = new[]
2016-10-14 04:55:15 +08:00
{
2017-03-14 21:51:26 +08:00
new WedgeBackground
2016-10-20 02:02:03 +08:00
{
2017-03-14 21:51:26 +08:00
RelativeSizeAxes = Axes.Both,
Padding = new MarginPadding { Right = carousel_width * 0.76f },
2016-10-20 02:02:03 +08:00
}
2017-03-14 21:51:26 +08:00
}
});
Add(LeftContent = new Container
{
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
RelativeSizeAxes = Axes.Both,
Size = new Vector2(wedged_container_size.X, 1),
Padding = new MarginPadding
{
Bottom = 50,
Top = wedged_container_size.Y + left_area_padding,
Left = left_area_padding,
Right = left_area_padding * 2,
}
});
2017-03-17 18:12:54 +08:00
Add(carousel = new BeatmapCarousel
2017-03-14 21:51:26 +08:00
{
RelativeSizeAxes = Axes.Y,
Size = new Vector2(carousel_width, 1),
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
2017-07-19 12:32:16 +08:00
SelectionChanged = carouselSelectionChanged,
BeatmapsChanged = carouselBeatmapsLoaded,
StartRequested = carouselRaisedStart
2017-03-14 21:51:26 +08:00
});
Add(FilterControl = new FilterControl
2017-03-14 21:51:26 +08:00
{
RelativeSizeAxes = Axes.X,
Height = filter_height,
FilterChanged = criteria => filterChanged(criteria),
2017-03-14 21:51:26 +08:00
Exit = Exit,
});
Add(beatmapInfoWedge = new BeatmapInfoWedge
{
Alpha = 0,
Size = wedged_container_size,
RelativeSizeAxes = Axes.X,
Margin = new MarginPadding
{
Top = left_area_padding,
Right = left_area_padding,
},
2017-03-14 21:51:26 +08:00
});
2017-03-15 10:10:59 +08:00
if (ShowFooter)
2017-03-14 21:51:26 +08:00
{
Add(FooterPanels = new Container
2017-02-17 04:05:03 +08:00
{
Anchor = Anchor.BottomLeft,
Origin = Anchor.BottomLeft,
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Margin = new MarginPadding
{
Bottom = Footer.HEIGHT,
},
2017-03-14 21:51:26 +08:00
});
Add(Footer = new Footer
2016-10-21 22:52:52 +08:00
{
OnBack = Exit,
2017-07-19 12:32:16 +08:00
OnStart = carouselRaisedStart,
2017-03-14 21:51:26 +08:00
});
FooterPanels.Add(BeatmapOptions = new BeatmapOptionsOverlay());
2017-03-14 21:51:26 +08:00
}
}
2017-03-14 21:51:26 +08:00
[BackgroundDependencyLoader(permitNulls: true)]
2017-06-05 17:24:28 +08:00
private void load(BeatmapDatabase beatmaps, AudioManager audio, DialogOverlay dialog, OsuGame osu, OsuColour colours, UserInputManager input)
2017-03-14 21:51:26 +08:00
{
if (Footer != null)
{
2017-06-05 17:24:28 +08:00
Footer.AddButton(@"random", colours.Green, () => triggerRandom(input), Key.F2);
2017-03-14 21:51:26 +08:00
Footer.AddButton(@"options", colours.Blue, BeatmapOptions.ToggleVisibility, Key.F3);
2017-03-14 20:18:14 +08:00
2017-03-14 21:51:26 +08:00
BeatmapOptions.AddButton(@"Delete", @"Beatmap", FontAwesome.fa_trash, colours.Pink, promptDelete, Key.Number4, float.MaxValue);
}
2017-03-14 20:18:14 +08:00
if (database == null)
2016-11-09 07:13:20 +08:00
database = beatmaps;
2017-04-15 04:22:41 +08:00
if (osu != null)
ruleset.BindTo(osu.Ruleset);
2017-02-24 16:21:14 +08:00
database.BeatmapSetAdded += onBeatmapSetAdded;
database.BeatmapSetRemoved += onBeatmapSetRemoved;
2016-11-09 07:13:20 +08:00
trackManager = audio.Track;
dialogOverlay = dialog;
2016-10-28 18:55:48 +08:00
2016-12-05 19:09:56 +08:00
sampleChangeDifficulty = audio.Sample.Get(@"SongSelect/select-difficulty");
sampleChangeBeatmap = audio.Sample.Get(@"SongSelect/select-expand");
initialAddSetsTask = new CancellationTokenSource();
2017-05-06 15:37:53 +08:00
carousel.Beatmaps = database.GetAllWithChildren<BeatmapSetInfo>(b => !b.DeletePending);
2017-07-19 12:32:16 +08:00
Beatmap.ValueChanged += beatmap_ValueChanged;
}
2017-07-19 12:32:16 +08:00
private void carouselBeatmapsLoaded()
{
if (Beatmap.Value != null && Beatmap.Value.BeatmapSetInfo?.DeletePending != false)
2017-07-19 12:32:16 +08:00
carousel.SelectBeatmap(Beatmap.Value.BeatmapInfo, false);
else
carousel.SelectNext();
}
2017-07-19 12:32:16 +08:00
private void carouselRaisedStart()
2017-03-14 21:51:26 +08:00
{
var pendingSelection = selectionChangedDebounce;
selectionChangedDebounce = null;
if (pendingSelection?.Completed == false)
{
2017-07-19 12:32:16 +08:00
pendingSelection.RunTask();
pendingSelection.Cancel(); // cancel the already scheduled task.
}
2017-03-14 22:22:23 +08:00
OnSelected();
2017-03-14 21:51:26 +08:00
}
2017-07-19 12:32:16 +08:00
private ScheduledDelegate selectionChangedDebounce;
// We need to keep track of the last selected beatmap ignoring debounce to play the correct selection sounds.
private BeatmapInfo beatmapNoDebounce;
/// <summary>
/// selection has been changed as the result of interaction with the carousel.
/// </summary>
private void carouselSelectionChanged(BeatmapInfo beatmap)
{
Action performLoad = delegate
{
// We may be arriving here due to another component changing the bindable Beatmap.
// In these cases, the other component has already loaded the beatmap, so we don't need to do so again.
if (!beatmap.Equals(Beatmap.Value.BeatmapInfo))
{
bool preview = beatmap?.BeatmapSetInfoID != Beatmap.Value.BeatmapInfo.BeatmapSetInfoID;
2017-07-19 16:56:43 +08:00
Beatmap.Value = database.GetWorkingBeatmap(beatmap, Beatmap);
ensurePlayingSelected(preview);
}
2017-07-19 16:56:43 +08:00
2017-07-19 12:32:16 +08:00
changeBackground(Beatmap.Value);
};
2017-07-19 16:56:43 +08:00
if (beatmap == null)
{
if (!Beatmap.IsDefault)
performLoad();
}
2017-07-19 12:32:16 +08:00
else
2017-07-19 16:56:43 +08:00
{
selectionChangedDebounce?.Cancel();
if (beatmap.Equals(beatmapNoDebounce))
return;
if (beatmap.BeatmapSetInfoID == beatmapNoDebounce?.BeatmapSetInfoID)
sampleChangeDifficulty.Play();
else
sampleChangeBeatmap.Play();
beatmapNoDebounce = beatmap;
if (beatmap == Beatmap.Value.BeatmapInfo)
performLoad();
else
selectionChangedDebounce = Scheduler.AddDelayed(performLoad, 100);
}
2017-07-19 12:32:16 +08:00
}
2017-06-05 17:24:28 +08:00
private void triggerRandom(UserInputManager input)
{
if (input.CurrentState.Keyboard.ShiftPressed)
carousel.SelectPreviousRandom();
else
carousel.SelectNextRandom();
}
2017-03-14 22:22:23 +08:00
protected abstract void OnSelected();
private void filterChanged(FilterCriteria criteria, bool debounce = true)
2017-01-18 08:18:15 +08:00
{
carousel.Filter(criteria, debounce);
2017-01-18 08:18:15 +08:00
}
private void onBeatmapSetAdded(BeatmapSetInfo s) => carousel.AddBeatmap(s);
2016-10-08 18:12:31 +08:00
2017-02-24 16:21:14 +08:00
private void onBeatmapSetRemoved(BeatmapSetInfo s) => Schedule(() => removeBeatmapSet(s));
2016-10-08 18:12:31 +08:00
2017-02-17 17:59:30 +08:00
protected override void OnEntering(Screen last)
2016-10-28 18:55:48 +08:00
{
base.OnEntering(last);
Content.FadeInFromZero(250);
FilterControl.Activate();
2016-10-28 18:55:48 +08:00
}
2017-07-19 12:32:16 +08:00
private void beatmap_ValueChanged(WorkingBeatmap beatmap)
{
if (!IsCurrentScreen) return;
carousel.SelectBeatmap(beatmap?.BeatmapInfo);
}
2017-02-17 17:59:30 +08:00
protected override void OnResuming(Screen last)
2016-10-28 18:55:48 +08:00
{
2017-07-19 12:32:16 +08:00
if (Beatmap != null && !Beatmap.Value.BeatmapSetInfo.DeletePending)
2017-07-19 09:59:13 +08:00
{
changeBackground(Beatmap);
ensurePlayingSelected();
}
2016-10-28 18:55:48 +08:00
base.OnResuming(last);
Content.FadeIn(250);
Content.ScaleTo(1, 250, EasingTypes.OutSine);
FilterControl.Activate();
}
2017-02-17 17:59:30 +08:00
protected override void OnSuspending(Screen next)
{
Content.ScaleTo(1.1f, 250, EasingTypes.InSine);
Content.FadeOut(250);
2017-02-08 19:30:05 +08:00
FilterControl.Deactivate();
base.OnSuspending(next);
}
2017-02-17 17:59:30 +08:00
protected override bool OnExiting(Screen next)
{
beatmapInfoWedge.State = Visibility.Hidden;
Content.FadeOut(100);
FilterControl.Deactivate();
return base.OnExiting(next);
2016-10-28 18:55:48 +08:00
}
2016-10-08 18:12:31 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
if (database != null)
{
database.BeatmapSetAdded -= onBeatmapSetAdded;
database.BeatmapSetRemoved -= onBeatmapSetRemoved;
}
initialAddSetsTask?.Cancel();
2016-10-06 22:33:28 +08:00
}
private void changeBackground(WorkingBeatmap beatmap)
2016-10-26 22:52:04 +08:00
{
2017-02-17 17:59:30 +08:00
var backgroundModeBeatmap = Background as BackgroundScreenBeatmap;
if (backgroundModeBeatmap != null)
{
backgroundModeBeatmap.Beatmap = beatmap;
2017-02-07 15:15:45 +08:00
backgroundModeBeatmap.BlurTo(background_blur, 1000);
2017-02-22 20:43:29 +08:00
backgroundModeBeatmap.FadeTo(1, 250);
}
2017-07-19 09:59:13 +08:00
beatmapInfoWedge.State = Visibility.Visible;
2017-03-14 04:59:44 +08:00
beatmapInfoWedge.UpdateBeatmap(beatmap);
}
private void ensurePlayingSelected(bool preview = false)
2016-10-28 18:55:48 +08:00
{
Track track = Beatmap.Value.Track;
2016-10-28 18:55:48 +08:00
trackManager.SetExclusive(track);
if (preview) track.Seek(Beatmap.Value.Metadata.PreviewTime);
track.Start();
2016-10-28 18:55:48 +08:00
}
private void removeBeatmapSet(BeatmapSetInfo beatmapSet)
{
carousel.RemoveBeatmap(beatmapSet);
if (carousel.SelectedBeatmap == null)
2017-07-19 12:32:16 +08:00
Beatmap.SetDefault();
}
2017-03-14 20:18:14 +08:00
private void promptDelete()
2017-03-02 20:40:55 +08:00
{
if (Beatmap != null)
dialogOverlay?.Push(new BeatmapDeleteDialog(Beatmap));
}
2016-12-15 19:55:37 +08:00
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
2017-03-14 21:51:26 +08:00
if (args.Repeat) return false;
switch (args.Key)
2016-12-15 19:55:37 +08:00
{
case Key.KeypadEnter:
2017-03-14 21:51:26 +08:00
case Key.Enter:
2017-07-19 12:32:16 +08:00
carouselRaisedStart();
2017-03-14 21:51:26 +08:00
return true;
case Key.Delete:
if (state.Keyboard.ShiftPressed)
{
promptDelete();
return true;
}
break;
2016-12-15 19:55:37 +08:00
}
return base.OnKeyDown(state, args);
}
2016-09-29 19:13:58 +08:00
}
}