예제 #1
0
        private UILayer CreateLayer(XElement node, Game game, Dictionary <string, string> texts, Dictionary <string, Action <object, EventArgs> > callbacks)
        {
            var layer = new UILayer(game);

            if (node.Name != "UILayer")
            {
                throw new Exception("XML: UILayer tag");
            }

            if (node.HasAttributes)
            {
                foreach (var attr in node.Attributes())
                {
                    var   name  = attr.Name.LocalName;
                    var   value = attr.Value;
                    Color color = Color.Transparent;
                    switch (name)
                    {
                    case "x":
                        if (value.IndexOf("%") != -1)
                        {
                            var percentX = int.Parse(value.Trim(new char[] { '%', ' ' }));
                            layer.X = percentX * game.GraphicsDevice.Viewport.Width / 100;
                        }
                        else
                        {
                            var x = int.Parse(value);
                            layer.X = x;
                        }
                        break;

                    case "y":
                        if (value.IndexOf("%") != -1)
                        {
                            var percentY = int.Parse(value.Trim(new char[] { '%', ' ' }));
                            layer.Y = percentY * game.GraphicsDevice.Viewport.Height / 100;
                        }
                        else
                        {
                            var y = int.Parse(value);
                            layer.Y = y;
                        }
                        break;

                    case "width":
                        if (value.IndexOf("%") != -1)
                        {
                            var percentWidth = int.Parse(value.Trim(new char[] { '%', ' ' }));
                            layer.Width = percentWidth * game.GraphicsDevice.Viewport.Width / 100;
                        }
                        else
                        {
                            var width = int.Parse(value);
                            layer.Width = width;
                        }
                        break;

                    case "height":
                        if (value.IndexOf("%") != -1)
                        {
                            var percentHeight = int.Parse(value.Trim(new char[] { '%', ' ' }));
                            layer.Height = percentHeight * game.GraphicsDevice.Viewport.Height / 100;
                        }
                        else
                        {
                            var height = int.Parse(value);
                            layer.Height = height;
                        }
                        break;

                    case "colorR":
                        var colorR = byte.Parse(value);
                        color   = layer.BackgroundColor;
                        color.R = colorR;
                        layer.BackgroundColor = color;
                        break;

                    case "colorG":
                        var colorG = byte.Parse(value);
                        color   = layer.BackgroundColor;
                        color.G = colorG;
                        layer.BackgroundColor = color;
                        break;

                    case "colorB":
                        var colorB = byte.Parse(value);
                        color   = layer.BackgroundColor;
                        color.B = colorB;
                        layer.BackgroundColor = color;
                        break;

                    case "colorA":
                        var colorA = byte.Parse(value);
                        color   = layer.BackgroundColor;
                        color.A = colorA;
                        layer.BackgroundColor = color;
                        break;
                    }
                }
            }

            var children = node.Elements();

            foreach (var childNode in children)
            {
                var child = CreateControl(childNode, game, texts, callbacks);

                if (child != null)
                {
                    layer.AddControl(child);
                }
            }

            return(layer);
        }