예제 #1
0
        public void ToggleTextMeshPro(bool on)
        {
            if (on)
            {
                // if we're already using TextMeshPro; do nothing
                if (TextComponentWrapper != null &&
                    TextComponentWrapper.xmlElement != null &&
                    TextComponentWrapper.xmlElement.tagType == "TextMeshPro")
                {
                    return;
                }

                var        tmp           = gameObject.GetComponentInChildren <TMPro.TextMeshProUGUI>(true);
                XmlElement tmpXmlElement = null;

                if (tmp == null)
                {
                    // create instance
                    var tagHandler = XmlLayoutUtilities.GetXmlTagHandler("TextMeshPro");
                    tmpXmlElement = tagHandler.GetInstance(this.rectTransform, xmlLayout);
                    tagHandler.SetInstance(tmpXmlElement);
                    tagHandler.ApplyAttributes(new AttributeDictionary());

                    tmp = tmpXmlElement.GetComponent <TMPro.TextMeshProUGUI>();

                    tmp.rectTransform.localScale = Vector3.one;
                }

                tmpXmlElement = tmp.GetComponent <XmlElement>();

                TextComponentWrapper = new TextComponentWrapper(tmp);

                // hide the regular text component
                TextComponent.gameObject.SetActive(false);

                // enable the TMP object if it wasn't already
                tmp.gameObject.SetActive(true);
            }
            else
            {
                if (TextComponentWrapper != null &&
                    TextComponentWrapper.xmlElement != null &&
                    TextComponentWrapper.xmlElement.tagType == "Text")
                {
                    return;
                }

                TextComponentWrapper = new TextComponentWrapper(TextComponent);

                TextComponent.gameObject.SetActive(true);

                var tmp = gameObject.GetComponentInChildren <TMPro.TextMeshProUGUI>();
                if (tmp != null)
                {
                    tmp.gameObject.SetActive(false);
                }
            }
        }
예제 #2
0
        private void InitialiseXmlElement()
        {
            if (m_XmlElement == null)
            {
                m_XmlElement = this.GetComponent <XmlElement>();
            }

            if (m_XmlElement == null)
            {
                m_XmlElement = this.gameObject.AddComponent <XmlElement>();
                m_XmlElement.Initialise(this, this.transform as RectTransform, XmlLayoutUtilities.GetXmlTagHandler("XmlLayout"));
            }
        }
예제 #3
0
        void LoadDefaults(XmlNode node)
        {
            if (node.HasChildNodes)
            {
                foreach (XmlNode childNode in node.ChildNodes)
                {
                    if (childNode.NodeType == XmlNodeType.Text || childNode.NodeType == XmlNodeType.Comment)
                    {
                        continue;
                    }

                    var type = childNode.Name.ToLower();

                    if (type == "tooltip")
                    {
                        HandleDefaultTooltipNode(childNode);
                        continue;
                    }

                    if (XmlLayoutUtilities.GetXmlTagHandler(type) == null)
                    {
                        continue;
                    }

                    var attributes = childNode.Attributes.ToAttributeDictionary();

                    var classes = attributes.ContainsKey("class") ? attributes["class"].Split(',', ' ').Select(s => s.Trim().ToLower()).ToList() : new List <string>()
                    {
                        "all"
                    };

                    foreach (var _class in classes)
                    {
                        if (!defaultAttributeValues.ContainsKey(type))
                        {
                            defaultAttributeValues.Add(type, new ClassAttributeCollectionDictionary());
                        }

                        if (!defaultAttributeValues[type].ContainsKey(_class))
                        {
                            defaultAttributeValues[type].Add(_class, new AttributeDictionary());
                        }

                        defaultAttributeValues[type][_class] = XmlLayoutUtilities.MergeAttributes(defaultAttributeValues[type][_class], attributes);
                        defaultAttributeValues[type][_class].Remove("class");
                    }
                }
            }
        }
예제 #4
0
        public void ProcessCustomTags()
        {
            var tags        = XmlLayoutUtilities.GetXmlTagHandlerNames();
            var groupedTags = new Dictionary <string, Dictionary <string, ElementTagHandler> >();

            foreach (var tag in tags)
            {
                var tagHandler = XmlLayoutUtilities.GetXmlTagHandler(tag);

                if (!tagHandler.isCustomElement)
                {
                    continue;
                }

                if (!groupedTags.ContainsKey(tagHandler.elementGroup))
                {
                    groupedTags.Add(tagHandler.elementGroup, new Dictionary <string, ElementTagHandler>());
                }

                groupedTags[tagHandler.elementGroup].Add(tag, tagHandler);
            }

            foreach (var group in groupedTags)
            {
                var existingTags = GetElementGroup(group.Key);

                foreach (var tag in group.Value)
                {
                    if (!existingTags.Contains(tag.Key, StringComparer.OrdinalIgnoreCase))
                    {
                        if (!customElementTagHandlersToAdd.ContainsKey(group.Key))
                        {
                            customElementTagHandlersToAdd.Add(group.Key, new Dictionary <string, ElementTagHandler>());
                        }

                        customElementTagHandlersToAdd[group.Key].Add(tag.Key, tag.Value);
                    }
                }
            }
        }
예제 #5
0
        IEnumerator Preload_Internal()
        {
            var tagHandlerNames = XmlLayoutUtilities.GetXmlTagHandlerNames();

            var customAttributeNames = XmlLayoutUtilities.GetCustomAttributeNames();

            foreach (var tagHandlerName in tagHandlerNames)
            {
                // instantiate the tag handler (which will create an instance of it we can use later)
                var tagHandler = XmlLayoutUtilities.GetXmlTagHandler(tagHandlerName);

                // load the prefab (this will cache it)
                XmlLayoutUtilities.LoadResource <GameObject>(tagHandler.prefabPath);
            }

            foreach (var customAttributeName in customAttributeNames)
            {
                // Load the custom attribute (which will create an instance of it we can use later)
                XmlLayoutUtilities.GetCustomAttribute(customAttributeName);
            }

            yield return(null);
        }
예제 #6
0
        void Preload_Internal()
        {
            var tagHandlerNames = XmlLayoutUtilities.GetXmlTagHandlerNames();

            var customAttributeNames = XmlLayoutUtilities.GetCustomAttributeNames();

            foreach (var tagHandlerName in tagHandlerNames)
            {
                // instantiate the tag handler (which will create an instance of it we can use later)
                var tagHandler = XmlLayoutUtilities.GetXmlTagHandler(tagHandlerName);

                // load the prefab (this will cache it)
                XmlLayoutUtilities.LoadResource <GameObject>(tagHandler.prefabPath);
            }

            foreach (var customAttributeName in customAttributeNames)
            {
                // Load the custom attribute (which will create an instance of it we can use later)
                XmlLayoutUtilities.GetCustomAttribute(customAttributeName);
            }

            /*foreach (var entry in XmlLayoutResourceDatabase.instance.entries)
             * {
             *  XmlLayoutResourceDatabase.instance.GetResource<UnityEngine.Object>(entry.path);
             * }
             *
             * var resourceDatabases = XmlLayoutResourceDatabase.instance.customResourceDatabases;
             *
             * foreach (var resourceDatabase in resourceDatabases)
             * {
             *  foreach(var entry in resourceDatabase.entries)
             *  {
             *      XmlLayoutResourceDatabase.instance.GetResource<UnityEngine.Object>(entry.path);
             *  }
             * }*/
        }
예제 #7
0
        private void Start()
        {
            if (started)
            {
                return;
            }

            TextComponentWrapper = new TextComponentWrapper(TextComponent);
            TextComponentWrapper.xmlElement.Initialise(xmlLayout, TextComponentWrapper.xmlElement.rectTransform, XmlLayoutUtilities.GetXmlTagHandler("Text"));

            contentSizeFitter = this.GetComponent <ContentSizeFitter>();

            started = true;
        }
예제 #8
0
        internal void ParseNode(XmlNode xmlNode, RectTransform parent, RectTransform element = null, bool parseChildren = true, XmlElement parentXmlElement = null)
        {
            if (xmlNode.NodeType == XmlNodeType.Text || xmlNode.NodeType == XmlNodeType.Comment)
            {
                return;
            }

            var type = xmlNode.Name.ToLower();

            if (type == "include")
            {
                //LoadIncludeFile(xmlNode);
                LoadInlineIncludeFile(xmlNode, parent);
                return;
            }

            if (type == "defaults")
            {
                LoadDefaults(xmlNode);
                return;
            }

            var attributes = xmlNode.Attributes.ToAttributeDictionary();

            var tagHandler = XmlLayoutUtilities.GetXmlTagHandler(type);

            if (tagHandler == null)
            {
                return;
            }

            tagHandler.SetInstance(element, this);

            XmlElement xmlElement = null;

            if (element == null)
            {
                xmlElement = tagHandler.GetInstance(parent, this, attributes.GetValue("prefabPath"));

                if (parentXmlElement != null)
                {
                    parentXmlElement.AddChildElement(xmlElement, false);
                }
            }

            var tagTransform = element ?? xmlElement.rectTransform;

            tagHandler.SetInstance(tagTransform, this);
            if (xmlElement != null)
            {
                xmlElement.attributes = attributes;
#if !ENABLE_IL2CPP
                xmlElement.DataSource = xmlElement.GetAttribute("vm-dataSource");
#endif
            }
            tagHandler.Open(attributes);

            // if the tag handler successfully parses the child nodes, then don't attempt to parse them here
            // (this is only used for specific elements, e.g. Dropdown)
            if (tagHandler.ParseChildElements(xmlNode))
            {
                parseChildren = false;
            }

            if (parseChildren && xmlNode.HasChildNodes)
            {
                foreach (XmlNode childNode in xmlNode.ChildNodes)
                {
                    if (childNode.NodeType == XmlNodeType.Text || childNode.NodeType == XmlNodeType.CDATA)
                    {
                        if (!attributes.ContainsKey("text"))
                        {
                            attributes.Add("text", "");
                        }
                        var text = childNode.NodeType == XmlNodeType.Text ? childNode.ParentNode.InnerText.Trim() : childNode.InnerText.Trim();

                        // Strip out any instances of multiple spaces (as in HTML)
                        while (text.Contains("  "))
                        {
                            text = text.Replace("  ", " ");
                        }

                        while (text.Contains("\r\n "))
                        {
                            text = text.Replace("\r\n ", "\r\n");
                        }

                        attributes["text"] = text;
                    }
                    else
                    {
                        tagHandler.SetInstance(tagTransform, this);
                        ParseNode(childNode, tagHandler.transformToAddChildrenTo, null, true, parentXmlElement);
                    }
                }
            }

            tagHandler.SetInstance(tagTransform, this);

            if (attributes.ContainsKey("id"))
            {
                if (ElementsById.ContainsKey(attributes["id"]))
                {
                    Debug.LogError("[XmlLayout] Ignoring duplicate id value '" + attributes["id"] + ". Id values must be unique.");
                }
                else
                {
                    if (xmlElement != null)
                    {
                        ElementsById.Add(attributes["id"], xmlElement);
                    }
                }
            }

            // Preserve which elements have been set manually (and aren't derived from a class)
            if (xmlElement != null)
            {
                xmlElement.elementAttributes = attributes.Keys.ToList();
            }

            if (defaultAttributeValues.ContainsKey(type))
            {
                var defaultAttributesMerged = new AttributeDictionary();

                if (defaultAttributeValues[type].ContainsKey("all"))
                {
                    defaultAttributesMerged = defaultAttributeValues[type]["all"];
                }

                if (attributes.ContainsKey("class"))
                {
                    var classes = attributes["class"].Split(',', ' ').Select(s => s.Trim().ToLower()).ToList();

                    if (xmlElement != null)
                    {
                        xmlElement.classes = classes;
                    }

                    foreach (var _class in classes)
                    {
                        if (defaultAttributeValues[type].ContainsKey(_class))
                        {
                            defaultAttributesMerged = XmlLayoutUtilities.MergeAttributes(defaultAttributesMerged, defaultAttributeValues[type][_class]);
                        }
                    }
                }

                attributes = XmlLayoutUtilities.MergeAttributes(defaultAttributesMerged, attributes);
            }

            if (xmlElement != null)
            {
                if (attributes.ContainsKey("hoverClass"))
                {
                    var hoverClasses = attributes["hoverClass"].Split(',', ' ').Select(s => s.Trim().ToLower()).Where(c => !String.IsNullOrEmpty(c)).ToList();
                    xmlElement.hoverClasses = hoverClasses;
                }

                xmlElement.ApplyAttributes(attributes);
            }
            else
            {
                tagHandler.ApplyAttributes(attributes);
            }

            tagHandler.Close();

            if (!tagHandler.renderElement)
            {
                tagHandler.RemoveElement();
            }
        }