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

only throw an exception in TextInputWrapper if it is required

This commit is contained in:
Ellpeck 2020-06-24 16:54:23 +02:00
parent 096131ce15
commit ca4c8731bc
4 changed files with 14 additions and 9 deletions

View file

@ -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>

View file

@ -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) {

View file

@ -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));

View file

@ -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>