1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 00:47:24 +08:00
osu-lazer/osu.Game/Overlays/Mods/ModSection.cs

153 lines
5.1 KiB
C#
Raw Normal View History

2018-01-05 19:21:19 +08:00
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
2017-02-23 10:16:23 +08:00
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Input;
2017-02-23 10:16:23 +08:00
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input;
2017-02-23 10:16:23 +08:00
using osu.Game.Graphics.Sprites;
2017-04-18 15:05:58 +08:00
using osu.Game.Rulesets.Mods;
using System;
using System.Linq;
using System.Collections.Generic;
2017-02-23 10:16:23 +08:00
namespace osu.Game.Overlays.Mods
{
2017-03-07 00:12:06 +08:00
public abstract class ModSection : Container
2017-02-23 10:16:23 +08:00
{
private readonly OsuSpriteText headerLabel;
2017-02-23 10:16:23 +08:00
public FillFlowContainer<ModButtonEmpty> ButtonsContainer { get; }
2017-02-23 10:16:23 +08:00
public Action<Mod> Action;
2017-03-07 00:12:06 +08:00
protected abstract Key[] ToggleKeys { get; }
2017-03-07 00:19:38 +08:00
public abstract ModType ModType { get; }
2017-03-07 00:12:06 +08:00
2017-02-23 10:16:23 +08:00
public string Header
{
2018-03-14 19:38:22 +08:00
get => headerLabel.Text;
set => headerLabel.Text = value;
2017-02-23 10:16:23 +08:00
}
public IEnumerable<Mod> SelectedMods => buttons.Select(b => b.SelectedMod).Where(m => m != null);
public IEnumerable<Mod> Mods
2017-02-23 10:16:23 +08:00
{
set
{
var modContainers = value.Select(m =>
2017-02-23 10:16:23 +08:00
{
if (m == null)
return new ModButtonEmpty();
2018-03-14 19:38:22 +08:00
return new ModButton(m)
{
SelectedColour = selectedColour,
SelectionChanged = Action,
};
}).ToArray();
2017-02-23 10:16:23 +08:00
ButtonsContainer.Children = modContainers;
buttons = modContainers.OfType<ModButton>().ToArray();
2017-02-23 10:16:23 +08:00
}
}
private ModButton[] buttons = { };
2017-02-23 10:16:23 +08:00
private Color4 selectedColour = Color4.White;
public Color4 SelectedColour
{
2018-03-14 19:38:22 +08:00
get => selectedColour;
2017-02-23 10:16:23 +08:00
set
{
if (value == selectedColour) return;
selectedColour = value;
foreach (ModButton button in buttons)
button.SelectedColour = value;
}
}
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
{
var index = Array.IndexOf(ToggleKeys, args.Key);
if (index > -1 && index < buttons.Length)
2018-01-02 15:55:03 +08:00
buttons[index].SelectNext(state.Keyboard.ShiftPressed ? -1 : 1);
return base.OnKeyDown(state, args);
}
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;
foreach (var button in buttons)
{
Mod selected = button.SelectedMod;
if (selected == null) continue;
2018-03-14 19:38:22 +08:00
foreach (var type in modTypes)
if (type.IsInstanceOfType(selected))
{
if (immediate)
button.Deselect();
else
2018-03-14 19:38:22 +08:00
Scheduler.AddDelayed(button.Deselect, delay += 50);
}
2017-02-23 10:16:23 +08:00
}
}
/// <summary>
/// Select one or more mods in this section and deselects all other ones.
/// </summary>
/// <param name="modTypes">The types of <see cref="Mod"/>s which should be selected.</param>
public void SelectTypes(IEnumerable<Type> 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();
}
}
2017-03-09 16:44:45 +08:00
protected ModSection()
2017-02-23 10:16:23 +08:00
{
AutoSizeAxes = Axes.Y;
Children = new Drawable[]
{
headerLabel = new OsuSpriteText
{
Origin = Anchor.TopLeft,
Anchor = Anchor.TopLeft,
Position = new Vector2(0f, 0f),
2017-03-06 17:14:41 +08:00
Font = @"Exo2.0-Bold"
2017-02-23 10:16:23 +08:00
},
ButtonsContainer = new FillFlowContainer<ModButtonEmpty>
2017-02-23 10:16:23 +08:00
{
AutoSizeAxes = Axes.Both,
Origin = Anchor.BottomLeft,
Anchor = Anchor.BottomLeft,
Spacing = new Vector2(50f, 0f),
Margin = new MarginPadding
{
Top = 6,
},
2017-03-07 00:12:06 +08:00
AlwaysPresent = true
2017-02-23 10:16:23 +08:00
},
};
}
}
}