1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 20:13:22 +08:00
osu-lazer/osu.Game/Overlays/Mods/ModPresetPanel.cs

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

138 lines
4.5 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 osu.Framework.Allocation;
using osu.Framework.Bindables;
2023-03-07 01:00:40 +08:00
using osu.Framework.Extensions;
using osu.Framework.Graphics.Cursor;
2022-07-24 04:03:31 +08:00
using osu.Framework.Graphics.UserInterface;
using osu.Game.Configuration;
2022-07-24 00:01:11 +08:00
using osu.Game.Database;
using osu.Game.Graphics;
2022-07-24 04:03:31 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Resources.Localisation.Web;
using osu.Game.Rulesets.Mods;
namespace osu.Game.Overlays.Mods
{
2023-03-16 18:15:50 +08:00
public partial class ModPresetPanel : ModSelectPanel, IHasCustomTooltip<ModPreset>, IHasContextMenu, IHasPopover
{
2022-07-24 00:01:11 +08:00
public readonly Live<ModPreset> Preset;
public override BindableBool Active { get; } = new BindableBool();
2022-07-24 04:03:31 +08:00
[Resolved]
private IDialogOverlay? dialogOverlay { get; set; }
[Resolved]
private Bindable<IReadOnlyList<Mod>> selectedMods { get; set; } = null!;
private ModSettingChangeTracker? settingChangeTracker;
2022-07-24 00:01:11 +08:00
public ModPresetPanel(Live<ModPreset> preset)
{
Preset = preset;
2022-07-24 00:01:11 +08:00
Title = preset.Value.Name;
Description = preset.Value.Description;
}
[BackgroundDependencyLoader]
private void load(OsuColour colours)
{
AccentColour = colours.Orange1;
}
protected override void LoadComplete()
{
base.LoadComplete();
selectedMods.BindValueChanged(_ => selectedModsChanged(), true);
}
protected override void Select()
{
// if the preset is not active at the point of the user click, then set the mods using the preset directly, discarding any previous selections,
// which will also have the side effect of activating the preset (see `updateActiveState()`).
selectedMods.Value = Preset.Value.Mods.ToArray();
}
protected override void Deselect()
{
// if the preset is active when the user has clicked it, then it means that the set of active mods is exactly equal to the set of mods in the preset
// (there are no other active mods than what the preset specifies, and the mod settings match exactly).
// therefore it's safe to just clear selected mods, since it will have the effect of toggling the preset off.
selectedMods.Value = Array.Empty<Mod>();
}
private void selectedModsChanged()
{
settingChangeTracker?.Dispose();
settingChangeTracker = new ModSettingChangeTracker(selectedMods.Value);
settingChangeTracker.SettingChanged = _ => updateActiveState();
updateActiveState();
}
private void updateActiveState()
{
2023-03-16 18:15:50 +08:00
Active.Value = new HashSet<Mod>(Preset.Value.Mods).SetEquals(selectedMods.Value);
}
2022-07-24 04:03:31 +08:00
#region IHasCustomTooltip
2023-03-16 18:15:50 +08:00
public ModPreset TooltipContent => Preset.Value;
public ITooltip<ModPreset> GetCustomTooltip() => new ModPresetTooltip(ColourProvider);
2022-07-24 04:03:31 +08:00
#endregion
#region IHasContextMenu
2023-03-09 21:43:11 +08:00
public MenuItem[] ContextMenuItems
2022-07-24 04:03:31 +08:00
{
2023-03-09 21:43:11 +08:00
get
{
var menu = new List<MenuItem>
{
new OsuMenuItem(CommonStrings.ButtonsEdit, MenuItemType.Highlighted, this.ShowPopover),
new OsuMenuItem(CommonStrings.ButtonsDelete, MenuItemType.Destructive, () => dialogOverlay?.Push(new DeleteModPresetDialog(Preset))),
};
2023-03-11 10:39:35 +08:00
if (CheckCurrentModCanBeSave())
2023-03-09 21:43:11 +08:00
{
menu.Insert(1, new OsuMenuItem("Use Current Mods", MenuItemType.Destructive, () => SaveCurrentMod()));
}
return menu.ToArray();
}
}
2022-07-24 04:03:31 +08:00
#endregion
2023-03-09 21:23:13 +08:00
public bool SaveCurrentMod()
{
2023-03-11 10:39:35 +08:00
if (!CheckCurrentModCanBeSave())
2023-03-09 21:23:13 +08:00
return false;
Preset.PerformWrite(s =>
{
s.Mods = selectedMods.Value.ToArray();
});
return true;
}
2023-03-11 10:39:35 +08:00
public bool CheckCurrentModCanBeSave() => (!Active.Value && selectedMods.Value.Any());
2023-03-09 21:23:13 +08:00
protected override void Dispose(bool isDisposing)
{
base.Dispose(isDisposing);
settingChangeTracker?.Dispose();
}
2023-03-07 01:00:40 +08:00
public Popover GetPopover() => new EditPresetPopover(this);
}
}