diff --git a/CHANGELOG.md b/CHANGELOG.md index d952736..983ef45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,12 +11,13 @@ Additions - Added RotateBy to Direction2Helper Improvements -- Improved NinePatch memory performance +- Improved NinePatch memory usage - Moved sound-related classes into Sound namespace - Added customizable overloads for Keybind, Combination and GenericInput ToString methods - Moved ColorHelper.Invert to ColorExtensions.Invert - Removed LINQ Any and All usage in various methods to improve memory usage - Allow enumerating SoundEffectInstanceHandler entries +- Improved KeysExtensions memory usage Fixes - Set default values for InputHandler held and pressed keys to avoid an exception if buttons are held in the very first frame diff --git a/MLEM/Input/KeysExtensions.cs b/MLEM/Input/KeysExtensions.cs index 3c2cfdd..29dc5ea 100644 --- a/MLEM/Input/KeysExtensions.cs +++ b/MLEM/Input/KeysExtensions.cs @@ -13,6 +13,14 @@ namespace MLEM.Input { /// All enum values of /// public static readonly ModifierKey[] ModifierKeys = EnumHelper.GetValues().ToArray(); + private static readonly Dictionary KeysLookup = new Dictionary { + {ModifierKey.Shift, new[] {Keys.LeftShift, Keys.RightShift}}, + {ModifierKey.Control, new[] {Keys.LeftControl, Keys.RightControl}}, + {ModifierKey.Alt, new[] {Keys.LeftAlt, Keys.RightAlt}} + }; + private static readonly Dictionary ModifiersLookup = KeysLookup + .SelectMany(kv => kv.Value.Select(v => (kv.Key, v))) + .ToDictionary(kv => kv.Item2, kv => kv.Item1); /// /// Returns all of the keys that the given modifier key represents @@ -20,20 +28,7 @@ namespace MLEM.Input { /// The modifier key /// All of the keys the modifier key represents public static IEnumerable GetKeys(this ModifierKey modifier) { - switch (modifier) { - case ModifierKey.Shift: - yield return Keys.LeftShift; - yield return Keys.RightShift; - break; - case ModifierKey.Control: - yield return Keys.LeftControl; - yield return Keys.RightControl; - break; - case ModifierKey.Alt: - yield return Keys.LeftAlt; - yield return Keys.RightAlt; - break; - } + return KeysLookup.TryGetValue(modifier, out var keys) ? keys : Enumerable.Empty(); } /// @@ -43,11 +38,7 @@ namespace MLEM.Input { /// The key to convert to a modifier key /// The modifier key, or public static ModifierKey GetModifier(this Keys key) { - foreach (var mod in ModifierKeys) { - if (GetKeys(mod).Contains(key)) - return mod; - } - return ModifierKey.None; + return ModifiersLookup.TryGetValue(key, out var mod) ? mod : ModifierKey.None; } ///