From d5ec0b8001d96796518b016785fa0474e5952afa Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Mon, 7 Mar 2022 12:00:33 +0100 Subject: [PATCH] Allow setting a default color for clickable links in UiStyle --- CHANGELOG.md | 2 ++ Demos/GameImpl.cs | 5 +++-- Demos/UiDemo.cs | 5 +++-- MLEM.Ui/Style/UiStyle.cs | 6 ++++++ MLEM.Ui/UiSystem.cs | 3 ++- MLEM/Formatting/Codes/LinkCode.cs | 9 ++++++++- 6 files changed, 24 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e204d7d..270d405 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ Improvements - Generify GenericFont's string drawing - Added InputHandler mouse and touch position querying that preserves the game's viewport - Added float version of GetRandomWeightedEntry +- Allow LinkCode to specify a color to draw with Fixes - **Fixed a formatting Code only knowing about the last Token that it is applied in** @@ -41,6 +42,7 @@ Improvements - Made custom values of Element.Style persist when a new ui style is set - Update elements less aggressively when changing a ui system's style - Automatically update all elements when changing a ui system's viewport +- Allow setting a default color for clickable links in UiStyle Fixes - Fixed paragraph links having incorrect hover locations when using special text alignments diff --git a/Demos/GameImpl.cs b/Demos/GameImpl.cs index 8b11a78..983113f 100644 --- a/Demos/GameImpl.cs +++ b/Demos/GameImpl.cs @@ -72,7 +72,7 @@ namespace Demos { IsHidden = true }); - selection.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Select the demo you want to see below using your mouse, touch input, your keyboard or a controller. Check the demos' source code for more in-depth explanations of their functionality or the website for tutorials and API documentation.")); + selection.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Select the demo you want to see below using your mouse, touch input, your keyboard or a controller. Check the demos' source code for more in-depth explanations of their functionality or the website for tutorials and API documentation.")); selection.AddChild(new VerticalSpace(5)); foreach (var demo in Demos) { selection.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), demo.Key, demo.Value.Item1) { @@ -99,7 +99,8 @@ namespace Demos { PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8), ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4), ScrollBarBackground = new NinePatch(new TextureRegion(tex, 12, 0, 4, 8), 1, 1, 2, 2), - ScrollBarScrollerTexture = new NinePatch(new TextureRegion(tex, 8, 0, 4, 8), 1, 1, 2, 2) + ScrollBarScrollerTexture = new NinePatch(new TextureRegion(tex, 8, 0, 4, 8), 1, 1, 2, 2), + LinkColor = Color.CornflowerBlue }; } diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 00164e7..afec596 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -47,7 +47,8 @@ namespace Demos { CheckboxCheckmark = new TextureRegion(this.testTexture, 24, 0, 8, 8), RadioTexture = new NinePatch(new TextureRegion(this.testTexture, 16, 0, 8, 8), 3), RadioCheckmark = new TextureRegion(this.testTexture, 32, 0, 8, 8), - AdditionalFonts = {{"Monospaced", new GenericSpriteFont(LoadContent("Fonts/MonospacedFont"))}} + AdditionalFonts = {{"Monospaced", new GenericSpriteFont(LoadContent("Fonts/MonospacedFont"))}}, + LinkColor = Color.CornflowerBlue }; var untexturedStyle = new UntexturedStyle(this.SpriteBatch) { TextScale = style.TextScale, @@ -208,7 +209,7 @@ namespace Demos { this.root.AddChild(new Button(Anchor.AutoLeft, new Vector2(1, 10), "Disabled button", "This button can't be clicked or moved to using automatic navigation") {IsDisabled = true}).PositionOffset = new Vector2(0, 1); this.root.AddChild(new Checkbox(Anchor.AutoLeft, new Vector2(1, 10), "Disabled checkbox") {IsDisabled = true}).PositionOffset = new Vector2(0, 1); - const string alignText = "Paragraphs can have left aligned text, right aligned text and center aligned text."; + const string alignText = "Paragraphs can have left aligned text, right aligned text and center aligned text."; this.root.AddChild(new VerticalSpace(3)); var alignPar = this.root.AddChild(new Paragraph(Anchor.AutoLeft, 1, alignText)); alignPar.LinkAction = (l, c) => { diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index 427342d..4870e84 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using Microsoft.Xna.Framework; using MLEM.Font; using MLEM.Formatting; +using MLEM.Formatting.Codes; using MLEM.Misc; using MLEM.Textures; using MLEM.Ui.Elements; @@ -208,6 +209,11 @@ namespace MLEM.Ui.Style { /// public SoundEffectInfo ActionSound; /// + /// The color that a 's codes should have. + /// This value is passed to . + /// + public Color? LinkColor; + /// /// A set of additional fonts that can be used for the <f FontName> formatting code /// public Dictionary AdditionalFonts = new Dictionary(); diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 596861d..302f2da 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -241,7 +241,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 l1 && l1.Token == t || this.Controls.TouchedElement is Paragraph.Link l2 && l2.Token == t)); + t => this.Controls.MousedElement is Paragraph.Link l1 && l1.Token == t || this.Controls.TouchedElement is Paragraph.Link l2 && l2.Token == t, + this.Style.LinkColor)); this.TextFormatter.Codes.Add(new Regex("]+)>"), (_, m, r) => new FontCode(m, r, f => this.Style.AdditionalFonts != null && this.Style.AdditionalFonts.TryGetValue(m.Groups[1].Value, out var c) ? c : f)); } diff --git a/MLEM/Formatting/Codes/LinkCode.cs b/MLEM/Formatting/Codes/LinkCode.cs index 866c639..c27f4e1 100644 --- a/MLEM/Formatting/Codes/LinkCode.cs +++ b/MLEM/Formatting/Codes/LinkCode.cs @@ -9,10 +9,12 @@ namespace MLEM.Formatting.Codes { public class LinkCode : UnderlineCode { private readonly Func isSelected; + private readonly Color? color; /// - public LinkCode(Match match, Regex regex, float thickness, float yOffset, Func isSelected) : base(match, regex, thickness, yOffset) { + public LinkCode(Match match, Regex regex, float thickness, float yOffset, Func isSelected, Color? color = null) : base(match, regex, thickness, yOffset) { this.isSelected = isSelected; + this.color = color; } /// @@ -27,6 +29,11 @@ namespace MLEM.Formatting.Codes { return false; } + /// + public override Color? GetColor(Color defaultPick) { + return this.color; + } + /// public override bool DrawCharacter(GameTime time, SpriteBatch batch, char c, string cString, Token token, 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