From 04050b914473d47cd7777b848125a5b5da5d120b Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Wed, 21 Jun 2023 00:10:52 +0200 Subject: [PATCH] Added TextField.EnterReceiver --- CHANGELOG.md | 1 + MLEM.Ui/Elements/TextField.cs | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6a83984..f416d60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -41,6 +41,7 @@ Additions - Added UiAnimation system - Added AddCustomStyle and ApplyCustomStyle to UiStyle to allow for easy custom styling of elements - Added UiControls.PressElement +- Added TextField.EnterReceiver Improvements - Increased Element area calculation recursion limit to 64 diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index 40fb0f7..e7cbb46 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -1,6 +1,7 @@ using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using Microsoft.Xna.Framework.Input; using MLEM.Font; using MLEM.Graphics; using MLEM.Input; @@ -139,6 +140,12 @@ namespace MLEM.Ui.Elements { /// The description of the KeyboardInput field on mobile devices and consoles /// public string MobileDescription; + /// + /// An element that should be pressed (using ) if is pressed while this text field is active. + /// Note that, for text fields that are , this is ignored. + /// This also occurs once the text input window is successfully closed on a mobile device. + /// + public Element EnterReceiver; private readonly TextInput textInput; private StyleProp font; @@ -188,12 +195,14 @@ namespace MLEM.Ui.Elements { this.OnPressed += async e => { var title = this.MobileTitle ?? this.PlaceholderText; var result = await MlemPlatform.Current.OpenOnScreenKeyboard(title, this.MobileDescription, this.Text, false); - if (result != null) + if (result != null) { this.SetText(this.Multiline ? result : result.Replace('\n', ' '), true); + this.EnterReceiver?.Controls?.PressElement(this.EnterReceiver); + } }; this.OnTextInput += (element, key, character) => { - if (this.IsSelectedActive && !this.IsHidden) - this.textInput.OnTextInput(key, character); + if (this.IsSelectedActive && !this.IsHidden && !this.textInput.OnTextInput(key, character) && key == Keys.Enter && !this.Multiline) + this.EnterReceiver?.Controls?.PressElement(this.EnterReceiver); }; this.OnDeselected += e => this.CaretPos = 0; this.OnSelected += e => this.CaretPos = this.textInput.Length; @@ -209,8 +218,14 @@ namespace MLEM.Ui.Elements { /// public override void Update(GameTime time) { base.Update(time); - if (this.IsSelectedActive && !this.IsHidden) + if (this.IsSelectedActive && !this.IsHidden) { this.textInput.Update(time, this.Input); +#if FNA + // this occurs in OnTextInput outside FNA, where special keys are also counted as text input + if (this.EnterReceiver != null && !this.Multiline && this.Input.TryConsumePressed(Keys.Enter)) + this.EnterReceiver.Controls?.PressElement(this.EnterReceiver); +#endif + } } ///