Skip to content

Commit

Permalink
Merge pull request #1814 from andy840119/use-enum-value-instead
Browse files Browse the repository at this point in the history
Use generic Enum and CreateDelegate methods.
  • Loading branch information
andy840119 authored Dec 27, 2022
2 parents 1a32c11 + 36d2304 commit 3781830
Show file tree
Hide file tree
Showing 18 changed files with 33 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Objects.Properties;
using osu.Game.Rulesets.Karaoke.Objects.Types;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Tests.Editor.Utils
{
Expand All @@ -24,7 +23,7 @@ public void TestIsRemoveLyricLocked()
test(new Lyric());

// test lock state.
foreach (var lockState in EnumUtils.GetValues<LockState>())
foreach (var lockState in Enum.GetValues<LockState>())
{
test(new Lyric
{
Expand Down Expand Up @@ -61,7 +60,7 @@ public void TestIsWriteLyricPropertyLocked()
test(new Lyric());

// test lock state.
foreach (var lockState in EnumUtils.GetValues<LockState>())
foreach (var lockState in Enum.GetValues<LockState>())
{
test(new Lyric
{
Expand Down Expand Up @@ -101,7 +100,7 @@ public void TestIsCreateOrRemoveNoteLocked()
test(new Lyric());

// test lock state.
foreach (var lockState in EnumUtils.GetValues<LockState>())
foreach (var lockState in Enum.GetValues<LockState>())
{
test(new Lyric
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Shapes;
using osu.Game.Graphics;
using osu.Game.Rulesets.Karaoke.Graphics.Shapes;
using osu.Game.Rulesets.Karaoke.Utils;
using osu.Game.Tests.Visual;
using osuTK;

Expand Down Expand Up @@ -45,7 +45,7 @@ private void load(OsuColour colours)
[Test]
public void TestRightAngleDirections()
{
foreach (var direction in EnumUtils.GetValues<TriangleRightAngleDirection>())
foreach (var direction in Enum.GetValues<TriangleRightAngleDirection>())
{
AddStep($"Test direction {direction}", () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using osu.Game.Graphics.Sprites;
using osu.Game.Overlays;
using osu.Game.Rulesets.Karaoke.Extensions;
using osu.Game.Rulesets.Karaoke.Utils;
using osu.Game.Tests.Visual;
using osuTK.Graphics;

Expand All @@ -28,8 +27,8 @@ public partial class TestSceneOverlayColourProvider : OsuTestScene
[Test]
public void ShowWithNoFetch()
{
var providers = EnumUtils.GetValues<OverlayColourScheme>()
.Select(x => new OverlayColourProvider(x));
var providers = Enum.GetValues<OverlayColourScheme>()
.Select(x => new OverlayColourProvider(x));
string[] colourName =
{
"Colour1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using osu.Game.Graphics.Sprites;
using osu.Game.Rulesets.Karaoke.Extensions;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics;
using osu.Game.Rulesets.Karaoke.Utils;
using osu.Game.Tests.Visual;
using osuTK.Graphics;

Expand All @@ -28,7 +27,7 @@ public partial class TestSceneLyricEditorColourProvider : OsuTestScene
public void ShowWithNoFetch()
{
var provider = new LyricEditorColourProvider();
var types = EnumUtils.GetValues<LyricEditorMode>();
var types = Enum.GetValues<LyricEditorMode>();

string[] colourName =
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Settings;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.States.Modes;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Tests.Screens.Edit.Beatmap
{
Expand Down Expand Up @@ -109,22 +108,22 @@ public void TestSingerMode()

private void switchToMode(LyricEditorMode mode)
{
AddStep($"switch to mode {Enum.GetName(typeof(LyricEditorMode), mode)}", () =>
AddStep($"switch to mode {Enum.GetName(mode)}", () =>
{
bindableLyricEditorMode.Value = mode;
});
AddWaitStep("wait for switch to new mode", 5);
}

private void clickEditModeButtons<T>() where T : Enum
private void clickEditModeButtons<T>() where T : struct, Enum
{
foreach (var editMode in EnumUtils.GetValues<T>())
foreach (var editMode in Enum.GetValues<T>())
{
clickTargetEditModeButton(editMode);
}
}

private void clickTargetEditModeButton<T>(T editMode) where T : Enum
private void clickTargetEditModeButton<T>(T editMode) where T : struct, Enum
{
AddStep("Click the button", () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using osu.Game.Rulesets.Karaoke.Screens.Edit.Import.Lyrics;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Import.Lyrics.DragFile;
using osu.Game.Rulesets.Karaoke.Tests.Resources;
using osu.Game.Rulesets.Karaoke.Utils;
using osu.Game.Screens.Edit;

namespace osu.Game.Rulesets.Karaoke.Tests.Screens.Edit.Import
Expand Down Expand Up @@ -59,11 +58,11 @@ private void load()
[Test]
public void TestGoToStep()
{
var steps = EnumUtils.GetValues<LyricImporterStep>();
var steps = Enum.GetValues<LyricImporterStep>();

foreach (var step in steps)
{
AddStep($"go to step {Enum.GetName(typeof(LyricImporterStep), step)}", () => { Screen.GoToStep(step); });
AddStep($"go to step {Enum.GetName(step)}", () => { Screen.GoToStep(step); });
}
}

Expand All @@ -88,7 +87,7 @@ public void GoToStep(LyricImporterStep step)
if (step <= lyricSubScreen.Step)
return;

var totalSteps = EnumUtils.GetValues<LyricImporterStep>().Where(x => x > lyricSubScreen.Step && x <= step);
var totalSteps = Enum.GetValues<LyricImporterStep>().Where(x => x > lyricSubScreen.Step && x <= step);

foreach (var gotoStep in totalSteps)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Linq;
using osu.Framework.Extensions;
using osu.Framework.Graphics;
using osu.Game.Rulesets.Judgements;
using osu.Game.Rulesets.Karaoke.UI;
using osu.Game.Rulesets.Karaoke.Utils;
using osu.Game.Rulesets.Objects;
using osu.Game.Rulesets.Scoring;

Expand All @@ -16,7 +16,7 @@ public partial class TestSceneDrawableJudgement : KaraokeSkinnableTestScene
{
public TestSceneDrawableJudgement()
{
foreach (var result in EnumUtils.GetValues<HitResult>().Skip(1))
foreach (var result in Enum.GetValues<HitResult>().Skip(1))
{
AddStep("Show " + result.GetDescription(), () => SetContents(_ =>
new DrawableNoteJudgement(new JudgementResult(new HitObject(), new Judgement()) { Type = result }, null)
Expand Down
13 changes: 0 additions & 13 deletions osu.Game.Rulesets.Karaoke.Tests/Utils/EnumUtilsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,6 @@ namespace osu.Game.Rulesets.Karaoke.Tests.Utils
[TestFixture]
public class EnumUtilsTest
{
[Test]
public void TestGetValues()
{
var expected = new[]
{
TestEnum.Enum1,
TestEnum.Enum2,
TestEnum.Enum3
};
var actual = EnumUtils.GetValues<TestEnum>();
Assert.AreEqual(expected, actual);
}

[TestCase(TestEnum.Enum1, TestEnum.Enum3)]
[TestCase(TestEnum.Enum2, TestEnum.Enum1)]
[TestCase(TestEnum.Enum3, TestEnum.Enum2)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets.Karaoke.Edit.ChangeHandlers;
using osu.Game.Rulesets.Karaoke.Objects;
using osu.Game.Rulesets.Karaoke.Objects.Types;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Edit.Components.ContextMenu
{
Expand All @@ -23,7 +21,7 @@ public LyricLockContextMenu(ILockChangeHandler lockChangeHandler, Lyric lyric, s
public LyricLockContextMenu(ILockChangeHandler lockChangeHandler, List<Lyric> lyrics, string name)
: base(name)
{
Items = EnumUtils.GetValues<LockState>().Select(l => new OsuMenuItem(l.ToString(), anyLyricInLockState(l) ? MenuItemType.Highlighted : MenuItemType.Standard, () =>
Items = Enum.GetValues<LockState>().Select(l => new OsuMenuItem(l.ToString(), anyLyricInLockState(l) ? MenuItemType.Highlighted : MenuItemType.Standard, () =>
{
// todo: how to make lyric as selected?
lockChangeHandler.Lock(l);
Expand Down
5 changes: 1 addition & 4 deletions osu.Game.Rulesets.Karaoke/Edit/Components/Menus/EnumMenu.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) andy840119 <[email protected]>. Licensed under the GPL Licence.
// See the LICENCE file in the repository root for full licence text.

#nullable disable

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -11,7 +9,6 @@
using osu.Framework.Extensions.IEnumerableExtensions;
using osu.Framework.Graphics.UserInterface;
using osu.Game.Graphics.UserInterface;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Edit.Components.Menus
{
Expand Down Expand Up @@ -46,7 +43,7 @@ private ToggleMenuItem[] createMenuItems()
}).ToArray();
}

protected virtual IEnumerable<T> ValidEnums => EnumUtils.GetValues<T>();
protected virtual IEnumerable<T> ValidEnums => Enum.GetValues<T>();

protected string GetName(T selection)
=> selection.GetDescription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Compose.BottomEditor;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Compose.Panels;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.States.Modes;
using osu.Game.Rulesets.Karaoke.Utils;

namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Compose
{
Expand Down Expand Up @@ -175,7 +174,7 @@ protected override bool OnInvalidate(Invalidation invalidation, InvalidationSour

private void initializePanel()
{
foreach (var panelType in EnumUtils.GetValues<PanelType>())
foreach (var panelType in Enum.GetValues<PanelType>())
{
var instance = getInstance(panelType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System;
using osu.Framework.Allocation;
using osu.Framework.Localisation;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Settings.Components.Markdown;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.States;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.States.Modes;
Expand All @@ -14,7 +13,7 @@
namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Settings
{
public abstract partial class LyricEditorEditModeSection<TEditModeState, TEditMode> : LyricEditorEditModeSection<TEditMode>
where TEditModeState : IHasEditModeState<TEditMode> where TEditMode : Enum
where TEditModeState : IHasEditModeState<TEditMode> where TEditMode : struct, Enum
{
[Resolved]
private TEditModeState tEditModeState { get; set; }
Expand All @@ -30,7 +29,7 @@ internal sealed override void UpdateEditMode(TEditMode mode)
}

public abstract partial class LyricEditorEditModeSection<TEditMode> : EditModeSection<TEditMode>
where TEditMode : Enum
where TEditMode : struct, Enum
{
[Resolved]
private ILyricSelectionState lyricSelectionState { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Settings.RubyRo
{
public abstract partial class TextTagEditModeSection<TEditModeState, TEditMode> : LyricEditorEditModeSection<TEditModeState, TEditMode>
where TEditModeState : IHasEditModeState<TEditMode>
where TEditMode : Enum
where TEditMode : struct, Enum
{
protected override OverlayColourScheme CreateColourScheme()
=> OverlayColourScheme.Pink;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.Settings.Components.Markdown;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Beatmaps.Lyrics.States;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Components.Markdown;
using osu.Game.Rulesets.Karaoke.Utils;
using osuTK;
using osuTK.Graphics;

Expand Down Expand Up @@ -75,7 +74,7 @@ public TimeTagCreateConfigSubsection()
},
Content = new[]
{
buttons = EnumUtils.GetValues<CreateTimeTagEditMode>().Select(x => new EditModeButton(x)
buttons = Enum.GetValues<CreateTimeTagEditMode>().Select(x => new EditModeButton(x)
{
Text = getButtonTitle(x),
Margin = new MarginPadding { Vertical = button_vertical_margin },
Expand Down
5 changes: 2 additions & 3 deletions osu.Game.Rulesets.Karaoke/Screens/Edit/EditModeSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,11 @@
using osu.Game.Overlays.Toolbar;
using osu.Game.Rulesets.Edit.Checks.Components;
using osu.Game.Rulesets.Karaoke.Screens.Edit.Components.Markdown;
using osu.Game.Rulesets.Karaoke.Utils;
using osuTK.Graphics;

namespace osu.Game.Rulesets.Karaoke.Screens.Edit;

public abstract partial class EditModeSection<TEditMode> : EditorSection where TEditMode : Enum
public abstract partial class EditModeSection<TEditMode> : EditorSection where TEditMode : struct, Enum
{
private const int horizontal_padding = 20;

Expand Down Expand Up @@ -73,7 +72,7 @@ protected EditModeSection()
}

private Selection[] createSelections()
=> EnumUtils.GetValues<TEditMode>().Select(mode =>
=> Enum.GetValues<TEditMode>().Select(mode =>
{
var selection = CreateSelection(mode);
selection.Mode = mode;
Expand Down
3 changes: 1 addition & 2 deletions osu.Game.Rulesets.Karaoke/Screens/Edit/IssueSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using osu.Game.Overlays;
using osu.Game.Rulesets.Edit;
using osu.Game.Rulesets.Edit.Checks.Components;
using osu.Game.Rulesets.Karaoke.Utils;
using osuTK;

namespace osu.Game.Rulesets.Karaoke.Screens.Edit;
Expand Down Expand Up @@ -217,7 +216,7 @@ protected IssueNavigator()
}

private IssueCategory[] createCategory()
=> EnumUtils.GetValues<IssueType>().Select(type => new IssueCategory
=> Enum.GetValues<IssueType>().Select(type => new IssueCategory
{
Type = type,
Text = getTextByIssueType(type),
Expand Down
2 changes: 1 addition & 1 deletion osu.Game.Rulesets.Karaoke/Skinning/KaraokeBeatmapSkin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public KaraokeBeatmapSkin(SkinInfo skin, IStorageResourceProvider? resources, IR
var globalSetting = SkinJsonSerializableExtensions.CreateSkinElementGlobalSettings();

// we may want to move this to some kind of async operation in the future.
foreach (ElementType skinnableTarget in Enum.GetValues(typeof(ElementType)))
foreach (ElementType skinnableTarget in Enum.GetValues<ElementType>())
{
string filename = $"{getFileNameByType(skinnableTarget)}.json";

Expand Down
11 changes: 4 additions & 7 deletions osu.Game.Rulesets.Karaoke/Utils/EnumUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@ namespace osu.Game.Rulesets.Karaoke.Utils
{
public static class EnumUtils
{
public static T[] GetValues<T>() where T : Enum
=> (T[])Enum.GetValues(typeof(T));
public static T GetPreviousValue<T>(T v) where T : struct, Enum
=> Enum.GetValues<T>().Concat(new[] { default(T)! }).Reverse().SkipWhile(e => !EqualityComparer<T>.Default.Equals(v, e)).Skip(1).First();

public static T GetPreviousValue<T>(T v) where T : Enum
=> GetValues<T>().Concat(new[] { default(T)! }).Reverse().SkipWhile(e => !EqualityComparer<T>.Default.Equals(v, e)).Skip(1).First();

public static T GetNextValue<T>(T v) where T : Enum
=> GetValues<T>().Concat(new[] { default(T)! }).SkipWhile(e => !EqualityComparer<T>.Default.Equals(v, e)).Skip(1).First();
public static T GetNextValue<T>(T v) where T : struct, Enum
=> Enum.GetValues<T>().Concat(new[] { default(T)! }).SkipWhile(e => !EqualityComparer<T>.Default.Equals(v, e)).Skip(1).First();

public static T Casting<T>(Enum mode)
=> (T)(object)mode;
Expand Down

0 comments on commit 3781830

Please sign in to comment.