mirror of
https://github.com/osukey/osukey.git
synced 2025-06-07 20:37:57 +09:00
Refactor ModSection and ModIcon to avoid null mods.
This commit is contained in:
parent
95f6c999bd
commit
6298b3effd
@ -19,7 +19,11 @@ using System.Linq;
|
|||||||
|
|
||||||
namespace osu.Game.Overlays.Mods
|
namespace osu.Game.Overlays.Mods
|
||||||
{
|
{
|
||||||
public class ModButton : FillFlowContainer
|
|
||||||
|
/// <summary>
|
||||||
|
/// Represents a clickable button which can cycle through one of more mods.
|
||||||
|
/// </summary>
|
||||||
|
public class ModButton : ModButtonEmpty
|
||||||
{
|
{
|
||||||
private ModIcon foregroundIcon { get; set; }
|
private ModIcon foregroundIcon { get; set; }
|
||||||
private readonly SpriteText text;
|
private readonly SpriteText text;
|
||||||
@ -116,7 +120,7 @@ namespace osu.Game.Overlays.Mods
|
|||||||
|
|
||||||
// the mods from Mod, only multiple if Mod is a MultiMod
|
// the mods from Mod, only multiple if Mod is a MultiMod
|
||||||
|
|
||||||
public Mod SelectedMod => Mods.ElementAtOrDefault(selectedIndex);
|
public override Mod SelectedMod => Mods.ElementAtOrDefault(selectedIndex);
|
||||||
|
|
||||||
[BackgroundDependencyLoader]
|
[BackgroundDependencyLoader]
|
||||||
private void load(AudioManager audio)
|
private void load(AudioManager audio)
|
||||||
@ -198,13 +202,8 @@ namespace osu.Game.Overlays.Mods
|
|||||||
buttonColour = foregroundIcon.Colour;
|
buttonColour = foregroundIcon.Colour;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ModButton(Mod m)
|
public ModButton(Mod mod)
|
||||||
{
|
{
|
||||||
Direction = FillDirection.Vertical;
|
|
||||||
Spacing = new Vector2(0f, -5f);
|
|
||||||
Size = new Vector2(100f);
|
|
||||||
AlwaysPresent = true;
|
|
||||||
|
|
||||||
Children = new Drawable[]
|
Children = new Drawable[]
|
||||||
{
|
{
|
||||||
new Container
|
new Container
|
||||||
@ -224,13 +223,14 @@ namespace osu.Game.Overlays.Mods
|
|||||||
},
|
},
|
||||||
text = new OsuSpriteText
|
text = new OsuSpriteText
|
||||||
{
|
{
|
||||||
|
Y = 75,
|
||||||
Origin = Anchor.TopCentre,
|
Origin = Anchor.TopCentre,
|
||||||
Anchor = Anchor.TopCentre,
|
Anchor = Anchor.TopCentre,
|
||||||
TextSize = 18,
|
TextSize = 18,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
Mod = m;
|
Mod = mod;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
23
osu.Game/Overlays/Mods/ModButtonEmpty.cs
Normal file
23
osu.Game/Overlays/Mods/ModButtonEmpty.cs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
||||||
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||||
|
|
||||||
|
using OpenTK;
|
||||||
|
using osu.Framework.Graphics.Containers;
|
||||||
|
using osu.Game.Rulesets.Mods;
|
||||||
|
|
||||||
|
namespace osu.Game.Overlays.Mods
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// A mod button used exclusively for providing an empty space the size of a mod button.
|
||||||
|
/// </summary>
|
||||||
|
public class ModButtonEmpty : Container
|
||||||
|
{
|
||||||
|
public virtual Mod SelectedMod => null;
|
||||||
|
|
||||||
|
public ModButtonEmpty()
|
||||||
|
{
|
||||||
|
Size = new Vector2(100f);
|
||||||
|
AlwaysPresent = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,8 @@ using osu.Framework.Input;
|
|||||||
using osu.Game.Graphics.Sprites;
|
using osu.Game.Graphics.Sprites;
|
||||||
using osu.Game.Rulesets.Mods;
|
using osu.Game.Rulesets.Mods;
|
||||||
using System;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace osu.Game.Overlays.Mods
|
namespace osu.Game.Overlays.Mods
|
||||||
{
|
{
|
||||||
@ -18,7 +20,7 @@ namespace osu.Game.Overlays.Mods
|
|||||||
{
|
{
|
||||||
private readonly OsuSpriteText headerLabel;
|
private readonly OsuSpriteText headerLabel;
|
||||||
|
|
||||||
public FillFlowContainer<ModButton> ButtonsContainer { get; }
|
public FillFlowContainer<ModButtonEmpty> ButtonsContainer { get; }
|
||||||
|
|
||||||
public Action<Mod> Action;
|
public Action<Mod> Action;
|
||||||
protected abstract Key[] ToggleKeys { get; }
|
protected abstract Key[] ToggleKeys { get; }
|
||||||
@ -36,27 +38,30 @@ namespace osu.Game.Overlays.Mods
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private ModButton[] buttons = { };
|
public IEnumerable<Mod> SelectedMods => buttons.Select(b => b.SelectedMod).Where(m => m != null);
|
||||||
public ModButton[] Buttons
|
|
||||||
|
public IEnumerable<Mod> Mods
|
||||||
{
|
{
|
||||||
get
|
|
||||||
{
|
|
||||||
return buttons;
|
|
||||||
}
|
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
if (value == buttons) return;
|
var modContainers = value.Select(m =>
|
||||||
buttons = value;
|
|
||||||
|
|
||||||
foreach (ModButton button in value)
|
|
||||||
{
|
{
|
||||||
button.SelectedColour = selectedColour;
|
if (m == null)
|
||||||
button.Action = Action;
|
return new ModButtonEmpty();
|
||||||
|
else
|
||||||
|
return new ModButton(m)
|
||||||
|
{
|
||||||
|
SelectedColour = selectedColour,
|
||||||
|
Action = Action,
|
||||||
|
};
|
||||||
|
}).ToArray();
|
||||||
|
|
||||||
|
ButtonsContainer.Children = modContainers;
|
||||||
|
buttons = modContainers.OfType<ModButton>().ToArray();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ButtonsContainer.Children = value;
|
private ModButton[] buttons = { };
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private Color4 selectedColour = Color4.White;
|
private Color4 selectedColour = Color4.White;
|
||||||
public Color4 SelectedColour
|
public Color4 SelectedColour
|
||||||
@ -71,17 +76,15 @@ namespace osu.Game.Overlays.Mods
|
|||||||
selectedColour = value;
|
selectedColour = value;
|
||||||
|
|
||||||
foreach (ModButton button in buttons)
|
foreach (ModButton button in buttons)
|
||||||
{
|
|
||||||
button.SelectedColour = value;
|
button.SelectedColour = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
|
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
|
||||||
{
|
{
|
||||||
var index = Array.IndexOf(ToggleKeys, args.Key);
|
var index = Array.IndexOf(ToggleKeys, args.Key);
|
||||||
if (index > -1 && index < Buttons.Length)
|
if (index > -1 && index < buttons.Length)
|
||||||
Buttons[index].SelectNext();
|
buttons[index].SelectNext();
|
||||||
|
|
||||||
return base.OnKeyDown(state, args);
|
return base.OnKeyDown(state, args);
|
||||||
}
|
}
|
||||||
@ -89,7 +92,17 @@ namespace osu.Game.Overlays.Mods
|
|||||||
public void DeselectAll()
|
public void DeselectAll()
|
||||||
{
|
{
|
||||||
foreach (ModButton button in buttons)
|
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();
|
button.Deselect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -107,7 +120,7 @@ namespace osu.Game.Overlays.Mods
|
|||||||
Position = new Vector2(0f, 0f),
|
Position = new Vector2(0f, 0f),
|
||||||
Font = @"Exo2.0-Bold"
|
Font = @"Exo2.0-Bold"
|
||||||
},
|
},
|
||||||
ButtonsContainer = new FillFlowContainer<ModButton>
|
ButtonsContainer = new FillFlowContainer<ModButtonEmpty>
|
||||||
{
|
{
|
||||||
AutoSizeAxes = Axes.Both,
|
AutoSizeAxes = Axes.Both,
|
||||||
Origin = Anchor.BottomLeft,
|
Origin = Anchor.BottomLeft,
|
||||||
|
@ -44,7 +44,7 @@ namespace osu.Game.Overlays.Mods
|
|||||||
var instance = newRuleset.CreateInstance();
|
var instance = newRuleset.CreateInstance();
|
||||||
|
|
||||||
foreach (ModSection section in modSectionsContainer.Children)
|
foreach (ModSection section in modSectionsContainer.Children)
|
||||||
section.Buttons = instance.GetModsFor(section.ModType).Select(m => new ModButton(m)).ToArray();
|
section.Mods = instance.GetModsFor(section.ModType);
|
||||||
refreshSelectedMods();
|
refreshSelectedMods();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,14 +103,7 @@ namespace osu.Game.Overlays.Mods
|
|||||||
{
|
{
|
||||||
if (modTypes.Length == 0) return;
|
if (modTypes.Length == 0) return;
|
||||||
foreach (ModSection section in modSectionsContainer.Children)
|
foreach (ModSection section in modSectionsContainer.Children)
|
||||||
foreach (ModButton button in section.Buttons)
|
section.DeselectTypes(modTypes);
|
||||||
{
|
|
||||||
Mod selected = button.SelectedMod;
|
|
||||||
if (selected == null) continue;
|
|
||||||
foreach (Type type in modTypes)
|
|
||||||
if (type.IsInstanceOfType(selected))
|
|
||||||
button.Deselect();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void modButtonPressed(Mod selectedMod)
|
private void modButtonPressed(Mod selectedMod)
|
||||||
@ -122,7 +115,7 @@ namespace osu.Game.Overlays.Mods
|
|||||||
|
|
||||||
private void refreshSelectedMods()
|
private void refreshSelectedMods()
|
||||||
{
|
{
|
||||||
SelectedMods.Value = modSectionsContainer.Children.SelectMany(s => s.Buttons.Select(x => x.SelectedMod).Where(x => x != null)).ToArray();
|
SelectedMods.Value = modSectionsContainer.Children.SelectMany(s => s.SelectedMods).ToArray();
|
||||||
|
|
||||||
double multiplier = 1.0;
|
double multiplier = 1.0;
|
||||||
bool ranked = true;
|
bool ranked = true;
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
|
||||||
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
||||||
|
|
||||||
|
using System;
|
||||||
using OpenTK.Graphics;
|
using OpenTK.Graphics;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
@ -42,6 +43,8 @@ namespace osu.Game.Rulesets.UI
|
|||||||
|
|
||||||
public ModIcon(Mod mod)
|
public ModIcon(Mod mod)
|
||||||
{
|
{
|
||||||
|
if (mod == null) throw new ArgumentNullException(nameof(mod));
|
||||||
|
|
||||||
Children = new Drawable[]
|
Children = new Drawable[]
|
||||||
{
|
{
|
||||||
background = new TextAwesome
|
background = new TextAwesome
|
||||||
@ -59,7 +62,7 @@ namespace osu.Game.Rulesets.UI
|
|||||||
Anchor = Anchor.Centre,
|
Anchor = Anchor.Centre,
|
||||||
Colour = OsuColour.Gray(84),
|
Colour = OsuColour.Gray(84),
|
||||||
TextSize = 20,
|
TextSize = 20,
|
||||||
Icon = (mod != null) ? mod.Icon : FontAwesome.fa_question,
|
Icon = mod.Icon
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -74,7 +77,7 @@ namespace osu.Game.Rulesets.UI
|
|||||||
|
|
||||||
private Color4 getBackgroundColourFromMod(Mod mod)
|
private Color4 getBackgroundColourFromMod(Mod mod)
|
||||||
{
|
{
|
||||||
switch (mod?.Type)
|
switch (mod.Type)
|
||||||
{
|
{
|
||||||
case ModType.DifficultyIncrease:
|
case ModType.DifficultyIncrease:
|
||||||
return OsuColour.FromHex(@"ffcc22");
|
return OsuColour.FromHex(@"ffcc22");
|
||||||
|
@ -391,6 +391,7 @@
|
|||||||
<Compile Include="Screens\Play\Pause\PauseProgressGraph.cs" />
|
<Compile Include="Screens\Play\Pause\PauseProgressGraph.cs" />
|
||||||
<Compile Include="Overlays\Mods\ModSelectOverlay.cs" />
|
<Compile Include="Overlays\Mods\ModSelectOverlay.cs" />
|
||||||
<Compile Include="Rulesets\Mods\Mod.cs" />
|
<Compile Include="Rulesets\Mods\Mod.cs" />
|
||||||
|
<Compile Include="Overlays\Mods\ModButtonEmpty.cs" />
|
||||||
<Compile Include="Overlays\Mods\ModButton.cs" />
|
<Compile Include="Overlays\Mods\ModButton.cs" />
|
||||||
<Compile Include="Rulesets\UI\ModIcon.cs" />
|
<Compile Include="Rulesets\UI\ModIcon.cs" />
|
||||||
<Compile Include="Overlays\Mods\ModSection.cs" />
|
<Compile Include="Overlays\Mods\ModSection.cs" />
|
||||||
|
Loading…
x
Reference in New Issue
Block a user