From f445aba45c2538bc50867efed23f4d551014c58b Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 27 Nov 2021 23:35:37 +0100 Subject: [PATCH] improved Token memory allocations --- MLEM/Formatting/Token.cs | 16 +++++++++++++--- MLEM/Formatting/TokenizedString.cs | 13 ++++++------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/MLEM/Formatting/Token.cs b/MLEM/Formatting/Token.cs index 8d0d8c6..9b47836 100644 --- a/MLEM/Formatting/Token.cs +++ b/MLEM/Formatting/Token.cs @@ -60,8 +60,13 @@ namespace MLEM.Formatting { /// /// The default color, if none is specified /// The color to render with - public Color? GetColor(Color defaultPick) { - return this.AppliedCodes.Select(c => c.GetColor(defaultPick)).FirstOrDefault(c => c.HasValue); + public Color GetColor(Color defaultPick) { + foreach (var code in this.AppliedCodes) { + var color = code.GetColor(defaultPick); + if (color.HasValue) + return color.Value; + } + return defaultPick; } /// @@ -70,7 +75,12 @@ namespace MLEM.Formatting { /// The default font, if none is specified /// The font to render with public GenericFont GetFont(GenericFont defaultPick) { - return this.AppliedCodes.Select(c => c.GetFont(defaultPick)).FirstOrDefault(f => f != null); + foreach (var code in this.AppliedCodes) { + var font = code.GetFont(defaultPick); + if (font != null) + return font; + } + return defaultPick; } /// diff --git a/MLEM/Formatting/TokenizedString.cs b/MLEM/Formatting/TokenizedString.cs index 68b0af4..d1ac51b 100644 --- a/MLEM/Formatting/TokenizedString.cs +++ b/MLEM/Formatting/TokenizedString.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -116,8 +115,8 @@ namespace MLEM.Formatting { var innerOffset = new Vector2(this.initialInnerOffset * scale, 0); for (var t = 0; t < this.Tokens.Length; t++) { var token = this.Tokens[t]; - var drawFont = token.GetFont(font) ?? font; - var drawColor = token.GetColor(color) ?? color; + var drawFont = token.GetFont(font); + var drawColor = token.GetColor(color); for (var l = 0; l < token.SplitDisplayString.Length; l++) { var line = token.SplitDisplayString[l]; for (var i = 0; i < line.Length; i++) { @@ -184,7 +183,7 @@ namespace MLEM.Formatting { var innerOffset = new Vector2(this.initialInnerOffset, 0); for (var t = 0; t < this.Tokens.Length; t++) { var token = this.Tokens[t]; - var tokenFont = token.GetFont(font) ?? font; + var tokenFont = token.GetFont(font); token.InnerOffsets = new float[token.SplitDisplayString.Length - 1]; var area = new List(); for (var l = 0; l < token.SplitDisplayString.Length; l++) { @@ -207,7 +206,7 @@ namespace MLEM.Formatting { private float GetInnerOffsetX(GenericFont defaultFont, int tokenIndex, int lineIndex, TextAlignment alignment) { if (alignment > TextAlignment.Left) { var token = this.Tokens[tokenIndex]; - var tokenFont = token.GetFont(defaultFont) ?? defaultFont; + var tokenFont = token.GetFont(defaultFont); // if we're the last line in our line array, then we don't contain a line split, so the line ends in a later token var endsLater = lineIndex >= token.SplitDisplayString.Length - 1; // if the line ends in our token, we should ignore trailing white space @@ -215,7 +214,7 @@ namespace MLEM.Formatting { if (endsLater) { for (var i = tokenIndex + 1; i < this.Tokens.Length; i++) { var other = this.Tokens[i]; - var otherFont = other.GetFont(defaultFont) ?? defaultFont; + var otherFont = other.GetFont(defaultFont); if (other.SplitDisplayString.Length > 1) { // the line ends in this token (so we also ignore trailing whitespaces) restOfLine += otherFont.MeasureString(other.SplitDisplayString[0], true).X; @@ -238,7 +237,7 @@ namespace MLEM.Formatting { foreach (var token in this.Tokens) { index -= token.Substring.Length; if (index <= 0) - return token.GetFont(font) ?? font; + return token.GetFont(font); } return font; }