From fad06f28be888411691ce214b402ab00f1b2a0a9 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 16 May 2020 01:56:00 +0200 Subject: [PATCH] modified link codes to use paragraphs' OnPressed --- MLEM.Ui/Elements/Paragraph.cs | 17 ++++++++++------- MLEM/Formatting/Codes/LinkCode.cs | 11 ++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 1ed171d..041787b 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -66,15 +66,18 @@ namespace MLEM.Ui.Elements { this.CanBeMoused = false; this.Formatter = new TextFormatter(() => this.BoldFont, () => this.ItalicFont); - this.Formatter.Codes.Add(new Regex("]+)>"), (f, m, r) => new LinkCode(m, r, 1 / 16F, 0.85F, t => t == this.HoveredToken, l => { - if (!this.Input.IsPressed(MouseButton.Left)) + this.Formatter.Codes.Add(new Regex("]+)>"), (f, m, r) => new LinkCode(m, r, 1 / 16F, 0.85F, t => t == this.HoveredToken)); + this.OnPressed += e => { + if (this.HoveredToken == null) return; - try { - Process.Start(l.Match.Groups[1].Value); - } catch (Exception) { - // ignored + foreach (var code in this.HoveredToken.AppliedCodes.OfType()) { + try { + Process.Start(code.Match.Groups[1].Value); + } catch (Exception) { + // ignored + } } - })); + }; } protected override Vector2 CalcActualSize(RectangleF parentArea) { diff --git a/MLEM/Formatting/Codes/LinkCode.cs b/MLEM/Formatting/Codes/LinkCode.cs index e74f4d1..590a633 100644 --- a/MLEM/Formatting/Codes/LinkCode.cs +++ b/MLEM/Formatting/Codes/LinkCode.cs @@ -7,22 +7,19 @@ using MLEM.Font; namespace MLEM.Formatting.Codes { public class LinkCode : UnderlineCode { - private readonly Action onSelected; private readonly Func isSelected; - public LinkCode(Match match, Regex regex, float thickness, float yOffset, Func isSelected, Action onSelected) : base(match, regex, thickness, yOffset) { - this.onSelected = onSelected; + public LinkCode(Match match, Regex regex, float thickness, float yOffset, Func isSelected) : base(match, regex, thickness, yOffset) { this.isSelected = isSelected; } - public override void Update(GameTime time) { - if (this.isSelected(this.Token)) - this.onSelected(this); + public bool IsSelected() { + return this.isSelected(this.Token); } public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, int indexInToken, ref Vector2 pos, GenericFont font, ref Color color, ref float scale, float depth) { // since we inherit from UnderlineCode, we can just call base if selected - return this.isSelected(this.Token) && base.DrawCharacter(time, batch, c, cString, indexInToken, ref pos, font, ref color, ref scale, depth); + return this.IsSelected() && base.DrawCharacter(time, batch, c, cString, indexInToken, ref pos, font, ref color, ref scale, depth); } }