mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-11-26 06:28:35 +01:00
fixed formatting codes at the beginning of strings being ignored
This commit is contained in:
parent
c41875d574
commit
78049c9cd8
2 changed files with 9 additions and 5 deletions
|
@ -57,6 +57,10 @@ namespace MLEM.Formatting {
|
||||||
public TokenizedString Tokenize(GenericFont font, string s) {
|
public TokenizedString Tokenize(GenericFont font, string s) {
|
||||||
var tokens = new List<Token>();
|
var tokens = new List<Token>();
|
||||||
var codes = new List<Code>();
|
var codes = new List<Code>();
|
||||||
|
// add the formatting code right at the start of the string
|
||||||
|
var firstCode = this.GetNextCode(s, 0, 0);
|
||||||
|
if (firstCode != null)
|
||||||
|
codes.Add(firstCode);
|
||||||
var rawIndex = 0;
|
var rawIndex = 0;
|
||||||
while (rawIndex < s.Length) {
|
while (rawIndex < s.Length) {
|
||||||
var index = StripFormatting(font, s.Substring(0, rawIndex), tokens.SelectMany(t => t.AppliedCodes)).Length;
|
var index = StripFormatting(font, s.Substring(0, rawIndex), tokens.SelectMany(t => t.AppliedCodes)).Length;
|
||||||
|
@ -82,10 +86,10 @@ namespace MLEM.Formatting {
|
||||||
return new TokenizedString(font, s, StripFormatting(font, s, tokens.SelectMany(t => t.AppliedCodes)), tokens.ToArray());
|
return new TokenizedString(font, s, StripFormatting(font, s, tokens.SelectMany(t => t.AppliedCodes)), tokens.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
private Code GetNextCode(string s, int index) {
|
private Code GetNextCode(string s, int index, int maxIndex = int.MaxValue) {
|
||||||
var (c, m, r) = this.Codes
|
var (c, m, r) = this.Codes
|
||||||
.Select(kv => (c: kv.Value, m: kv.Key.Match(s, index), r: kv.Key))
|
.Select(kv => (c: kv.Value, m: kv.Key.Match(s, index), r: kv.Key))
|
||||||
.Where(kv => kv.m.Success)
|
.Where(kv => kv.m.Success && kv.m.Index <= maxIndex)
|
||||||
.OrderBy(kv => kv.m.Index)
|
.OrderBy(kv => kv.m.Index)
|
||||||
.FirstOrDefault();
|
.FirstOrDefault();
|
||||||
return c?.Invoke(this, m, r);
|
return c?.Invoke(this, m, r);
|
||||||
|
|
|
@ -124,21 +124,21 @@ namespace Sandbox {
|
||||||
|
|
||||||
var formatter = new TextFormatter();
|
var formatter = new TextFormatter();
|
||||||
formatter.AddImage("Test", new TextureRegion(tex, 0, 8, 24, 24));
|
formatter.AddImage("Test", new TextureRegion(tex, 0, 8, 24, 24));
|
||||||
var strg = "This <c CornflowerBlue>is a formatted string</c> with <c #ffff0000>two bits of formatting</c>! It also includesavery<c Pink>long</c>wordthatis<c Blue>formatted</c>aswell. Additionally, it <a wobbly>wobbles</a> and has a <s>shadow</s> or a <s #ffff0000 4>weird shadow</s>. We like icons too! <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test>";
|
var strg = "<s>This</s> <c CornflowerBlue>is a formatted string</c> with <c #ffff0000>two bits of formatting</c>! It also includesavery<c Pink>long</c>wordthatis<c Blue>formatted</c>aswell. Additionally, it <a wobbly>wobbles</a> and has a <s>shadow</s> or a <s #ffff0000 4>weird shadow</s>. We like icons too! <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test> <i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test><i Test>";
|
||||||
//var strg = "Lorem Ipsum <i Test> is simply dummy text of the <i Test> printing and typesetting <i Test> industry. Lorem Ipsum has been the industry's standard dummy text <i Test> ever since the <i Test> 1500s, when <i Test><i Test><i Test><i Test><i Test><i Test><i Test> an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
|
//var strg = "Lorem Ipsum <i Test> is simply dummy text of the <i Test> printing and typesetting <i Test> industry. Lorem Ipsum has been the industry's standard dummy text <i Test> ever since the <i Test> 1500s, when <i Test><i Test><i Test><i Test><i Test><i Test><i Test> an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
|
||||||
//var strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
|
//var strg = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";
|
||||||
//var strg = "This is <u>a test of the underlined formatting code</u>!";
|
//var strg = "This is <u>a test of the underlined formatting code</u>!";
|
||||||
this.tokenized = formatter.Tokenize(font, strg);
|
this.tokenized = formatter.Tokenize(font, strg);
|
||||||
this.tokenized.Split(font, 400, 5);
|
this.tokenized.Split(font, 400, 5);
|
||||||
|
|
||||||
/*this.OnDraw += (g, time) => {
|
this.OnDraw += (g, time) => {
|
||||||
this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
|
this.SpriteBatch.Begin(samplerState: SamplerState.PointClamp);
|
||||||
this.SpriteBatch.FillRectangle(new RectangleF(400, 20, 400, 1000), Color.Green);
|
this.SpriteBatch.FillRectangle(new RectangleF(400, 20, 400, 1000), Color.Green);
|
||||||
font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, 5, SpriteEffects.None, 0);
|
font.DrawString(this.SpriteBatch, this.tokenized.DisplayString, new Vector2(400, 20), Color.White * 0.25F, 0, Vector2.Zero, 5, SpriteEffects.None, 0);
|
||||||
this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, 5, 0);
|
this.tokenized.Draw(time, this.SpriteBatch, new Vector2(400, 20), font, Color.White, 5, 0);
|
||||||
this.SpriteBatch.DrawGrid(new Vector2(30, 30), new Vector2(40, 60), new Point(10, 5), Color.Yellow, 3);
|
this.SpriteBatch.DrawGrid(new Vector2(30, 30), new Vector2(40, 60), new Point(10, 5), Color.Yellow, 3);
|
||||||
this.SpriteBatch.End();
|
this.SpriteBatch.End();
|
||||||
};*/
|
};
|
||||||
this.OnUpdate += (g, time) => {
|
this.OnUpdate += (g, time) => {
|
||||||
if (this.InputHandler.IsPressed(Keys.W)) {
|
if (this.InputHandler.IsPressed(Keys.W)) {
|
||||||
this.tokenized = formatter.Tokenize(font, strg);
|
this.tokenized = formatter.Tokenize(font, strg);
|
||||||
|
|
Loading…
Reference in a new issue