All pastes #271652 Raw Edit

Stuff

public text v1 · immutable
·published 2006-12-08 10:07 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using System.Xml;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using XBMC;
namespace XBMC.GUI
{
    public class GUIWindow
    {
        const string LogFile = "C:\\XBMC-XNA.log.txt";
        int _windowId;
        int _defaultControlId = 0;
        bool _isOverlayAllowed;
        bool _isSkinLoaded;
        bool isWindowVisible;
        List<GUIImage> GUIControlIList = new List<GUIImage>();
        List<GUIButton> GUIControlBList = new List<GUIButton>();
        List<GUILabel> GUIControlLList =new List<GUILabel>();
        private GraphicsDevice GD;
        private SpriteBatch thebuffer;
        private BitmapFont m_font;
        /// <summary>
        /// Return the id of this window
        /// </summary>
        public virtual int GetID
        {
            get { return _windowId; }
            set { _windowId = value; }
        }
        public void init(GraphicsDevice GD, string xmlPath, SpriteBatch thebuffer, BitmapFont m_font)
        {
            this.GD = GD;
            this.thebuffer = thebuffer;
            //MessageBox.Show("New nodes must have a label.");
            Logger(1,"Initalziing Window using XML:" + xmlPath);
            this.m_font = m_font;
            LoadSkin(xmlPath);
            this.isWindowVisible = true;
            
            
        }
        public bool OnWindowLoaded()
        {
            return true;
        }
        public bool LoadSkin(string xmlPathFile)
        {
            _defaultControlId = 0;

            if (!System.IO.File.Exists(xmlPathFile))
            {
                Logger(2,"SKIN: Missing {0}" + xmlPathFile);
                return false;
            }
            // Load the XML file
            XmlDocument doc = new XmlDocument();
            doc.Load(xmlPathFile);
            if (doc.DocumentElement == null) return false;
            try
            {
                if (doc.DocumentElement == null) return false;
                string root = doc.DocumentElement.Name;
                // Check root element
                if (root != "window") return false;
                /*
                XmlNodeList nodeListAnimations = doc.DocumentElement.SelectNodes("/window/animation");
                if (nodeListAnimations != null)
                {
                    foreach (XmlNode nodeAnimation in nodeListAnimations)
                    {
                        //Disabled
                    }
                }*/
                Logger(5, "Woring 1");

                int nodeId;
                if (doc.DocumentElement.HasAttribute("id"))
                    nodeId = (int)System.Int32.Parse(doc.DocumentElement.GetAttribute("id"));   
                else
                    return false;              
                // Set the default control that has the focus after loading the window
                XmlNode nodeDefault = doc.DocumentElement.SelectSingleNode("/window/defaultcontrol");
                if (nodeDefault == null) return false;
  
                // Convert the id of the default control to an int
                try
                {
                    _defaultControlId = System.Int32.Parse(nodeDefault.InnerText);
                }
                catch (Exception)
                {
                    // TODO Add some error when conversion fails message here.
                    Logger(1, "Specifed Window XML has no DefaultControlId");
                }

                // find any XAML complex/compound properties
                foreach (XmlNode node in doc.DocumentElement.SelectNodes("/window/*[contains(name(), '.')]"))
                {
                    string xml = node.OuterXml;

                    if (xml.IndexOf("Button.") != -1)
                        xml = xml.Replace("Button.", "GUIControl.");

                    if (xml.IndexOf("Window.") != -1)
                        xml = xml.Replace("Window.", "GUIWindow.");
                    
                    LoadXML(xml);//, XmlNodeType.Element, this);
                }

                // Configure the overlay settings
                XmlNode nodeOverlay = doc.DocumentElement.SelectSingleNode("/window/allowoverlay");
                if (nodeOverlay != null)
                {
                    if (nodeOverlay.InnerText != null)
                    {
                        string allowed = nodeOverlay.InnerText.ToLower();
                        if (allowed == "yes" || allowed == "true")
                            _isOverlayAllowed = true;
                        if (allowed == "no" || allowed == "false")
                            _isOverlayAllowed = false;
                    }
                }


                XmlNodeList nodeList = doc.DocumentElement.SelectNodes("/window/controls/*");

                foreach (XmlNode node in nodeList)
                {
                    if (node.Name == null)
                        continue;

                    switch (node.Name)
                    {
                        case "control":
                            LoadControl(node);
                            break;
                    }
                }

                // TODO: remove this when all XAML parser or will result in double initialization

                //				PrepareTriggers();

                // initialize the controls
                OnWindowLoaded();
                _isSkinLoaded = true;
                return true;
            }
            catch (Exception ex)
            {
                Logger(5, "4124 to Get Window ID" + ex.Message );
                //Log.Error("exception loading window {0} err:{1}\r\n\r\n{2}\r\n\r\n", xmlPathFile, ex.Message, ex.StackTrace);
                return false;
            }
        }

        public void Logger(int loglevel, string dat)
        {

            System.IO.FileStream fs = new System.IO.FileStream(LogFile, System.IO.FileMode.Append);
            System.IO.StreamWriter objWriter = new System.IO.StreamWriter(fs);
            objWriter.WriteLine(dat);
            objWriter.Close();
            fs.Close();
        }

        protected void LoadControl(XmlNode node)
        {
            /* Notes: node.name should == control no matter what */
            if (node == null ) //Children == null)
                return;
            Logger(1, "XBMC.GUI.GUIWindo.LoadControl()");
            try
            {
                // Contorl must have    type element,  id is optional
                //GUIControlType ctrltype;
                int cid = 0 ;
                string typename ="";
                foreach (XmlAttribute xa in node.Attributes)
                {
                    if (xa.Name == "type")
                    {
                        typename = xa.Value;
                    }
                    if (xa.Name == "id")
                    {
                        cid = Int32.Parse(xa.Value);
                    }                   
                }
                int posx = 0;
                int posy = 0;
                int width = 0;
                int hieght = 0;
                string label = "";
                string texture1 = "";
                string texture2 = "";
                foreach( XmlNode cn in node.ChildNodes )
                {
                    switch (cn.Name)
                    {
                        case "posx":
                            posx = Int32.Parse(cn.InnerText);
                            break;
                        case "posy":
                            posy = Int32.Parse(cn.InnerText);
                            break;
                        case "width":
                            width = Int32.Parse(cn.InnerText);
                            break;
                        case "height":
                            hieght = Int32.Parse(cn.InnerText);
                            break;
                        case "label":
                            label = cn.InnerText;
                            break;
                        case "texture":
                            texture1 = @"D:\TheSVN\XBMC\skin\Project Mayhem III\media\" + cn.InnerText;
                            break;
                        case "texturefocus":
                            texture1 = @"D:\TheSVN\XBMC\skin\Project Mayhem III\media\" + cn.InnerText;
                            break;
                        case "texturenofocus":
                            texture2 = @"D:\TheSVN\XBMC\skin\Project Mayhem III\media\" + cn.InnerText;
                            break;
                    }
                }
                switch (typename)
                {
                    case "image":
                        GUIImage newImage = new GUIImage();
                        //Logger(1, controldoc.SelectSingleNode("/posy").InnerText);
                        newImage.init(thebuffer, GD, posx, posy, width, hieght, texture1, Color.White);
                        GUIControlIList.Add(newImage);
                        break;
                    case "label":
                        GUILabel newLabel = new GUILabel();
                        newLabel.init(posx, posy, width, hieght, label, HorizontalAlignment.Left, m_font);
                        GUIControlLList.Add(newLabel);
                        break;
                    case "button":
                        GUIButton newButton = new GUIButton();
                        newButton.init(thebuffer, GD, cid, posx, posy, width, hieght, label, texture1, texture2, HorizontalAlignment.Left, VerticalAlignment.Top, m_font);
                        GUIControlBList.Add(newButton);
                        break;
                }
                /*GUIImage img = new GUIImage();
                if (img != null)
                {   
                    if (img.Width == 0 || img.Height == 0)
                    {
                        Log.Info("xml:{0} image id:{1} width:{2} height:{3} gfx:{4}",
                          _windowXmlFileName, img.GetID, img.Width, img.Height, img.FileName);
                    }
                }*/

                //Children.Add(newControl);
            }
            catch (Exception ex)
            {
                //Log.Error("Unable to load control. exception:{0}", ex.ToString());
                Logger(1, ex.ToString());
            }
        }


        /*public UIElementCollection Children
        {
            get { if (_children == null) _children = new UIElementCollection(); return _children; }
        }*/
        public void Render()
        {
            if (_isSkinLoaded && isWindowVisible)
            {
                foreach (GUIImage gi in GUIControlIList)
                {
                    gi.Render();
                }
                foreach (GUIButton gb in GUIControlBList)
                {
                    gb.Render();
                }
                foreach (GUILabel gl in GUIControlLList)
                {
                    gl.Render();
                }
            }
        }
        private void LoadXML(string XML)
        {
            /*
            System.IO.FileStream fs = new System.IO.FileStream(LogFile, System.IO.FileMode.Open);
            System.IO.StreamReader objReader = new System.IO.StreamReader(fs);

            string XML = "";
            string line;
            while ((line = objReader.ReadLine()) != null)
            {
                XML += line + "\n";
            }*/

            Logger(1, XML);
            XmlReader objXmlTextReader = new XmlTextReader(XML,XmlNodeType.Document, null);
            
          System.IO.StringWriter objString = new System.IO.StringWriter();
            while (objXmlTextReader.Read())
            {
                Logger(9, objXmlTextReader.Name);
            }
            /*
            If objXmlTextReader.NodeType = XmlNodeType.Element Then
                objXmlTextWriter.WriteStartElement(objXmlTextReader.Name.ToLower)
                For intLoop As Integer = 0 To objXmlTextReader.AttributeCount - 1
                    objXmlTextReader.MoveToAttribute(intLoop)
                    objXmlTextWriter.WriteAttributeString(objXmlTextReader.Name.ToLower, objXmlTextReader.Value)
                Next intLoop
            ElseIf objXmlTextReader.NodeType = XmlNodeType.EndElement Then
                objXmlTextWriter.WriteEndElement()
            ElseIf objXmlTextReader.NodeType = XmlNodeType.Comment Then
                objXmlTextWriter.WriteComment(objXmlTextReader.Value)
            ElseIf objXmlTextReader.NodeType = XmlNodeType.CDATA Then
                objXmlTextWriter.WriteCData(objXmlTextReader.Value)
            ElseIf objXmlTextReader.NodeType = XmlNodeType.Text Then
                objXmlTextWriter.WriteString(EncodeString(objXmlTextReader.Value))
            End If
        End While
        objXmlTextWriter.Close()
        objXmlTextReader.Close()
        Return objString.ToString*/
            objXmlTextReader.Close();
        }

    }

}


// Stuff to CODE
// CONTROL
//   COTROL_BUTTON 
// List of Controls