Пример #1
0
        void LoadFontFile(Stream XMLStream, string FontPath, bool LoadFromContentArchive)
        {
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(XMLStream);
            FileManager.Dispose();

            FontName      = xmlDoc.SelectSingleNode("font/info").Attributes.GetNamedItem("face").Value;
            FontSize      = int.Parse(xmlDoc.SelectSingleNode("font/info").Attributes.GetNamedItem("size").Value);
            NewLineHeight = int.Parse(xmlDoc.SelectSingleNode("font/common").Attributes.GetNamedItem("lineHeight").Value);

            #region Load Bitmap textures
            List <Bitmap> Bitmaps = new List <Bitmap>();

            foreach (XmlNode node in xmlDoc.SelectNodes("font/pages/page"))
            {
                MemoryStream TextureStream;
                try
                {
                    string BitmapPath = node.Attributes.GetNamedItem("file").Value;
                    TextureStream = FileManager.GetFileMemoryStreamFromArchive(FontPath, BitmapPath);

                    if (TextureStream == null)
                    {
                        throw new Exception("Could not find file \"" + FileManager.ContentFolder + FontPath + "/" + BitmapPath + "\"");
                    }
                }
                catch (Exception ex) { throw ex; }

                Bitmaps.Add(new Bitmap(int.Parse(node.Attributes.GetNamedItem("id").Value), Texture2D.FromStream(Graphics.GraphicsDevice, TextureStream)));
                TextureStream.Dispose();
                FileManager.Dispose();
            }
            #endregion

            #region Load characters data
            foreach (XmlNode node in xmlDoc.SelectNodes("font/chars/char"))
            {
                int       ASCII_ID        = int.Parse(node.Attributes.GetNamedItem("id").Value);
                Rectangle SourceRectangle = new Rectangle(int.Parse(node.Attributes.GetNamedItem("x").Value), int.Parse(node.Attributes.GetNamedItem("y").Value),
                                                          int.Parse(node.Attributes.GetNamedItem("width").Value), int.Parse(node.Attributes.GetNamedItem("height").Value));
                Vector2   OffsetPosition = new Vector2(int.Parse(node.Attributes.GetNamedItem("xoffset").Value), int.Parse(node.Attributes.GetNamedItem("yoffset").Value));
                int       Spacing        = int.Parse(node.Attributes.GetNamedItem("xadvance").Value);
                int       TextureID      = int.Parse(node.Attributes.GetNamedItem("page").Value);
                Texture2D BitmapTexture  = null;

                for (int i = 0; i < Bitmaps.Count; i++)
                {
                    if (TextureID == Bitmaps[i].ID)
                    {
                        BitmapTexture = Bitmaps[i].Texture;
                    }
                }

                characterData.Add(new CharacterData(ASCII_ID, BitmapTexture, SourceRectangle, OffsetPosition, Spacing));
            }

            FileManager.Dispose();
            #endregion
        }
Пример #2
0
        void LoadStyleFile(Stream ContentStream, string XmlPath, string ComponentNodeNameInXml, bool LoadFromArchive)
        {
            XmlDocument xmlDoc = new XmlDocument();

            if (LoadFromArchive)
            {
                xmlDoc.Load(ContentStream);
                ContentStream.Dispose();
                FileManager.Dispose();
            }
            else
            {
                xmlDoc.Load(FileManager.ContentFolder + XmlPath);
            }

            if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures") != null)
            {
                if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/NormalTexture") != null)
                {
                    NormalComponentTexture = new ComponentTexture();

                    if (LoadFromArchive)
                    {
                        MemoryStream TextureStream;
                        try
                        {
                            string TexturePath = xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/NormalTexture").Attributes.GetNamedItem("Path").Value;
                            TextureStream = FileManager.GetFileMemoryStreamFromArchive(TexturePath);
                            NormalComponentTexture.OriginalTexture = Texture2D.FromStream(Graphics.GraphicsDevice, TextureStream);

                            if (ContentStream == null)
                            {
                                throw new Exception("Could not find file \"" + FileManager.ContentFolder + FileManager.SourceArchiveFileName + "/" + XmlPath + "\"");
                            }
                            else if (TextureStream == null)
                            {
                                throw new Exception("Could not find file \"" + FileManager.ContentFolder + FileManager.SourceArchiveFileName + "/" + TexturePath + "\"");
                            }
                        }
                        catch (Exception ex) { throw ex; }

                        TextureStream.Dispose();
                        FileManager.Dispose();
                    }
                    else
                    {
                        NormalComponentTexture.OriginalTexture = StreamTexture.LoadTextureFromStream(Graphics, xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/NormalTexture").Attributes.GetNamedItem("Path").Value);
                    }

                    NormalComponentTexture.Color = new Color(int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/NormalTextureColor").Attributes.GetNamedItem("Red").Value),
                                                             int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/NormalTextureColor").Attributes.GetNamedItem("Green").Value),
                                                             int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/NormalTextureColor").Attributes.GetNamedItem("Blue").Value),
                                                             int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/NormalTextureColor").Attributes.GetNamedItem("Alpha").Value));

                    NormalComponentTexture.Initialize(Graphics);
                }

                if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/HoveredTexture") != null)
                {
                    HoveredComponentTexture = new ComponentTexture();

                    if (LoadFromArchive)
                    {
                        MemoryStream TextureStream;
                        try
                        {
                            string TexturePath = xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/HoveredTexture").Attributes.GetNamedItem("Path").Value;
                            TextureStream = FileManager.GetFileMemoryStreamFromArchive(TexturePath);
                            HoveredComponentTexture.OriginalTexture = Texture2D.FromStream(Graphics.GraphicsDevice, TextureStream);

                            if (TextureStream == null)
                            {
                                throw new Exception("Could not find file \"" + FileManager.ContentFolder + FileManager.SourceArchiveFileName + "/" + TexturePath + "\"");
                            }
                        }
                        catch (Exception ex) { throw ex; }

                        TextureStream.Dispose();
                        FileManager.Dispose();
                    }
                    else
                    {
                        HoveredComponentTexture.OriginalTexture = StreamTexture.LoadTextureFromStream(Graphics, xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/HoveredTexture").Attributes.GetNamedItem("Path").Value);
                    }

                    HoveredComponentTexture.Color = new Color(int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/HoveredTextureColor").Attributes.GetNamedItem("Red").Value),
                                                              int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/HoveredTextureColor").Attributes.GetNamedItem("Green").Value),
                                                              int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/HoveredTextureColor").Attributes.GetNamedItem("Blue").Value),
                                                              int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/HoveredTextureColor").Attributes.GetNamedItem("Alpha").Value));

                    HoveredComponentTexture.Initialize(Graphics);
                }

                if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/PressedTexture") != null)
                {
                    PressedComponentTexture = new ComponentTexture();

                    if (LoadFromArchive)
                    {
                        MemoryStream TextureStream;
                        try
                        {
                            string TexturePath = xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/PressedTexture").Attributes.GetNamedItem("Path").Value;
                            TextureStream = FileManager.GetFileMemoryStreamFromArchive(TexturePath);
                            PressedComponentTexture.OriginalTexture = Texture2D.FromStream(Graphics.GraphicsDevice, TextureStream);

                            if (TextureStream == null)
                            {
                                throw new Exception("Could not find file \"" + FileManager.ContentFolder + FileManager.SourceArchiveFileName + "/" + TexturePath + "\"");
                            }
                        }
                        catch (Exception ex) { throw ex; }

                        TextureStream.Dispose();
                        FileManager.Dispose();
                    }
                    else
                    {
                        PressedComponentTexture.OriginalTexture = StreamTexture.LoadTextureFromStream(Graphics, xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/PressedTexture").Attributes.GetNamedItem("Path").Value);
                    }

                    PressedComponentTexture.Color = new Color(int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/PressedTextureColor").Attributes.GetNamedItem("Red").Value),
                                                              int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/PressedTextureColor").Attributes.GetNamedItem("Green").Value),
                                                              int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/PressedTextureColor").Attributes.GetNamedItem("Blue").Value),
                                                              int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Textures/PressedTextureColor").Attributes.GetNamedItem("Alpha").Value));

                    PressedComponentTexture.Initialize(Graphics);
                }
            }

            if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Rendering") != null)
            {
                if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Rendering").Attributes.GetNamedItem("Mode").Value == "None Scalable")
                {
                    if (NormalComponentTexture != null)
                    {
                        NormalComponentTexture.RendererMode = ComponentTexture.RenderMode.NoneScalable;
                    }

                    if (HoveredComponentTexture != null)
                    {
                        HoveredComponentTexture.RendererMode = ComponentTexture.RenderMode.NoneScalable;
                    }

                    if (PressedComponentTexture != null)
                    {
                        PressedComponentTexture.RendererMode = ComponentTexture.RenderMode.NoneScalable;
                    }
                }
                else if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Rendering").Attributes.GetNamedItem("Mode").Value == "Scalable")
                {
                    if (NormalComponentTexture != null)
                    {
                        NormalComponentTexture.RendererMode = ComponentTexture.RenderMode.Scalable;
                    }

                    if (HoveredComponentTexture != null)
                    {
                        HoveredComponentTexture.RendererMode = ComponentTexture.RenderMode.Scalable;
                    }

                    if (PressedComponentTexture != null)
                    {
                        PressedComponentTexture.RendererMode = ComponentTexture.RenderMode.Scalable;
                    }
                }
                else if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Rendering").Attributes.GetNamedItem("Mode").Value == "Scalable RealTime")
                {
                    if (NormalComponentTexture != null)
                    {
                        NormalComponentTexture.RendererMode = ComponentTexture.RenderMode.ScalableRealTime;
                    }

                    if (HoveredComponentTexture != null)
                    {
                        HoveredComponentTexture.RendererMode = ComponentTexture.RenderMode.ScalableRealTime;
                    }

                    if (PressedComponentTexture != null)
                    {
                        PressedComponentTexture.RendererMode = ComponentTexture.RenderMode.ScalableRealTime;
                    }
                }
            }

            if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions") != null)
            {
                borderThickness = new BorderThickness(int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions/BorderThickness").Attributes.GetNamedItem("Top").Value),
                                                      int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions/BorderThickness").Attributes.GetNamedItem("Right").Value),
                                                      int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions/BorderThickness").Attributes.GetNamedItem("Bottom").Value),
                                                      int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions/BorderThickness").Attributes.GetNamedItem("Left").Value));

                CornerSize = new Vector2(int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions/CornerSize").Attributes.GetNamedItem("X").Value),
                                         int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions/CornerSize").Attributes.GetNamedItem("Y").Value));


                if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions/DefaultSize") != null)
                {
                    if (Size.X <= 0 || Size.Y <= 0)
                    {
                        DefaultSize = new Vector2(int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions/DefaultSize").Attributes.GetNamedItem("X").Value),
                                                  int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Dimensions/DefaultSize").Attributes.GetNamedItem("Y").Value));
                    }
                }
            }

            if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text") != null)
            {
                string alignment = "";

                // if (LoadFromArchive)
                //fontRenderer.LoadContentFromArchive(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/Font").Attributes.GetNamedItem("Path").Value);
                //else
                fontRenderer.LoadContent(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/Font").Attributes.GetNamedItem("Path").Value);

                if (xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/DefaultText") != null)
                {
                    if (String.IsNullOrEmpty(Text))
                    {
                        Text = xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/DefaultText").Attributes.GetNamedItem("Value").Value;
                    }
                }

                if (TextAlignment == Alignment.Default)
                {
                    alignment = xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/DefaultAlignment").Attributes.GetNamedItem("Value").Value;

                    if (alignment.ToLowerInvariant() == "left")
                    {
                        TextAlignment = Alignment.Left;
                    }

                    if (alignment.ToLowerInvariant() == "center")
                    {
                        TextAlignment = Alignment.Center;
                    }

                    if (alignment.ToLowerInvariant() == "right")
                    {
                        TextAlignment = Alignment.Right;
                    }
                }

                TextOffsetPosition = new Vector2(int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/OffsetPosition").Attributes.GetNamedItem("X").Value),
                                                 int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/OffsetPosition").Attributes.GetNamedItem("Y").Value));

                if (TextColor == null)
                {
                    TextColor = new Color(int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/DefaultTextColor").Attributes.GetNamedItem("Red").Value),
                                          int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/DefaultTextColor").Attributes.GetNamedItem("Green").Value),
                                          int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/DefaultTextColor").Attributes.GetNamedItem("Blue").Value),
                                          int.Parse(xmlDoc.SelectSingleNode(ComponentNodeNameInXml + "/Text/DefaultTextColor").Attributes.GetNamedItem("Alpha").Value));
                }
            }
        }