mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
a lot of font cleanup
This commit is contained in:
parent
138f683776
commit
80f36c78bd
14 changed files with 76 additions and 176 deletions
|
@ -1,19 +0,0 @@
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
|
||||||
using MLEM.Extensions;
|
|
||||||
using MonoGame.Extended.BitmapFonts;
|
|
||||||
|
|
||||||
namespace MLEM.Extended.Extensions {
|
|
||||||
public static class BitmapFontExtensions {
|
|
||||||
|
|
||||||
public static string SplitString(this BitmapFont font, string text, float width, float scale) {
|
|
||||||
return SpriteFontExtensions.SplitString(s => font.MeasureString(s).Width, text, width, scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string TruncateString(this BitmapFont font, string text, float width, float scale, bool fromBack = false) {
|
|
||||||
return SpriteFontExtensions.TruncateString(s => font.MeasureString(s).Width, text, width, scale, fromBack);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -8,16 +8,6 @@ using MonoGame.Extended.BitmapFonts;
|
||||||
namespace MLEM.Extended.Extensions {
|
namespace MLEM.Extended.Extensions {
|
||||||
public static class SpriteBatchExtensions {
|
public static class SpriteBatchExtensions {
|
||||||
|
|
||||||
public static void DrawCenteredString(this SpriteBatch batch, BitmapFont font, string text, Vector2 position, float scale, Color color, bool horizontal = true, bool vertical = false, float addedScale = 0) {
|
|
||||||
var size = font.MeasureString(text);
|
|
||||||
var center = new Vector2(
|
|
||||||
horizontal ? size.Width * scale / 2F : 0,
|
|
||||||
vertical ? size.Height * scale / 2F : 0);
|
|
||||||
batch.DrawString(font, text,
|
|
||||||
position + (Vector2) size * scale / 2 - center,
|
|
||||||
color, 0, size / 2, scale + addedScale, SpriteEffects.None, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) {
|
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) {
|
||||||
batch.Draw(texture, destinationRectangle.ToMlem(), sourceRectangle, color, rotation, origin, effects, layerDepth);
|
batch.Draw(texture, destinationRectangle.ToMlem(), sourceRectangle, color, rotation, origin, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
@ -30,6 +20,5 @@ namespace MLEM.Extended.Extensions {
|
||||||
batch.Draw(texture, destinationRectangle, null, color);
|
batch.Draw(texture, destinationRectangle, null, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -7,58 +7,46 @@ using MLEM.Font;
|
||||||
using MonoGame.Extended.BitmapFonts;
|
using MonoGame.Extended.BitmapFonts;
|
||||||
|
|
||||||
namespace MLEM.Extended.Font {
|
namespace MLEM.Extended.Font {
|
||||||
public class GenericBitmapFont : IGenericFont {
|
public class GenericBitmapFont : GenericFont {
|
||||||
|
|
||||||
public readonly BitmapFont Font;
|
public readonly BitmapFont Font;
|
||||||
public float LineHeight => this.Font.LineHeight;
|
public override float LineHeight => this.Font.LineHeight;
|
||||||
|
|
||||||
public GenericBitmapFont(BitmapFont font) {
|
public GenericBitmapFont(BitmapFont font) {
|
||||||
this.Font = font;
|
this.Font = font;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 MeasureString(string text) {
|
public override Vector2 MeasureString(string text) {
|
||||||
return this.Font.MeasureString(text);
|
return this.Font.MeasureString(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 MeasureString(StringBuilder text) {
|
public override Vector2 MeasureString(StringBuilder text) {
|
||||||
return this.Font.MeasureString(text);
|
return this.Font.MeasureString(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
|
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
|
||||||
batch.DrawString(this.Font, text, position, color);
|
batch.DrawString(this.Font, text, position, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
|
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
|
||||||
batch.DrawString(this.Font, text, position, color);
|
batch.DrawString(this.Font, text, position, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawCenteredString(SpriteBatch batch, string text, Vector2 position, float scale, Color color, bool horizontal = true, bool vertical = false, float addedScale = 0) {
|
|
||||||
batch.DrawCenteredString(this.Font, text, position, scale, color, horizontal, vertical, addedScale);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string SplitString(string text, float width, float scale) {
|
|
||||||
return this.Font.SplitString(text, width, scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string TruncateString(string text, float width, float scale, bool fromBack = false) {
|
|
||||||
return this.Font.TruncateString(text, width, scale, fromBack);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -17,9 +17,9 @@ namespace MLEM.Ui.Elements {
|
||||||
private string text;
|
private string text;
|
||||||
private string splitText;
|
private string splitText;
|
||||||
public FormattingCodeCollection Formatting;
|
public FormattingCodeCollection Formatting;
|
||||||
public StyleProp<IGenericFont> RegularFont;
|
public StyleProp<GenericFont> RegularFont;
|
||||||
public StyleProp<IGenericFont> BoldFont;
|
public StyleProp<GenericFont> BoldFont;
|
||||||
public StyleProp<IGenericFont> ItalicFont;
|
public StyleProp<GenericFont> ItalicFont;
|
||||||
public StyleProp<FormatSettings> FormatSettings;
|
public StyleProp<FormatSettings> FormatSettings;
|
||||||
|
|
||||||
public StyleProp<Color> TextColor;
|
public StyleProp<Color> TextColor;
|
||||||
|
|
|
@ -26,7 +26,7 @@ namespace MLEM.Ui.Elements {
|
||||||
public StyleProp<NinePatch> HoveredTexture;
|
public StyleProp<NinePatch> HoveredTexture;
|
||||||
public StyleProp<Color> HoveredColor;
|
public StyleProp<Color> HoveredColor;
|
||||||
public StyleProp<float> TextScale;
|
public StyleProp<float> TextScale;
|
||||||
public StyleProp<IGenericFont> Font;
|
public StyleProp<GenericFont> Font;
|
||||||
private readonly StringBuilder text = new StringBuilder();
|
private readonly StringBuilder text = new StringBuilder();
|
||||||
public string Text => this.text.ToString();
|
public string Text => this.text.ToString();
|
||||||
public string PlaceholderText;
|
public string PlaceholderText;
|
||||||
|
@ -53,7 +53,7 @@ namespace MLEM.Ui.Elements {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextField(Anchor anchor, Vector2 size, Rule rule = null, IGenericFont font = null) : base(anchor, size) {
|
public TextField(Anchor anchor, Vector2 size, Rule rule = null, GenericFont font = null) : base(anchor, size) {
|
||||||
this.InputRule = rule ?? DefaultRule;
|
this.InputRule = rule ?? DefaultRule;
|
||||||
if (font != null)
|
if (font != null)
|
||||||
this.Font.Set(font);
|
this.Font.Set(font);
|
||||||
|
@ -159,17 +159,17 @@ namespace MLEM.Ui.Elements {
|
||||||
batch.Draw(tex, this.DisplayArea, color, this.Scale);
|
batch.Draw(tex, this.DisplayArea, color, this.Scale);
|
||||||
|
|
||||||
if (this.displayedText != null) {
|
if (this.displayedText != null) {
|
||||||
var textPos = this.DisplayArea.Location + new Vector2(this.TextOffsetX * this.Scale, this.DisplayArea.Height / 2);
|
var lineHeight = this.Font.Value.LineHeight * this.TextScale * this.Scale;
|
||||||
|
var textPos = this.DisplayArea.Location + new Vector2(this.TextOffsetX * this.Scale, this.DisplayArea.Height / 2 - lineHeight / 2);
|
||||||
if (this.text.Length > 0 || this.IsSelected) {
|
if (this.text.Length > 0 || this.IsSelected) {
|
||||||
var textColor = this.TextColor.OrDefault(Color.White);
|
var textColor = this.TextColor.OrDefault(Color.White);
|
||||||
this.Font.Value.DrawCenteredString(batch, this.displayedText, textPos, this.TextScale * this.Scale, textColor * alpha, false, true);
|
this.Font.Value.DrawString(batch, this.displayedText, textPos, textColor * alpha, 0, Vector2.Zero, this.TextScale * this.Scale, SpriteEffects.None, 0);
|
||||||
if (this.IsSelected && this.caretBlinkTimer >= 0.5F) {
|
if (this.IsSelected && this.caretBlinkTimer >= 0.5F) {
|
||||||
var textSize = this.Font.Value.MeasureString(this.displayedText.Substring(0, this.CaretPos - this.textOffset)) * this.TextScale * this.Scale;
|
var textSize = this.Font.Value.MeasureString(this.displayedText.Substring(0, this.CaretPos - this.textOffset)) * this.TextScale * this.Scale;
|
||||||
var caretHeight = this.Font.Value.LineHeight * this.TextScale * this.Scale;
|
batch.Draw(batch.GetBlankTexture(), new RectangleF(textPos.X + textSize.X, textPos.Y, this.CaretWidth * this.Scale, lineHeight), null, textColor * alpha);
|
||||||
batch.Draw(batch.GetBlankTexture(), new RectangleF(textPos.X + textSize.X, textPos.Y - caretHeight / 2, this.CaretWidth * this.Scale, caretHeight), null, textColor * alpha);
|
|
||||||
}
|
}
|
||||||
} else if (this.PlaceholderText != null) {
|
} else if (this.PlaceholderText != null) {
|
||||||
this.Font.Value.DrawCenteredString(batch, this.PlaceholderText, textPos, this.TextScale * this.Scale, this.PlaceholderColor.OrDefault(Color.Gray) * alpha, false, true);
|
this.Font.Value.DrawString(batch, this.PlaceholderText, textPos, this.PlaceholderColor.OrDefault(Color.Gray) * alpha, 0, Vector2.Zero, this.TextScale * this.Scale, SpriteEffects.None, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
|
base.Draw(time, batch, alpha, blendState, samplerState, matrix);
|
||||||
|
|
|
@ -35,9 +35,9 @@ namespace MLEM.Ui.Style {
|
||||||
public Vector2 ProgressBarProgressPadding;
|
public Vector2 ProgressBarProgressPadding;
|
||||||
public NinePatch ProgressBarProgressTexture;
|
public NinePatch ProgressBarProgressTexture;
|
||||||
public Color ProgressBarProgressColor;
|
public Color ProgressBarProgressColor;
|
||||||
public IGenericFont Font;
|
public GenericFont Font;
|
||||||
public IGenericFont BoldFont;
|
public GenericFont BoldFont;
|
||||||
public IGenericFont ItalicFont;
|
public GenericFont ItalicFont;
|
||||||
public FormatSettings FormatSettings;
|
public FormatSettings FormatSettings;
|
||||||
public float TextScale = 1;
|
public float TextScale = 1;
|
||||||
public SoundEffect ActionSound;
|
public SoundEffect ActionSound;
|
||||||
|
|
|
@ -34,45 +34,34 @@ namespace MLEM.Ui.Style {
|
||||||
this.Font = new EmptyFont();
|
this.Font = new EmptyFont();
|
||||||
}
|
}
|
||||||
|
|
||||||
private class EmptyFont : IGenericFont {
|
private class EmptyFont : GenericFont {
|
||||||
|
|
||||||
public float LineHeight => 1;
|
public override float LineHeight => 1;
|
||||||
|
|
||||||
public Vector2 MeasureString(string text) {
|
public override Vector2 MeasureString(string text) {
|
||||||
return Vector2.One;
|
return Vector2.One;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 MeasureString(StringBuilder text) {
|
public override Vector2 MeasureString(StringBuilder text) {
|
||||||
return Vector2.One;
|
return Vector2.One;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
|
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
|
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
}
|
|
||||||
|
|
||||||
public void DrawCenteredString(SpriteBatch batch, string text, Vector2 position, float scale, Color color, bool horizontal = true, bool vertical = false, float addedScale = 0) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public string SplitString(string text, float width, float scale) {
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
|
|
||||||
public string TruncateString(string text, float width, float scale, bool fromBack = false) {
|
|
||||||
return text;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,16 +39,6 @@ namespace MLEM.Extensions {
|
||||||
return new NinePatch(tex, 1);
|
return new NinePatch(tex, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void DrawCenteredString(this SpriteBatch batch, SpriteFont font, string text, Vector2 position, float scale, Color color, bool horizontal = true, bool vertical = false, float addedScale = 0) {
|
|
||||||
var size = font.MeasureString(text);
|
|
||||||
var center = new Vector2(
|
|
||||||
horizontal ? size.X * scale / 2F : 0,
|
|
||||||
vertical ? size.Y * scale / 2F : 0);
|
|
||||||
batch.DrawString(font, text,
|
|
||||||
position + size * scale / 2 - center,
|
|
||||||
color, 0, size / 2, scale + addedScale, SpriteEffects.None, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) {
|
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) {
|
||||||
var source = sourceRectangle ?? new Rectangle(0, 0, texture.Width, texture.Height);
|
var source = sourceRectangle ?? new Rectangle(0, 0, texture.Width, texture.Height);
|
||||||
var scale = new Vector2(1F / source.Width, 1F / source.Height) * destinationRectangle.Size;
|
var scale = new Vector2(1F / source.Width, 1F / source.Height) * destinationRectangle.Size;
|
||||||
|
|
|
@ -1,21 +1,30 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
|
using Microsoft.Xna.Framework;
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Font;
|
|
||||||
|
|
||||||
namespace MLEM.Extensions {
|
namespace MLEM.Font {
|
||||||
public static class SpriteFontExtensions {
|
public abstract class GenericFont {
|
||||||
|
|
||||||
public static string SplitString(this SpriteFont font, string text, float width, float scale) {
|
public abstract float LineHeight { get; }
|
||||||
return SplitString(s => font.MeasureString(s).X, text, width, scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string TruncateString(this SpriteFont font, string text, float width, float scale, bool fromBack = false) {
|
public abstract Vector2 MeasureString(string text);
|
||||||
return TruncateString(s => font.MeasureString(s).X, text, width, scale, fromBack);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static string TruncateString(Func<StringBuilder, float> widthFunc, string text, float width, float scale, bool fromBack = false) {
|
public abstract Vector2 MeasureString(StringBuilder text);
|
||||||
|
|
||||||
|
public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color);
|
||||||
|
|
||||||
|
public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);
|
||||||
|
|
||||||
|
public abstract void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
|
||||||
|
|
||||||
|
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color);
|
||||||
|
|
||||||
|
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);
|
||||||
|
|
||||||
|
public abstract void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
|
||||||
|
|
||||||
|
public string TruncateString(string text, float width, float scale, bool fromBack = false) {
|
||||||
var total = new StringBuilder();
|
var total = new StringBuilder();
|
||||||
for (var i = 0; i < text.Length; i++) {
|
for (var i = 0; i < text.Length; i++) {
|
||||||
if (fromBack) {
|
if (fromBack) {
|
||||||
|
@ -24,18 +33,18 @@ namespace MLEM.Extensions {
|
||||||
total.Append(text[i]);
|
total.Append(text[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (widthFunc(total) * scale >= width)
|
if (this.MeasureString(total).X * scale >= width)
|
||||||
return total.ToString(fromBack ? 1 : 0, total.Length - 1);
|
return total.ToString(fromBack ? 1 : 0, total.Length - 1);
|
||||||
}
|
}
|
||||||
return total.ToString();
|
return total.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string SplitString(Func<string, float> widthFunc, string text, float width, float scale) {
|
public string SplitString(string text, float width, float scale) {
|
||||||
var total = new StringBuilder();
|
var total = new StringBuilder();
|
||||||
foreach (var line in text.Split('\n')) {
|
foreach (var line in text.Split('\n')) {
|
||||||
var curr = new StringBuilder();
|
var curr = new StringBuilder();
|
||||||
foreach (var word in line.Split(' ')) {
|
foreach (var word in line.Split(' ')) {
|
||||||
if (widthFunc(word) * scale >= width) {
|
if (this.MeasureString(word).X * scale >= width) {
|
||||||
if (curr.Length > 0) {
|
if (curr.Length > 0) {
|
||||||
total.Append(curr).Append('\n');
|
total.Append(curr).Append('\n');
|
||||||
curr.Clear();
|
curr.Clear();
|
||||||
|
@ -43,7 +52,7 @@ namespace MLEM.Extensions {
|
||||||
var wordBuilder = new StringBuilder();
|
var wordBuilder = new StringBuilder();
|
||||||
for (var i = 0; i < word.Length; i++) {
|
for (var i = 0; i < word.Length; i++) {
|
||||||
wordBuilder.Append(word[i]);
|
wordBuilder.Append(word[i]);
|
||||||
if (widthFunc(wordBuilder.ToString()) * scale >= width) {
|
if (this.MeasureString(wordBuilder.ToString()).X * scale >= width) {
|
||||||
total.Append(wordBuilder.ToString(0, wordBuilder.Length - 1)).Append('\n');
|
total.Append(wordBuilder.ToString(0, wordBuilder.Length - 1)).Append('\n');
|
||||||
wordBuilder.Remove(0, wordBuilder.Length - 1);
|
wordBuilder.Remove(0, wordBuilder.Length - 1);
|
||||||
}
|
}
|
||||||
|
@ -51,7 +60,7 @@ namespace MLEM.Extensions {
|
||||||
curr.Append(wordBuilder).Append(' ');
|
curr.Append(wordBuilder).Append(' ');
|
||||||
} else {
|
} else {
|
||||||
curr.Append(word).Append(' ');
|
curr.Append(word).Append(' ');
|
||||||
if (widthFunc(curr.ToString()) * scale >= width) {
|
if (this.MeasureString(curr.ToString()).X * scale >= width) {
|
||||||
var len = curr.Length - word.Length - 1;
|
var len = curr.Length - word.Length - 1;
|
||||||
if (len > 0) {
|
if (len > 0) {
|
||||||
total.Append(curr.ToString(0, len)).Append('\n');
|
total.Append(curr.ToString(0, len)).Append('\n');
|
||||||
|
@ -65,9 +74,9 @@ namespace MLEM.Extensions {
|
||||||
return total.ToString(0, total.Length - 2);
|
return total.ToString(0, total.Length - 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetWidthString(IGenericFont font, float width, char content = ' ') {
|
public string GetWidthString(float width, char content = ' ') {
|
||||||
var strg = content.ToString();
|
var strg = content.ToString();
|
||||||
while (font.MeasureString(strg).X < width)
|
while (this.MeasureString(strg).X < width)
|
||||||
strg += content;
|
strg += content;
|
||||||
return strg;
|
return strg;
|
||||||
}
|
}
|
|
@ -5,58 +5,46 @@ using Microsoft.Xna.Framework.Graphics;
|
||||||
using MLEM.Extensions;
|
using MLEM.Extensions;
|
||||||
|
|
||||||
namespace MLEM.Font {
|
namespace MLEM.Font {
|
||||||
public class GenericSpriteFont : IGenericFont {
|
public class GenericSpriteFont : GenericFont {
|
||||||
|
|
||||||
public readonly SpriteFont Font;
|
public readonly SpriteFont Font;
|
||||||
public float LineHeight => this.Font.LineSpacing;
|
public override float LineHeight => this.Font.LineSpacing;
|
||||||
|
|
||||||
public GenericSpriteFont(SpriteFont font) {
|
public GenericSpriteFont(SpriteFont font) {
|
||||||
this.Font = font;
|
this.Font = font;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 MeasureString(string text) {
|
public override Vector2 MeasureString(string text) {
|
||||||
return this.Font.MeasureString(text);
|
return this.Font.MeasureString(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Vector2 MeasureString(StringBuilder text) {
|
public override Vector2 MeasureString(StringBuilder text) {
|
||||||
return this.Font.MeasureString(text);
|
return this.Font.MeasureString(text);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
|
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color) {
|
||||||
batch.DrawString(this.Font, text, position, color);
|
batch.DrawString(this.Font, text, position, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
|
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color) {
|
||||||
batch.DrawString(this.Font, text, position, color);
|
batch.DrawString(this.Font, text, position, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
public override void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth) {
|
||||||
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
batch.DrawString(this.Font, text, position, color, rotation, origin, scale, effects, layerDepth);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DrawCenteredString(SpriteBatch batch, string text, Vector2 position, float scale, Color color, bool horizontal = true, bool vertical = false, float addedScale = 0) {
|
|
||||||
batch.DrawCenteredString(this.Font, text, position, scale, color, horizontal, vertical, addedScale);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string SplitString(string text, float width, float scale) {
|
|
||||||
return this.Font.SplitString(text, width, scale);
|
|
||||||
}
|
|
||||||
|
|
||||||
public string TruncateString(string text, float width, float scale, bool fromBack = false) {
|
|
||||||
return this.Font.TruncateString(text, width, scale, fromBack);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,34 +0,0 @@
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.Xna.Framework;
|
|
||||||
using Microsoft.Xna.Framework.Graphics;
|
|
||||||
|
|
||||||
namespace MLEM.Font {
|
|
||||||
public interface IGenericFont {
|
|
||||||
|
|
||||||
float LineHeight { get; }
|
|
||||||
|
|
||||||
Vector2 MeasureString(string text);
|
|
||||||
|
|
||||||
Vector2 MeasureString(StringBuilder text);
|
|
||||||
|
|
||||||
void DrawString(SpriteBatch batch, string text, Vector2 position, Color color);
|
|
||||||
|
|
||||||
void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);
|
|
||||||
|
|
||||||
void DrawString(SpriteBatch batch, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
|
|
||||||
|
|
||||||
void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color);
|
|
||||||
|
|
||||||
void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);
|
|
||||||
|
|
||||||
void DrawString(SpriteBatch batch, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);
|
|
||||||
|
|
||||||
void DrawCenteredString(SpriteBatch batch, string text, Vector2 position, float scale, Color color, bool horizontal = true, bool vertical = false, float addedScale = 0);
|
|
||||||
|
|
||||||
string SplitString(string text, float width, float scale);
|
|
||||||
|
|
||||||
string TruncateString(string text, float width, float scale, bool fromBack = false);
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -37,8 +37,8 @@ namespace MLEM.Formatting {
|
||||||
this.CodeType = Type.Animation;
|
this.CodeType = Type.Animation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public virtual string GetReplacementString(IGenericFont font) {
|
public virtual string GetReplacementString(GenericFont font) {
|
||||||
return this.CodeType == Type.Icon ? SpriteFontExtensions.GetWidthString(font, font.LineHeight) : string.Empty;
|
return this.CodeType == Type.Icon ? font.GetWidthString(font.LineHeight) : string.Empty;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum Type {
|
public enum Type {
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace MLEM.Formatting {
|
||||||
font.DrawString(batch, charSt, position, color, 0, Vector2.Zero, scale, SpriteEffects.None, layerDepth);
|
font.DrawString(batch, charSt, position, color, 0, Vector2.Zero, scale, SpriteEffects.None, layerDepth);
|
||||||
};
|
};
|
||||||
|
|
||||||
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);
|
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);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -44,14 +44,14 @@ namespace MLEM.Formatting {
|
||||||
formatRegex = new Regex($"{op}[^{op}{cl}]*{cl}");
|
formatRegex = new Regex($"{op}[^{op}{cl}]*{cl}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string RemoveFormatting(this string s, IGenericFont font) {
|
public static string RemoveFormatting(this string s, GenericFont font) {
|
||||||
return formatRegex.Replace(s, match => {
|
return formatRegex.Replace(s, match => {
|
||||||
var code = FromMatch(match);
|
var code = FromMatch(match);
|
||||||
return code != null ? code.GetReplacementString(font) : match.Value;
|
return code != null ? code.GetReplacementString(font) : match.Value;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static FormattingCodeCollection GetFormattingCodes(this string s, IGenericFont font) {
|
public static FormattingCodeCollection GetFormattingCodes(this string s, GenericFont font) {
|
||||||
var codes = new FormattingCodeCollection();
|
var codes = new FormattingCodeCollection();
|
||||||
var codeLengths = 0;
|
var codeLengths = 0;
|
||||||
foreach (Match match in formatRegex.Matches(s)) {
|
foreach (Match match in formatRegex.Matches(s)) {
|
||||||
|
@ -70,7 +70,7 @@ namespace MLEM.Formatting {
|
||||||
return codes;
|
return codes;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void DrawFormattedString(this IGenericFont regularFont, SpriteBatch batch, Vector2 pos, string unformattedText, FormattingCodeCollection formatting, Color color, float scale, IGenericFont boldFont = null, IGenericFont italicFont = null, float depth = 0, TimeSpan timeIntoAnimation = default, FormatSettings formatSettings = null) {
|
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 settings = formatSettings ?? FormatSettings.Default;
|
||||||
var currColor = color;
|
var currColor = color;
|
||||||
var currFont = regularFont;
|
var currFont = regularFont;
|
||||||
|
|
Loading…
Reference in a new issue