From 3d0c3cd6d1aba6cc9b076a7010c47b26e01464cd Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Thu, 26 Dec 2019 19:30:17 +0100 Subject: [PATCH] made format settings a bit better and also added drop shadow --- Demos/UiDemo.cs | 2 +- MLEM.Ui/Elements/Paragraph.cs | 4 +++- MLEM.Ui/Style/UiStyle.cs | 2 ++ MLEM/Formatting/FormatSettings.cs | 21 +++++++++++++++++++++ MLEM/Formatting/FormattingCode.cs | 3 ++- MLEM/Formatting/TextAnimation.cs | 16 ++++++---------- MLEM/Formatting/TextFormatting.cs | 12 ++++++++---- 7 files changed, 43 insertions(+), 17 deletions(-) create mode 100644 MLEM/Formatting/FormatSettings.cs diff --git a/Demos/UiDemo.cs b/Demos/UiDemo.cs index 0bacbda..23d3c1d 100644 --- a/Demos/UiDemo.cs +++ b/Demos/UiDemo.cs @@ -90,7 +90,7 @@ namespace Demos { root.AddChild(new VerticalSpace(3)); // a paragraph with formatting codes. To see them all or to add more, check the TextFormatting class - root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Paragraphs can also contain [Blue]formatting codes[White], including colors and [Italic]text styles[Regular]. The names of all [Orange]MonoGame Colors[White] can be used, as well as the codes [Italic]Italic[Regular] and [Bold]Bold[Regular]. \n[Italic]Even [CornflowerBlue]Cornflower Blue[White] works!")); + root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "Paragraphs can also contain [Blue]formatting codes[White], including colors and [Italic]text styles[Regular]. The names of all [Orange]MonoGame Colors[White] can be used, as well as the codes [Italic]Italic[Regular], [Bold]Bold[Regular] and [Shadow]Drop Shadow'd[Regular]. \n[Italic]Even [CornflowerBlue]Cornflower Blue[White] works!")); // adding some custom image formatting codes // note that all added formatting codes need to be lowercase, while their casing doesn't matter when used diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index 0bc6364..d91d7cd 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -20,6 +20,7 @@ namespace MLEM.Ui.Elements { public StyleProp RegularFont; public StyleProp BoldFont; public StyleProp ItalicFont; + public StyleProp FormatSettings; public StyleProp Background; public StyleProp BackgroundColor; @@ -93,7 +94,7 @@ namespace MLEM.Ui.Elements { this.RegularFont.Value.DrawString(batch, this.splitText, pos, color, 0, Vector2.Zero, sc, SpriteEffects.None, 0); } else { // if we have formatting codes, we should do it - this.RegularFont.Value.DrawFormattedString(batch, pos, this.splitText, this.codeLocations, color, sc, this.BoldFont.Value, this.ItalicFont.Value, 0, this.TimeIntoAnimation); + this.RegularFont.Value.DrawFormattedString(batch, pos, this.splitText, this.codeLocations, color, sc, this.BoldFont.Value, this.ItalicFont.Value, 0, this.TimeIntoAnimation, this.FormatSettings); } base.Draw(time, batch, alpha, blendState, samplerState, matrix); } @@ -104,6 +105,7 @@ namespace MLEM.Ui.Elements { this.RegularFont.SetFromStyle(style.Font); this.BoldFont.SetFromStyle(style.BoldFont ?? style.Font); this.ItalicFont.SetFromStyle(style.ItalicFont ?? style.Font); + this.FormatSettings.SetFromStyle(style.FormatSettings ?? Formatting.FormatSettings.Default); } public delegate string TextCallback(Paragraph paragraph); diff --git a/MLEM.Ui/Style/UiStyle.cs b/MLEM.Ui/Style/UiStyle.cs index 9c4748e..6271646 100644 --- a/MLEM.Ui/Style/UiStyle.cs +++ b/MLEM.Ui/Style/UiStyle.cs @@ -1,5 +1,6 @@ using Microsoft.Xna.Framework; using MLEM.Font; +using MLEM.Formatting; using MLEM.Textures; namespace MLEM.Ui.Style { @@ -33,6 +34,7 @@ namespace MLEM.Ui.Style { public IGenericFont Font; public IGenericFont BoldFont; public IGenericFont ItalicFont; + public FormatSettings FormatSettings; public float TextScale = 1; } diff --git a/MLEM/Formatting/FormatSettings.cs b/MLEM/Formatting/FormatSettings.cs new file mode 100644 index 0000000..cb17a70 --- /dev/null +++ b/MLEM/Formatting/FormatSettings.cs @@ -0,0 +1,21 @@ +using Microsoft.Xna.Framework; + +namespace MLEM.Formatting { + public class FormatSettings { + + public static readonly FormatSettings Default = new FormatSettings { + WobbleModifier = 5, + WobbleHeightModifier = 1 / 8F, + TypingSpeed = 20, + DropShadowColor = Color.Black, + DropShadowOffset = new Vector2(2) + }; + + public float WobbleModifier; + public float WobbleHeightModifier; + public float TypingSpeed; + public Color DropShadowColor; + public Vector2 DropShadowOffset; + + } +} \ No newline at end of file diff --git a/MLEM/Formatting/FormattingCode.cs b/MLEM/Formatting/FormattingCode.cs index 33c60f9..2a418a1 100644 --- a/MLEM/Formatting/FormattingCode.cs +++ b/MLEM/Formatting/FormattingCode.cs @@ -50,7 +50,8 @@ namespace MLEM.Formatting { Regular, Bold, - Italic + Italic, + Shadow } } \ No newline at end of file diff --git a/MLEM/Formatting/TextAnimation.cs b/MLEM/Formatting/TextAnimation.cs index a7684a7..b1a7622 100644 --- a/MLEM/Formatting/TextAnimation.cs +++ b/MLEM/Formatting/TextAnimation.cs @@ -6,25 +6,21 @@ using MLEM.Font; namespace MLEM.Formatting { public static class TextAnimation { - public static float WobbleModifier = 5; - public static float WobbleHeightModifier = 1 / 8F; - public static float TypingSpeed = 20; - - public static readonly DrawCharacter Default = (font, batch, totalText, index, effectStartIndex, charSt, position, color, scale, layerDepth, timeIntoAnimation) => { + public static readonly DrawCharacter Default = (settings, font, batch, totalText, index, effectStartIndex, charSt, position, color, scale, layerDepth, timeIntoAnimation) => { font.DrawString(batch, charSt, position, color, 0, Vector2.Zero, scale, SpriteEffects.None, layerDepth); }; - public static readonly DrawCharacter Wobbly = (font, batch, totalText, index, effectStartIndex, charSt, position, color, scale, layerDepth, timeIntoAnimation) => { - var offset = new Vector2(0, (float) Math.Sin(index + timeIntoAnimation.TotalSeconds * WobbleModifier) * font.LineHeight * WobbleHeightModifier * scale); + public static readonly DrawCharacter Wobbly = (settings, font, batch, totalText, index, effectStartIndex, charSt, position, color, scale, layerDepth, timeIntoAnimation) => { + var offset = new Vector2(0, (float) Math.Sin(index + timeIntoAnimation.TotalSeconds * settings.WobbleModifier) * font.LineHeight * settings.WobbleHeightModifier * scale); font.DrawString(batch, charSt, position + offset, color, 0, Vector2.Zero, scale, SpriteEffects.None, layerDepth); }; - public static readonly DrawCharacter Typing = (font, batch, totalText, index, effectStartIndex, charSt, position, color, scale, layerDepth, timeIntoAnimation) => { - if (timeIntoAnimation.TotalSeconds * TypingSpeed > index - effectStartIndex + 1) + public static readonly DrawCharacter Typing = (settings, font, batch, totalText, index, effectStartIndex, charSt, position, color, scale, layerDepth, timeIntoAnimation) => { + if (timeIntoAnimation.TotalSeconds * settings.TypingSpeed > index - effectStartIndex + 1) font.DrawString(batch, charSt, position, color, 0, Vector2.Zero, scale, SpriteEffects.None, layerDepth); }; - public delegate void DrawCharacter(IGenericFont font, SpriteBatch batch, string totalText, int index, int effectStartIndex, string charSt, Vector2 position, Color color, float scale, float layerDepth, TimeSpan timeIntoAnimation); + public delegate void DrawCharacter(FormatSettings settings, IGenericFont font, SpriteBatch batch, string totalText, int index, int effectStartIndex, string charSt, Vector2 position, Color color, float scale, float layerDepth, TimeSpan timeIntoAnimation); } } \ No newline at end of file diff --git a/MLEM/Formatting/TextFormatting.cs b/MLEM/Formatting/TextFormatting.cs index dcd6d31..4f450b9 100644 --- a/MLEM/Formatting/TextFormatting.cs +++ b/MLEM/Formatting/TextFormatting.cs @@ -21,6 +21,7 @@ namespace MLEM.Formatting { FormattingCodes["regular"] = new FormattingCode(TextStyle.Regular); FormattingCodes["italic"] = new FormattingCode(TextStyle.Italic); FormattingCodes["bold"] = new FormattingCode(TextStyle.Bold); + FormattingCodes["shadow"] = new FormattingCode(TextStyle.Shadow); // color codes var colors = typeof(Color).GetProperties(); @@ -73,9 +74,11 @@ namespace MLEM.Formatting { return codes; } - public static void DrawFormattedString(this IGenericFont regularFont, SpriteBatch batch, Vector2 pos, string text, Dictionary codeLocations, Color color, float scale, IGenericFont boldFont = null, IGenericFont italicFont = null, float depth = 0, TimeSpan timeIntoAnimation = default) { + public static void DrawFormattedString(this IGenericFont regularFont, SpriteBatch batch, Vector2 pos, string text, Dictionary codeLocations, Color color, float scale, IGenericFont boldFont = null, IGenericFont italicFont = null, float depth = 0, TimeSpan timeIntoAnimation = default, FormatSettings formatSettings = null) { + var settings = formatSettings ?? FormatSettings.Default; var currColor = color; var currFont = regularFont; + var currStyle = TextStyle.Regular; var currAnim = TextAnimation.Default; var animStart = 0; @@ -101,6 +104,7 @@ namespace MLEM.Formatting { currFont = italicFont ?? regularFont; break; } + currStyle = code.Style; break; case FormattingCode.Type.Icon: var iconSc = new Vector2(1F / code.Icon.Width, 1F / code.Icon.Height) * regularFont.LineHeight * scale; @@ -119,9 +123,9 @@ namespace MLEM.Formatting { innerOffset.X = 0; innerOffset.Y += regularFont.LineHeight * scale; } else { - currAnim(currFont, batch, text, i, animStart, cSt, pos + innerOffset, currColor, scale, depth, timeIntoAnimation); - // we measure the string with the regular font here so that previously split - // strings don't get too long with a bolder font. This shouldn't effect visuals too much + if (currStyle == TextStyle.Shadow) + currAnim(settings, currFont, batch, text, i, animStart, cSt, pos + innerOffset + settings.DropShadowOffset * scale, settings.DropShadowColor, scale, depth, timeIntoAnimation); + currAnim(settings, currFont, batch, text, i, animStart, cSt, pos + innerOffset, currColor, scale, depth, timeIntoAnimation); innerOffset.X += regularFont.MeasureString(cSt).X * scale; } }