Exemple #1
0
        public override void ApplyAttributes(AttributeDictionary attributes)
        {
            TableCell cellTemplate = null;

            switch (DataTableRowTemplateTagHandler.currentTemplateType)
            {
            case "HeaderRow":
                cellTemplate = DataTableTagHandler.currentDataTable.templateHeaderCell;
                break;

            case "DataRow":
                cellTemplate = DataTableTagHandler.currentDataTable.templateDataCell;
                break;
            }

            if (cellTemplate != null)
            {
                var cellTagHandler = XmlLayoutUtilities.GetXmlTagHandler("Cell");

                var cellXmlElement = cellTemplate.GetComponent <XmlElement>();
                if (cellXmlElement == null)
                {
                    cellXmlElement = cellTemplate.gameObject.AddComponent <XmlElement>();
                    cellXmlElement.Initialise(currentXmlElement.xmlLayoutInstance, cellTemplate.transform as RectTransform, cellTagHandler);
                }

                cellXmlElement.ApplyAttributes(attributes);
            }
        }
Exemple #2
0
        public override void ApplyAttributes(AttributeDictionary attributesToApply)
        {
            // this should never happen, but just in case
            if (TextMeshProDropdownTagHandler.CurrentHandler == null)
            {
                return;
            }

            attributesToApply.AddIfKeyNotExists("alignment", "Left");
            attributesToApply.AddIfKeyNotExists("dontMatchParentDimensions", "true");

            var dropdown = TextMeshProDropdownTagHandler.CurrentHandler.CurrentDropdown;

            var TMPHandler = XmlLayoutUtilities.GetXmlTagHandler("TextMeshPro");

            TMPHandler.SetInstance(dropdown.itemText.rectTransform, currentXmlLayoutInstance);
            TMPHandler.ApplyAttributes(attributesToApply);

            if (attributesToApply.ContainsKey("padding"))
            {
                var padding = attributesToApply["padding"].ToRectOffset();
                dropdown.itemText.rectTransform.offsetMin = new Vector2(padding.left, padding.bottom);
                dropdown.itemText.rectTransform.offsetMax = new Vector2(-padding.right, -padding.top);
            }

            var xmlElement = dropdown.itemText.GetComponent <XmlElement>();

            //xmlElement.attributes = xmlElement.attributes.Merge(attributesToApply);
            xmlElement.attributes.Merge(attributesToApply);
        }
Exemple #3
0
        public override void ApplyAttributes(AttributeDictionary attributes)
        {
            base.ApplyAttributes(attributes);

            var textComponent = currentInstanceTransform.GetComponentInChildren <Text>();

            var tagHandler = XmlLayoutUtilities.GetXmlTagHandler("Text");

            tagHandler.SetInstance(textComponent.rectTransform, this.currentXmlLayoutInstance);

            var textAttributes = new AttributeDictionary(
                attributes.Where(a => TextTagHandler.TextAttributes.Contains(a.Key, StringComparer.OrdinalIgnoreCase))
                .ToDictionary(a => a.Key, b => b.Value));

            if (attributes.ContainsKey("textshadow"))
            {
                textAttributes.Add("shadow", attributes["textshadow"]);
            }
            if (attributes.ContainsKey("textoutline"))
            {
                textAttributes.Add("outline", attributes["textoutline"]);
            }
            if (attributes.ContainsKey("textcolor"))
            {
                textAttributes.Add("color", attributes["textcolor"]);
            }
            if (attributes.ContainsKey("textalignment"))
            {
                textAttributes.Add("alignment", attributes["textalignment"]);
            }

            tagHandler.ApplyAttributes(textAttributes);

            // preserve aspect for button background
            var imageComponent = currentInstanceTransform.GetComponent <Image>();

            if (attributes.ContainsKey("preserveaspect"))
            {
                imageComponent.preserveAspect = attributes["preserveaspect"].ToBoolean();
            }



            // I've encountered a bit of a problem where the XmlElement object added to the PaginationButton's text object by ApplyAttributes->tagHandler->SetInstance
            // is intercepting and preventing click events from being fired for these buttons
            // I've tried to figure out a way to prevent this from happening entirely, as it is an issue I'd like to resolve, but I haven't had any luck
            // so instead, what we're going to do, for now, is disable the XmlElement object on the text object (as we don't need it anyway once the attributes have been applied)
            var xmlElement = textComponent.GetComponent <XmlElement>();

            if (xmlElement != null)
            {
                xmlElement.enabled = false;
            }
        }
Exemple #4
0
        public override void ApplyAttributes(AttributeDictionary attributesToApply)
        {
            base.ApplyAttributes(attributesToApply);

            var inputField = primaryComponent as InputField;

            var textComponents = new List <Text> {
                inputField.textComponent
            };

            if (inputField.placeholder != null)
            {
                var placeholderText = inputField.placeholder.GetComponent <Text>();
                if (placeholderText != null)
                {
                    textComponents.Add(placeholderText);
                }

                if (attributesToApply.ContainsKey("placeholdertext"))
                {
                    placeholderText.text = attributesToApply["placeholdertext"];
                }
            }

            foreach (var textComponent in textComponents)
            {
                var tagHandler = XmlLayoutUtilities.GetXmlTagHandler("Text");
                tagHandler.SetInstance(textComponent.rectTransform, this.currentXmlLayoutInstance);

                var textAttributes = new AttributeDictionary(
                    attributesToApply.Where(a => TextTagHandler.TextAttributes.Contains(a.Key, StringComparer.OrdinalIgnoreCase))
                    .ToDictionary(a => a.Key, b => b.Value));

                if (attributesToApply.ContainsKey("textshadow"))
                {
                    textAttributes.Add("shadow", attributesToApply["textshadow"]);
                }
                if (attributesToApply.ContainsKey("textoutline"))
                {
                    textAttributes.Add("outline", attributesToApply["textoutline"]);
                }
                if (attributesToApply.ContainsKey("textcolor"))
                {
                    textAttributes.Add("color", attributesToApply["textcolor"]);
                }
                if (attributesToApply.ContainsKey("textalignment"))
                {
                    textAttributes.Add("alignment", attributesToApply["textalignment"]);
                }

                tagHandler.ApplyAttributes(textAttributes);
            }
        }
Exemple #5
0
        public override void ApplyAttributes(AttributeDictionary attributesToApply)
        {
            base.ApplyAttributes(attributesToApply);

            var progressBar = primaryComponent as XmlLayoutProgressBar;

            var textComponent = progressBar.ref_text;

            var tagHandler = XmlLayoutUtilities.GetXmlTagHandler("Text");

            tagHandler.SetInstance(textComponent.rectTransform, this.currentXmlLayoutInstance);

            var textAttributes = new AttributeDictionary(
                attributesToApply.Where(a => TextTagHandler.TextAttributes.Contains(a.Key, StringComparer.OrdinalIgnoreCase))
                .ToDictionary(a => a.Key, b => b.Value));

            if (attributesToApply.ContainsKey("textshadow"))
            {
                textAttributes.Add("shadow", attributesToApply["textshadow"]);
            }
            if (attributesToApply.ContainsKey("textoutline"))
            {
                textAttributes.Add("outline", attributesToApply["textoutline"]);
            }
            if (attributesToApply.ContainsKey("textcolor"))
            {
                textAttributes.Add("color", attributesToApply["textcolor"]);
            }
            if (attributesToApply.ContainsKey("textalignment"))
            {
                textAttributes.Add("alignment", attributesToApply["textalignment"]);
            }

            tagHandler.ApplyAttributes(textAttributes);

            var fillImage = progressBar.ref_fillImage;

            if (attributesToApply.ContainsKey("fillImage"))
            {
                fillImage.sprite = attributesToApply.GetValue <Sprite>("fillImage");
            }
            if (attributesToApply.ContainsKey("fillImageColor"))
            {
                fillImage.color = attributesToApply["fillImageColor"].ToColor(currentXmlLayoutInstance);
            }
        }
        public override void ApplyAttributes(AttributeDictionary attributes)
        {
            if (!attributes.ContainsKey("templateType"))
            {
                Debug.LogWarningFormat("[XmlLayout][DataTableRowTemplate] The 'templateType' attribute is required.");
                return;
            }

            var templateType = attributes["templateType"];
            var dataTable    = currentXmlElement.GetComponentInParent <XmlLayoutDataTable>();

            List <TableRow> rows = new List <TableRow>();

            switch (templateType)
            {
            case "HeaderRow":
                rows.Add(dataTable.templateHeaderRow);
                rows.Add(dataTable.headerRow);
                break;

            case "DataRow":
                rows.Add(dataTable.templateDataRow);
                rows.AddRange(dataTable.dataRows);
                break;
            }

            rows = rows.Where(r => r != null).ToList();

            if (rows.Count > 0)
            {
                var rowTagHandler = XmlLayoutUtilities.GetXmlTagHandler("Row");

                foreach (var row in rows)
                {
                    var rowXmlElement = row.GetComponent <XmlElement>();
                    if (rowXmlElement == null)
                    {
                        rowXmlElement = row.gameObject.AddComponent <XmlElement>();
                        rowXmlElement.Initialise(currentXmlElement.xmlLayoutInstance, row.transform as RectTransform, rowTagHandler);
                    }

                    rowXmlElement.ApplyAttributes(attributes);
                }
            }
        }
Exemple #7
0
        public override void ApplyAttributes(AttributeDictionary attributesToApply)
        {
            // this should never happen, but just in case
            if (TextMeshProDropdownTagHandler.CurrentHandler == null)
            {
                return;
            }

            attributesToApply.AddIfKeyNotExists("alignment", "Left");
            attributesToApply.AddIfKeyNotExists("dontMatchParentDimensions", "true");

            var dropdown = TextMeshProDropdownTagHandler.CurrentHandler.CurrentDropdown;

            var TMPHandler = XmlLayoutUtilities.GetXmlTagHandler("TextMeshPro");

            TMPHandler.SetInstance(dropdown.itemText.rectTransform, currentXmlLayoutInstance);
            TMPHandler.ApplyAttributes(attributesToApply);
        }
Exemple #8
0
        public override void ApplyAttributes(AttributeDictionary attributesToApply)
        {
            base.ApplyAttributes(attributesToApply);

            var xmlLayoutButton = currentInstanceTransform.GetComponent <XmlLayoutButton>();

            var  textComponent = currentInstanceTransform.GetComponentInChildren <Text>(true);
            bool applyTextAttributesFromButton = true;

            ColorBlock textColors = new ColorBlock()
            {
                normalColor = Color.black, highlightedColor = Color.black, disabledColor = Color.black, pressedColor = Color.black, colorMultiplier = 1
            };

            if (attributesToApply.ContainsKey("textcolors"))
            {
                textColors = attributesToApply["textcolors"].ToColorBlock(currentXmlLayoutInstance);
            }
            else if (attributesToApply.ContainsKey("textcolor") || attributesToApply.ContainsKey("deselectedtextcolor"))
            {
                // draw the text colors from the textColor attribute
                var textColor = (attributesToApply.ContainsKey("textcolor") ? attributesToApply["textcolor"] : attributesToApply["deselectedtextcolor"]).ToColor(currentXmlLayoutInstance);

                SetColorBlockColor(ref textColors, textColor);
            }

            if (currentXmlElement.childElements.Count > 0)
            {
                var child = currentXmlElement.childElements.First();

                if (child.tagType == "Text")
                {
                    if (child.gameObject != textComponent.gameObject)
                    {
                        // Replace the original text component with the child element
                        child.rectTransform.SetParent(textComponent.rectTransform.parent);
                        if (Application.isPlaying)
                        {
                            GameObject.Destroy(textComponent.gameObject);
                        }
                        else
                        {
                            GameObject.DestroyImmediate(textComponent.gameObject);
                        }

                        textComponent = child.GetComponent <Text>();
                    }

                    applyTextAttributesFromButton = false;

                    if (child.attributes.ContainsKey("color"))
                    {
                        SetColorBlockColor(ref textColors, textComponent.color);
                    }

                    xmlLayoutButton.TextComponent = new TextComponentWrapper(textComponent);
                }
#if TEXTMESHPRO_PRESENT
                else if (child.tagType == "TextMeshPro")
                {
                    if (textComponent != null && child.gameObject != textComponent.gameObject)
                    {
                        // Replace the original text component with the child element
                        child.rectTransform.SetParent(textComponent.rectTransform.parent);
                        if (Application.isPlaying)
                        {
                            GameObject.Destroy(textComponent.gameObject);
                        }
                        else
                        {
                            GameObject.DestroyImmediate(textComponent.gameObject);
                        }
                    }

                    var textMeshPro = child.GetComponent <TextMeshProUGUI>();

                    applyTextAttributesFromButton = false;

                    if (child.attributes.ContainsKey("color"))
                    {
                        SetColorBlockColor(ref textColors, textMeshPro.color);
                    }

                    xmlLayoutButton.TextComponent = new TextComponentWrapper(textMeshPro);
                }
#endif

                if (!child.attributes.ContainsKey("text") && attributesToApply.ContainsKey("text"))
                {
                    child.SetAndApplyAttribute("text", attributesToApply["text"]);
                }

                child.rectTransform.localScale = Vector3.one;
            }
            else
            {
                xmlLayoutButton.TextComponent = new TextComponentWrapper(textComponent);
            }

            if (applyTextAttributesFromButton)
            {
                var tagHandler = XmlLayoutUtilities.GetXmlTagHandler("Text");
                tagHandler.SetInstance(textComponent.rectTransform, this.currentXmlLayoutInstance);

                var textAttributes = new AttributeDictionary(
                    attributesToApply.Where(a => TextTagHandler.TextAttributes.Contains(a.Key, StringComparer.OrdinalIgnoreCase))
                    .ToDictionary(a => a.Key, b => b.Value));

                if (attributesToApply.ContainsKey("textshadow"))
                {
                    textAttributes.Add("shadow", attributesToApply["textshadow"]);
                }
                if (attributesToApply.ContainsKey("textoutline"))
                {
                    textAttributes.Add("outline", attributesToApply["textoutline"]);
                }
                if (attributesToApply.ContainsKey("textcolor"))
                {
                    textAttributes.Add("color", attributesToApply["textcolor"]);
                }
                if (attributesToApply.ContainsKey("textalignment"))
                {
                    textAttributes.Add("alignment", attributesToApply["textalignment"]);
                }

                tagHandler.ApplyAttributes(textAttributes);
            }

            // preserve aspect for button background
            var imageComponent = currentInstanceTransform.GetComponent <Image>();
            if (attributesToApply.ContainsKey("preserveaspect"))
            {
                imageComponent.preserveAspect = attributesToApply["preserveaspect"].ToBoolean();
            }

            // Button image
            if (attributesToApply.ContainsKey("icon") || this.currentXmlElement.attributes.ContainsKey("icon"))
            {
                var cell = xmlLayoutButton.IconCell;

                // position the cell on the left or the right
                var alignmentParameter = attributesToApply.ContainsKey("iconAlignment")
                                       ? attributesToApply["iconAlignment"]
                                       : (currentXmlElement.attributes.ContainsKey("iconAlignment") ? currentXmlElement.attributes["iconAlignment"] : "Left");

                var imageAlignment = (ButtonIconAlignment)Enum.Parse(typeof(ButtonIconAlignment), alignmentParameter);

                var buttonImageWidth = attributesToApply.ContainsKey("iconwidth")
                                        ? attributesToApply["iconwidth"].ToFloat()
                                        : currentXmlElement.attributes.ContainsKey("iconwidth") ? currentXmlElement.attributes["iconwidth"].ToFloat() : 0;

                xmlLayoutButton.ButtonTableLayout.ColumnWidths = new List <float>()
                {
                    0, 0
                };

                if (imageAlignment == ButtonIconAlignment.Left)
                {
                    cell.transform.SetAsFirstSibling();
                    xmlLayoutButton.ButtonTableLayout.ColumnWidths[0] = buttonImageWidth;
                }
                else
                {
                    cell.transform.SetAsLastSibling();
                    xmlLayoutButton.ButtonTableLayout.ColumnWidths[1] = buttonImageWidth;
                }

                xmlLayoutButton.IconComponent.preserveAspect = true;

                if (attributesToApply.ContainsKey("icon"))
                {
                    xmlLayoutButton.IconComponent.sprite = attributesToApply["icon"].ToSprite();
                }

                if (attributesToApply.ContainsKey("iconcolor"))
                {
                    xmlLayoutButton.IconColor = attributesToApply["iconcolor"].ToColor(currentXmlLayoutInstance);
                }

                if (attributesToApply.ContainsKey("iconhovercolor"))
                {
                    xmlLayoutButton.IconHoverColor = attributesToApply["iconhovercolor"].ToColor(currentXmlLayoutInstance);
                }

                if (attributesToApply.ContainsKey("icondisabledcolor"))
                {
                    xmlLayoutButton.IconDisabledColor = attributesToApply["icondisabledcolor"].ToColor(currentXmlLayoutInstance);
                }

                if (attributesToApply.ContainsKey("iconimagetype"))
                {
                    xmlLayoutButton.IconComponent.type = (Image.Type)Enum.Parse(typeof(Image.Type), attributesToApply["iconimagetype"]);
                }

                cell.gameObject.SetActive(true);

                // show or hide the text cell if there is (or is not) any text
                if ((!attributesToApply.ContainsKey("text") || String.IsNullOrEmpty(attributesToApply["text"])) &&
                    !currentXmlElement.attributes.ContainsKey("text"))
                {
                    xmlLayoutButton.TextCell.gameObject.SetActive(false);
                }
                else
                {
                    xmlLayoutButton.TextCell.gameObject.SetActive(true);
                }
            }

            if (attributesToApply.ContainsKey("padding"))
            {
                xmlLayoutButton.ButtonTableLayout.padding = attributesToApply["padding"].ToRectOffset();
            }

            xmlLayoutButton.TextColors = textColors;

            XmlLayoutTimer.DelayedCall(0,
                                       () =>
            {
                if (xmlLayoutButton.mouseIsOver)
                {
                    xmlLayoutButton.OnPointerEnter(null);
                }
                else
                {
                    xmlLayoutButton.OnPointerExit(null);
                }
            }, xmlLayoutButton);
        }
Exemple #9
0
        public override void ApplyAttributes(AttributeDictionary attributes)
        {
            var pagedRectTagHandler = PagedRectTagHandler.CurrentPagedRectTagHandler;

            if (pagedRectTagHandler == null)
            {
                Debug.Log("[XmlLayout] Error: PaginationButtonTemplate: Unable to locate PagedRect instance.");
            }
            else
            {
                var pagedRectInstance = pagedRectTagHandler.currentInstanceTransform.GetComponent <PagedRect>();

                var type = attributes.GetValue("type");

                Button buttonInstance = null;

                switch (type.ToLower())
                {
                case "currentpage":
                    if (pagedRectInstance.ButtonTemplate_CurrentPage != null)
                    {
                        buttonInstance = pagedRectInstance.ButtonTemplate_CurrentPage.Button;
                    }
                    break;

                case "otherpages":
                    if (pagedRectInstance.ButtonTemplate_OtherPages != null)
                    {
                        buttonInstance = pagedRectInstance.ButtonTemplate_OtherPages.Button;
                    }
                    break;

                case "disabledpages":
                    if (pagedRectInstance.ButtonTemplate_DisabledPage != null)
                    {
                        buttonInstance = pagedRectInstance.ButtonTemplate_DisabledPage.Button;
                    }
                    break;

                case "next":
                    if (pagedRectInstance.Button_NextPage != null)
                    {
                        buttonInstance = pagedRectInstance.Button_NextPage.Button;
                    }
                    break;

                case "previous":
                    if (pagedRectInstance.Button_PreviousPage != null)
                    {
                        buttonInstance = pagedRectInstance.Button_PreviousPage.Button;
                    }
                    break;

                case "first":
                    if (pagedRectInstance.Button_FirstPage != null)
                    {
                        buttonInstance = pagedRectInstance.Button_FirstPage.Button;
                    }
                    break;

                case "last":
                    if (pagedRectInstance.Button_LastPage != null)
                    {
                        buttonInstance = pagedRectInstance.Button_LastPage.Button;
                    }
                    break;
                }

                if (buttonInstance == null)
                {
                    Debug.Log("[XmlLayout][PaginationButtonTemplate] Unable to locate button template for '" + type + "'");
                }
                else
                {
                    var tagHandler = XmlLayoutUtilities.GetXmlTagHandler("PaginationButton");
                    tagHandler.SetInstance(buttonInstance.transform as RectTransform, this.currentXmlLayoutInstance);
                    attributes.Remove("type");
                    tagHandler.ApplyAttributes(attributes);
                }
            }
        }
        public override void ApplyAttributes(AttributeDictionary attributesToApply)
        {
            base.ApplyAttributes(attributesToApply);

            var inputField = primaryComponent as InputField;

            var textComponents = new List <Text> {
                inputField.textComponent
            };

            if (inputField.placeholder != null)
            {
                var placeholderText = inputField.placeholder.GetComponent <Text>();
                if (placeholderText != null)
                {
                    textComponents.Add(placeholderText);
                }

                if (attributesToApply.ContainsKey("placeholdertext"))
                {
                    placeholderText.text = StringExtensions.DecodeEncodedNonAsciiCharacters(attributesToApply["placeholdertext"]);
                }
            }

            foreach (var textComponent in textComponents)
            {
                var tagHandler = XmlLayoutUtilities.GetXmlTagHandler("Text");
                tagHandler.SetInstance(textComponent.rectTransform, this.currentXmlLayoutInstance);

                var textAttributes = new AttributeDictionary(
                    attributesToApply.Where(a => TextTagHandler.TextAttributes.Contains(a.Key, StringComparer.OrdinalIgnoreCase))
                    .ToDictionary(a => a.Key, b => b.Value));

                if (attributesToApply.ContainsKey("textshadow"))
                {
                    textAttributes.Add("shadow", attributesToApply["textshadow"]);
                }
                if (attributesToApply.ContainsKey("textoutline"))
                {
                    textAttributes.Add("outline", attributesToApply["textoutline"]);
                }
                if (attributesToApply.ContainsKey("textcolor"))
                {
                    textAttributes.Add("color", attributesToApply["textcolor"]);
                }
                if (attributesToApply.ContainsKey("textalignment"))
                {
                    textAttributes.Add("alignment", attributesToApply["textalignment"]);
                }

                textAttributes.Remove("text");

                if (attributesToApply.ContainsKey("textoffset"))
                {
                    var offset = attributesToApply["textoffset"].ToRectOffset();
                    textAttributes.Add("offsetMin", String.Format("{0} {1}", offset.left, offset.bottom));
                    textAttributes.Add("offsetMax", String.Format("-{0} -{1}", offset.right, offset.top));
                }

                tagHandler.ApplyAttributes(textAttributes);
            }
        }
Exemple #11
0
        public override void ApplyAttributes(AttributeDictionary attributesToApply)
        {
            base.ApplyAttributes(attributesToApply);

            var toggle        = primaryComponent as Toggle;
            var checkMark     = toggle.graphic as Image;
            var textComponent = currentXmlElement.GetComponentInChildren <Text>();

            if (textComponent != null && currentXmlElement.childElements.Count > 0)
            {
                var childAttributes = new AttributeDictionary();

                var child = currentXmlElement.childElements.First();
                if (child.tagType == "Text")
                {
                    if (textComponent.gameObject != child.gameObject)
                    {
                        // Replace the original text component with the child element
                        child.rectTransform.SetParent(textComponent.rectTransform.parent);

                        if (Application.isPlaying)
                        {
                            GameObject.Destroy(textComponent.gameObject);
                        }
                        else
                        {
                            GameObject.DestroyImmediate(textComponent.gameObject);
                        }


                        textComponent = child.GetComponent <Text>();

                        if (!child.HasAttribute("alignment"))
                        {
                            childAttributes.Add("alignment", "MiddleLeft");
                        }
                    }
                }
#if TEXTMESHPRO_PRESENT
                else if (child.tagType == "TextMeshPro")
                {
                    // Replace the original text component with the child element
                    child.rectTransform.SetParent(textComponent.rectTransform.parent);
                    if (Application.isPlaying)
                    {
                        GameObject.Destroy(textComponent.gameObject);
                    }
                    else
                    {
                        GameObject.DestroyImmediate(textComponent.gameObject);
                    }

                    if (!child.HasAttribute("alignment"))
                    {
                        childAttributes.Add("alignment", "Left");
                    }
                }
#endif

                if (!child.HasAttribute("text"))
                {
                    if (attributesToApply.ContainsKey("text"))
                    {
                        childAttributes.Add("text", attributesToApply["text"]);
                    }
                    else
                    {
                        // if the child has no text, and the parent has no text, disable the text component
                        if (!currentXmlElement.HasAttribute("text"))
                        {
                            childAttributes.Add("active", "false");
                        }
                    }
                }
                else
                {
                    // override the parent value if need be
                    attributesToApply.SetValue("text", child.GetAttribute("text"));
                }

                // default padding value as per standard toggle element
                if (!child.HasAttribute("padding"))
                {
                    childAttributes.Add("padding", "23 5 2 1");
                }
                if (!child.HasAttribute("flexibleWidth"))
                {
                    childAttributes.Add("flexibleWidth", "1");
                }

                if (childAttributes.Count > 0)
                {
                    child.ApplyAttributes(childAttributes);
                }
            }

            if (attributesToApply.ContainsKey("checkcolor"))
            {
                checkMark.color = attributesToApply["checkcolor"].ToColor(currentXmlLayoutInstance);
            }

            var targetGraphic = toggle.targetGraphic as Image;
            if (attributesToApply.ContainsKey("togglewidth"))
            {
                var width = float.Parse(attributesToApply["togglewidth"]);

                /*targetGraphic.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
                 * checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
                 *
                 * if (textComponent != null)
                 * {
                 *  textComponent.rectTransform.localPosition = new Vector2(width, textComponent.rectTransform.localPosition.y);
                 * }*/

                var toggleLayoutElement = targetGraphic.GetComponent <LayoutElement>() ?? targetGraphic.gameObject.AddComponent <LayoutElement>();
                toggleLayoutElement.preferredWidth = width;
                toggleLayoutElement.minWidth       = width;
            }

            if (attributesToApply.ContainsKey("toggleheight"))
            {
                var height = float.Parse(attributesToApply["toggleheight"]);

                /*targetGraphic.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
                 * checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);*/

                var toggleLayoutElement = targetGraphic.GetComponent <LayoutElement>() ?? targetGraphic.gameObject.AddComponent <LayoutElement>();
                toggleLayoutElement.preferredHeight = height;
                toggleLayoutElement.minHeight       = height;
            }

            if (ToggleGroupTagHandler.CurrentToggleGroupInstance != null)
            {
                var xmlLayoutToggleGroupInstance = ToggleGroupTagHandler.CurrentToggleGroupInstance;

                xmlLayoutToggleGroupInstance.AddToggle(toggle);
                xmlLayoutToggleGroupInstance.UpdateToggleElement(toggle);

                toggle.onValueChanged.AddListener((e) =>
                {
                    if (e)
                    {
                        var value = xmlLayoutToggleGroupInstance.GetValueForElement(toggle);
                        xmlLayoutToggleGroupInstance.SetSelectedValue(value);
                    }
                });
            }


            // Text attributes
            if (textComponent != null)
            {
                var tagHandler = XmlLayoutUtilities.GetXmlTagHandler("Text");
                tagHandler.SetInstance(textComponent.rectTransform, this.currentXmlLayoutInstance);

                var textAttributes = new AttributeDictionary(
                    attributesToApply.Where(a => TextTagHandler.TextAttributes.Contains(a.Key, StringComparer.OrdinalIgnoreCase))
                    .ToDictionary(a => a.Key, b => b.Value));

                if (attributesToApply.ContainsKey("textshadow"))
                {
                    textAttributes.Add("shadow", attributesToApply["textshadow"]);
                }
                if (attributesToApply.ContainsKey("textoutline"))
                {
                    textAttributes.Add("outline", attributesToApply["textoutline"]);
                }
                if (attributesToApply.ContainsKey("textcolor"))
                {
                    textAttributes.Add("color", attributesToApply["textcolor"]);
                }

                tagHandler.ApplyAttributes(textAttributes);

                // disable the XmlElement component, it can interfere with mouse clicks/etc.
                textComponent.GetComponent <XmlElement>().enabled = false;

                // disable the text component if there is no text
                textComponent.gameObject.SetActive(!String.IsNullOrEmpty(textComponent.text));
            }

            if (!attributesToApply.ContainsKey("text") && !currentXmlElement.attributes.ContainsKey("text"))
            {
                var background          = (Image)toggle.targetGraphic;
                var backgroundTransform = background.rectTransform;

                if (!attributesToApply.ContainsKey("dontModifyBackground"))
                {
                    backgroundTransform.anchorMin          = new Vector2(0.5f, 0.5f);
                    backgroundTransform.anchorMax          = new Vector2(0.5f, 0.5f);
                    backgroundTransform.anchoredPosition3D = new Vector3(0, 0, 0);
                }
            }

            if (attributesToApply.ContainsKey("togglebackgroundimage"))
            {
                targetGraphic.sprite = attributesToApply["togglebackgroundimage"].ToSprite();
            }

            if (attributesToApply.ContainsKey("togglecheckmarkimage"))
            {
                checkMark.sprite = attributesToApply["togglecheckmarkimage"].ToSprite();
            }

            if (attributesToApply.ContainsKey("togglecheckmarkimagepreserveaspect"))
            {
                checkMark.preserveAspect = attributesToApply["togglecheckmarkimagepreserveaspect"].ToBoolean();
            }

            if (attributesToApply.ContainsKey("togglecheckmarksize"))
            {
                float checkSize = float.Parse(attributesToApply["togglecheckmarksize"]);

                //var checkMarkLayoutElement = checkMark.GetComponent<LayoutElement>();
                //checkMarkLayoutElement.preferredWidth = checkSize;
                //checkMarkLayoutElement.preferredHeight = checkSize;

                checkMark.rectTransform.anchorMin = new Vector2(0.5f, 0.5f);
                checkMark.rectTransform.anchorMax = new Vector2(0.5f, 0.5f);
                checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, checkSize);
                checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, checkSize);
            }
        }
Exemple #12
0
        public override void ApplyAttributes(AttributeDictionary attributesToApply)
        {
            base.ApplyAttributes(attributesToApply);

            var dropdown              = primaryComponent as Dropdown;
            var templateComponent     = dropdown.template.GetComponent <Image>();
            var itemTemplateComponent = dropdown.template.GetComponentInChildren <Toggle>();
            var textComponent         = dropdown.captionText;

            var textTagHandler = XmlLayoutUtilities.GetXmlTagHandler("Text");

            textTagHandler.SetInstance(textComponent.rectTransform, this.currentXmlLayoutInstance);

            var textAttributes = new AttributeDictionary(
                attributesToApply.Where(a => TextTagHandler.TextAttributes.Contains(a.Key, StringComparer.OrdinalIgnoreCase))
                .ToDictionary(a => a.Key, b => b.Value));

            if (attributesToApply.ContainsKey("textshadow"))
            {
                textAttributes.Add("shadow", attributesToApply["textshadow"]);
            }
            if (attributesToApply.ContainsKey("textoutline"))
            {
                textAttributes.Add("outline", attributesToApply["textoutline"]);
            }
            if (attributesToApply.ContainsKey("textcolor"))
            {
                textAttributes.Add("color", attributesToApply["textcolor"]);
            }
            if (attributesToApply.ContainsKey("textalignment"))
            {
                textAttributes.Add("alignment", attributesToApply["textalignment"]);
            }

            textTagHandler.ApplyAttributes(textAttributes);
            // disable the XmlElement component, it can interfere with mouse clicks/etc.
            textComponent.GetComponent <XmlElement>().enabled = false;

            var xmlLayoutDropdown = dropdown.GetComponent <XmlLayoutDropdown>();
            var arrow             = xmlLayoutDropdown.Arrow;

            if (attributesToApply.ContainsKey("arrowimage"))
            {
                arrow.sprite = attributesToApply["arrowimage"].ToSprite();
            }

            if (attributesToApply.ContainsKey("arrowcolor"))
            {
                arrow.color = attributesToApply["arrowcolor"].ToColor();
            }

            // dropdownHeight property
            if (attributesToApply.ContainsKey("dropdownheight"))
            {
                dropdown.template.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, float.Parse(attributesToApply["dropdownheight"]));
            }

            // Apply text attributes to the item template
            var itemTemplate   = xmlLayoutDropdown.ItemTemplate;
            var itemTagHandler = XmlLayoutUtilities.GetXmlTagHandler("Toggle");
            var itemAttributes = attributesToApply.Clone();

            if (attributesToApply.ContainsKey("itemheight"))
            {
                var itemHeight = float.Parse(attributesToApply["itemheight"]);
                (itemTemplate.transform as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, itemHeight);

                // it's also necessary to set the height of the content transform, otherwise we end up with weird issues
                var contentTransform = templateComponent.GetComponentInChildren <ScrollRect>().content;

                contentTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, itemHeight);
            }

            if (attributesToApply.ContainsKey("checkcolor"))
            {
                itemAttributes.Add("togglecheckmarkcolor", attributesToApply["checkcolor"]);
            }

            if (attributesToApply.ContainsKey("checksize"))
            {
                itemAttributes.Add("togglecheckmarksize", attributesToApply["checksize"]);
            }

            if (attributesToApply.ContainsKey("checkimage"))
            {
                itemAttributes.Add("togglecheckmarkimage", attributesToApply["checkimage"]);
            }

            if (attributesToApply.ContainsKey("checkimagepreserveaspect"))
            {
                itemAttributes.Add("togglecheckmarkimagepreserveaspect", attributesToApply["checkimagepreserveaspect"]);
            }

            // don't attempt to apply data source attributes to the item template
            itemAttributes.Remove("vm-dataSource");
            itemAttributes.Remove("vm-options");

            itemAttributes.Remove("color");
            itemAttributes.Remove("colors");

            // this attribute is checked by the Toggle to see if changes to the background should be permitted
            // (used by regular toggles to center the background if there is no text)
            itemAttributes.Add("dontModifyBackground", "");

            var itemXmlElement = itemTemplate.transform.GetComponent <XmlElement>();

            if (itemXmlElement == null)
            {
                itemTagHandler.SetInstance(itemTemplate.transform as RectTransform, this.currentXmlLayoutInstance);
                itemTagHandler.ApplyAttributes(itemAttributes);

                itemXmlElement = itemTemplate.transform.GetComponent <XmlElement>();
            }
            else
            {
                itemXmlElement.ApplyAttributes(itemAttributes);
            }

            if (itemXmlElement != null)
            {
                itemXmlElement.enabled = false;
            }

            if (attributesToApply.ContainsKey("itembackgroundcolors"))
            {
                itemTemplateComponent.colors = attributesToApply["itembackgroundcolors"].ToColorBlock();
            }

            if (attributesToApply.ContainsKey("dropdownbackgroundcolor"))
            {
                dropdown.template.GetComponent <Image>().color = attributesToApply["dropdownbackgroundcolor"].ToColor();
            }

            if (attributesToApply.ContainsKey("dropdownbackgroundimage"))
            {
                dropdown.template.GetComponent <Image>().sprite = attributesToApply["dropdownbackgroundimage"].ToSprite();
            }

            if (attributesToApply.ContainsKey("itemtextcolor"))
            {
                var itemTextComponent = dropdown.itemText;
                itemTextComponent.color = attributesToApply["itemtextcolor"].ToColor();
            }

            if (attributesToApply.ContainsKey("scrollbarcolors"))
            {
                xmlLayoutDropdown.DropdownScrollbar.colors = attributesToApply["scrollbarcolors"].ToColorBlock();
            }

            if (attributesToApply.ContainsKey("scrollbarimage"))
            {
                xmlLayoutDropdown.DropdownScrollbar.image.sprite = attributesToApply["scrollbarimage"].ToSprite();
            }

            if (attributesToApply.ContainsKey("scrollbarbackgroundcolor"))
            {
                xmlLayoutDropdown.DropdownScrollbar.GetComponent <Image>().color = attributesToApply["scrollbarbackgroundcolor"].ToColor();
            }

            if (attributesToApply.ContainsKey("scrollbarbackgroundimage"))
            {
                xmlLayoutDropdown.DropdownScrollbar.GetComponent <Image>().sprite = attributesToApply["scrollbarbackgroundimage"].ToSprite();
            }

            foreach (var attribute in attributesToApply)
            {
                SetPropertyValue(templateComponent, attribute.Key, attribute.Value);
            }

            // data source
#if !ENABLE_IL2CPP
            if (attributesToApply.ContainsKey("vm-options"))
            {
                xmlLayoutDropdown.optionsDataSource = attributesToApply["vm-options"];
            }

            if (attributesToApply.ContainsKey("vm-dataSource"))
            {
                HandleDataSourceAttribute(attributesToApply.GetValue("vm-dataSource"), attributesToApply.GetValue("vm-options"));
            }
#endif
        }