1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-17 05:52:54 +08:00
osu-lazer/osu.Game/Overlays/Mods/EditPresetPopover.cs

187 lines
6.7 KiB
C#
Raw Normal View History

2023-03-07 01:00:40 +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.Collections.Generic;
using System.Linq;
2023-03-07 01:00:40 +08:00
using osu.Framework.Allocation;
using osu.Framework.Bindables;
2023-03-07 01:00:40 +08:00
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
2023-05-04 10:19:09 +08:00
using osu.Game.Database;
2023-03-07 01:00:40 +08:00
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
2023-03-07 01:00:40 +08:00
using osu.Game.Graphics.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Input.Bindings;
2023-03-07 01:00:40 +08:00
using osu.Game.Localisation;
using osu.Game.Rulesets.Mods;
using osuTK;
namespace osu.Game.Overlays.Mods
{
internal partial class EditPresetPopover : OsuPopover
{
private LabelledTextBox nameTextBox = null!;
private LabelledTextBox descriptionTextBox = null!;
private ShearedButton useCurrentModsButton = null!;
private ShearedButton saveButton = null!;
private FillFlowContainer scrollContent = null!;
2023-03-07 01:00:40 +08:00
2023-05-04 10:19:09 +08:00
private readonly Live<ModPreset> preset;
private HashSet<Mod> saveableMods;
[Resolved]
private Bindable<IReadOnlyList<Mod>> selectedMods { get; set; } = null!;
[Resolved]
private OsuColour colours { get; set; } = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
2023-03-07 01:00:40 +08:00
2023-05-04 10:19:09 +08:00
public EditPresetPopover(Live<ModPreset> preset)
2023-03-07 01:00:40 +08:00
{
2023-05-04 10:19:09 +08:00
this.preset = preset;
saveableMods = preset.PerformRead(p => p.Mods).ToHashSet();
}
2023-03-07 01:00:40 +08:00
[BackgroundDependencyLoader]
private void load()
{
2023-03-07 01:00:40 +08:00
Child = new FillFlowContainer
{
Width = 300,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(7),
2023-03-07 20:13:35 +08:00
Direction = FillDirection.Vertical,
2023-03-07 01:00:40 +08:00
Children = new Drawable[]
{
nameTextBox = new LabelledTextBox
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Label = CommonStrings.Name,
TabbableContentContainer = this,
2023-05-04 10:19:09 +08:00
Current = { Value = preset.PerformRead(p => p.Name) },
2023-03-07 01:00:40 +08:00
},
descriptionTextBox = new LabelledTextBox
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Label = CommonStrings.Description,
TabbableContentContainer = this,
2023-05-04 10:19:09 +08:00
Current = { Value = preset.PerformRead(p => p.Description) },
2023-03-07 01:00:40 +08:00
},
new OsuScrollContainer
{
RelativeSizeAxes = Axes.X,
Height = 100,
Padding = new MarginPadding(7),
Child = scrollContent = new FillFlowContainer
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Padding = new MarginPadding(7),
Spacing = new Vector2(7),
}
},
2023-03-07 20:13:35 +08:00
new FillFlowContainer
2023-03-07 01:00:40 +08:00
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
RelativeSizeAxes = Axes.X,
2023-03-07 20:13:35 +08:00
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(7),
Children = new Drawable[]
{
2023-05-04 10:10:05 +08:00
useCurrentModsButton = new ShearedButton
2023-03-07 20:13:35 +08:00
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = ModSelectOverlayStrings.UseCurrentMods,
DarkerColour = colours.Blue1,
LighterColour = colours.Blue0,
TextColour = colourProvider.Background6,
Action = useCurrentMods,
2023-03-07 20:13:35 +08:00
},
2023-05-04 10:10:05 +08:00
saveButton = new ShearedButton
2023-03-07 20:13:35 +08:00
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = Resources.Localisation.Web.CommonStrings.ButtonsSave,
DarkerColour = colours.Orange1,
LighterColour = colours.Orange0,
TextColour = colourProvider.Background6,
Action = save,
2023-03-07 20:13:35 +08:00
},
}
2023-03-07 01:00:40 +08:00
}
}
};
Body.BorderThickness = 3;
Body.BorderColour = colours.Orange1;
selectedMods.BindValueChanged(_ => updateState(), true);
nameTextBox.Current.BindValueChanged(s =>
{
2023-05-04 10:10:05 +08:00
saveButton.Enabled.Value = !string.IsNullOrWhiteSpace(s.NewValue);
}, true);
2023-03-07 01:00:40 +08:00
}
protected override void LoadComplete()
{
base.LoadComplete();
ScheduleAfterChildren(() => GetContainingInputManager().ChangeFocus(nameTextBox));
}
public override bool OnPressed(KeyBindingPressEvent<GlobalAction> e)
{
switch (e.Action)
{
case GlobalAction.Select:
saveButton.TriggerClick();
return true;
}
return base.OnPressed(e);
}
2023-05-04 10:10:05 +08:00
private void useCurrentMods()
2023-03-07 01:00:40 +08:00
{
saveableMods = selectedMods.Value.Where(mod => mod.Type != ModType.System).ToHashSet();
updateState();
}
private void updateState()
{
scrollContent.ChildrenEnumerable = saveableMods.AsOrdered().Select(mod => new ModPresetRow(mod));
2023-05-04 10:10:05 +08:00
useCurrentModsButton.Enabled.Value = checkSelectedModsDiffersFromSaved();
2023-03-07 01:00:40 +08:00
}
private bool checkSelectedModsDiffersFromSaved()
{
if (!selectedMods.Value.Any())
return false;
return !saveableMods.SetEquals(selectedMods.Value.Where(mod => mod.Type != ModType.System));
}
2023-05-04 10:10:05 +08:00
private void save()
2023-03-07 01:00:40 +08:00
{
2023-05-04 10:19:09 +08:00
preset.PerformWrite(s =>
2023-03-07 01:00:40 +08:00
{
s.Name = nameTextBox.Current.Value;
s.Description = descriptionTextBox.Current.Value;
s.Mods = saveableMods;
});
this.HidePopover();
}
2023-03-07 01:00:40 +08:00
}
}