2019-01-24 16:43:03 +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.
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-11-20 15:51:59 +08:00
|
|
|
using osuTK;
|
|
|
|
using osuTK.Input;
|
2018-04-13 17:19:50 +08:00
|
|
|
using osu.Framework.Graphics;
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
using osu.Game.Rulesets.Mods;
|
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Collections.Generic;
|
2019-06-20 22:06:07 +08:00
|
|
|
using System.Threading;
|
2018-10-02 11:02:47 +08:00
|
|
|
using osu.Framework.Input.Events;
|
2019-02-12 12:04:46 +08:00
|
|
|
using osu.Game.Graphics;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
namespace osu.Game.Overlays.Mods
|
|
|
|
{
|
|
|
|
public abstract class ModSection : Container
|
|
|
|
{
|
|
|
|
private readonly OsuSpriteText headerLabel;
|
|
|
|
|
|
|
|
public FillFlowContainer<ModButtonEmpty> ButtonsContainer { get; }
|
|
|
|
|
|
|
|
public Action<Mod> Action;
|
|
|
|
protected abstract Key[] ToggleKeys { get; }
|
|
|
|
public abstract ModType ModType { get; }
|
|
|
|
|
|
|
|
public string Header
|
|
|
|
{
|
|
|
|
get => headerLabel.Text;
|
|
|
|
set => headerLabel.Text = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<Mod> SelectedMods => buttons.Select(b => b.SelectedMod).Where(m => m != null);
|
|
|
|
|
2019-06-20 22:06:07 +08:00
|
|
|
private CancellationTokenSource modsLoadCts;
|
|
|
|
|
2019-06-20 23:12:39 +08:00
|
|
|
/// <summary>
|
|
|
|
/// True when all mod icons have completed loading.
|
|
|
|
/// </summary>
|
|
|
|
public bool ModIconsLoaded { get; private set; } = true;
|
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
public IEnumerable<Mod> Mods
|
|
|
|
{
|
|
|
|
set
|
|
|
|
{
|
|
|
|
var modContainers = value.Select(m =>
|
|
|
|
{
|
|
|
|
if (m == null)
|
|
|
|
return new ModButtonEmpty();
|
|
|
|
|
|
|
|
return new ModButton(m)
|
|
|
|
{
|
|
|
|
SelectionChanged = Action,
|
|
|
|
};
|
|
|
|
}).ToArray();
|
|
|
|
|
2019-06-20 22:06:07 +08:00
|
|
|
modsLoadCts?.Cancel();
|
2019-12-06 18:04:55 +08:00
|
|
|
|
|
|
|
if (modContainers.Length == 0)
|
|
|
|
{
|
|
|
|
ModIconsLoaded = true;
|
|
|
|
headerLabel.Hide();
|
|
|
|
Hide();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-20 23:12:39 +08:00
|
|
|
ModIconsLoaded = false;
|
|
|
|
|
|
|
|
LoadComponentsAsync(modContainers, c =>
|
|
|
|
{
|
|
|
|
ModIconsLoaded = true;
|
|
|
|
ButtonsContainer.ChildrenEnumerable = c;
|
|
|
|
}, (modsLoadCts = new CancellationTokenSource()).Token);
|
2019-06-20 22:06:07 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
buttons = modContainers.OfType<ModButton>().ToArray();
|
2019-06-14 00:12:56 +08:00
|
|
|
|
2019-12-06 18:04:55 +08:00
|
|
|
headerLabel.FadeIn(200);
|
|
|
|
this.FadeIn(200);
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 21:41:29 +08:00
|
|
|
private ModButton[] buttons = Array.Empty<ModButton>();
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
protected override bool OnKeyDown(KeyDownEvent e)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
2020-11-10 16:57:57 +08:00
|
|
|
if (e.ControlPressed) return false;
|
|
|
|
|
2018-07-31 17:00:42 +08:00
|
|
|
if (ToggleKeys != null)
|
|
|
|
{
|
2018-10-02 11:02:47 +08:00
|
|
|
var index = Array.IndexOf(ToggleKeys, e.Key);
|
2018-07-31 17:00:42 +08:00
|
|
|
if (index > -1 && index < buttons.Length)
|
2018-10-02 11:44:14 +08:00
|
|
|
buttons[index].SelectNext(e.ShiftPressed ? -1 : 1);
|
2018-07-31 17:00:42 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
2018-10-02 11:02:47 +08:00
|
|
|
return base.OnKeyDown(e);
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DeselectAll() => DeselectTypes(buttons.Select(b => b.SelectedMod?.GetType()).Where(t => t != null));
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Deselect one or more mods in this section.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="modTypes">The types of <see cref="Mod"/>s which should be deselected.</param>
|
|
|
|
/// <param name="immediate">Set to true to bypass animations and update selections immediately.</param>
|
|
|
|
public void DeselectTypes(IEnumerable<Type> modTypes, bool immediate = false)
|
|
|
|
{
|
|
|
|
int delay = 0;
|
2019-04-01 11:16:05 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
foreach (var button in buttons)
|
|
|
|
{
|
|
|
|
Mod selected = button.SelectedMod;
|
|
|
|
if (selected == null) continue;
|
2019-02-28 12:31:40 +08:00
|
|
|
|
2018-04-13 17:19:50 +08:00
|
|
|
foreach (var type in modTypes)
|
2019-11-11 19:53:22 +08:00
|
|
|
{
|
2018-04-13 17:19:50 +08:00
|
|
|
if (type.IsInstanceOfType(selected))
|
|
|
|
{
|
|
|
|
if (immediate)
|
|
|
|
button.Deselect();
|
|
|
|
else
|
|
|
|
Scheduler.AddDelayed(button.Deselect, delay += 50);
|
|
|
|
}
|
2019-11-11 19:53:22 +08:00
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2021-01-01 08:47:13 +08:00
|
|
|
/// Updates all buttons with the given list of selected mods.
|
2018-04-13 17:19:50 +08:00
|
|
|
/// </summary>
|
2021-01-01 20:34:09 +08:00
|
|
|
/// <param name="newSelectedMods">The new list of selected mods to select.</param>
|
2021-01-01 08:47:13 +08:00
|
|
|
public void UpdateSelectedMods(IReadOnlyList<Mod> newSelectedMods)
|
2018-04-13 17:19:50 +08:00
|
|
|
{
|
|
|
|
foreach (var button in buttons)
|
2021-01-01 21:16:00 +08:00
|
|
|
updateButtonMods(button, newSelectedMods);
|
|
|
|
}
|
2021-01-01 08:47:13 +08:00
|
|
|
|
2021-01-01 21:16:00 +08:00
|
|
|
private void updateButtonMods(ModButton button, IReadOnlyList<Mod> newSelectedMods)
|
|
|
|
{
|
|
|
|
foreach (var mod in newSelectedMods)
|
|
|
|
{
|
|
|
|
var index = Array.FindIndex(button.Mods, m1 => mod.GetType() == m1.GetType());
|
2021-01-01 08:47:13 +08:00
|
|
|
if (index < 0)
|
2021-01-01 21:16:00 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
var buttonMod = button.Mods[index];
|
|
|
|
buttonMod.CopyFrom(mod);
|
|
|
|
button.SelectAt(index);
|
|
|
|
return;
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
2021-01-01 21:16:00 +08:00
|
|
|
|
|
|
|
button.Deselect();
|
2018-04-13 17:19:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
protected ModSection()
|
|
|
|
{
|
|
|
|
AutoSizeAxes = Axes.Y;
|
2018-07-31 17:00:42 +08:00
|
|
|
RelativeSizeAxes = Axes.X;
|
|
|
|
|
|
|
|
Origin = Anchor.TopCentre;
|
|
|
|
Anchor = Anchor.TopCentre;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
{
|
|
|
|
headerLabel = new OsuSpriteText
|
|
|
|
{
|
|
|
|
Origin = Anchor.TopLeft,
|
|
|
|
Anchor = Anchor.TopLeft,
|
|
|
|
Position = new Vector2(0f, 0f),
|
2019-02-12 12:04:46 +08:00
|
|
|
Font = OsuFont.GetFont(weight: FontWeight.Bold)
|
2018-04-13 17:19:50 +08:00
|
|
|
},
|
|
|
|
ButtonsContainer = new FillFlowContainer<ModButtonEmpty>
|
|
|
|
{
|
2019-11-03 23:32:47 +08:00
|
|
|
AutoSizeAxes = Axes.Y,
|
|
|
|
RelativeSizeAxes = Axes.X,
|
2018-04-13 17:19:50 +08:00
|
|
|
Origin = Anchor.BottomLeft,
|
|
|
|
Anchor = Anchor.BottomLeft,
|
|
|
|
Spacing = new Vector2(50f, 0f),
|
|
|
|
Margin = new MarginPadding
|
|
|
|
{
|
2019-11-23 02:23:48 +08:00
|
|
|
Top = 20,
|
2018-04-13 17:19:50 +08:00
|
|
|
},
|
|
|
|
AlwaysPresent = true
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|