1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-03 05:43:37 +02:00

Added optional isKeybindAllowed parameter to KeybindButton

This commit is contained in:
Ell 2022-05-18 15:54:29 +02:00
parent 8968e6025d
commit 6dc4011ef5
2 changed files with 13 additions and 8 deletions

View file

@ -30,6 +30,7 @@ Improvements
- Improved ElementHelper.AddTooltip overloads - Improved ElementHelper.AddTooltip overloads
- Don't query a paragraph's text callback in the constructor - Don't query a paragraph's text callback in the constructor
- Allow manually hiding a paragraph without its text overriding the hidden state - Allow manually hiding a paragraph without its text overriding the hidden state
- Added optional isKeybindAllowed parameter to KeybindButton
Fixes Fixes
- Fixed auto-nav tooltip displaying on the selected element even when not in auto-nav mode - Fixed auto-nav tooltip displaying on the selected element even when not in auto-nav mode

View file

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Input; using MLEM.Input;
@ -115,9 +116,9 @@ namespace MLEM.Ui.Elements {
return group; return group;
} }
/// <inheritdoc cref="KeybindButton(MLEM.Ui.Anchor,Microsoft.Xna.Framework.Vector2,MLEM.Input.Keybind,MLEM.Input.InputHandler,string,MLEM.Input.Keybind,string,System.Func{MLEM.Input.GenericInput,string},int)"/> /// <inheritdoc cref="KeybindButton(MLEM.Ui.Anchor,Microsoft.Xna.Framework.Vector2,MLEM.Input.Keybind,MLEM.Input.InputHandler,string,MLEM.Input.GenericInput,string,System.Func{MLEM.Input.GenericInput,string},int,System.Func{MLEM.Input.GenericInput,System.Collections.Generic.IEnumerable{MLEM.Input.GenericInput},bool})"/>
public static Button KeybindButton(Anchor anchor, Vector2 size, Keybind keybind, InputHandler inputHandler, string activePlaceholder, GenericInput unbindKey = default, string unboundPlaceholder = "", Func<GenericInput, string> inputName = null, int index = 0) { public static Button KeybindButton(Anchor anchor, Vector2 size, Keybind keybind, InputHandler inputHandler, string activePlaceholder, GenericInput unbindKey = default, string unboundPlaceholder = "", Func<GenericInput, string> inputName = null, int index = 0, Func<GenericInput, IEnumerable<GenericInput>, bool> isKeybindAllowed = null) {
return KeybindButton(anchor, size, keybind, inputHandler, activePlaceholder, new Keybind(unbindKey), unboundPlaceholder, inputName, index); return KeybindButton(anchor, size, keybind, inputHandler, activePlaceholder, new Keybind(unbindKey), unboundPlaceholder, inputName, index, isKeybindAllowed);
} }
/// <summary> /// <summary>
@ -135,8 +136,9 @@ namespace MLEM.Ui.Elements {
/// <param name="unboundPlaceholder">A placeholder text that is displayed if the keybind is unbound</param> /// <param name="unboundPlaceholder">A placeholder text that is displayed if the keybind is unbound</param>
/// <param name="inputName">An optional function to give each input a display name that is easier to read. If this is null, <see cref="GenericInput.ToString"/> is used.</param> /// <param name="inputName">An optional function to give each input a display name that is easier to read. If this is null, <see cref="GenericInput.ToString"/> is used.</param>
/// <param name="index">The index in the <paramref name="keybind"/> that the button should display. Note that, if the index is greater than the amount of combinations, combinations entered using this button will be stored at an earlier index.</param> /// <param name="index">The index in the <paramref name="keybind"/> that the button should display. Note that, if the index is greater than the amount of combinations, combinations entered using this button will be stored at an earlier index.</param>
/// <param name="isKeybindAllowed">A function that can optionally determine whether a given input and modifier combination is allowed. If this is null, all combinations are allowed.</param>
/// <returns>A keybind button with the given settings</returns> /// <returns>A keybind button with the given settings</returns>
public static Button KeybindButton(Anchor anchor, Vector2 size, Keybind keybind, InputHandler inputHandler, string activePlaceholder, Keybind unbind = default, string unboundPlaceholder = "", Func<GenericInput, string> inputName = null, int index = 0) { public static Button KeybindButton(Anchor anchor, Vector2 size, Keybind keybind, InputHandler inputHandler, string activePlaceholder, Keybind unbind = default, string unboundPlaceholder = "", Func<GenericInput, string> inputName = null, int index = 0, Func<GenericInput, IEnumerable<GenericInput>, bool> isKeybindAllowed = null) {
string GetCurrentName() => keybind.TryGetCombination(index, out var combination) ? combination.ToString(" + ", inputName) : unboundPlaceholder; string GetCurrentName() => keybind.TryGetCombination(index, out var combination) ? combination.ToString(" + ", inputName) : unboundPlaceholder;
var button = new Button(anchor, size, GetCurrentName()); var button = new Button(anchor, size, GetCurrentName());
@ -157,12 +159,14 @@ namespace MLEM.Ui.Elements {
} else if (inputHandler.InputsPressed.Length > 0) { } else if (inputHandler.InputsPressed.Length > 0) {
var key = inputHandler.InputsPressed.FirstOrDefault(i => !i.IsModifier()); var key = inputHandler.InputsPressed.FirstOrDefault(i => !i.IsModifier());
if (key != default) { if (key != default) {
var mods = inputHandler.InputsDown.Where(i => i.IsModifier()); var mods = inputHandler.InputsDown.Where(i => i.IsModifier()).ToArray();
keybind.Remove((c, i) => i == index).Insert(index, key, mods.ToArray()); if (isKeybindAllowed == null || isKeybindAllowed(key, mods)) {
keybind.Remove((c, i) => i == index).Insert(index, key, mods);
button.Text.Text = GetCurrentName(); button.Text.Text = GetCurrentName();
button.SetData("Active", false); button.SetData("Active", false);
} }
} }
}
} else { } else {
button.Text.Text = GetCurrentName(); button.Text.Text = GetCurrentName();
} }