osu/osu.Game/Overlays/Mods/ModPresetColumn.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

97 lines
2.8 KiB
C#
Raw Normal View History

// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
2022-07-23 16:01:11 +00:00
using osu.Framework.Bindables;
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Localisation;
2022-07-23 16:01:11 +00:00
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osuTK;
2022-07-23 16:01:11 +00:00
using Realms;
namespace osu.Game.Overlays.Mods
{
public class ModPresetColumn : ModSelectColumn
{
2022-07-23 16:01:11 +00:00
[Resolved]
private RealmAccess realm { get; set; } = null!;
2022-07-23 16:01:11 +00:00
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; } = null!;
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
AccentColour = colours.Orange1;
HeaderText = ModSelectOverlayStrings.PersonalPresets;
}
protected override void LoadComplete()
{
base.LoadComplete();
2022-07-23 16:01:11 +00:00
ruleset.BindValueChanged(_ => rulesetChanged(), true);
}
private IDisposable? presetSubscription;
private void rulesetChanged()
{
presetSubscription?.Dispose();
presetSubscription = realm.RegisterForNotifications(r =>
r.All<ModPreset>()
.Filter($"{nameof(ModPreset.Ruleset)}.{nameof(RulesetInfo.ShortName)} == $0"
+ $" && {nameof(ModPreset.DeletePending)} == false", ruleset.Value.ShortName)
.OrderBy(preset => preset.Name),
(presets, _, _) => asyncLoadPanels(presets));
}
private CancellationTokenSource? cancellationTokenSource;
private Task? latestLoadTask;
internal bool ItemsLoaded => latestLoadTask == null;
2022-07-23 16:01:11 +00:00
private void asyncLoadPanels(IReadOnlyList<ModPreset> presets)
{
cancellationTokenSource?.Cancel();
2022-07-23 16:01:11 +00:00
if (!presets.Any())
{
ItemsFlow.Clear();
return;
}
var panels = presets.Select(preset => new ModPresetPanel(preset.ToLive(realm))
{
Shear = Vector2.Zero
});
Task? loadTask;
latestLoadTask = loadTask = LoadComponentsAsync(panels, loaded =>
{
ItemsFlow.ChildrenEnumerable = loaded;
}, (cancellationTokenSource = new CancellationTokenSource()).Token);
loadTask.ContinueWith(_ =>
{
if (loadTask == latestLoadTask)
latestLoadTask = null;
});
}
2022-07-23 16:01:11 +00:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
presetSubscription?.Dispose();
}
}
}