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/EditPresetPopover.cs
2023-05-04 11:14:51 +09:00

180 lines
6.4 KiB
C#

// 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;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterface;
using osu.Game.Graphics.UserInterfaceV2;
using osu.Game.Localisation;
using osu.Game.Rulesets.Mods;
using osuTK;
namespace osu.Game.Overlays.Mods
{
internal partial class EditPresetPopover : OsuPopover
{
private readonly ModPresetPanel presetPanel;
private LabelledTextBox nameTextBox = null!;
private LabelledTextBox descriptionTextBox = null!;
private ShearedButton useCurrentModsButton = null!;
private ShearedButton saveButton = null!;
private FillFlowContainer scrollContent = null!;
private readonly ModPreset preset;
private HashSet<Mod>? newMods;
[Resolved]
private Bindable<IReadOnlyList<Mod>> selectedMods { get; set; } = null!;
[Resolved]
private OsuColour colours { get; set; } = null!;
[Resolved]
private OverlayColourProvider colourProvider { get; set; } = null!;
public EditPresetPopover(ModPresetPanel presetPanel)
{
this.presetPanel = presetPanel;
preset = presetPanel.Preset.Value;
}
[BackgroundDependencyLoader]
private void load()
{
Child = new FillFlowContainer
{
Width = 300,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(7),
Direction = FillDirection.Vertical,
Children = new Drawable[]
{
nameTextBox = new LabelledTextBox
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Label = CommonStrings.Name,
TabbableContentContainer = this,
Current = { Value = preset.Name },
},
descriptionTextBox = new LabelledTextBox
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Label = CommonStrings.Description,
TabbableContentContainer = this,
Current = { Value = preset.Description },
},
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),
}
},
new FillFlowContainer
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Direction = FillDirection.Horizontal,
AutoSizeAxes = Axes.Y,
Spacing = new Vector2(7),
Children = new Drawable[]
{
useCurrentModsButton = new ShearedButton
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = ModSelectOverlayStrings.UseCurrentMods,
DarkerColour = colours.Blue1,
LighterColour = colours.Blue0,
TextColour = colourProvider.Background6,
Action = useCurrentMods,
},
saveButton = new ShearedButton
{
Anchor = Anchor.TopCentre,
Origin = Anchor.TopCentre,
Text = Resources.Localisation.Web.CommonStrings.ButtonsSave,
DarkerColour = colours.Orange1,
LighterColour = colours.Orange0,
TextColour = colourProvider.Background6,
Action = save,
},
}
}
}
};
Body.BorderThickness = 3;
Body.BorderColour = colours.Orange1;
selectedMods.BindValueChanged(_ => updateActiveState(), true);
nameTextBox.Current.BindValueChanged(s =>
{
saveButton.Enabled.Value = !string.IsNullOrWhiteSpace(s.NewValue);
}, true);
}
private void useCurrentMods()
{
newMods = selectedMods.Value.ToHashSet();
scrollContent.Clear();
updateActiveState();
}
private void updateActiveState()
{
scrollContent.ChildrenEnumerable = preset.Mods.Select(mod => new ModPresetRow(mod));
useCurrentModsButton.Enabled.Value = checkSelectedModsDiffersFromSaved();
}
private bool checkSelectedModsDiffersFromSaved()
{
if (!selectedMods.Value.Any())
return false;
if (newMods?.SetEquals(selectedMods.Value) == false)
return true;
return presetPanel.CheckCurrentModCanBeSave();
}
protected override void LoadComplete()
{
base.LoadComplete();
ScheduleAfterChildren(() => GetContainingInputManager().ChangeFocus(nameTextBox));
}
private void save()
{
presetPanel.Preset.PerformWrite(s =>
{
s.Name = nameTextBox.Current.Value;
s.Description = descriptionTextBox.Current.Value;
if (newMods != null)
s.Mods = newMods;
});
this.HidePopover();
}
}
}