Пример #1
0
        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);
        }
Пример #2
0
 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.");
 }
Пример #3
0
        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);
        }
Пример #4
0
        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;
        }
Пример #5
0
        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.");
            }
        }
Пример #6
0
        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;
        }
Пример #7
0
        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;
        }
Пример #8
0
        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);
        }
Пример #9
0
        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;
        }
Пример #10
0
        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);
        }
Пример #11
0
 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);
     }
 }
Пример #12
0
 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]);
         }
     }
 }
Пример #13
0
        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);
        }
Пример #14
0
        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);
        }
Пример #15
0
        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);
        }
Пример #16
0
        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
                    });
                }
            }
        }
Пример #17
0
        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;
            }
        }
Пример #18
0
 public static Vector2 MeasureParsedString(this SpriteFont font, StringPart text, int wrapWidth)
 {
     // bit of a hack..
     return font.MeasureParsedString(text, Vector2.One, wrapWidth);
 }
Пример #19
0
        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;
        }
Пример #20
0
        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;
        }
Пример #21
0
        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);
        }
Пример #22
0
 private static void SetBuffer(StringPart text)
 {
     buffer.Clear();
     buffer.AppendPart(text);
 }
Пример #23
0
 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]);
     }
 }
Пример #24
0
 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));
 }
Пример #25
0
 private static void SetBuffer(StringPart text)
 {
     buffer.Clear();
     buffer.AppendPart(text);
 }
Пример #26
0
        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 });
            }
        }
Пример #27
0
 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);
 }
Пример #28
0
        /// <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();
        }
Пример #29
0
 /// <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;
 }
Пример #30
0
 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));
 }
Пример #31
0
        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));
        }
Пример #32
0
 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);
 }