1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-16 14:57:25 +08:00
osu-lazer/osu.Game/Overlays/Mods/ModPresetColumn.cs

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

106 lines
3.2 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.Linq;
using System.Threading;
using System.Threading.Tasks;
using osu.Framework.Allocation;
2022-07-24 00:01:11 +08:00
using osu.Framework.Bindables;
using osu.Framework.Graphics;
2022-07-24 00:01:11 +08:00
using osu.Game.Database;
using osu.Game.Graphics;
using osu.Game.Localisation;
2022-07-24 00:01:11 +08:00
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osuTK;
2022-07-24 00:01:11 +08:00
using Realms;
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-24 00:01:11 +08:00
[Resolved]
private IBindable<RulesetInfo> ruleset { get; set; } = null!;
private const float contracted_width = WIDTH - 120;
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
AccentColour = colours.Orange1;
HeaderText = ModSelectOverlayStrings.PersonalPresets;
AddPresetButton addPresetButton;
ItemsFlow.Add(addPresetButton = new AddPresetButton());
ItemsFlow.SetLayoutPosition(addPresetButton, float.PositiveInfinity);
}
protected override void LoadComplete()
{
base.LoadComplete();
2022-07-24 00:01:11 +08:00
ruleset.BindValueChanged(_ => rulesetChanged(), true);
Width = contracted_width;
2022-07-24 00:01:11 +08:00
}
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), asyncLoadPanels);
}
private CancellationTokenSource? cancellationTokenSource;
private Task? latestLoadTask;
internal bool ItemsLoaded => latestLoadTask?.IsCompleted == true;
2023-07-06 12:37:42 +08:00
private void asyncLoadPanels(IRealmCollection<ModPreset> presets, ChangeSet? changes)
{
cancellationTokenSource?.Cancel();
bool hasPresets = presets.Any();
this.ResizeWidthTo(hasPresets ? WIDTH : contracted_width, 200, Easing.OutQuint);
if (!hasPresets)
2022-07-24 00:01:11 +08:00
{
removeAndDisposePresetPanels();
2022-07-24 00:01:11 +08:00
return;
}
latestLoadTask = LoadComponentsAsync(presets.Select(p => new ModPresetPanel(p.ToLive(realm))
{
Shear = Vector2.Zero
}), loaded =>
{
removeAndDisposePresetPanels();
ItemsFlow.AddRange(loaded);
}, (cancellationTokenSource = new CancellationTokenSource()).Token);
void removeAndDisposePresetPanels()
{
foreach (var panel in ItemsFlow.OfType<ModPresetPanel>().ToArray())
panel.RemoveAndDisposeImmediately();
}
}
2022-07-24 00:01:11 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
presetSubscription?.Dispose();
}
}
}