From f2c544dc65f6fb8a83b05f71bb757044cbe371bf Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Tue, 9 Jun 2020 18:56:01 +0200 Subject: [PATCH] modified link behavior for paragraphs to be easier to modify --- Demos.Android/Activity1.cs | 10 ++++++++-- Demos.Android/Demos.Android.csproj | 4 ++++ MLEM.Ui/Elements/Paragraph.cs | 9 ++------- MLEM.Ui/UiSystem.cs | 10 +++++++++- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/Demos.Android/Activity1.cs b/Demos.Android/Activity1.cs index 6ee2b0e..4ae7af1 100644 --- a/Demos.Android/Activity1.cs +++ b/Demos.Android/Activity1.cs @@ -1,5 +1,7 @@ using Android.App; +using Android.Content; using Android.Content.PM; +using Android.Net; using Android.OS; using Android.Views; using Microsoft.Xna.Framework; @@ -31,8 +33,12 @@ namespace Demos.Android { this.game = new GameImpl(); // reset MlemGame width and height to use device's aspect ratio this.game.GraphicsDeviceManager.ResetWidthAndHeight(this.game.Window); - // disable mouse handling for android to make emulator behavior more coherent - this.game.OnLoadContent += game => game.InputHandler.HandleMouse = false; + this.game.OnLoadContent += game => { + // disable mouse handling for android to make emulator behavior more coherent + game.InputHandler.HandleMouse = false; + // make text links be opened properly + game.UiSystem.LinkBehavior = l => this.StartActivity(new Intent(Intent.ActionView, Uri.Parse(l.Match.Groups[1].Value))); + }; // set the game to fullscreen to cause the status bar to be hidden this.game.GraphicsDeviceManager.IsFullScreen = true; this.view = this.game.Services.GetService(typeof(View)) as View; diff --git a/Demos.Android/Demos.Android.csproj b/Demos.Android/Demos.Android.csproj index d396e9a..ea3454a 100644 --- a/Demos.Android/Demos.Android.csproj +++ b/Demos.Android/Demos.Android.csproj @@ -83,6 +83,10 @@ {997f4739-7bec-4621-b9ca-68deb2d74412} MLEM.Startup + + {6f00629a-8b87-4264-8896-19983285e32f} + MLEM.Ui + {1d6ab762-43c4-4775-8924-707c7ec3f142} MLEM diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index dd86630..8dedb66 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -241,13 +241,8 @@ namespace MLEM.Ui.Elements { link.IsSelected = false; }; this.OnPressed += e => { - foreach (var code in token.AppliedCodes.OfType()) { - try { - Process.Start(code.Match.Groups[1].Value); - } catch (Exception) { - // ignored - } - } + foreach (var code in token.AppliedCodes.OfType()) + this.System?.LinkBehavior?.Invoke(code); }; } diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 2a398b6..e69af66 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Text.RegularExpressions; using Microsoft.Xna.Framework; @@ -99,6 +100,12 @@ namespace MLEM.Ui { /// public TextFormatter TextFormatter; /// + /// The action that should be executed when a in a paragraph's is pressed. + /// The actual link stored in the link code is stored in its 's 1st group. + /// By default, the browser is opened with the given link's address. + /// + public Action LinkBehavior = l => Process.Start(l.Match.Groups[1].Value); + /// /// The that this ui system is controlled by. /// The ui controls are also the place to change bindings for controller and keyboard input. /// @@ -213,7 +220,8 @@ namespace MLEM.Ui { }; this.TextFormatter = new TextFormatter(); - this.TextFormatter.Codes.Add(new Regex("]+))?>"), (f, m, r) => new LinkCode(m, r, 1 / 16F, 0.85F, t => this.Controls.MousedElement is Paragraph.Link link && link.Token == t)); + this.TextFormatter.Codes.Add(new Regex("]+))?>"), (f, m, r) => new LinkCode(m, r, 1 / 16F, 0.85F, + t => this.Controls.MousedElement is Paragraph.Link l1 && l1.Token == t || this.Controls.TouchedElement is Paragraph.Link l2 && l2.Token == t)); } ///