Ejemplo n.º 1
0
        /// <summary>
        /// Dispose resources
        /// </summary>
        public void Dispose()
        {
            if (GlyphTileset != null)
            {
                GlyphTileset.Dispose();
            }
            GlyphTileset = null;

            if (TextTileset != null)
            {
                TextTileset.Dispose();
            }
            TextTileset = null;

            LineHeight  = 0;
            Advance     = 0;
            TTFFileName = string.Empty;
            TTFSize     = -1;

            IsDisposed = true;

            GC.SuppressFinalize(this);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Prints some text on the screen within a rectangle with justification
        /// </summary>
        /// <param name="batch">Spritebatch handle</param>
        /// <param name="zone">Rectangle of the text</param>
        /// <param name="justification">Text justifcation</param>
        /// <param name="color">Text color</param>
        /// <param name="text">Text to print</param>
        internal void DrawText(SpriteBatch batch, Vector4 zone, TextJustification justification, Color color, string text)
        {
            if (string.IsNullOrEmpty(text) || batch == null)
            {
                return;
            }

            if (GlyphTileset == null)
            {
            }

            // Encode string to xml
            string          msg  = "<?xml version=\"1.0\" encoding=\"unicode\" standalone=\"yes\"?><root>" + text + "</root>";
            UnicodeEncoding utf8 = new UnicodeEncoding();

            byte[]        buffer = utf8.GetBytes(msg);
            MemoryStream  stream = new MemoryStream(buffer);
            XmlTextReader reader = new XmlTextReader(stream);

            reader.WhitespaceHandling = WhitespaceHandling.All;


            // Color stack
            Stack <Color> ColorStack = new Stack <Color>();

            ColorStack.Push(color);
            Color currentcolor = color;


            Vector4 rect = zone;


            // Extra offset when displaying tile
            int tileoffset = 0;

            try
            {
                // Skip the first tags "<?...?>" and "<root>"
                reader.MoveToContent();

                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Attribute:
                    {
                    }
                    break;


                        #region control tags

                    // Special tags
                    case XmlNodeType.Element:
                    {
                        switch (reader.Name.ToLower())
                        {
                        case "tile":
                        {
                            if (TextTileset == null)
                            {
                                break;
                            }

                            int  id   = int.Parse(reader.GetAttribute("id"));
                            Tile tile = TextTileset.GetTile(id);
                            batch.DrawTile(TextTileset, id, rect.Xy, Color.White);

                            rect.X += tile.Size.Width;
                            //rect.Offset(tile.Size.Width, 0);

                            tileoffset = tile.Size.Height - LineHeight;
                        }
                        break;

                        case "br":
                        {
                            rect.X     = zone.X;
                            rect.Y    += (int)(LineHeight * GlyphTileset.Scale.Y) + tileoffset + Interline;
                            tileoffset = 0;
                        }
                        break;


                        // Change the color
                        case "color":
                        {
                            ColorStack.Push(currentcolor);

                            currentcolor = Color.FromArgb(int.Parse(reader.GetAttribute("a")),
                                                          int.Parse(reader.GetAttribute("r")),
                                                          int.Parse(reader.GetAttribute("g")),
                                                          int.Parse(reader.GetAttribute("b")));
                        }
                        break;
                        }
                    }
                    break;

                        #endregion


                        #region closing control tags

                    case XmlNodeType.EndElement:
                    {
                        switch (reader.Name.ToLower())
                        {
                        case "color":
                        {
                            currentcolor = ColorStack.Pop();
                        }
                        break;
                        }
                    }

                    break;

                        #endregion


                        #region Raw text
                    case XmlNodeType.Text:
                    {
                        foreach (char c in reader.Value)
                        {
                            // Get the tile
                            Tile tile = GlyphTileset.GetTile(c - GlyphOffset);
                            if (tile == null)
                            {
                                continue;
                            }

                            // Move the glyph according to its origin
                            Vector4 tmp = new Vector4(
                                rect.X - tile.Pivot.X * GlyphTileset.Scale.X, rect.Y - tile.Pivot.Y * GlyphTileset.Scale.Y,
                                tile.Rectangle.Width * GlyphTileset.Scale.X, tile.Rectangle.Height * GlyphTileset.Scale.Y);

                            // Out of the bouding box => new line
                            if (tmp.Right >= zone.Right && zone.Size != Vector2.Zero)
                            {
                                tmp.X = zone.X;
                                tmp.Y = tmp.Y + (LineHeight * GlyphTileset.Scale.Y);

                                rect.X     = zone.X;
                                rect.Y    += (LineHeight * GlyphTileset.Scale.Y) + tileoffset + Interline;
                                tileoffset = 0;
                            }

                            // Add glyph to the batch
                            batch.DrawTile(GlyphTileset, c - GlyphOffset, tmp.Xy, currentcolor);


                            // Move to the next glyph
                            rect.X += tmp.Width + Advance;
                            //rect = Vector4.Add(rect, new Vector4(tmp.Width + Advance, 0.0f, 0.0f, 0.0f));
                        }
                    }
                    break;
                        #endregion
                    }
                }
            }
            catch (XmlException ex)
            {
            }

            finally
            {
                // Close streams
                reader.Close();
                stream.Close();
            }
        }