mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-22 20:58:34 +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;
|
public GenericCallback OnTouchExit;
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Event that is called when text input is made.
|
/// 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.
|
/// 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.
|
/// Also note that if <see cref="TextInputWrapper.RequiresOnScreenKeyboard"/> is true, this event is never called.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
@ -132,6 +132,7 @@ namespace MLEM.Ui.Elements {
|
||||||
if (font != null)
|
if (font != null)
|
||||||
this.Font.Set(font);
|
this.Font.Set(font);
|
||||||
|
|
||||||
|
TextInputWrapper.EnsureExists();
|
||||||
if (TextInputWrapper.Current.RequiresOnScreenKeyboard()) {
|
if (TextInputWrapper.Current.RequiresOnScreenKeyboard()) {
|
||||||
this.OnPressed += async e => {
|
this.OnPressed += async e => {
|
||||||
if (!KeyboardInput.IsVisible) {
|
if (!KeyboardInput.IsVisible) {
|
||||||
|
|
|
@ -199,7 +199,8 @@ namespace MLEM.Ui {
|
||||||
root.Element.ForceUpdateArea();
|
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.OnMousedElementChanged = e => this.ApplyToAll(t => t.OnMousedElementChanged?.Invoke(t, e));
|
||||||
this.OnTouchedElementChanged = e => this.ApplyToAll(t => t.OnTouchedElementChanged?.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));
|
this.OnSelectedElementChanged = e => this.ApplyToAll(t => t.OnSelectedElementChanged?.Invoke(t, e));
|
||||||
|
|
|
@ -13,19 +13,20 @@ namespace MLEM.Misc {
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class TextInputWrapper {
|
public abstract class TextInputWrapper {
|
||||||
|
|
||||||
private static TextInputWrapper current;
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The current text input wrapper.
|
/// The current text input wrapper.
|
||||||
/// Set this value before starting your game if you want to use text input wrapping.
|
/// Set this value before starting your game if you want to use text input wrapping.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <exception cref="InvalidOperationException"></exception>
|
/// <exception cref="InvalidOperationException"></exception>
|
||||||
public static TextInputWrapper Current {
|
public static TextInputWrapper Current;
|
||||||
get {
|
|
||||||
if (current == null)
|
/// <summary>
|
||||||
throw new InvalidOperationException("The TextInputWrapper was not initialized. For more information, see https://mlem.ellpeck.de/articles/ui.html#text-input");
|
/// 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.
|
||||||
return current;
|
/// </summary>
|
||||||
}
|
/// <exception cref="InvalidOperationException">If <see cref="Current"/> is null</exception>
|
||||||
set => current = value;
|
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>
|
/// <summary>
|
||||||
|
|
Loading…
Reference in a new issue