From a5a73af01c6dd8a27347c7cac6eb3a6d9d9ef1f4 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 20 Jun 2021 23:48:02 +0200 Subject: [PATCH] improved Keybind constructors --- MLEM.Ui/UiControls.cs | 4 ++-- MLEM/Input/Keybind.cs | 29 +++++++++++++++++++++++++---- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/MLEM.Ui/UiControls.cs b/MLEM.Ui/UiControls.cs index 9821353..f8f5ebb 100644 --- a/MLEM.Ui/UiControls.cs +++ b/MLEM.Ui/UiControls.cs @@ -57,11 +57,11 @@ namespace MLEM.Ui { /// /// AA that acts as the buttons on a gamepad that perform the action. /// - public readonly Keybind GamepadButtons = new Keybind().Add(Buttons.A); + public readonly Keybind GamepadButtons = new Keybind(Buttons.A); /// /// A that acts as the buttons on a gamepad that perform the action. /// - public readonly Keybind SecondaryGamepadButtons = new Keybind().Add(Buttons.X); + public readonly Keybind SecondaryGamepadButtons = new Keybind(Buttons.X); /// /// A that acts as the buttons that select a that is above the currently selected element. /// diff --git a/MLEM/Input/Keybind.cs b/MLEM/Input/Keybind.cs index 18756cc..8fdca99 100644 --- a/MLEM/Input/Keybind.cs +++ b/MLEM/Input/Keybind.cs @@ -1,3 +1,4 @@ +using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; @@ -12,7 +13,27 @@ namespace MLEM.Input { public class Keybind { [DataMember] - private readonly List combinations = new List(); + private Combination[] combinations = Array.Empty(); + + /// + /// Creates a new keybind and adds the given key and modifiers using + /// + /// The key to be pressed. + /// The modifier keys that have to be held down. + public Keybind(GenericInput key, params GenericInput[] modifiers) { + this.Add(key, modifiers); + } + + /// + public Keybind(GenericInput key, ModifierKey modifier) { + this.Add(key, modifier); + } + + /// + /// Creates a new keybind with no default combinations + /// + public Keybind() { + } /// /// Adds a new key combination to this keybind that can optionally be pressed for the keybind to trigger. @@ -21,7 +42,7 @@ namespace MLEM.Input { /// The modifier keys that have to be held down. /// This keybind, for chaining public Keybind Add(GenericInput key, params GenericInput[] modifiers) { - this.combinations.Add(new Combination(key, modifiers)); + this.combinations = this.combinations.Append(new Combination(key, modifiers)).ToArray(); return this; } @@ -35,7 +56,7 @@ namespace MLEM.Input { /// /// This keybind, for chaining public Keybind Clear() { - this.combinations.Clear(); + this.combinations = Array.Empty(); return this; } @@ -46,7 +67,7 @@ namespace MLEM.Input { /// The keybind to copy from /// This keybind, for chaining public Keybind CopyFrom(Keybind other) { - this.combinations.AddRange(other.combinations); + this.combinations = this.combinations.Concat(other.combinations).ToArray(); return this; }