2022-07-23 03:05:02 +08:00
|
|
|
// 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.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using osu.Framework.Allocation;
|
2022-07-24 00:01:11 +08:00
|
|
|
using osu.Framework.Bindables;
|
2022-08-09 02:30:21 +08:00
|
|
|
using osu.Framework.Graphics;
|
2022-07-24 00:01:11 +08:00
|
|
|
using osu.Game.Database;
|
2022-07-23 03:05:02 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Localisation;
|
2022-07-24 00:01:11 +08:00
|
|
|
using osu.Game.Rulesets;
|
2022-07-23 03:05:02 +08:00
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
using osuTK;
|
2022-07-24 00:01:11 +08:00
|
|
|
using Realms;
|
2022-07-23 03:05:02 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Mods
|
|
|
|
{
|
|
|
|
public partial class ModPresetColumn : ModSelectColumn
|
|
|
|
{
|
2022-07-24 00:01:11 +08:00
|
|
|
[Resolved]
|
|
|
|
private RealmAccess realm { get; set; } = null!;
|
2022-07-23 03:05:02 +08:00
|
|
|
|
2022-07-24 00:01:11 +08:00
|
|
|
[Resolved]
|
|
|
|
private IBindable<RulesetInfo> ruleset { get; set; } = null!;
|
2022-07-23 03:05:02 +08:00
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuColour colours)
|
|
|
|
{
|
|
|
|
AccentColour = colours.Orange1;
|
2022-07-25 05:30:52 +08:00
|
|
|
HeaderText = ModSelectOverlayStrings.PersonalPresets;
|
2022-07-23 03:17:27 +08:00
|
|
|
|
|
|
|
AddPresetButton addPresetButton;
|
|
|
|
ItemsFlow.Add(addPresetButton = new AddPresetButton());
|
|
|
|
ItemsFlow.SetLayoutPosition(addPresetButton, float.PositiveInfinity);
|
2022-07-23 03:05:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
|
|
|
|
2022-07-24 00:01:11 +08:00
|
|
|
ruleset.BindValueChanged(_ => rulesetChanged(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private IDisposable? presetSubscription;
|
|
|
|
|
|
|
|
private void rulesetChanged()
|
|
|
|
{
|
|
|
|
presetSubscription?.Dispose();
|
|
|
|
presetSubscription = realm.RegisterForNotifications(r =>
|
2022-08-08 15:12:07 +08:00
|
|
|
r.All<ModPreset>()
|
|
|
|
.Filter($"{nameof(ModPreset.Ruleset)}.{nameof(RulesetInfo.ShortName)} == $0"
|
|
|
|
+ $" && {nameof(ModPreset.DeletePending)} == false", ruleset.Value.ShortName)
|
|
|
|
.OrderBy(preset => preset.Name), asyncLoadPanels);
|
2022-07-23 03:05:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private CancellationTokenSource? cancellationTokenSource;
|
|
|
|
|
|
|
|
private Task? latestLoadTask;
|
2022-08-09 01:19:13 +08:00
|
|
|
internal bool ItemsLoaded => latestLoadTask?.IsCompleted == true;
|
2022-07-23 03:05:02 +08:00
|
|
|
|
2022-08-08 15:12:07 +08:00
|
|
|
private void asyncLoadPanels(IRealmCollection<ModPreset> presets, ChangeSet changes, Exception error)
|
2022-07-23 03:05:02 +08:00
|
|
|
{
|
|
|
|
cancellationTokenSource?.Cancel();
|
|
|
|
|
2022-07-24 00:01:11 +08:00
|
|
|
if (!presets.Any())
|
|
|
|
{
|
2022-08-09 02:30:21 +08:00
|
|
|
removeAndDisposePresetPanels();
|
2022-07-24 00:01:11 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-08-08 15:12:07 +08:00
|
|
|
latestLoadTask = LoadComponentsAsync(presets.Select(p => new ModPresetPanel(p.ToLive(realm))
|
2022-07-23 03:05:02 +08:00
|
|
|
{
|
|
|
|
Shear = Vector2.Zero
|
2022-08-08 15:12:07 +08:00
|
|
|
}), loaded =>
|
2022-07-23 03:05:02 +08:00
|
|
|
{
|
2022-08-09 02:30:21 +08:00
|
|
|
removeAndDisposePresetPanels();
|
2022-07-23 03:17:27 +08:00
|
|
|
ItemsFlow.AddRange(loaded);
|
2022-07-23 03:05:02 +08:00
|
|
|
}, (cancellationTokenSource = new CancellationTokenSource()).Token);
|
2022-08-09 02:30:21 +08:00
|
|
|
|
|
|
|
void removeAndDisposePresetPanels()
|
|
|
|
{
|
2022-08-09 03:08:54 +08:00
|
|
|
foreach (var panel in ItemsFlow.OfType<ModPresetPanel>().ToArray())
|
|
|
|
panel.RemoveAndDisposeImmediately();
|
2022-08-09 02:30:21 +08:00
|
|
|
}
|
2022-07-23 03:05:02 +08:00
|
|
|
}
|
2022-07-24 00:01:11 +08:00
|
|
|
|
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
|
|
|
|
presetSubscription?.Dispose();
|
|
|
|
}
|
2022-07-23 03:05:02 +08:00
|
|
|
}
|
|
|
|
}
|