2022-02-20 00:52:16 +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.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
2022-03-01 04:36:13 +08:00
|
|
|
using System.Threading.Tasks;
|
2022-02-20 00:52:16 +08:00
|
|
|
using Humanizer;
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
using osu.Framework.Bindables;
|
2022-02-20 01:33:13 +08:00
|
|
|
using osu.Framework.Extensions.Color4Extensions;
|
2022-02-20 00:52:16 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Colour;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Framework.Graphics.Shapes;
|
2022-02-20 01:33:13 +08:00
|
|
|
using osu.Framework.Graphics.Sprites;
|
2022-02-20 21:10:35 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2022-02-20 00:52:16 +08:00
|
|
|
using osu.Game.Graphics;
|
|
|
|
using osu.Game.Graphics.Containers;
|
2022-02-20 01:33:13 +08:00
|
|
|
using osu.Game.Graphics.UserInterface;
|
2022-02-20 00:52:16 +08:00
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
using osu.Game.Utils;
|
|
|
|
using osuTK;
|
2022-02-20 01:33:13 +08:00
|
|
|
using osuTK.Graphics;
|
2022-02-20 21:10:35 +08:00
|
|
|
using osuTK.Input;
|
2022-02-20 00:52:16 +08:00
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Mods
|
|
|
|
{
|
|
|
|
public class ModColumn : CompositeDrawable
|
|
|
|
{
|
2022-02-20 20:40:52 +08:00
|
|
|
private Func<Mod, bool>? filter;
|
|
|
|
|
2022-03-01 04:39:21 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Function determining whether each mod in the column should be displayed.
|
|
|
|
/// A return value of <see langword="true"/> means that the mod is not filtered and therefore its corresponding panel should be displayed.
|
|
|
|
/// A return value of <see langword="false"/> means that the mod is filtered out and therefore its corresponding panel should be hidden.
|
|
|
|
/// </summary>
|
2022-02-20 20:40:52 +08:00
|
|
|
public Func<Mod, bool>? Filter
|
|
|
|
{
|
|
|
|
get => filter;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
filter = value;
|
|
|
|
updateFilter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-27 05:43:17 +08:00
|
|
|
public Action<Mod, bool>? ModStateChanged { get; set; }
|
|
|
|
|
2022-02-20 00:52:16 +08:00
|
|
|
private readonly ModType modType;
|
2022-02-20 21:10:35 +08:00
|
|
|
private readonly Key[]? toggleKeys;
|
2022-02-20 00:52:16 +08:00
|
|
|
|
|
|
|
private readonly Bindable<Dictionary<ModType, IReadOnlyList<Mod>>> availableMods = new Bindable<Dictionary<ModType, IReadOnlyList<Mod>>>();
|
|
|
|
|
|
|
|
private readonly TextFlowContainer headerText;
|
|
|
|
private readonly Box headerBackground;
|
|
|
|
private readonly Container contentContainer;
|
|
|
|
private readonly Box contentBackground;
|
|
|
|
private readonly FillFlowContainer<ModPanel> panelFlow;
|
2022-02-20 01:33:13 +08:00
|
|
|
private readonly ToggleAllCheckbox? toggleAllCheckbox;
|
2022-02-20 00:52:16 +08:00
|
|
|
|
|
|
|
private Colour4 accentColour;
|
|
|
|
|
2022-03-01 04:36:13 +08:00
|
|
|
private Task? latestLoadTask;
|
|
|
|
internal bool ItemsLoaded => latestLoadTask == null;
|
|
|
|
|
2022-02-28 06:08:31 +08:00
|
|
|
private const float header_height = 42;
|
2022-02-20 00:52:16 +08:00
|
|
|
|
2022-02-20 21:10:35 +08:00
|
|
|
public ModColumn(ModType modType, bool allowBulkSelection, Key[]? toggleKeys = null)
|
2022-02-20 00:52:16 +08:00
|
|
|
{
|
|
|
|
this.modType = modType;
|
2022-02-20 21:10:35 +08:00
|
|
|
this.toggleKeys = toggleKeys;
|
2022-02-20 00:52:16 +08:00
|
|
|
|
2022-02-28 06:08:31 +08:00
|
|
|
Width = 320;
|
2022-02-20 00:52:16 +08:00
|
|
|
RelativeSizeAxes = Axes.Y;
|
|
|
|
Shear = new Vector2(ModPanel.SHEAR_X, 0);
|
|
|
|
CornerRadius = ModPanel.CORNER_RADIUS;
|
|
|
|
Masking = true;
|
|
|
|
|
2022-02-20 01:33:13 +08:00
|
|
|
Container controlContainer;
|
2022-02-20 00:52:16 +08:00
|
|
|
InternalChildren = new Drawable[]
|
|
|
|
{
|
|
|
|
new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Height = header_height + ModPanel.CORNER_RADIUS,
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
headerBackground = new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
Height = header_height + ModPanel.CORNER_RADIUS
|
|
|
|
},
|
|
|
|
headerText = new OsuTextFlowContainer(t =>
|
|
|
|
{
|
2022-02-28 06:08:31 +08:00
|
|
|
t.Font = OsuFont.TorusAlternate.With(size: 17);
|
2022-02-20 00:52:16 +08:00
|
|
|
t.Shadow = false;
|
|
|
|
t.Colour = Colour4.Black;
|
|
|
|
})
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
|
|
|
Shear = new Vector2(-ModPanel.SHEAR_X, 0),
|
|
|
|
Padding = new MarginPadding
|
|
|
|
{
|
2022-02-28 06:08:31 +08:00
|
|
|
Horizontal = 17,
|
2022-02-20 00:52:16 +08:00
|
|
|
Bottom = ModPanel.CORNER_RADIUS
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Padding = new MarginPadding { Top = header_height },
|
|
|
|
Child = contentContainer = new Container
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
Masking = true,
|
|
|
|
CornerRadius = ModPanel.CORNER_RADIUS,
|
2022-02-28 06:08:31 +08:00
|
|
|
BorderThickness = 3,
|
2022-02-20 00:52:16 +08:00
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
contentBackground = new Box
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both
|
|
|
|
},
|
|
|
|
new GridContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
RowDimensions = new[]
|
|
|
|
{
|
2022-02-20 19:18:59 +08:00
|
|
|
new Dimension(GridSizeMode.AutoSize),
|
|
|
|
new Dimension()
|
2022-02-20 00:52:16 +08:00
|
|
|
},
|
|
|
|
Content = new[]
|
|
|
|
{
|
2022-02-20 01:33:13 +08:00
|
|
|
new Drawable[]
|
2022-02-20 00:52:16 +08:00
|
|
|
{
|
2022-02-20 01:33:13 +08:00
|
|
|
controlContainer = new Container
|
|
|
|
{
|
2022-02-20 19:18:59 +08:00
|
|
|
RelativeSizeAxes = Axes.X,
|
2022-02-28 06:08:31 +08:00
|
|
|
Padding = new MarginPadding { Horizontal = 14 }
|
2022-02-20 01:33:13 +08:00
|
|
|
}
|
2022-02-20 00:52:16 +08:00
|
|
|
},
|
|
|
|
new Drawable[]
|
|
|
|
{
|
|
|
|
new OsuScrollContainer
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
ScrollbarOverlapsContent = false,
|
|
|
|
Child = panelFlow = new FillFlowContainer<ModPanel>
|
|
|
|
{
|
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
AutoSizeAxes = Axes.Y,
|
2022-02-28 06:08:31 +08:00
|
|
|
Spacing = new Vector2(0, 7),
|
|
|
|
Padding = new MarginPadding(7)
|
2022-02-20 00:52:16 +08:00
|
|
|
}
|
|
|
|
}
|
2022-02-20 19:18:59 +08:00
|
|
|
}
|
2022-02-20 00:52:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
createHeaderText();
|
2022-02-20 01:33:13 +08:00
|
|
|
|
|
|
|
if (allowBulkSelection)
|
|
|
|
{
|
2022-02-28 06:08:31 +08:00
|
|
|
controlContainer.Height = 35;
|
2022-02-20 01:45:04 +08:00
|
|
|
controlContainer.Add(toggleAllCheckbox = new ToggleAllCheckbox(this)
|
2022-02-20 01:33:13 +08:00
|
|
|
{
|
|
|
|
Anchor = Anchor.CentreLeft,
|
|
|
|
Origin = Anchor.CentreLeft,
|
2022-02-28 06:08:31 +08:00
|
|
|
Scale = new Vector2(0.8f),
|
2022-02-20 01:33:13 +08:00
|
|
|
RelativeSizeAxes = Axes.X,
|
|
|
|
LabelText = "Enable All",
|
|
|
|
Shear = new Vector2(-ModPanel.SHEAR_X, 0)
|
|
|
|
});
|
2022-02-20 19:18:59 +08:00
|
|
|
panelFlow.Padding = new MarginPadding
|
|
|
|
{
|
|
|
|
Top = 0,
|
2022-02-28 06:08:31 +08:00
|
|
|
Bottom = 7,
|
|
|
|
Horizontal = 7
|
2022-02-20 19:18:59 +08:00
|
|
|
};
|
2022-02-20 01:33:13 +08:00
|
|
|
}
|
2022-02-20 00:52:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private void createHeaderText()
|
|
|
|
{
|
|
|
|
IEnumerable<string> headerTextWords = modType.Humanize(LetterCasing.Title).Split(' ');
|
|
|
|
|
|
|
|
if (headerTextWords.Count() > 1)
|
|
|
|
{
|
|
|
|
headerText.AddText($"{headerTextWords.First()} ", t => t.Font = t.Font.With(weight: FontWeight.SemiBold));
|
|
|
|
headerTextWords = headerTextWords.Skip(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
headerText.AddText(string.Join(' ', headerTextWords));
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load(OsuGameBase game, OverlayColourProvider colourProvider, OsuColour colours)
|
|
|
|
{
|
|
|
|
availableMods.BindTo(game.AvailableMods);
|
|
|
|
|
|
|
|
headerBackground.Colour = accentColour = colours.ForModType(modType);
|
|
|
|
|
2022-02-20 01:33:13 +08:00
|
|
|
if (toggleAllCheckbox != null)
|
|
|
|
{
|
|
|
|
toggleAllCheckbox.AccentColour = accentColour;
|
|
|
|
toggleAllCheckbox.AccentHoverColour = accentColour.Lighten(0.3f);
|
|
|
|
}
|
|
|
|
|
2022-02-20 00:52:16 +08:00
|
|
|
contentContainer.BorderColour = ColourInfo.GradientVertical(colourProvider.Background4, colourProvider.Background3);
|
|
|
|
contentBackground.Colour = colourProvider.Background4;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void LoadComplete()
|
|
|
|
{
|
|
|
|
base.LoadComplete();
|
2022-03-01 04:46:58 +08:00
|
|
|
availableMods.BindValueChanged(_ => Scheduler.AddOnce(updateMods));
|
|
|
|
updateMods();
|
2022-02-20 00:52:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
private CancellationTokenSource? cancellationTokenSource;
|
|
|
|
|
|
|
|
private void updateMods()
|
|
|
|
{
|
|
|
|
var newMods = ModUtils.FlattenMods(availableMods.Value.GetValueOrDefault(modType) ?? Array.Empty<Mod>()).ToList();
|
|
|
|
|
|
|
|
if (newMods.SequenceEqual(panelFlow.Children.Select(p => p.Mod)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
cancellationTokenSource?.Cancel();
|
|
|
|
|
|
|
|
var panels = newMods.Select(mod => new ModPanel(mod)
|
|
|
|
{
|
|
|
|
Shear = new Vector2(-ModPanel.SHEAR_X, 0)
|
|
|
|
});
|
|
|
|
|
2022-03-01 04:36:13 +08:00
|
|
|
Task? loadTask;
|
|
|
|
|
|
|
|
latestLoadTask = loadTask = LoadComponentsAsync(panels, loaded =>
|
2022-02-20 00:52:16 +08:00
|
|
|
{
|
|
|
|
panelFlow.ChildrenEnumerable = loaded;
|
2022-02-20 20:40:52 +08:00
|
|
|
|
2022-02-20 01:45:04 +08:00
|
|
|
foreach (var panel in panelFlow)
|
2022-03-27 05:43:17 +08:00
|
|
|
{
|
|
|
|
panel.Active.BindValueChanged(_ =>
|
|
|
|
{
|
|
|
|
updateToggleState();
|
|
|
|
ModStateChanged?.Invoke(panel.Mod, panel.Active.Value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-02-20 01:45:04 +08:00
|
|
|
updateToggleState();
|
2022-02-20 20:40:52 +08:00
|
|
|
|
|
|
|
updateFilter();
|
2022-02-20 00:52:16 +08:00
|
|
|
}, (cancellationTokenSource = new CancellationTokenSource()).Token);
|
2022-03-01 04:36:13 +08:00
|
|
|
loadTask.ContinueWith(_ =>
|
|
|
|
{
|
|
|
|
if (loadTask == latestLoadTask)
|
|
|
|
latestLoadTask = null;
|
|
|
|
});
|
2022-02-20 00:52:16 +08:00
|
|
|
}
|
2022-02-20 01:33:13 +08:00
|
|
|
|
2022-02-20 01:45:04 +08:00
|
|
|
#region Bulk select / deselect
|
|
|
|
|
|
|
|
private const double initial_multiple_selection_delay = 120;
|
|
|
|
|
|
|
|
private double selectionDelay = initial_multiple_selection_delay;
|
|
|
|
private double lastSelection;
|
|
|
|
|
|
|
|
private readonly Queue<Action> pendingSelectionOperations = new Queue<Action>();
|
|
|
|
|
2022-02-20 20:40:52 +08:00
|
|
|
protected bool SelectionAnimationRunning => pendingSelectionOperations.Count > 0;
|
|
|
|
|
2022-02-20 01:45:04 +08:00
|
|
|
protected override void Update()
|
|
|
|
{
|
|
|
|
base.Update();
|
|
|
|
|
|
|
|
if (selectionDelay == initial_multiple_selection_delay || Time.Current - lastSelection >= selectionDelay)
|
|
|
|
{
|
|
|
|
if (pendingSelectionOperations.TryDequeue(out var dequeuedAction))
|
|
|
|
{
|
|
|
|
dequeuedAction();
|
|
|
|
|
|
|
|
// each time we play an animation, we decrease the time until the next animation (to ramp the visual and audible elements).
|
|
|
|
selectionDelay = Math.Max(30, selectionDelay * 0.8f);
|
|
|
|
lastSelection = Time.Current;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// reset the selection delay after all animations have been completed.
|
|
|
|
// this will cause the next action to be immediately performed.
|
|
|
|
selectionDelay = initial_multiple_selection_delay;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateToggleState()
|
|
|
|
{
|
2022-02-20 20:40:52 +08:00
|
|
|
if (toggleAllCheckbox != null && !SelectionAnimationRunning)
|
2022-02-20 21:14:19 +08:00
|
|
|
{
|
|
|
|
toggleAllCheckbox.Alpha = panelFlow.Any(panel => !panel.Filtered.Value) ? 1 : 0;
|
2022-02-20 20:40:52 +08:00
|
|
|
toggleAllCheckbox.Current.Value = panelFlow.Where(panel => !panel.Filtered.Value).All(panel => panel.Active.Value);
|
2022-02-20 21:14:19 +08:00
|
|
|
}
|
2022-02-20 01:45:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Selects all mods.
|
|
|
|
/// </summary>
|
|
|
|
public void SelectAll()
|
|
|
|
{
|
|
|
|
pendingSelectionOperations.Clear();
|
|
|
|
|
2022-02-20 20:40:52 +08:00
|
|
|
foreach (var button in panelFlow.Where(b => !b.Active.Value && !b.Filtered.Value))
|
2022-02-20 01:45:04 +08:00
|
|
|
pendingSelectionOperations.Enqueue(() => button.Active.Value = true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deselects all mods.
|
|
|
|
/// </summary>
|
|
|
|
public void DeselectAll()
|
|
|
|
{
|
|
|
|
pendingSelectionOperations.Clear();
|
|
|
|
|
2022-02-20 20:40:52 +08:00
|
|
|
foreach (var button in panelFlow.Where(b => b.Active.Value && !b.Filtered.Value))
|
2022-02-20 01:45:04 +08:00
|
|
|
pendingSelectionOperations.Enqueue(() => button.Active.Value = false);
|
|
|
|
}
|
|
|
|
|
2022-02-20 01:33:13 +08:00
|
|
|
private class ToggleAllCheckbox : OsuCheckbox
|
|
|
|
{
|
|
|
|
private Color4 accentColour;
|
|
|
|
|
|
|
|
public Color4 AccentColour
|
|
|
|
{
|
|
|
|
get => accentColour;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
accentColour = value;
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Color4 accentHoverColour;
|
|
|
|
|
|
|
|
public Color4 AccentHoverColour
|
|
|
|
{
|
|
|
|
get => accentHoverColour;
|
|
|
|
set
|
|
|
|
{
|
|
|
|
accentHoverColour = value;
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-20 01:45:04 +08:00
|
|
|
private readonly ModColumn column;
|
|
|
|
|
|
|
|
public ToggleAllCheckbox(ModColumn column)
|
2022-02-20 01:33:13 +08:00
|
|
|
: base(false)
|
|
|
|
{
|
2022-02-20 01:45:04 +08:00
|
|
|
this.column = column;
|
2022-02-20 01:33:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void ApplyLabelParameters(SpriteText text)
|
|
|
|
{
|
|
|
|
base.ApplyLabelParameters(text);
|
|
|
|
text.Font = text.Font.With(weight: FontWeight.SemiBold);
|
|
|
|
}
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
private void load()
|
|
|
|
{
|
|
|
|
updateState();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void updateState()
|
|
|
|
{
|
|
|
|
Nub.AccentColour = AccentColour;
|
|
|
|
Nub.GlowingAccentColour = AccentHoverColour;
|
|
|
|
Nub.GlowColour = AccentHoverColour.Opacity(0.2f);
|
|
|
|
}
|
2022-02-20 01:45:04 +08:00
|
|
|
|
|
|
|
protected override void OnUserChange(bool value)
|
|
|
|
{
|
|
|
|
if (value)
|
|
|
|
column.SelectAll();
|
|
|
|
else
|
|
|
|
column.DeselectAll();
|
|
|
|
}
|
2022-02-20 01:33:13 +08:00
|
|
|
}
|
2022-02-20 01:45:04 +08:00
|
|
|
|
|
|
|
#endregion
|
2022-02-20 20:40:52 +08:00
|
|
|
|
|
|
|
#region Filtering support
|
|
|
|
|
|
|
|
private void updateFilter()
|
|
|
|
{
|
|
|
|
foreach (var modPanel in panelFlow)
|
|
|
|
modPanel.ApplyFilter(Filter);
|
|
|
|
|
|
|
|
updateToggleState();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2022-02-20 21:10:35 +08:00
|
|
|
|
|
|
|
#region Keyboard selection support
|
|
|
|
|
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
|
|
|
{
|
|
|
|
if (e.ControlPressed || e.AltPressed) return false;
|
|
|
|
if (toggleKeys == null) return false;
|
|
|
|
|
|
|
|
int index = Array.IndexOf(toggleKeys, e.Key);
|
|
|
|
if (index < 0) return false;
|
|
|
|
|
|
|
|
var panel = panelFlow.ElementAtOrDefault(index);
|
|
|
|
if (panel == null || panel.Filtered.Value) return false;
|
|
|
|
|
|
|
|
panel.Active.Toggle();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
2022-02-20 00:52:16 +08:00
|
|
|
}
|
|
|
|
}
|