mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +01:00
Added customizable overloads for Keybind, Combination and GenericInput ToString methods
This commit is contained in:
parent
9a0b8ef846
commit
6aa9ec03d4
4 changed files with 43 additions and 16 deletions
|
@ -10,12 +10,13 @@ Jump to version:
|
||||||
Additions
|
Additions
|
||||||
- Added RotateBy to Direction2Helper
|
- 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
|
Improvements
|
||||||
- Improved NinePatch memory performance
|
- Improved NinePatch memory performance
|
||||||
- Moved sound-related classes into Sound namespace
|
- 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
|
### MLEM.Ui
|
||||||
Additions
|
Additions
|
||||||
|
|
|
@ -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) {
|
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() {
|
string GetCurrentName() {
|
||||||
var combination = keybind.GetCombinations().FirstOrDefault();
|
var combination = keybind.GetCombinations().FirstOrDefault();
|
||||||
if (combination == null)
|
return combination?.ToString(" + ", inputName) ?? unboundPlaceholder;
|
||||||
return unboundPlaceholder;
|
|
||||||
return string.Join(" + ", combination.Modifiers
|
|
||||||
.Append(combination.Key)
|
|
||||||
.Select(i => inputName?.Invoke(i) ?? i.ToString()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var button = new Button(anchor, size, GetCurrentName());
|
var button = new Button(anchor, size, GetCurrentName());
|
||||||
|
|
|
@ -27,19 +27,16 @@ namespace MLEM.Input {
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override string ToString() {
|
public override string ToString() {
|
||||||
var ret = this.Type.ToString();
|
|
||||||
switch (this.Type) {
|
switch (this.Type) {
|
||||||
case InputType.Mouse:
|
case InputType.Mouse:
|
||||||
ret += ((MouseButton) this).ToString();
|
return $"Mouse{(MouseButton) this}";
|
||||||
break;
|
|
||||||
case InputType.Keyboard:
|
case InputType.Keyboard:
|
||||||
ret += ((Keys) this).ToString();
|
return ((Keys) this).ToString();
|
||||||
break;
|
|
||||||
case InputType.Gamepad:
|
case InputType.Gamepad:
|
||||||
ret += ((Buttons) this).ToString();
|
return $"Gamepad{(Buttons) this}";
|
||||||
break;
|
default:
|
||||||
|
return this.Type.ToString();
|
||||||
}
|
}
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
|
|
|
@ -123,6 +123,23 @@ namespace MLEM.Input {
|
||||||
yield return combination;
|
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>
|
/// <summary>
|
||||||
/// A key combination is a combination of a set of modifier keys and a key.
|
/// 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.
|
/// 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));
|
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(" + ");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue