From 6dc4011ef51b52334ae81cecd0cdd34d9d0c2fe7 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 18 May 2022 15:54:29 +0200 Subject: [PATCH] Added optional isKeybindAllowed parameter to KeybindButton --- CHANGELOG.md | 1 + MLEM.Ui/Elements/ElementHelper.cs | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9465a3e..f133cb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,6 +30,7 @@ Improvements - Improved ElementHelper.AddTooltip overloads - Don't query a paragraph's text callback in the constructor - Allow manually hiding a paragraph without its text overriding the hidden state +- Added optional isKeybindAllowed parameter to KeybindButton Fixes - Fixed auto-nav tooltip displaying on the selected element even when not in auto-nav mode diff --git a/MLEM.Ui/Elements/ElementHelper.cs b/MLEM.Ui/Elements/ElementHelper.cs index 426e1e4..f1a2366 100644 --- a/MLEM.Ui/Elements/ElementHelper.cs +++ b/MLEM.Ui/Elements/ElementHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using MLEM.Input; @@ -115,9 +116,9 @@ namespace MLEM.Ui.Elements { return group; } - /// - public static Button KeybindButton(Anchor anchor, Vector2 size, Keybind keybind, InputHandler inputHandler, string activePlaceholder, GenericInput unbindKey = default, string unboundPlaceholder = "", Func inputName = null, int index = 0) { - return KeybindButton(anchor, size, keybind, inputHandler, activePlaceholder, new Keybind(unbindKey), unboundPlaceholder, inputName, index); + /// + public static Button KeybindButton(Anchor anchor, Vector2 size, Keybind keybind, InputHandler inputHandler, string activePlaceholder, GenericInput unbindKey = default, string unboundPlaceholder = "", Func inputName = null, int index = 0, Func, bool> isKeybindAllowed = null) { + return KeybindButton(anchor, size, keybind, inputHandler, activePlaceholder, new Keybind(unbindKey), unboundPlaceholder, inputName, index, isKeybindAllowed); } /// @@ -135,8 +136,9 @@ namespace MLEM.Ui.Elements { /// A placeholder text that is displayed if the keybind is unbound /// An optional function to give each input a display name that is easier to read. If this is null, is used. /// The index in the 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. + /// A function that can optionally determine whether a given input and modifier combination is allowed. If this is null, all combinations are allowed. /// A keybind button with the given settings - public static Button KeybindButton(Anchor anchor, Vector2 size, Keybind keybind, InputHandler inputHandler, string activePlaceholder, Keybind unbind = default, string unboundPlaceholder = "", Func 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 inputName = null, int index = 0, Func, bool> isKeybindAllowed = null) { string GetCurrentName() => keybind.TryGetCombination(index, out var combination) ? combination.ToString(" + ", inputName) : unboundPlaceholder; var button = new Button(anchor, size, GetCurrentName()); @@ -157,10 +159,12 @@ namespace MLEM.Ui.Elements { } else if (inputHandler.InputsPressed.Length > 0) { var key = inputHandler.InputsPressed.FirstOrDefault(i => !i.IsModifier()); if (key != default) { - var mods = inputHandler.InputsDown.Where(i => i.IsModifier()); - keybind.Remove((c, i) => i == index).Insert(index, key, mods.ToArray()); - button.Text.Text = GetCurrentName(); - button.SetData("Active", false); + var mods = inputHandler.InputsDown.Where(i => i.IsModifier()).ToArray(); + if (isKeybindAllowed == null || isKeybindAllowed(key, mods)) { + keybind.Remove((c, i) => i == index).Insert(index, key, mods); + button.Text.Text = GetCurrentName(); + button.SetData("Active", false); + } } } } else {