Example #1
0
        /// <summary> Cuts a texture from the main atlas based on given parameters. </summary>
        /// <param name="elementStyle"> The style of the element to read. </param>
        /// <param name="graphicsDevice"> The graphics device to create the texture on. </param>
        /// <returns></returns>
        private Texture2D calculateBackground(ElementStyle elementStyle, GraphicsDevice graphicsDevice)
        {
            //A new texture to be returned
            Texture2D backgroundTexture;

            //Information about the style parameters
            bool containsBackgroundNode  = elementStyle.Parameters.ContainsKey("Background");
            bool containsImageParameter  = containsBackgroundNode ? elementStyle.Parameters["Background"].Parameters.ContainsKey("Image") : false;
            bool containsColourParameter = containsBackgroundNode ? elementStyle.Parameters["Background"].Parameters.ContainsKey("Colour") : false;

            //If there's no background style node or no image name parameter, return a default background
            if (!containsBackgroundNode || (containsBackgroundNode && !containsImageParameter))
            {
                //Create a 1x1 texture
                backgroundTexture = new Texture2D(graphicsDevice, 1, 1);

                //Since we know there's no image parameter, check if there's a colour parameter and use that to colour the texture instead of the default colour
                Color backgroundColour = containsColourParameter ? elementStyle.Parameters["Background"].Parameters["Colour"].ParseColour() : Color.Honeydew;

                //Set the texture to the colour and return it
                backgroundTexture.SetData(new Color[1] {
                    backgroundColour
                });
                return(backgroundTexture);
            }

            //The Background node of the style
            StyleParameter backgroundParameter = elementStyle.Parameters["Background"];

            //If the texture doesn't exist within the list, throw an error
            if (!textures.ContainsKey(backgroundParameter.Parameters["Image"]))
            {
                throw new Exception("Image parameter does not match texture defined in Resources node.");
            }

            //The main image the background will be cut from
            Texture2D backgroundAtlas = textures[backgroundParameter.Parameters["Image"]];

            //If a source is given, return that cut from the atlas, otherwise return the atlas itself
            if (backgroundParameter.Parameters.ContainsKey("Source"))
            {
                return(backgroundAtlas.GetTexture(backgroundParameter.Parameters["Source"].ParseRectangle()));
            }
            else
            {
                return(backgroundAtlas);
            }
        }
Example #2
0
        /// <summary> Calculates the corners and edges of the border and fills them into a 3x3 array. </summary>
        /// <param name="elementStyle"> The style of the element. </param>
        /// <param name="graphicsDevice"> The graphics device to load onto. </param>
        /// <returns> A 3x3 Texture2D array with the edges and corners filled in. </returns>
        private Texture2D[,] calculateBorder(ElementStyle elementStyle, GraphicsDevice graphicsDevice)
        {
            //A new texture to be returned
            Texture2D borderAtlas;

            //Information about the style parameters
            bool containsBorderNode      = elementStyle.Parameters.ContainsKey("Border");
            bool containsImageParameter  = containsBorderNode ? elementStyle.Parameters["Border"].Parameters.ContainsKey("Image") : false;
            bool containsColourParameter = containsBorderNode ? elementStyle.Parameters["Border"].Parameters.ContainsKey("Colour") : false;

            //If there's no border style node or no image name parameter, return a default background
            if (!containsBorderNode || (containsBorderNode && !containsImageParameter))
            {
                //Create a 1x1 texture
                borderAtlas = new Texture2D(graphicsDevice, 1, 1);

                //Since we know there's no image parameter, check if there's a colour parameter and use that to colour the texture instead of the default colour
                Color borderColour = containsColourParameter ? elementStyle.Parameters["Border"].Parameters["Colour"].ParseColour() : Color.Black;

                //Set the texture to the colour
                borderAtlas.SetData(new Color[1] {
                    borderColour
                });

                //Use this texture for the edges of an array and return it
                return(new Texture2D[3, 3]
                {
                    { borderAtlas, borderAtlas, borderAtlas },
                    { borderAtlas, null, borderAtlas },
                    { borderAtlas, borderAtlas, borderAtlas }
                });
            }

            //The border node of the style
            StyleParameter borderParameter = elementStyle.Parameters["Border"];

            //If the texture doesn't exist within the list, throw an error
            if (!textures.ContainsKey(borderParameter.Parameters["Image"]))
            {
                throw new Exception("Image parameter does not match texture defined in Resources node.");
            }

            //The atlas that the edges and corners are cut from
            borderAtlas = textures[borderParameter.Parameters["Image"]];

            //Initialises the array
            Texture2D[,] borderArray = new Texture2D[3, 3];

            //Local function that cuts from the given atlas using the given string rectangle
            Texture2D calculateBorderPiece(string source, Texture2D atlas) => atlas.GetTexture(source.ParseRectangle());

            //Given a corner source, all corners will use this unless specified otherwise. Defaults to the atlas
            Texture2D defaultCorner = (borderParameter.Parameters.ContainsKey("Corner")) ? calculateBorderPiece(borderParameter.Parameters["Corner"], borderAtlas) : borderAtlas;

            //Sets the corners
            borderArray[0, 0] = (borderParameter.Parameters.ContainsKey("TopLeft-Corner")) ? calculateBorderPiece(borderParameter.Parameters["TopLeft-Corner"], borderAtlas) : defaultCorner;
            borderArray[2, 0] = (borderParameter.Parameters.ContainsKey("TopRight-Corner")) ? calculateBorderPiece(borderParameter.Parameters["TopRight-Corner"], borderAtlas) : defaultCorner.Rotate(1);
            borderArray[0, 2] = (borderParameter.Parameters.ContainsKey("BottomLeft-Corner")) ? calculateBorderPiece(borderParameter.Parameters["BottomLeft-Corner"], borderAtlas) : defaultCorner.Rotate(3);
            borderArray[2, 2] = (borderParameter.Parameters.ContainsKey("BottomRight-Corner")) ? calculateBorderPiece(borderParameter.Parameters["BottomRight-Corner"], borderAtlas) : defaultCorner.Rotate(2);

            //Given an edge source, all edges will use this unless specified otherwise. Defaults to the atlas
            Texture2D defaultEdge = (borderParameter.Parameters.ContainsKey("Edge")) ? calculateBorderPiece(borderParameter.Parameters["Edge"], borderAtlas) : borderAtlas;

            //Sets the edges
            borderArray[1, 0] = (borderParameter.Parameters.ContainsKey("Top-Edge")) ? calculateBorderPiece(borderParameter.Parameters["Top-Edge"], borderAtlas) : defaultEdge;
            borderArray[0, 1] = (borderParameter.Parameters.ContainsKey("Left-Edge")) ? calculateBorderPiece(borderParameter.Parameters["Left-Edge"], borderAtlas) : defaultEdge.Rotate(3);
            borderArray[1, 2] = (borderParameter.Parameters.ContainsKey("Bottom-Edge")) ? calculateBorderPiece(borderParameter.Parameters["Bottom-Edge"], borderAtlas) : defaultEdge.Rotate(2);
            borderArray[2, 1] = (borderParameter.Parameters.ContainsKey("Right-Edge")) ? calculateBorderPiece(borderParameter.Parameters["Right-Edge"], borderAtlas) : defaultEdge.Rotate(1);

            //Returns the final array
            return(borderArray);
        }
Example #3
0
        /// <summary> Goes through the main style node and loads it into a list of data. </summary>
        /// <param name="styleNode"> The style node. </param>
        private void loadStyles(XmlNode styleNode)
        {
            //Checks that the style node exists and has children
            if (styleNode == null)
            {
                throw new Exception("Missing Style node.");
            }
            if (!styleNode.HasChildNodes)
            {
                throw new Exception("Style node has no child nodes.");
            }

            //Initialises the style list
            styles = new List <ElementStyle>(styleNode.ChildNodes.Count);

            //Goes through each element style node and adds it to the styles
            for (int s = 0; s < styleNode.ChildNodes.Count; s++)
            {
                //This style's node
                XmlNode elementStyleNode = styleNode.ChildNodes[s];

                //Creates a new ElementStyle to store this style
                ElementStyle style = new ElementStyle();

                //Sets the style's type, as long as the type is within the Element namespace
                if (typeof(Element).Assembly.GetType(typeof(Element).Namespace + "." + elementStyleNode.Name) != null)
                {
                    style.Type = typeof(Element).Assembly.GetType(typeof(Element).Namespace + "." + elementStyleNode.Name);
                }
                else
                {
                    throw new Exception("Invalid style type, style node's name must match element name.");
                }

                //Sets the style's name if supplied, otherwise defaults to string.Empty
                style.Name = (elementStyleNode.Attributes.GetNamedItem("Name") == null) ? string.Empty : elementStyleNode.Attributes.GetNamedItem("Name").Value;

                //Initialises the style's parameters array
                style.Parameters = new Dictionary <string, StyleParameter>(elementStyleNode.ChildNodes.Count);

                //Goes through each node within the element style node
                foreach (XmlNode styleParameter in elementStyleNode.ChildNodes)
                {
                    //Creates a StyleParameter with the given name
                    StyleParameter parameter = new StyleParameter()
                    {
                        Name       = styleParameter.Name,
                        Parameters = new Dictionary <string, string>(styleParameter.Attributes.Count)
                    };

                    //Goes through each attribute of the parameter and adds it to the styleParameter's dictionary
                    foreach (XmlAttribute attribute in styleParameter.Attributes)
                    {
                        parameter.Parameters[attribute.Name] = attribute.Value;
                    }

                    //Adds the parameter to the style
                    style.Parameters[styleParameter.Name] = parameter;
                }

                //Adds the style to the list
                styles.Add(style);
            }
        }