1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-05-16 14:18:46 +02:00

removed some obsolete features

This commit is contained in:
Ellpeck 2020-06-12 17:19:19 +02:00
parent 251261f3d7
commit 0a8a3c3660
7 changed files with 2 additions and 325 deletions

View file

@ -23,20 +23,11 @@ namespace MLEM.Ui.Elements {
public class Paragraph : Element {
private string text;
private string splitText;
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public FormattingCodeCollection Formatting;
/// <summary>
/// The font that this paragraph draws text with.
/// To set its bold and italic font, use <see cref="GenericFont.Bold"/> and <see cref="GenericFont.Italic"/>.
/// </summary>
public StyleProp<GenericFont> RegularFont;
[Obsolete("Use the new GenericFont.Bold and GenericFont.Italic instead")]
public StyleProp<GenericFont> BoldFont;
[Obsolete("Use the new GenericFont.Bold and GenericFont.Italic instead")]
public StyleProp<GenericFont> ItalicFont;
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public StyleProp<FormatSettings> FormatSettings;
/// <summary>
/// The tokenized version of the <see cref="Text"/>
/// </summary>
@ -78,10 +69,6 @@ namespace MLEM.Ui.Elements {
/// Use this event for setting this paragraph's text if it changes frequently.
/// </summary>
public TextCallback GetTextCallback;
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public TextModifier RenderedTextModifier = text => text;
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public TimeSpan TimeIntoAnimation;
/// <summary>
/// Creates a new paragraph with the given settings.
@ -112,13 +99,6 @@ namespace MLEM.Ui.Elements {
protected override Vector2 CalcActualSize(RectangleF parentArea) {
var size = base.CalcActualSize(parentArea);
this.ParseText(size);
// old formatting stuff
if (this.Formatting.Count > 0) {
var textDims = this.RegularFont.Value.MeasureString(this.splitText) * this.TextScale * this.Scale;
return new Vector2(this.AutoAdjustWidth ? textDims.X + this.ScaledPadding.Width : size.X, textDims.Y + this.ScaledPadding.Height);
}
var dims = this.TokenizedText.Measure(this.RegularFont) * this.TextScale * this.Scale;
return new Vector2(this.AutoAdjustWidth ? dims.X + this.ScaledPadding.Width : size.X, dims.Y + this.ScaledPadding.Height);
}
@ -127,9 +107,6 @@ namespace MLEM.Ui.Elements {
public override void Update(GameTime time) {
this.QueryTextCallback();
base.Update(time);
this.TimeIntoAnimation += time.ElapsedGameTime;
if (this.TokenizedText != null)
this.TokenizedText.Update(time);
}
@ -138,15 +115,8 @@ namespace MLEM.Ui.Elements {
public override void Draw(GameTime time, SpriteBatch batch, float alpha, BlendState blendState, SamplerState samplerState, Matrix matrix) {
var pos = this.DisplayArea.Location;
var sc = this.TextScale * this.Scale;
var color = this.TextColor.OrDefault(Color.White) * alpha;
// legacy formatting stuff
if (this.Formatting.Count > 0) {
var toRender = this.RenderedTextModifier(this.splitText);
this.RegularFont.Value.DrawFormattedString(batch, pos, toRender, this.Formatting, color, sc, this.BoldFont.Value, this.ItalicFont.Value, 0, this.TimeIntoAnimation, this.FormatSettings);
} else {
this.TokenizedText.Draw(time, batch, pos, this.RegularFont, color, sc, 0);
}
this.TokenizedText.Draw(time, batch, pos, this.RegularFont, color, sc, 0);
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
}
@ -154,10 +124,7 @@ namespace MLEM.Ui.Elements {
protected override void InitStyle(UiStyle style) {
base.InitStyle(style);
this.TextScale.SetFromStyle(style.TextScale);
this.RegularFont.SetFromStyle(style.Font);
this.BoldFont.SetFromStyle(style.BoldFont ?? style.Font);
this.ItalicFont.SetFromStyle(style.ItalicFont ?? style.Font);
this.FormatSettings.SetFromStyle(style.FormatSettings);
this.RegularFont.SetFromStyle(style.Font);
}
/// <summary>
@ -166,10 +133,6 @@ namespace MLEM.Ui.Elements {
/// </summary>
/// <param name="size">The paragraph's default size</param>
protected virtual void ParseText(Vector2 size) {
// old formatting stuff
this.splitText = this.RegularFont.Value.SplitString(this.Text.RemoveFormatting(this.RegularFont.Value), size.X - this.ScaledPadding.Width, this.TextScale * this.Scale);
this.Formatting = this.Text.GetFormattingCodes(this.RegularFont.Value);
if (this.TokenizedText == null)
this.TokenizedText = this.System.TextFormatter.Tokenize(this.RegularFont, this.Text);
@ -204,9 +167,6 @@ namespace MLEM.Ui.Elements {
/// <param name="paragraph">The current paragraph</param>
public delegate string TextCallback(Paragraph paragraph);
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public delegate string TextModifier(string text);
/// <summary>
/// A link is a sub-element of the <see cref="Paragraph"/> that is added onto it as a child for any tokens that contain <see cref="LinkCode"/>, to make them selectable and clickable.
/// </summary>

View file

@ -129,12 +129,6 @@ namespace MLEM.Ui.Style {
/// Note that, to specify a bold and italic font for <see cref="TextFormatter"/>, you should use <see cref="GenericFont.Bold"/> and <see cref="GenericFont.Italic"/>.
/// </summary>
public GenericFont Font;
[Obsolete("Use the new GenericFont.Bold and GenericFont.Italic instead")]
public GenericFont BoldFont;
[Obsolete("Use the new GenericFont.Bold and GenericFont.Italic instead")]
public GenericFont ItalicFont;
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public FormatSettings FormatSettings;
/// <summary>
/// The scale that text should be rendered with in <see cref="Paragraph"/> and other elements
/// </summary>

View file

@ -1,18 +0,0 @@
using System;
using Microsoft.Xna.Framework;
using MLEM.Misc;
namespace MLEM.Formatting {
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public class FormatSettings : GenericDataHolder {
public static readonly FormatSettings Default = new FormatSettings();
public float WobbleModifier = 5;
public float WobbleHeightModifier = 1 / 8F;
public float TypingSpeed = 20;
public Color DropShadowColor = Color.Black;
public Vector2 DropShadowOffset = new Vector2(2);
}
}

View file

@ -1,65 +0,0 @@
using System;
using Microsoft.Xna.Framework;
using MLEM.Animations;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Textures;
namespace MLEM.Formatting {
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public class FormattingCode {
public readonly Type CodeType;
public readonly Color Color;
public readonly TextStyle Style;
public readonly SpriteAnimation Icon;
public readonly TextAnimation.DrawCharacter Animation;
public FormattingCode(Color color) {
this.Color = color;
this.CodeType = Type.Color;
}
public FormattingCode(TextStyle style) {
this.Style = style;
this.CodeType = Type.Style;
}
public FormattingCode(TextureRegion icon) :
this(new SpriteAnimation(0, icon)) {
}
public FormattingCode(SpriteAnimation icon) {
this.Icon = icon;
this.CodeType = Type.Icon;
}
public FormattingCode(TextAnimation.DrawCharacter animation) {
this.Animation = animation;
this.CodeType = Type.Animation;
}
public virtual string GetReplacementString(GenericFont font) {
return this.CodeType == Type.Icon ? font.GetWidthString(font.LineHeight) : string.Empty;
}
public enum Type {
Color,
Style,
Icon,
Animation
}
}
public enum TextStyle {
Regular,
Bold,
Italic,
Shadow
}
}

View file

@ -1,26 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using MLEM.Misc;
namespace MLEM.Formatting {
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public class FormattingCodeCollection : Dictionary<int, List<FormattingCodeData>> {
}
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public class FormattingCodeData : GenericDataHolder {
public readonly FormattingCode Code;
public readonly Match Match;
public readonly int UnformattedIndex;
public FormattingCodeData(FormattingCode code, Match match, int unformattedIndex) {
this.Code = code;
this.Match = match;
this.UnformattedIndex = unformattedIndex;
}
}
}

View file

@ -1,27 +0,0 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Font;
namespace MLEM.Formatting {
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public static class TextAnimation {
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 = (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 = (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(FormatSettings settings, GenericFont font, SpriteBatch batch, string totalText, int index, int effectStartIndex, string charSt, Vector2 position, Color color, float scale, float layerDepth, TimeSpan timeIntoAnimation);
}
}

View file

@ -1,141 +0,0 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
using MLEM.Font;
using MLEM.Misc;
using MLEM.Textures;
namespace MLEM.Formatting {
[Obsolete("Use the new text formatting system in MLEM.Formatting instead")]
public static class TextFormatting {
public static readonly Dictionary<string, FormattingCode> FormattingCodes = new Dictionary<string, FormattingCode>(StringComparer.OrdinalIgnoreCase);
private static Regex formatRegex;
static TextFormatting() {
SetFormatIndicators('[', ']');
// style codes
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();
foreach (var color in colors) {
if (color.GetGetMethod().IsStatic)
FormattingCodes[color.Name] = new FormattingCode((Color) color.GetValue(null));
}
// animations
FormattingCodes["unanimated"] = new FormattingCode(TextAnimation.Default);
FormattingCodes["wobbly"] = new FormattingCode(TextAnimation.Wobbly);
FormattingCodes["typing"] = new FormattingCode(TextAnimation.Typing);
}
public static void SetFormatIndicators(char opener, char closer) {
// escape the opener and closer so that any character can be used
var op = "\\" + opener;
var cl = "\\" + closer;
// find any text that is surrounded by the opener and closer
formatRegex = new Regex($"{op}[^{op}{cl}]*{cl}");
}
public static string RemoveFormatting(this string s, GenericFont font) {
return formatRegex.Replace(s, match => {
var code = FromMatch(match);
return code != null ? code.GetReplacementString(font) : match.Value;
});
}
public static FormattingCodeCollection GetFormattingCodes(this string s, GenericFont font) {
var codes = new FormattingCodeCollection();
var codeLengths = 0;
foreach (Match match in formatRegex.Matches(s)) {
var code = FromMatch(match);
if (code == null)
continue;
var index = match.Index - codeLengths;
var data = new FormattingCodeData(code, match, index);
if (codes.TryGetValue(index, out var curr)) {
curr.Add(data);
} else {
codes.Add(index, new List<FormattingCodeData> {data});
}
codeLengths += match.Length - code.GetReplacementString(font).Length;
}
return codes;
}
public static void DrawFormattedString(this GenericFont regularFont, SpriteBatch batch, Vector2 pos, string unformattedText, FormattingCodeCollection formatting, Color color, float scale, GenericFont boldFont = null, GenericFont 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;
var innerOffset = new Vector2();
var formatIndex = 0;
foreach (var c in unformattedText) {
// check if the current character's index has a formatting code
if (formatting.TryGetValue(formatIndex, out var codes)) {
foreach (var data in codes) {
var code = data.Code;
// if so, apply it
switch (code.CodeType) {
case FormattingCode.Type.Color:
currColor = code.Color.CopyAlpha(color);
break;
case FormattingCode.Type.Style:
switch (code.Style) {
case TextStyle.Regular:
currFont = regularFont;
break;
case TextStyle.Bold:
currFont = boldFont ?? regularFont;
break;
case TextStyle.Italic:
currFont = italicFont ?? regularFont;
break;
}
currStyle = code.Style;
break;
case FormattingCode.Type.Icon:
code.Icon.SetTime(timeIntoAnimation.TotalSeconds * code.Icon.SpeedMultiplier % code.Icon.TotalTime);
batch.Draw(code.Icon.CurrentRegion, new RectangleF(pos + innerOffset, new Vector2(regularFont.LineHeight * scale)), color, 0, Vector2.Zero, SpriteEffects.None, depth);
break;
case FormattingCode.Type.Animation:
currAnim = code.Animation;
animStart = formatIndex;
break;
}
}
}
var cSt = c.ToString();
if (c == '\n') {
innerOffset.X = 0;
innerOffset.Y += regularFont.LineHeight * scale;
} else {
if (currStyle == TextStyle.Shadow)
currAnim(settings, currFont, batch, unformattedText, formatIndex, animStart, cSt, pos + innerOffset + settings.DropShadowOffset * scale, settings.DropShadowColor, scale, depth, timeIntoAnimation);
currAnim(settings, currFont, batch, unformattedText, formatIndex, animStart, cSt, pos + innerOffset, currColor, scale, depth, timeIntoAnimation);
innerOffset.X += regularFont.MeasureString(cSt).X * scale;
formatIndex++;
}
}
}
private static FormattingCode FromMatch(Capture match) {
var rawCode = match.Value.Substring(1, match.Value.Length - 2);
FormattingCodes.TryGetValue(rawCode, out var val);
return val;
}
}
}