diff --git a/CHANGELOG.md b/CHANGELOG.md index 9778719..cb8d1f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,12 +10,13 @@ Jump to version: Additions - Added RotateBy to Direction2Helper -Fixes -- Set default values for InputHandler held and pressed keys to avoid an exception if buttons are held in the very first frame - Improvements - Improved NinePatch memory performance - Moved sound-related classes into Sound namespace +- Added customizable overloads for Keybind, Combination and GenericInput ToString methods + +Fixes +- Set default values for InputHandler held and pressed keys to avoid an exception if buttons are held in the very first frame ### MLEM.Ui Additions diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index 66b6bb9..b0d76cb 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -131,11 +131,7 @@ namespace MLEM.Ui.Elements { public static Button KeybindButton(Anchor anchor, Vector2 size, Keybind keybind, InputHandler inputHandler, string activePlaceholder, GenericInput unbindKey = default, string unboundPlaceholder = "", Func inputName = null) { string GetCurrentName() { var combination = keybind.GetCombinations().FirstOrDefault(); - if (combination == null) - return unboundPlaceholder; - return string.Join(" + ", combination.Modifiers - .Append(combination.Key) - .Select(i => inputName?.Invoke(i) ?? i.ToString())); + return combination?.ToString(" + ", inputName) ?? unboundPlaceholder; } var button = new Button(anchor, size, GetCurrentName()); diff --git a/MLEM/Input/GenericInput.cs b/MLEM/Input/GenericInput.cs index 220ef31..f53da33 100644 --- a/MLEM/Input/GenericInput.cs +++ b/MLEM/Input/GenericInput.cs @@ -27,19 +27,16 @@ namespace MLEM.Input { /// public override string ToString() { - var ret = this.Type.ToString(); switch (this.Type) { case InputType.Mouse: - ret += ((MouseButton) this).ToString(); - break; + return $"Mouse{(MouseButton) this}"; case InputType.Keyboard: - ret += ((Keys) this).ToString(); - break; + return ((Keys) this).ToString(); case InputType.Gamepad: - ret += ((Buttons) this).ToString(); - break; + return $"Gamepad{(Buttons) this}"; + default: + return this.Type.ToString(); } - return ret; } /// diff --git a/MLEM/Input/Keybind.cs b/MLEM/Input/Keybind.cs index c9888f2..7d6d8a2 100644 --- a/MLEM/Input/Keybind.cs +++ b/MLEM/Input/Keybind.cs @@ -123,6 +123,23 @@ namespace MLEM.Input { yield return combination; } + /// + /// Converts this keybind into an easily human-readable string. + /// When using , this method is used with set to ", ". + /// + /// The string to use to join combinations + /// The string to use for combination-internal joining, see + /// The function to use for determining the display name of generic inputs, see + /// A human-readable string representing this keybind + public string ToString(string joiner, string combinationJoiner = " + ", Func inputName = null) { + return string.Join(joiner, this.combinations.Select(c => c.ToString(combinationJoiner, inputName))); + } + + /// + public override string ToString() { + return this.ToString(", "); + } + /// /// A key combination is a combination of a set of modifier keys and a key. /// All of the keys are instances, so they can be keyboard-, mouse- or gamepad-based. @@ -184,6 +201,22 @@ namespace MLEM.Input { return this.Modifiers.Length <= 0 || this.Modifiers.Any(m => handler.IsDown(m, gamepadIndex)); } + /// + /// Converts this combination into an easily human-readable string. + /// When using , this method is used with set to " + ". + /// + /// The string to use to join this combination's and together + /// The function to use for determining the display name of a . If this is null, the generic input's default method is used. + /// A human-readable string representing this combination + public string ToString(string joiner, Func inputName = null) { + return string.Join(joiner, this.Modifiers.Append(this.Key).Select(i => inputName?.Invoke(i) ?? i.ToString())); + } + + /// + public override string ToString() { + return this.ToString(" + "); + } + } }