From 8044cb59cbc27865032a1a470ac87688967b65fc Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Fri, 29 Jul 2022 19:52:01 +0200 Subject: [PATCH] Improved EnumHelper.GetValues signature to return an array --- CHANGELOG.md | 4 ++++ MLEM.Ui/Parsers/UiMarkdownParser.cs | 3 +-- MLEM/Input/KeysExtensions.cs | 2 +- MLEM/Input/MouseExtensions.cs | 3 +-- MLEM/Misc/Direction2.cs | 2 +- MLEM/Misc/EnumHelper.cs | 13 ++++++------- Sandbox/GameImpl.cs | 11 +---------- 7 files changed, 15 insertions(+), 23 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 58f86fa..47ac492 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ Jump to version: - [5.0.0](#500) ## 6.1.0 +### MLEM +Improvements +- Improved EnumHelper.GetValues signature to return an array + ### MLEM.Ui Additions - Added some extension methods for querying Anchor types diff --git a/MLEM.Ui/Parsers/UiMarkdownParser.cs b/MLEM.Ui/Parsers/UiMarkdownParser.cs index 449ba95..e5f6971 100644 --- a/MLEM.Ui/Parsers/UiMarkdownParser.cs +++ b/MLEM.Ui/Parsers/UiMarkdownParser.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.IO; -using System.Linq; using System.Net.Http; using System.Text.RegularExpressions; using Microsoft.Xna.Framework; @@ -28,7 +27,7 @@ namespace MLEM.Ui.Parsers { /// public class UiMarkdownParser { - private static readonly ElementType[] ElementTypes = EnumHelper.GetValues().ToArray(); + private static readonly ElementType[] ElementTypes = EnumHelper.GetValues(); /// /// The base path for markdown images, which is prepended to the image link. diff --git a/MLEM/Input/KeysExtensions.cs b/MLEM/Input/KeysExtensions.cs index 8577835..f1f5131 100644 --- a/MLEM/Input/KeysExtensions.cs +++ b/MLEM/Input/KeysExtensions.cs @@ -12,7 +12,7 @@ namespace MLEM.Input { /// /// All enum values of /// - public static readonly ModifierKey[] ModifierKeys = EnumHelper.GetValues().ToArray(); + public static readonly ModifierKey[] ModifierKeys = EnumHelper.GetValues(); private static readonly Dictionary KeysLookup = new Dictionary { {ModifierKey.Shift, new[] {Keys.LeftShift, Keys.RightShift}}, {ModifierKey.Control, new[] {Keys.LeftControl, Keys.RightControl}}, diff --git a/MLEM/Input/MouseExtensions.cs b/MLEM/Input/MouseExtensions.cs index f31b198..b3d0c42 100644 --- a/MLEM/Input/MouseExtensions.cs +++ b/MLEM/Input/MouseExtensions.cs @@ -1,5 +1,4 @@ using System; -using System.Linq; using Microsoft.Xna.Framework.Input; using MLEM.Misc; @@ -12,7 +11,7 @@ namespace MLEM.Input { /// /// All enum values of /// - public static readonly MouseButton[] MouseButtons = EnumHelper.GetValues().ToArray(); + public static readonly MouseButton[] MouseButtons = EnumHelper.GetValues(); /// /// Returns the of the given mouse button. diff --git a/MLEM/Misc/Direction2.cs b/MLEM/Misc/Direction2.cs index 443bbb6..111e6b7 100644 --- a/MLEM/Misc/Direction2.cs +++ b/MLEM/Misc/Direction2.cs @@ -71,7 +71,7 @@ namespace MLEM.Misc { /// /// All enum values /// - public static readonly Direction2[] All = EnumHelper.GetValues().ToArray(); + public static readonly Direction2[] All = EnumHelper.GetValues(); /// /// The through directions /// diff --git a/MLEM/Misc/EnumHelper.cs b/MLEM/Misc/EnumHelper.cs index b7a7337..645f2e8 100644 --- a/MLEM/Misc/EnumHelper.cs +++ b/MLEM/Misc/EnumHelper.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; using Microsoft.Xna.Framework.Input; namespace MLEM.Misc { @@ -12,19 +10,20 @@ namespace MLEM.Misc { /// /// All values of the enum. /// - public static readonly Buttons[] Buttons = EnumHelper.GetValues().ToArray(); + public static readonly Buttons[] Buttons = EnumHelper.GetValues(); /// /// All values of the enum. /// - public static readonly Keys[] Keys = EnumHelper.GetValues().ToArray(); + public static readonly Keys[] Keys = EnumHelper.GetValues(); /// - /// Returns all of the values of the given enum type. + /// Returns an array containing all of the values of the given enum type. + /// Note that this method is a version-independent equivalent of .NET 5's Enum.GetValues<TEnum>. /// /// The type whose enum to get /// An enumerable of the values of the enum, in declaration order. - public static IEnumerable GetValues() { - return Enum.GetValues(typeof(T)).Cast(); + public static T[] GetValues() { + return (T[]) Enum.GetValues(typeof(T)); } } diff --git a/Sandbox/GameImpl.cs b/Sandbox/GameImpl.cs index 738af60..9680d65 100644 --- a/Sandbox/GameImpl.cs +++ b/Sandbox/GameImpl.cs @@ -1,17 +1,11 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; using System.Text.RegularExpressions; using FontStashSharp; -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Graphics; -using Microsoft.Xna.Framework.Input; using MLEM.Cameras; using MLEM.Data; using MLEM.Data.Content; using MLEM.Extended.Font; -using MLEM.Extended.Tiled; using MLEM.Extensions; using MLEM.Font; using MLEM.Formatting; @@ -23,9 +17,6 @@ using MLEM.Textures; using MLEM.Ui; using MLEM.Ui.Elements; using MLEM.Ui.Style; -using MonoGame.Extended; -using MonoGame.Extended.Tiled; -using MonoGame.Extended.ViewportAdapters; namespace Sandbox; @@ -348,7 +339,7 @@ public class GameImpl : MlemGame { widthPanel.AddChild(new Paragraph(Anchor.AutoCenter, 100000, "Test String " + Math.Pow(10, i), true) { OnUpdated = (e, time) => { if (Input.IsPressed(Keys.A)) { - e.Anchor = (Anchor) (((int) e.Anchor + 1) % EnumHelper.GetValues().Count()); + e.Anchor = (Anchor) (((int) e.Anchor + 1) % EnumHelper.GetValues().Length); Console.WriteLine(e.Anchor); } }