1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

improved Keybind constructors

This commit is contained in:
Ell 2021-06-20 23:48:02 +02:00
parent 2118837062
commit a5a73af01c
2 changed files with 27 additions and 6 deletions

View file

@ -57,11 +57,11 @@ namespace MLEM.Ui {
/// <summary> /// <summary>
/// AA <see cref="Keybind"/> that acts as the buttons on a gamepad that perform the <see cref="Element.OnPressed"/> action. /// AA <see cref="Keybind"/> that acts as the buttons on a gamepad that perform the <see cref="Element.OnPressed"/> action.
/// </summary> /// </summary>
public readonly Keybind GamepadButtons = new Keybind().Add(Buttons.A); public readonly Keybind GamepadButtons = new Keybind(Buttons.A);
/// <summary> /// <summary>
/// A <see cref="Keybind"/> that acts as the buttons on a gamepad that perform the <see cref="Element.OnSecondaryPressed"/> action. /// A <see cref="Keybind"/> that acts as the buttons on a gamepad that perform the <see cref="Element.OnSecondaryPressed"/> action.
/// </summary> /// </summary>
public readonly Keybind SecondaryGamepadButtons = new Keybind().Add(Buttons.X); public readonly Keybind SecondaryGamepadButtons = new Keybind(Buttons.X);
/// <summary> /// <summary>
/// A <see cref="Keybind"/> that acts as the buttons that select a <see cref="Element"/> that is above the currently selected element. /// A <see cref="Keybind"/> that acts as the buttons that select a <see cref="Element"/> that is above the currently selected element.
/// </summary> /// </summary>

View file

@ -1,3 +1,4 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.Serialization; using System.Runtime.Serialization;
@ -12,7 +13,27 @@ namespace MLEM.Input {
public class Keybind { public class Keybind {
[DataMember] [DataMember]
private readonly List<Combination> combinations = new List<Combination>(); private Combination[] combinations = Array.Empty<Combination>();
/// <summary>
/// Creates a new keybind and adds the given key and modifiers using <see cref="Add(MLEM.Input.GenericInput,MLEM.Input.GenericInput[])"/>
/// </summary>
/// <param name="key">The key to be pressed.</param>
/// <param name="modifiers">The modifier keys that have to be held down.</param>
public Keybind(GenericInput key, params GenericInput[] modifiers) {
this.Add(key, modifiers);
}
/// <inheritdoc cref="Keybind(GenericInput, GenericInput[])"/>
public Keybind(GenericInput key, ModifierKey modifier) {
this.Add(key, modifier);
}
/// <summary>
/// Creates a new keybind with no default combinations
/// </summary>
public Keybind() {
}
/// <summary> /// <summary>
/// Adds a new key combination to this keybind that can optionally be pressed for the keybind to trigger. /// 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 {
/// <param name="modifiers">The modifier keys that have to be held down.</param> /// <param name="modifiers">The modifier keys that have to be held down.</param>
/// <returns>This keybind, for chaining</returns> /// <returns>This keybind, for chaining</returns>
public Keybind Add(GenericInput key, params GenericInput[] modifiers) { 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; return this;
} }
@ -35,7 +56,7 @@ namespace MLEM.Input {
/// </summary> /// </summary>
/// <returns>This keybind, for chaining</returns> /// <returns>This keybind, for chaining</returns>
public Keybind Clear() { public Keybind Clear() {
this.combinations.Clear(); this.combinations = Array.Empty<Combination>();
return this; return this;
} }
@ -46,7 +67,7 @@ namespace MLEM.Input {
/// <param name="other">The keybind to copy from</param> /// <param name="other">The keybind to copy from</param>
/// <returns>This keybind, for chaining</returns> /// <returns>This keybind, for chaining</returns>
public Keybind CopyFrom(Keybind other) { public Keybind CopyFrom(Keybind other) {
this.combinations.AddRange(other.combinations); this.combinations = this.combinations.Concat(other.combinations).ToArray();
return this; return this;
} }