mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 12:58:33 +01:00
only throw an exception in TextInputWrapper if it is required
This commit is contained in:
parent
096131ce15
commit
ca4c8731bc
4 changed files with 14 additions and 9 deletions
|
@ -313,6 +313,8 @@ namespace MLEM.Ui.Elements {
|
|||
public GenericCallback OnTouchExit;
|
||||
/// <summary>
|
||||
/// Event that is called when text input is made.
|
||||
/// When an element uses this event, it should call <see cref="TextInputWrapper.EnsureExists"/> on construction to ensure that a text input wrapper was set.
|
||||
///
|
||||
/// Note that this event is called for every element, even if it is not selected.
|
||||
/// Also note that if <see cref="TextInputWrapper.RequiresOnScreenKeyboard"/> is true, this event is never called.
|
||||
/// </summary>
|
||||
|
|
|
@ -132,6 +132,7 @@ namespace MLEM.Ui.Elements {
|
|||
if (font != null)
|
||||
this.Font.Set(font);
|
||||
|
||||
TextInputWrapper.EnsureExists();
|
||||
if (TextInputWrapper.Current.RequiresOnScreenKeyboard()) {
|
||||
this.OnPressed += async e => {
|
||||
if (!KeyboardInput.IsVisible) {
|
||||
|
|
|
@ -199,7 +199,8 @@ namespace MLEM.Ui {
|
|||
root.Element.ForceUpdateArea();
|
||||
};
|
||||
|
||||
TextInputWrapper.Current.AddListener(window, (sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character)));
|
||||
if (TextInputWrapper.Current != null)
|
||||
TextInputWrapper.Current.AddListener(window, (sender, key, character) => this.ApplyToAll(e => e.OnTextInput?.Invoke(e, key, character)));
|
||||
this.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e));
|
||||
this.OnTouchedElementChanged = e => this.ApplyToAll(t => t.OnTouchedElementChanged?.Invoke(t, e));
|
||||
this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e));
|
||||
|
|
|
@ -13,19 +13,20 @@ namespace MLEM.Misc {
|
|||
/// </summary>
|
||||
public abstract class TextInputWrapper {
|
||||
|
||||
private static TextInputWrapper current;
|
||||
/// <summary>
|
||||
/// The current text input wrapper.
|
||||
/// Set this value before starting your game if you want to use text input wrapping.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException"></exception>
|
||||
public static TextInputWrapper Current {
|
||||
get {
|
||||
if (current == null)
|
||||
throw new InvalidOperationException("The TextInputWrapper was not initialized. For more information, see https://mlem.ellpeck.de/articles/ui.html#text-input");
|
||||
return current;
|
||||
}
|
||||
set => current = value;
|
||||
public static TextInputWrapper Current;
|
||||
|
||||
/// <summary>
|
||||
/// Ensures that <see cref="Current"/> is set to a valid <see cref="TextInputWrapper"/> value by throwing an <see cref="InvalidOperationException"/> exception if <see cref="Current"/> is null.
|
||||
/// </summary>
|
||||
/// <exception cref="InvalidOperationException">If <see cref="Current"/> is null</exception>
|
||||
public static void EnsureExists() {
|
||||
if (Current == null)
|
||||
throw new InvalidOperationException("The TextInputWrapper was not initialized. For more information, see https://mlem.ellpeck.de/articles/ui.html#text-input");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
Loading…
Reference in a new issue