mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-25 05:58:35 +01:00
Some keybind and keybind button improvements
This commit is contained in:
parent
d5ec0b8001
commit
48a4aa0588
4 changed files with 56 additions and 9 deletions
|
@ -20,6 +20,7 @@ Improvements
|
||||||
- Added InputHandler mouse and touch position querying that preserves the game's viewport
|
- Added InputHandler mouse and touch position querying that preserves the game's viewport
|
||||||
- Added float version of GetRandomWeightedEntry
|
- Added float version of GetRandomWeightedEntry
|
||||||
- Allow LinkCode to specify a color to draw with
|
- Allow LinkCode to specify a color to draw with
|
||||||
|
- Allow better control over the order and layout of a Keybind's combinations
|
||||||
|
|
||||||
Fixes
|
Fixes
|
||||||
- **Fixed a formatting Code only knowing about the last Token that it is applied in**
|
- **Fixed a formatting Code only knowing about the last Token that it is applied in**
|
||||||
|
@ -43,6 +44,7 @@ Improvements
|
||||||
- Update elements less aggressively when changing a ui system's style
|
- Update elements less aggressively when changing a ui system's style
|
||||||
- Automatically update all elements when changing a ui system's viewport
|
- Automatically update all elements when changing a ui system's viewport
|
||||||
- Allow setting a default color for clickable links in UiStyle
|
- Allow setting a default color for clickable links in UiStyle
|
||||||
|
- Allow ElementHelper's KeybindButton to query a combination at a given index
|
||||||
|
|
||||||
Fixes
|
Fixes
|
||||||
- Fixed paragraph links having incorrect hover locations when using special text alignments
|
- Fixed paragraph links having incorrect hover locations when using special text alignments
|
||||||
|
|
|
@ -117,7 +117,7 @@ namespace MLEM.Ui.Elements {
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates a <see cref="Button"/> that acts as a way to input a custom value for a <see cref="Keybind"/>.
|
/// Creates a <see cref="Button"/> that acts as a way to input a custom value for a <see cref="Keybind"/>.
|
||||||
/// Note that only the first <see cref="Keybind.Combination"/> of the given keybind is displayed and edited, all others are ignored. The exception is that, if <paramref name="unbindKey"/> is set, unbinding the keybind clears all combinations.
|
/// Note that only the <see cref="Keybind.Combination"/> at index <paramref name="index"/> of the given keybind is displayed and edited, all others are ignored.
|
||||||
/// Inputting custom keybinds using this element supports <see cref="ModifierKey"/>-based modifiers and any <see cref="GenericInput"/>-based keys.
|
/// Inputting custom keybinds using this element supports <see cref="ModifierKey"/>-based modifiers and any <see cref="GenericInput"/>-based keys.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="anchor">The button's anchor</param>
|
/// <param name="anchor">The button's anchor</param>
|
||||||
|
@ -128,12 +128,10 @@ namespace MLEM.Ui.Elements {
|
||||||
/// <param name="unbindKey">An optional generic input that allows the keybind value to be unbound, clearing all combinations</param>
|
/// <param name="unbindKey">An optional generic input that allows the keybind value to be unbound, clearing all combinations</param>
|
||||||
/// <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>
|
||||||
/// <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, 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, int index = 0) {
|
||||||
string GetCurrentName() {
|
string GetCurrentName() => keybind.TryGetCombination(index, out var combination) ? combination.ToString(" + ", inputName) : unboundPlaceholder;
|
||||||
var combination = keybind.GetCombinations().FirstOrDefault();
|
|
||||||
return combination?.ToString(" + ", inputName) ?? unboundPlaceholder;
|
|
||||||
}
|
|
||||||
|
|
||||||
var button = new Button(anchor, size, GetCurrentName());
|
var button = new Button(anchor, size, GetCurrentName());
|
||||||
var active = false;
|
var active = false;
|
||||||
|
@ -148,18 +146,20 @@ namespace MLEM.Ui.Elements {
|
||||||
activeNext = false;
|
activeNext = false;
|
||||||
} else if (active) {
|
} else if (active) {
|
||||||
if (unbindKey != default && inputHandler.IsPressed(unbindKey)) {
|
if (unbindKey != default && inputHandler.IsPressed(unbindKey)) {
|
||||||
keybind.Clear();
|
keybind.Remove((c, i) => i == index);
|
||||||
button.Text.Text = unboundPlaceholder;
|
button.Text.Text = unboundPlaceholder;
|
||||||
active = false;
|
active = false;
|
||||||
} 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());
|
||||||
keybind.Remove((c, i) => i == 0).Add(key, mods.ToArray());
|
keybind.Remove((c, i) => i == index).Insert(index, key, mods.ToArray());
|
||||||
button.Text.Text = GetCurrentName();
|
button.Text.Text = GetCurrentName();
|
||||||
active = false;
|
active = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
button.Text.Text = GetCurrentName();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
return button;
|
return button;
|
||||||
|
|
|
@ -47,7 +47,28 @@ namespace MLEM.Input {
|
||||||
|
|
||||||
/// <inheritdoc cref="Add(MLEM.Input.GenericInput,MLEM.Input.GenericInput[])"/>
|
/// <inheritdoc cref="Add(MLEM.Input.GenericInput,MLEM.Input.GenericInput[])"/>
|
||||||
public Keybind Add(GenericInput key, ModifierKey modifier) {
|
public Keybind Add(GenericInput key, ModifierKey modifier) {
|
||||||
return this.Add(key, modifier.GetKeys().Select(m => (GenericInput) m).ToArray());
|
foreach (var mod in modifier.GetKeys())
|
||||||
|
this.Add(key, mod);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Inserts a new key combination into the given <paramref name="index"/> of this keybind's combinations that can optionally be pressed for the keybind to trigger.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="index">The index to insert this combination into.</param>
|
||||||
|
/// <param name="key">The key to be pressed.</param>
|
||||||
|
/// <param name="modifiers">The modifier keys that have to be held down.</param>
|
||||||
|
/// <returns>This keybind, for chaining.</returns>
|
||||||
|
public Keybind Insert(int index, GenericInput key, params GenericInput[] modifiers) {
|
||||||
|
this.combinations = this.combinations.Take(index).Append(new Combination(key, modifiers)).Concat(this.combinations.Skip(index)).ToArray();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="Insert(int,MLEM.Input.GenericInput,MLEM.Input.GenericInput[])"/>
|
||||||
|
public Keybind Insert(int index, GenericInput key, ModifierKey modifier) {
|
||||||
|
foreach (var mod in modifier.GetKeys().Reverse())
|
||||||
|
this.Insert(index, key, mod);
|
||||||
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -134,6 +155,22 @@ namespace MLEM.Input {
|
||||||
yield return combination;
|
yield return combination;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tries to retrieve the combination at the given <paramref name="index"/> within this keybind.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="index">The index of the combination to retrieve.</param>
|
||||||
|
/// <param name="combination">The combination, or default if this method returns false.</param>
|
||||||
|
/// <returns>Whether the combination could be successfully retrieved or the index was out of bounds of this keybind's combination collection.</returns>
|
||||||
|
public bool TryGetCombination(int index, out Combination combination) {
|
||||||
|
if (index >= 0 && index < this.combinations.Length) {
|
||||||
|
combination = this.combinations[index];
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
combination = default;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts this keybind into an easily human-readable string.
|
/// Converts this keybind into an easily human-readable string.
|
||||||
/// When using <see cref="ToString()"/>, this method is used with <paramref name="joiner"/> set to ", ".
|
/// When using <see cref="ToString()"/>, this method is used with <paramref name="joiner"/> set to ", ".
|
||||||
|
|
|
@ -304,6 +304,14 @@ namespace Sandbox {
|
||||||
var newPanel = new Panel(Anchor.TopLeft, new Vector2(200, 100), new Vector2(10, 10));
|
var newPanel = new Panel(Anchor.TopLeft, new Vector2(200, 100), new Vector2(10, 10));
|
||||||
newPanel.AddChild(new Button(Anchor.TopLeft, new Vector2(100, 20), "Text", "Tooltip text"));
|
newPanel.AddChild(new Button(Anchor.TopLeft, new Vector2(100, 20), "Text", "Tooltip text"));
|
||||||
this.UiSystem.Add("Panel", newPanel);
|
this.UiSystem.Add("Panel", newPanel);
|
||||||
|
|
||||||
|
var keybind = new Keybind(MouseButton.Left, ModifierKey.Shift);
|
||||||
|
var keybindPanel = new Panel(Anchor.BottomRight, new Vector2(100, 100), new Vector2(5));
|
||||||
|
for (var i = 0; i < 3; i++) {
|
||||||
|
var button = keybindPanel.AddChild(ElementHelper.KeybindButton(Anchor.AutoLeft, new Vector2(0.5F, 12), keybind, Input, "Press", Keys.Escape, index: i));
|
||||||
|
button.Text.TextScale = 0.1F;
|
||||||
|
}
|
||||||
|
this.UiSystem.Add("Keybinds", keybindPanel);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void DoUpdate(GameTime gameTime) {
|
protected override void DoUpdate(GameTime gameTime) {
|
||||||
|
|
Loading…
Reference in a new issue