// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. using osuTK; using osuTK.Input; 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; using System.Threading; using Humanizer; using osu.Framework.Input.Events; using osu.Game.Graphics; namespace osu.Game.Overlays.Mods { public class ModSection : CompositeDrawable { private readonly Drawable header; public FillFlowContainer ButtonsContainer { get; } public Action Action; public Key[] ToggleKeys; public readonly ModType ModType; public IEnumerable SelectedMods => buttons.Select(b => b.SelectedMod).Where(m => m != null); private CancellationTokenSource modsLoadCts; /// /// True when all mod icons have completed loading. /// public bool ModIconsLoaded { get; private set; } = true; public IEnumerable Mods { set { var modContainers = value.Select(m => { if (m == null) return new ModButtonEmpty(); return new ModButton(m) { SelectionChanged = Action, }; }).ToArray(); modsLoadCts?.Cancel(); if (modContainers.Length == 0) { ModIconsLoaded = true; header.Hide(); Hide(); return; } ModIconsLoaded = false; LoadComponentsAsync(modContainers, c => { ModIconsLoaded = true; ButtonsContainer.ChildrenEnumerable = c; }, (modsLoadCts = new CancellationTokenSource()).Token); buttons = modContainers.OfType().ToArray(); header.FadeIn(200); this.FadeIn(200); } } private ModButton[] buttons = Array.Empty(); protected override bool OnKeyDown(KeyDownEvent e) { if (e.ControlPressed) return false; if (ToggleKeys != null) { var index = Array.IndexOf(ToggleKeys, e.Key); if (index > -1 && index < buttons.Length) buttons[index].SelectNext(e.ShiftPressed ? -1 : 1); } return base.OnKeyDown(e); } /// /// Selects all mods. /// public void SelectAll() { foreach (var button in buttons.Where(b => !b.Selected)) button.SelectAt(0); } /// /// Deselects all mods. /// /// Set to true to bypass animations and update selections immediately. public void DeselectAll(bool immediate = false) => DeselectTypes(buttons.Select(b => b.SelectedMod?.GetType()).Where(t => t != null), immediate); /// /// Deselect one or more mods in this section. /// /// The types of s which should be deselected. /// Set to true to bypass animations and update selections immediately. public void DeselectTypes(IEnumerable modTypes, bool immediate = false) { int delay = 0; foreach (var button in buttons) { Mod selected = button.SelectedMod; if (selected == null) continue; foreach (var type in modTypes) { if (type.IsInstanceOfType(selected)) { if (immediate) button.Deselect(); else Scheduler.AddDelayed(button.Deselect, delay += 50); } } } } /// /// Updates all buttons with the given list of selected mods. /// /// The new list of selected mods to select. public void UpdateSelectedButtons(IReadOnlyList newSelectedMods) { foreach (var button in buttons) updateButtonSelection(button, newSelectedMods); } private void updateButtonSelection(ModButton button, IReadOnlyList newSelectedMods) { foreach (var mod in newSelectedMods) { var index = Array.FindIndex(button.Mods, m1 => mod.GetType() == m1.GetType()); if (index < 0) continue; var buttonMod = button.Mods[index]; buttonMod.CopyFrom(mod); button.SelectAt(index); return; } button.Deselect(); } public ModSection(ModType type) { ModType = type; AutoSizeAxes = Axes.Y; RelativeSizeAxes = Axes.X; Origin = Anchor.TopCentre; Anchor = Anchor.TopCentre; InternalChildren = new[] { header = CreateHeader(type.Humanize(LetterCasing.Title)), ButtonsContainer = new FillFlowContainer { AutoSizeAxes = Axes.Y, RelativeSizeAxes = Axes.X, Origin = Anchor.BottomLeft, Anchor = Anchor.BottomLeft, Spacing = new Vector2(50f, 0f), Margin = new MarginPadding { Top = 20, }, AlwaysPresent = true }, }; } protected virtual Drawable CreateHeader(string text) => new OsuSpriteText { Font = OsuFont.GetFont(weight: FontWeight.Bold), Text = text }; } }