public static Vector2 DrawParsedString(this SpriteBatch spriteBatch, SpriteFont font, StringPart text, Vector2 position, Color colour, float rotation, Vector2 origin, Vector2 scale, int wrapWidth, Justification justification) { if (wrapWidth <= 0) throw new ArgumentOutOfRangeException("wrapWidth", "wrapWidth must be greater than 0."); return DrawParsedString(spriteBatch, font, text, position, colour, rotation, origin, scale, wrapWidth, justification, true); }
public static Color Parse(StringPart text) { Color value; if (TryParse(text, out value)) return value; else throw new FormatException(text + " could not be parsed into a colour."); }
private static bool TryUppercase(StringPart text, out Color value) { var name = text.ToString().ToUpper(); if (Colours.TryGetValue(name, out value)) { Colours.Add(text, value); return(true); } return(false); }
private static bool TryUppercase(StringPart text, out Color value) { var name = text.ToString().ToUpper(); if (Colours.TryGetValue(name, out value)) { Colours.Add(text, value); return true; } return false; }
public static Color Parse(StringPart text) { Color value; if (TryParse(text, out value)) { return(value); } else { throw new FormatException(text + " could not be parsed into a colour."); } }
private static bool TryComponents(StringPart text, out Color value) { var name = text.ToString().ToUpper(); var parts = name.Split(new char[] { ' ', ',', ';', ':' }); if (ReadColourComponents(parts, out value)) { Colours.Add(text, value); return true; } return false; }
public static bool TryParse(StringPart text, out Color value) { if (Colours.TryGetValue(text, out value)) return true; if (TryUppercase(text, out value)) return true; if (TryComponents(text, out value)) return true; return false; }
private static bool TryComponents(StringPart text, out Color value) { var name = text.ToString().ToUpper(); var parts = name.Split(new char[] { ' ', ',', ';', ':' }); if (ReadColourComponents(parts, out value)) { Colours.Add(text, value); return(true); } return(false); }
public bool StartsWith(StringPart s) { if (Length < s.Length) return false; for (int i = 0; i < s.Length; i++) { if (this[i] != s[i]) return false; } return true; }
public bool TryParse(StringPart name, out T item) { if (items.TryGetValue(name, out item)) { return(true); } if (TryLoad(name, out item)) { return(true); } return(false); }
private bool TryLoad(StringPart name, out T item) { try { item = content.Load <T>(name.ToString()); items.Add(name, item); return(true); } catch { item = default(T); return(false); } }
public static void AppendPart(this StringBuilder sb, StringPart part) { if (part.String != null) { sb.Append(part.String, part.Start, part.Length); } else if (part.StringBuilder != null) { sb.EnsureCapacity(sb.Length + part.Length); for (int i = 0; i < part.Length; i++) { sb.Append(part[i]); } } }
public bool StartsWith(StringPart s) { if (this.Length < s.Length) { return(false); } for (int i = 0; i < s.Length; i++) { if (this[i] != s[i]) { return(false); } } return(true); }
public static bool TryParse(StringPart text, out Color value) { if (Colours.TryGetValue(text, out value)) { return(true); } if (TryUppercase(text, out value)) { return(true); } if (TryComponents(text, out value)) { return(true); } return(false); }
private static TagType ParseTag(StringPart text, ref Vector2 position, ref int lineSpacing, ref int lineIndex) { var type = TagType.None; if (text.Equals("\n")) { NewLine(ref position, ref lineSpacing, ref lineIndex); type = TagType.NewLine; } else if (text.StartsWith("f:") && fonts != null) { var fontName = text.Substring(2); SpriteFont font; if (fonts.TryParse(fontName, out font)) { PushFont(font); type = TagType.FontStart; } } else if (text.Equals("/f")) { PopFont(); type = TagType.FontEnd; } else if (text.StartsWith("c:")) { var colourName = text.Substring(2); Color colour; if (ColourParser.TryParse(colourName, out colour)) { PushColour(colour); type = TagType.ColourStart; } } else if (text.Equals("/c")) { PopColour(); type = TagType.ColourEnd; } return(type); }
private static void RecordFontChanges(StringBuilder input) { fontChanges.Clear(); int tagStart = 0; int tagEnd = 0; while ((tagStart = IndexOf(input, "[", tagEnd)) != -1 && (tagEnd = IndexOf(input, "]", tagStart)) != -1) { var tag = new StringPart(input, tagStart + 1, tagEnd - tagStart - 1); var fontChanged = false; if (tag.StartsWith("f:") && fonts != null) { var fontName = tag.Substring(2); SpriteFont font; if (fonts.TryParse(fontName, out font)) { PushFont(font); fontChanged = true; } } else if (tag.Equals("/f")) { PopFont(); fontChanged = true; } if (fontChanged) { fontChanges.Add(new FontChange() { Font = CurrentFont(), Index = tagStart }); } } }
public bool Equals(StringPart other) { if (this.Length != other.Length) return false; if (this.String == other.String && this.StringBuilder == other.StringBuilder) { return this.Start == other.Start; } else { if ((this.String == null && this.StringBuilder == null) || (other.String == null && other.StringBuilder == null)) return false; for (int i = 0; i < this.Length; i++) { if (other[i] != this[i]) return false; } return true; } }
public static Vector2 MeasureParsedString(this SpriteFont font, StringPart text, int wrapWidth) { // bit of a hack.. return font.MeasureParsedString(text, Vector2.One, wrapWidth); }
private static Vector2 DrawParsedString(this SpriteBatch spriteBatch, SpriteFont font, StringPart text, Vector2 position, Color colour, float rotation, Vector2 origin, Vector2 scale, int wrapWidth, Justification justification, bool drawingEnabled) { input.Clear(); input.AppendPart(text); defaultFont = font; defaultColour = colour; lineWidths.Clear(); if (wrapWidth > 0) Wrap(input, scale, wrapWidth); input.Replace("\n", "[\n]"); colourHistory.Clear(); fontHistory.Clear(); var currentPositionOffset = Vector2.Zero; var lineSpacing = CurrentFont().LineSpacing; var width = 0f; int lineIndex = 0; Vector2 justificationOffset = new Vector2(Justify(lineWidths.Count > 0 ? lineWidths[0] : 0, wrapWidth, justification), 0); int i = 0; int tagStart = 0; int tagEnd = 0; Vector2 size = Vector2.Zero; while (i < input.Length && (tagStart = IndexOf(input, "[", i)) != -1 && (tagEnd = IndexOf(input, "]", tagStart)) != -1) { if (tagStart > i) { SetBuffer(new StringPart(input, i, tagStart - i)); if (drawingEnabled) DrawBuffer(spriteBatch, ref position, rotation, ref origin, ref scale, currentPositionOffset + justificationOffset); size = CurrentFont().MeasureString(buffer); currentPositionOffset.X += size.X; lineSpacing = Math.Max(lineSpacing, (int)size.Y); width = Math.Max(width, currentPositionOffset.X); } i = tagStart; if (ParseTag(new StringPart(input, tagStart + 1, tagEnd - tagStart - 1), ref currentPositionOffset, ref lineSpacing, ref lineIndex) != TagType.None) { i = tagEnd; if (lineWidths.Count > lineIndex) justificationOffset.X = Justify(lineWidths[lineIndex], wrapWidth, justification); } else { SetBuffer(new StringPart(input, i, 1)); if (drawingEnabled) DrawBuffer(spriteBatch, ref position, rotation, ref origin, ref scale, currentPositionOffset + justificationOffset); size = CurrentFont().MeasureString(buffer); currentPositionOffset.X += size.X; lineSpacing = Math.Max(lineSpacing, (int)size.Y); width = Math.Max(width, currentPositionOffset.X); } i++; } if (i != input.Length) { SetBuffer(new StringPart(input, i, input.Length - i)); if (drawingEnabled) DrawBuffer(spriteBatch, ref position, rotation, ref origin, ref scale, currentPositionOffset + justificationOffset); size = CurrentFont().MeasureString(buffer); currentPositionOffset.X += size.X; lineSpacing = Math.Max(lineSpacing, (int)size.Y); width = Math.Max(width, currentPositionOffset.X); } return new Vector2(width, currentPositionOffset.Y + lineSpacing) * scale; }
private static TagType ParseTag(StringPart text, ref Vector2 position, ref int lineSpacing, ref int lineIndex) { var type = TagType.None; if (text.Equals("\n")) { NewLine(ref position, ref lineSpacing, ref lineIndex); type = TagType.NewLine; } else if (text.StartsWith("f:") && fonts != null) { var fontName = text.Substring(2); SpriteFont font; if (fonts.TryParse(fontName, out font)) { PushFont(font); type = TagType.FontStart; } } else if (text.Equals("/f")) { PopFont(); type = TagType.FontEnd; } else if (text.StartsWith("c:")) { var colourName = text.Substring(2); Color colour; if (ColourParser.TryParse(colourName, out colour)) { PushColour(colour); type = TagType.ColourStart; } } else if (text.Equals("/c")) { PopColour(); type = TagType.ColourEnd; } return type; }
private static Vector2 DrawParsedString(this SpriteBatch spriteBatch, SpriteFont font, StringPart text, Vector2 position, Color colour, float rotation, Vector2 origin, Vector2 scale, int wrapWidth, Justification justification, bool drawingEnabled) { input.Clear(); input.AppendPart(text); defaultFont = font; defaultColour = colour; lineWidths.Clear(); if (wrapWidth > 0) { Wrap(input, scale, wrapWidth); } input.Replace("\n", "[\n]"); colourHistory.Clear(); fontHistory.Clear(); var currentPositionOffset = Vector2.Zero; var lineSpacing = CurrentFont().LineSpacing; var width = 0f; int lineIndex = 0; Vector2 justificationOffset = new Vector2(Justify(lineWidths.Count > 0 ? lineWidths[0] : 0, wrapWidth, justification), 0); int i = 0; int tagStart = 0; int tagEnd = 0; Vector2 size = Vector2.Zero; while (i < input.Length && (tagStart = IndexOf(input, "[", i)) != -1 && (tagEnd = IndexOf(input, "]", tagStart)) != -1) { if (tagStart > i) { SetBuffer(new StringPart(input, i, tagStart - i)); if (drawingEnabled) { DrawBuffer(spriteBatch, ref position, rotation, ref origin, ref scale, currentPositionOffset + justificationOffset); } size = CurrentFont().MeasureString(buffer); currentPositionOffset.X += size.X; lineSpacing = Math.Max(lineSpacing, (int)size.Y); width = Math.Max(width, currentPositionOffset.X); } i = tagStart; if (ParseTag(new StringPart(input, tagStart + 1, tagEnd - tagStart - 1), ref currentPositionOffset, ref lineSpacing, ref lineIndex) != TagType.None) { i = tagEnd; if (lineWidths.Count > lineIndex) { justificationOffset.X = Justify(lineWidths[lineIndex], wrapWidth, justification); } } else { SetBuffer(new StringPart(input, i, 1)); if (drawingEnabled) { DrawBuffer(spriteBatch, ref position, rotation, ref origin, ref scale, currentPositionOffset + justificationOffset); } size = CurrentFont().MeasureString(buffer); currentPositionOffset.X += size.X; lineSpacing = Math.Max(lineSpacing, (int)size.Y); width = Math.Max(width, currentPositionOffset.X); } i++; } if (i != input.Length) { SetBuffer(new StringPart(input, i, input.Length - i)); if (drawingEnabled) { DrawBuffer(spriteBatch, ref position, rotation, ref origin, ref scale, currentPositionOffset + justificationOffset); } size = CurrentFont().MeasureString(buffer); currentPositionOffset.X += size.X; lineSpacing = Math.Max(lineSpacing, (int)size.Y); width = Math.Max(width, currentPositionOffset.X); } return(new Vector2(width, currentPositionOffset.Y + lineSpacing) * scale); }
private static void SetBuffer(StringPart text) { buffer.Clear(); buffer.AppendPart(text); }
public static void AppendPart(this StringBuilder sb, StringPart part) { if (part.String != null) sb.Append(part.String, part.Start, part.Length); else if (part.StringBuilder != null) { sb.EnsureCapacity(sb.Length + part.Length); for (int i = 0; i < part.Length; i++) sb.Append(part[i]); } }
public static Vector2 MeasureParsedString(this SpriteFont font, StringPart text, Vector2 scale, int wrapWidth) { // bit of a hack.. return(DrawParsedString(null, font, text, Vector2.Zero, Color.White, 0, Vector2.Zero, Vector2.One, wrapWidth, Justification.Left, false)); }
private static void RecordFontChanges(StringBuilder input) { fontChanges.Clear(); int tagStart = 0; int tagEnd = 0; while ((tagStart = IndexOf(input, "[", tagEnd)) != -1 && (tagEnd = IndexOf(input, "]", tagStart)) != -1) { var tag = new StringPart(input, tagStart + 1, tagEnd - tagStart - 1); var fontChanged = false; if (tag.StartsWith("f:") && fonts != null) { var fontName = tag.Substring(2); SpriteFont font; if (fonts.TryParse(fontName, out font)) { PushFont(font); fontChanged = true; } } else if (tag.Equals("/f")) { PopFont(); fontChanged = true; } if (fontChanged) fontChanges.Add(new FontChange() { Font = CurrentFont(), Index = tagStart }); } }
public static Vector2 MeasureParsedString(this SpriteFont font, StringPart text, Vector2 scale, int wrapWidth) { // bit of a hack.. return DrawParsedString(null, font, text, Vector2.Zero, Color.White, 0, Vector2.Zero, Vector2.One, wrapWidth, Justification.Left, false); }
/// <summary> /// Appends text onto the last line written. /// </summary> /// <param name="text">The text to append.</param> public void Write(StringPart line) { if (moveNextDrawToNewLine || text.Count == 0) { text.Add(line); moveNextDrawToNewLine = false; } else { var current = text[text.Count - 1]; text[text.Count - 1] = current.ToString() + line.ToString(); } //if (line[line.Length - 1] == '\n') // moveNextDrawToNewLine = true; text.RemoveRange(0, Math.Max(0, text.Count - historyCapacity)); if (startIndex == text.Count - 1) ScrollToNewest(); }
/// <summary> /// Writes the line. /// </summary> /// <param name="line">The line.</param> public void WriteLine(StringPart line) { //var t = text[text.Count - 1]; //text.RemoveAt(text.Count - 1); //t.Batch.Clear(); //t.Batch.Write(line); //t.Height = t.Batch.CalculateArea(Int2D.Zero, Justification, Area.Width).Height; //text.Insert(0, t); Write(line.ToString()); moveNextDrawToNewLine = true; }
public static Vector2 DrawParsedString(this SpriteBatch spriteBatch, SpriteFont font, StringPart text, Vector2 position, Color colour) { return(DrawParsedString(spriteBatch, font, text, position, colour, 0, Vector2.Zero, Vector2.One, 0, Justification.Left)); }
public static Vector2 DrawParsedString(this SpriteBatch spriteBatch, SpriteFont font, StringPart text, Vector2 position, Color colour, float rotation, Vector2 origin, Vector2 scale, int wrapWidth, Justification justification) { if (wrapWidth <= 0) { throw new ArgumentOutOfRangeException("wrapWidth", "wrapWidth must be greater than 0."); } return(DrawParsedString(spriteBatch, font, text, position, colour, rotation, origin, scale, wrapWidth, justification, true)); }
public static Vector2 DrawParsedString(this SpriteBatch spriteBatch, SpriteFont font, StringPart text, Vector2 position, Color colour) { return DrawParsedString(spriteBatch, font, text, position, colour, 0, Vector2.Zero, Vector2.One, 0, Justification.Left); }