1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-06-04 22:23:37 +02:00

made format settings a bit better and also added drop shadow

This commit is contained in:
Ellpeck 2019-12-26 19:30:17 +01:00
parent e0b83f6644
commit 3d0c3cd6d1
7 changed files with 43 additions and 17 deletions

View file

@ -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

View file

@ -20,6 +20,7 @@ namespace MLEM.Ui.Elements {
public StyleProp<IGenericFont> RegularFont;
public StyleProp<IGenericFont> BoldFont;
public StyleProp<IGenericFont> ItalicFont;
public StyleProp<FormatSettings> FormatSettings;
public StyleProp<NinePatch> Background;
public StyleProp<Color> 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);

View file

@ -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;
}

View file

@ -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;
}
}

View file

@ -50,7 +50,8 @@ namespace MLEM.Formatting {
Regular,
Bold,
Italic
Italic,
Shadow
}
}

View file

@ -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);
}
}

View file

@ -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<int, FormattingCode> 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<int, FormattingCode> 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;
}
}