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

Added customizable overloads for Keybind, Combination and GenericInput ToString methods

This commit is contained in:
Ell 2021-07-13 15:41:42 +02:00
parent 9a0b8ef846
commit 6aa9ec03d4
4 changed files with 43 additions and 16 deletions

View file

@ -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

View file

@ -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<GenericInput, string> 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());

View file

@ -27,19 +27,16 @@ namespace MLEM.Input {
/// <inheritdoc />
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;
}
/// <inheritdoc />

View file

@ -123,6 +123,23 @@ namespace MLEM.Input {
yield return combination;
}
/// <summary>
/// Converts this keybind into an easily human-readable string.
/// When using <see cref="ToString()"/>, this method is used with <paramref name="joiner"/> set to ", ".
/// </summary>
/// <param name="joiner">The string to use to join combinations</param>
/// <param name="combinationJoiner">The string to use for combination-internal joining, see <see cref="Combination.ToString(string,System.Func{MLEM.Input.GenericInput,string})"/></param>
/// <param name="inputName">The function to use for determining the display name of generic inputs, see <see cref="Combination.ToString(string,System.Func{MLEM.Input.GenericInput,string})"/></param>
/// <returns>A human-readable string representing this keybind</returns>
public string ToString(string joiner, string combinationJoiner = " + ", Func<GenericInput, string> inputName = null) {
return string.Join(joiner, this.combinations.Select(c => c.ToString(combinationJoiner, inputName)));
}
/// <inheritdoc />
public override string ToString() {
return this.ToString(", ");
}
/// <summary>
/// A key combination is a combination of a set of modifier keys and a key.
/// All of the keys are <see cref="GenericInput"/> 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));
}
/// <summary>
/// Converts this combination into an easily human-readable string.
/// When using <see cref="ToString()"/>, this method is used with <paramref name="joiner"/> set to " + ".
/// </summary>
/// <param name="joiner">The string to use to join this combination's <see cref="Modifiers"/> and <see cref="Key"/> together</param>
/// <param name="inputName">The function to use for determining the display name of a <see cref="GenericInput"/>. If this is null, the generic input's default <see cref="GenericInput.ToString"/> method is used.</param>
/// <returns>A human-readable string representing this combination</returns>
public string ToString(string joiner, Func<GenericInput, string> inputName = null) {
return string.Join(joiner, this.Modifiers.Append(this.Key).Select(i => inputName?.Invoke(i) ?? i.ToString()));
}
/// <inheritdoc />
public override string ToString() {
return this.ToString(" + ");
}
}
}