// Copyright (c) 2007-2018 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE 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 osu.Framework.Input.Events; namespace osu.Game.Overlays.Mods { public abstract class ModSection : Container { private readonly OsuSpriteText headerLabel; public FillFlowContainer ButtonsContainer { get; } public Action Action; protected abstract Key[] ToggleKeys { get; } public abstract ModType ModType { get; } public string Header { get => headerLabel.Text; set => headerLabel.Text = value; } public IEnumerable SelectedMods => buttons.Select(b => b.SelectedMod).Where(m => m != null); public IEnumerable Mods { set { var modContainers = value.Select(m => { if (m == null) return new ModButtonEmpty(); return new ModButton(m) { SelectionChanged = Action, }; }).ToArray(); ButtonsContainer.Children = modContainers; buttons = modContainers.OfType().ToArray(); } } private ModButton[] buttons = { }; protected override bool OnKeyDown(KeyDownEvent e) { 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); } public void DeselectAll() => DeselectTypes(buttons.Select(b => b.SelectedMod?.GetType()).Where(t => t != null)); /// /// 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); } } } /// /// Select one or more mods in this section and deselects all other ones. /// /// The types of s which should be selected. public void SelectTypes(IEnumerable modTypes) { foreach (var button in buttons) { int i = Array.FindIndex(button.Mods, m => modTypes.Any(t => t.IsInstanceOfType(m))); if (i >= 0) button.SelectAt(i); else button.Deselect(); } } protected ModSection() { AutoSizeAxes = Axes.Y; RelativeSizeAxes = Axes.X; Origin = Anchor.TopCentre; Anchor = Anchor.TopCentre; Children = new Drawable[] { headerLabel = new OsuSpriteText { Origin = Anchor.TopLeft, Anchor = Anchor.TopLeft, Position = new Vector2(0f, 0f), Font = @"Exo2.0-Bold" }, ButtonsContainer = new FillFlowContainer { AutoSizeAxes = Axes.Both, Origin = Anchor.BottomLeft, Anchor = Anchor.BottomLeft, Spacing = new Vector2(50f, 0f), Margin = new MarginPadding { Top = 6, }, AlwaysPresent = true }, }; } } }