From ca4c8731bc21117efdc938df63fcc07fa97aa2b0 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 24 Jun 2020 16:54:23 +0200 Subject: [PATCH] only throw an exception in TextInputWrapper if it is required --- MLEM.Ui/Elements/Element.cs | 2 ++ MLEM.Ui/Elements/TextField.cs | 1 + MLEM.Ui/UiSystem.cs | 3 ++- MLEM/Misc/TextInputWrapper.cs | 17 +++++++++-------- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/MLEM.Ui/Elements/Element.cs b/MLEM.Ui/Elements/Element.cs index d29e0c4..645cc08 100644 --- a/MLEM.Ui/Elements/Element.cs +++ b/MLEM.Ui/Elements/Element.cs @@ -313,6 +313,8 @@ namespace MLEM.Ui.Elements { public GenericCallback OnTouchExit; /// /// Event that is called when text input is made. + /// When an element uses this event, it should call 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 is true, this event is never called. /// diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 1330fdb..4176ed0 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -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) { diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 37e88c4..2754393 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -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)); diff --git a/MLEM/Misc/TextInputWrapper.cs b/MLEM/Misc/TextInputWrapper.cs index e9bade5..e3fb4a8 100644 --- a/MLEM/Misc/TextInputWrapper.cs +++ b/MLEM/Misc/TextInputWrapper.cs @@ -13,19 +13,20 @@ namespace MLEM.Misc { /// public abstract class TextInputWrapper { - private static TextInputWrapper current; /// /// The current text input wrapper. /// Set this value before starting your game if you want to use text input wrapping. /// /// - 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; + + /// + /// Ensures that is set to a valid value by throwing an exception if is null. + /// + /// If is null + 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"); } ///