Refactor ModSection and ModIcon to avoid null mods.

This commit is contained in:
Dean Herbert
2017-05-05 12:16:41 +09:00
parent 95f6c999bd
commit 6298b3effd
6 changed files with 76 additions and 43 deletions

View File

@ -11,6 +11,8 @@ using osu.Framework.Input;
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets.Mods;
using System;
using System.Linq;
using System.Collections.Generic;
namespace osu.Game.Overlays.Mods
{
@ -18,7 +20,7 @@ namespace osu.Game.Overlays.Mods
{
private readonly OsuSpriteText headerLabel;
public FillFlowContainer<ModButton> ButtonsContainer { get; }
public FillFlowContainer<ModButtonEmpty> ButtonsContainer { get; }
public Action<Mod> Action;
protected abstract Key[] ToggleKeys { get; }
@ -36,28 +38,31 @@ namespace osu.Game.Overlays.Mods
}
}
private ModButton[] buttons = { };
public ModButton[] Buttons
public IEnumerable<Mod> SelectedMods => buttons.Select(b => b.SelectedMod).Where(m => m != null);
public IEnumerable<Mod> Mods
{
get
{
return buttons;
}
set
{
if (value == buttons) return;
buttons = value;
foreach (ModButton button in value)
var modContainers = value.Select(m =>
{
button.SelectedColour = selectedColour;
button.Action = Action;
}
if (m == null)
return new ModButtonEmpty();
else
return new ModButton(m)
{
SelectedColour = selectedColour,
Action = Action,
};
}).ToArray();
ButtonsContainer.Children = value;
ButtonsContainer.Children = modContainers;
buttons = modContainers.OfType<ModButton>().ToArray();
}
}
private ModButton[] buttons = { };
private Color4 selectedColour = Color4.White;
public Color4 SelectedColour
{
@ -71,17 +76,15 @@ namespace osu.Game.Overlays.Mods
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)
Buttons[index].SelectNext();
if (index > -1 && index < buttons.Length)
buttons[index].SelectNext();
return base.OnKeyDown(state, args);
}
@ -89,8 +92,18 @@ namespace osu.Game.Overlays.Mods
public void DeselectAll()
{
foreach (ModButton button in buttons)
{
button.Deselect();
}
public void DeselectTypes(Type[] modTypes)
{
foreach (var button in buttons)
{
Mod selected = button.SelectedMod;
if (selected == null) continue;
foreach (Type type in modTypes)
if (type.IsInstanceOfType(selected))
button.Deselect();
}
}
@ -107,7 +120,7 @@ namespace osu.Game.Overlays.Mods
Position = new Vector2(0f, 0f),
Font = @"Exo2.0-Bold"
},
ButtonsContainer = new FillFlowContainer<ModButton>
ButtonsContainer = new FillFlowContainer<ModButtonEmpty>
{
AutoSizeAxes = Axes.Both,
Origin = Anchor.BottomLeft,