1
0
mirror of https://github.com/ppy/osu.git synced 2024-12-14 23:52:57 +08:00

Refactor mod sections and make them overridable

This commit is contained in:
smoogipoo 2021-02-02 20:11:40 +09:00
parent 328bd191d4
commit 3741f05ab3
7 changed files with 50 additions and 122 deletions

View File

@ -11,26 +11,23 @@ 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 abstract class ModSection : Container
public class ModSection : CompositeDrawable
{
private readonly OsuSpriteText headerLabel;
private readonly Drawable header;
public FillFlowContainer<ModButtonEmpty> ButtonsContainer { get; }
public Action<Mod> Action;
protected abstract Key[] ToggleKeys { get; }
public abstract ModType ModType { get; }
public string Header
{
get => headerLabel.Text;
set => headerLabel.Text = value;
}
public Key[] ToggleKeys;
public readonly ModType ModType;
public IEnumerable<Mod> SelectedMods => buttons.Select(b => b.SelectedMod).Where(m => m != null);
@ -61,7 +58,7 @@ namespace osu.Game.Overlays.Mods
if (modContainers.Length == 0)
{
ModIconsLoaded = true;
headerLabel.Hide();
header.Hide();
Hide();
return;
}
@ -76,7 +73,7 @@ namespace osu.Game.Overlays.Mods
buttons = modContainers.OfType<ModButton>().ToArray();
headerLabel.FadeIn(200);
header.FadeIn(200);
this.FadeIn(200);
}
}
@ -153,23 +150,19 @@ namespace osu.Game.Overlays.Mods
button.Deselect();
}
protected ModSection()
public ModSection(ModType type)
{
ModType = type;
AutoSizeAxes = Axes.Y;
RelativeSizeAxes = Axes.X;
Origin = Anchor.TopCentre;
Anchor = Anchor.TopCentre;
Children = new Drawable[]
InternalChildren = new[]
{
headerLabel = new OsuSpriteText
{
Origin = Anchor.TopLeft,
Anchor = Anchor.TopLeft,
Position = new Vector2(0f, 0f),
Font = OsuFont.GetFont(weight: FontWeight.Bold)
},
header = CreateHeader(type.Humanize(LetterCasing.Title)),
ButtonsContainer = new FillFlowContainer<ModButtonEmpty>
{
AutoSizeAxes = Axes.Y,
@ -185,5 +178,11 @@ namespace osu.Game.Overlays.Mods
},
};
}
protected virtual Drawable CreateHeader(string text) => new OsuSpriteText
{
Font = OsuFont.GetFont(weight: FontWeight.Bold),
Text = text
};
}
}

View File

@ -19,7 +19,6 @@ using osu.Game.Graphics.Containers;
using osu.Game.Graphics.Sprites;
using osu.Game.Graphics.UserInterface;
using osu.Game.Input.Bindings;
using osu.Game.Overlays.Mods.Sections;
using osu.Game.Rulesets.Mods;
using osu.Game.Screens;
using osuTK;
@ -190,13 +189,31 @@ namespace osu.Game.Overlays.Mods
Width = content_width,
LayoutDuration = 200,
LayoutEasing = Easing.OutQuint,
Children = new ModSection[]
Children = new[]
{
new DifficultyReductionSection { Action = modButtonPressed },
new DifficultyIncreaseSection { Action = modButtonPressed },
new AutomationSection { Action = modButtonPressed },
new ConversionSection { Action = modButtonPressed },
new FunSection { Action = modButtonPressed },
CreateModSection(ModType.DifficultyReduction).With(s =>
{
s.ToggleKeys = new[] { Key.Q, Key.W, Key.E, Key.R, Key.T, Key.Y, Key.U, Key.I, Key.O, Key.P };
s.Action = modButtonPressed;
}),
CreateModSection(ModType.DifficultyIncrease).With(s =>
{
s.ToggleKeys = new[] { Key.A, Key.S, Key.D, Key.F, Key.G, Key.H, Key.J, Key.K, Key.L };
s.Action = modButtonPressed;
}),
CreateModSection(ModType.Automation).With(s =>
{
s.ToggleKeys = new[] { Key.Z, Key.X, Key.C, Key.V, Key.B, Key.N, Key.M };
s.Action = modButtonPressed;
}),
CreateModSection(ModType.Conversion).With(s =>
{
s.Action = modButtonPressed;
}),
CreateModSection(ModType.Fun).With(s =>
{
s.Action = modButtonPressed;
}),
}
},
}
@ -454,6 +471,13 @@ namespace osu.Game.Overlays.Mods
private void refreshSelectedMods() => SelectedMods.Value = ModSectionsContainer.Children.SelectMany(s => s.SelectedMods).ToArray();
/// <summary>
/// Creates a <see cref="ModSection"/> that groups <see cref="Mod"/>s with the same <see cref="ModType"/>.
/// </summary>
/// <param name="type">The <see cref="ModType"/> of <see cref="Mod"/>s in the section.</param>
/// <returns>The <see cref="ModSection"/>.</returns>
protected virtual ModSection CreateModSection(ModType type) => new ModSection(type);
#region Disposal
protected override void Dispose(bool isDisposing)

View File

@ -1,19 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Game.Rulesets.Mods;
using osuTK.Input;
namespace osu.Game.Overlays.Mods.Sections
{
public class AutomationSection : ModSection
{
protected override Key[] ToggleKeys => new[] { Key.Z, Key.X, Key.C, Key.V, Key.B, Key.N, Key.M };
public override ModType ModType => ModType.Automation;
public AutomationSection()
{
Header = @"Automation";
}
}
}

View File

@ -1,19 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Game.Rulesets.Mods;
using osuTK.Input;
namespace osu.Game.Overlays.Mods.Sections
{
public class ConversionSection : ModSection
{
protected override Key[] ToggleKeys => null;
public override ModType ModType => ModType.Conversion;
public ConversionSection()
{
Header = @"Conversion";
}
}
}

View File

@ -1,19 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Game.Rulesets.Mods;
using osuTK.Input;
namespace osu.Game.Overlays.Mods.Sections
{
public class DifficultyIncreaseSection : ModSection
{
protected override Key[] ToggleKeys => new[] { Key.A, Key.S, Key.D, Key.F, Key.G, Key.H, Key.J, Key.K, Key.L };
public override ModType ModType => ModType.DifficultyIncrease;
public DifficultyIncreaseSection()
{
Header = @"Difficulty Increase";
}
}
}

View File

@ -1,19 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Game.Rulesets.Mods;
using osuTK.Input;
namespace osu.Game.Overlays.Mods.Sections
{
public class DifficultyReductionSection : ModSection
{
protected override Key[] ToggleKeys => new[] { Key.Q, Key.W, Key.E, Key.R, Key.T, Key.Y, Key.U, Key.I, Key.O, Key.P };
public override ModType ModType => ModType.DifficultyReduction;
public DifficultyReductionSection()
{
Header = @"Difficulty Reduction";
}
}
}

View File

@ -1,19 +0,0 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using osu.Game.Rulesets.Mods;
using osuTK.Input;
namespace osu.Game.Overlays.Mods.Sections
{
public class FunSection : ModSection
{
protected override Key[] ToggleKeys => null;
public override ModType ModType => ModType.Fun;
public FunSection()
{
Header = @"Fun";
}
}
}