예제 #1
0
        public StyledQuad(string name, IGuiStyle style)
        {
            mDisplayObject = new GameObject(name);
            mDisplayObject.AddComponent <MeshFilter>();
            Renderer renderer = mDisplayObject.AddComponent <MeshRenderer>();

            renderer.material = new Material(Shader.Find("GUI/Flat Color"));

            Texture2D texture       = style.Normal.Background;
            GuiMargin ninePartScale = style.NinePartScale;

            float left  = ninePartScale.Left / texture.width;
            float right = 1.0f - (ninePartScale.Right / texture.width);

            float top    = ninePartScale.Top / texture.height;
            float bottom = 1.0f - (ninePartScale.Bottom / texture.height);

            float scalar = 1.0f / (float)(texture.width > texture.height ? texture.width : texture.height);

            mNinePartScale = ninePartScale * scalar;
            mSize          = new Vector2(texture.width, texture.height) * scalar;

            mUv    = new Vector2[16];
            mUv[0] = new Vector2(0.0f, 0.0f);
            mUv[1] = new Vector2(left, 0.0f);
            mUv[2] = new Vector2(right, 0.0f);
            mUv[3] = new Vector2(1.0f, 0.0f);

            mUv[4] = new Vector2(0.0f, top);
            mUv[5] = new Vector2(left, top);
            mUv[6] = new Vector2(right, top);
            mUv[7] = new Vector2(1.0f, top);

            mUv[8]  = new Vector2(0.0f, bottom);
            mUv[9]  = new Vector2(left, bottom);
            mUv[10] = new Vector2(right, bottom);
            mUv[11] = new Vector2(1.0f, bottom);

            mUv[12] = new Vector2(0.0f, 1.0f);
            mUv[13] = new Vector2(left, 1.0f);
            mUv[14] = new Vector2(right, 1.0f);
            mUv[15] = new Vector2(1.0f, 1.0f);

            renderer.material.mainTexture = texture;

            Resize();
        }
예제 #2
0
        private IGuiStyle BuildStyle(string name, XmlNode node, IGuiStyle basedOn)
        {
            GuiStyle result = null;

            if (basedOn == null)
            {
                result = new GuiStyle(name);
            }
            else
            {
                result = new GuiStyle(basedOn, name);
            }

            // DefaultFor
            if (node.Attributes["defaultFor"] != null)
            {
                string[] defaultTypes = node.Attributes["defaultFor"].InnerText.Split(new char[1] {
                    ' '
                }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string defaultType in defaultTypes)
                {
                    System.Type t = System.Type.GetType("Hangout.Client.Gui." + defaultType, true);
                    result.SetAsDefaultFor(t);
                }
            }

            // Normal, Hover, Disbaled and Active
            foreach (XmlNode styleStateNode in node.SelectNodes("StyleState"))
            {
                string styleStateName = BuildName(styleStateNode);

                switch (styleStateName)
                {
                case "NormalState":
                    if (basedOn == null)
                    {
                        result.Normal = BuildStyleState(styleStateNode, null);
                    }
                    else
                    {
                        result.Normal = BuildStyleState(styleStateNode, basedOn.Normal);
                    }
                    break;

                case "HoverState":
                    if (basedOn == null)
                    {
                        result.Hover = BuildStyleState(styleStateNode, null);
                    }
                    else
                    {
                        result.Hover = BuildStyleState(styleStateNode, basedOn.Normal);
                    }
                    break;

                case "DisabledState":
                    if (basedOn == null)
                    {
                        result.Disabled = BuildStyleState(styleStateNode, null);
                    }
                    else
                    {
                        result.Disabled = BuildStyleState(styleStateNode, basedOn.Normal);
                    }
                    break;

                case "ActiveState":
                    if (basedOn == null)
                    {
                        result.Active = BuildStyleState(styleStateNode, null);
                    }
                    else
                    {
                        result.Active = BuildStyleState(styleStateNode, basedOn.Normal);
                    }
                    break;

                default:
                    throw new GuiConstructionException("Unknown style state found in style (" + name + "): " + styleStateName);
                }
            }

            // InternalMargins, ExternalMargins, NinePartScale
            foreach (XmlNode marginNode in node.SelectNodes("Margin"))
            {
                string    marginName = BuildName(marginNode);
                GuiMargin newMargins = BuildMargin(marginNode);
                switch (marginName)
                {
                case "InternalMargins":
                    result.InternalMargins = newMargins;
                    break;

                case "ExternalMargins":
                    result.ExternalMargins = newMargins;
                    break;

                case "NinePartScale":
                    result.NinePartScale = newMargins;
                    break;

                default:
                    throw new GuiConstructionException("Unknown margin found in style (" + name + "): " + marginName);
                }
            }

            // DefaultFont
            if (node.SelectSingleNode("Font") != null)
            {
                result.DefaultFont = BuildFont(node.SelectSingleNode("Font"), "path");
            }

            // DefaultTextAnchor
            if (node.SelectSingleNode("Anchor") != null)
            {
                result.DefaultTextAnchor = BuildAnchor(node.SelectSingleNode("Anchor"), "value");
            }

            // WordWrap
            try
            {
                XmlNode wordWrapNode = node.SelectSingleNode("WordWrap");
                if (wordWrapNode != null)
                {
                    XmlAttribute valueAttrib = wordWrapNode.Attributes["value"];
                    if (valueAttrib != null)
                    {
                        result.WordWrap = bool.Parse(valueAttrib.InnerText);
                    }
                }
            }
            catch (FormatException e)
            {
                throw new GuiConstructionException("Unable to parse the 'value' attribute on " + name + "'s WordWrap node.", e);
            }

            // ClipText
            try
            {
                XmlNode clipTextNode = node.SelectSingleNode("ClipText");
                if (clipTextNode != null)
                {
                    XmlAttribute valueAttrib = clipTextNode.Attributes["value"];
                    if (valueAttrib != null)
                    {
                        result.ClipText = bool.Parse(valueAttrib.InnerText);
                    }
                }
            }
            catch (FormatException e)
            {
                throw new GuiConstructionException("Unable to parse the 'value' attribute on " + name + "'s ClipText node.", e);
            }

            return(result);
        }