1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 12:58:33 +01:00

Allow setting a default color for clickable links in UiStyle

This commit is contained in:
Ell 2022-03-07 12:00:33 +01:00
parent b260aa6901
commit d5ec0b8001
6 changed files with 24 additions and 6 deletions

View file

@ -19,6 +19,7 @@ Improvements
- Generify GenericFont's string drawing - Generify GenericFont's string drawing
- Added InputHandler mouse and touch position querying that preserves the game's viewport - Added InputHandler mouse and touch position querying that preserves the game's viewport
- Added float version of GetRandomWeightedEntry - Added float version of GetRandomWeightedEntry
- Allow LinkCode to specify a color to draw with
Fixes Fixes
- **Fixed a formatting Code only knowing about the last Token that it is applied in** - **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 - 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 - Update elements less aggressively when changing a ui system's style
- Automatically update all elements when changing a ui system's viewport - Automatically update all elements when changing a ui system's viewport
- Allow setting a default color for clickable links in UiStyle
Fixes Fixes
- Fixed paragraph links having incorrect hover locations when using special text alignments - Fixed paragraph links having incorrect hover locations when using special text alignments

View file

@ -72,7 +72,7 @@ namespace Demos {
IsHidden = true 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' <c CornflowerBlue><l https://github.com/Ellpeck/MLEM/tree/main/Demos>source code</l></c> for more in-depth explanations of their functionality or the <c CornflowerBlue><l https://mlem.ellpeck.de/>website</l></c> 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' <l https://github.com/Ellpeck/MLEM/tree/main/Demos>source code</l> for more in-depth explanations of their functionality or the <l https://mlem.ellpeck.de/>website</l> for tutorials and API documentation."));
selection.AddChild(new VerticalSpace(5)); selection.AddChild(new VerticalSpace(5));
foreach (var demo in Demos) { foreach (var demo in Demos) {
selection.AddChild(new Button(Anchor.AutoCenter, new Vector2(1, 10), demo.Key, demo.Value.Item1) { 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), PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8),
ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4), 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), 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
}; };
} }

View file

@ -47,7 +47,8 @@ namespace Demos {
CheckboxCheckmark = new TextureRegion(this.testTexture, 24, 0, 8, 8), CheckboxCheckmark = new TextureRegion(this.testTexture, 24, 0, 8, 8),
RadioTexture = new NinePatch(new TextureRegion(this.testTexture, 16, 0, 8, 8), 3), RadioTexture = new NinePatch(new TextureRegion(this.testTexture, 16, 0, 8, 8), 3),
RadioCheckmark = new TextureRegion(this.testTexture, 32, 0, 8, 8), RadioCheckmark = new TextureRegion(this.testTexture, 32, 0, 8, 8),
AdditionalFonts = {{"Monospaced", new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/MonospacedFont"))}} AdditionalFonts = {{"Monospaced", new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/MonospacedFont"))}},
LinkColor = Color.CornflowerBlue
}; };
var untexturedStyle = new UntexturedStyle(this.SpriteBatch) { var untexturedStyle = new UntexturedStyle(this.SpriteBatch) {
TextScale = style.TextScale, 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 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); 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 <c CornflowerBlue><l Left>left</l></c> aligned text, <c CornflowerBlue><l Right>right</l></c> aligned text and <c CornflowerBlue><l Center>center</l></c> aligned text."; const string alignText = "Paragraphs can have <l Left>left</l> aligned text, <l Right>right</l> aligned text and <l Center>center</l> aligned text.";
this.root.AddChild(new VerticalSpace(3)); this.root.AddChild(new VerticalSpace(3));
var alignPar = this.root.AddChild(new Paragraph(Anchor.AutoLeft, 1, alignText)); var alignPar = this.root.AddChild(new Paragraph(Anchor.AutoLeft, 1, alignText));
alignPar.LinkAction = (l, c) => { alignPar.LinkAction = (l, c) => {

View file

@ -3,6 +3,7 @@ using System.Collections.Generic;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using MLEM.Font; using MLEM.Font;
using MLEM.Formatting; using MLEM.Formatting;
using MLEM.Formatting.Codes;
using MLEM.Misc; using MLEM.Misc;
using MLEM.Textures; using MLEM.Textures;
using MLEM.Ui.Elements; using MLEM.Ui.Elements;
@ -208,6 +209,11 @@ namespace MLEM.Ui.Style {
/// </summary> /// </summary>
public SoundEffectInfo ActionSound; public SoundEffectInfo ActionSound;
/// <summary> /// <summary>
/// The color that a <see cref="Paragraph"/>'s <see cref="Paragraph.Link"/> codes should have.
/// This value is passed to <see cref="LinkCode"/>.
/// </summary>
public Color? LinkColor;
/// <summary>
/// A set of additional fonts that can be used for the <c>&lt;f FontName&gt;</c> formatting code /// A set of additional fonts that can be used for the <c>&lt;f FontName&gt;</c> formatting code
/// </summary> /// </summary>
public Dictionary<string, GenericFont> AdditionalFonts = new Dictionary<string, GenericFont>(); public Dictionary<string, GenericFont> AdditionalFonts = new Dictionary<string, GenericFont>();

View file

@ -241,7 +241,8 @@ namespace MLEM.Ui {
this.TextFormatter = new TextFormatter(); this.TextFormatter = new TextFormatter();
this.TextFormatter.Codes.Add(new Regex("<l(?: ([^>]+))?>"), (f, m, r) => new LinkCode(m, r, 1 / 16F, 0.85F, this.TextFormatter.Codes.Add(new Regex("<l(?: ([^>]+))?>"), (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("<f ([^>]+)>"), (_, m, r) => new FontCode(m, r, this.TextFormatter.Codes.Add(new Regex("<f ([^>]+)>"), (_, m, r) => new FontCode(m, r,
f => this.Style.AdditionalFonts != null && this.Style.AdditionalFonts.TryGetValue(m.Groups[1].Value, out var c) ? c : f)); f => this.Style.AdditionalFonts != null && this.Style.AdditionalFonts.TryGetValue(m.Groups[1].Value, out var c) ? c : f));
} }

View file

@ -9,10 +9,12 @@ namespace MLEM.Formatting.Codes {
public class LinkCode : UnderlineCode { public class LinkCode : UnderlineCode {
private readonly Func<Token, bool> isSelected; private readonly Func<Token, bool> isSelected;
private readonly Color? color;
/// <inheritdoc /> /// <inheritdoc />
public LinkCode(Match match, Regex regex, float thickness, float yOffset, Func<Token, bool> isSelected) : base(match, regex, thickness, yOffset) { public LinkCode(Match match, Regex regex, float thickness, float yOffset, Func<Token, bool> isSelected, Color? color = null) : base(match, regex, thickness, yOffset) {
this.isSelected = isSelected; this.isSelected = isSelected;
this.color = color;
} }
/// <summary> /// <summary>
@ -27,6 +29,11 @@ namespace MLEM.Formatting.Codes {
return false; return false;
} }
/// <inheritdoc />
public override Color? GetColor(Color defaultPick) {
return this.color;
}
/// <inheritdoc /> /// <inheritdoc />
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) { 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 // since we inherit from UnderlineCode, we can just call base if selected