Ejemplo n.º 1
0
        /// <summary>
        /// Create "scrollView" node. If children supported - GameObject container for them should be returned.
        /// </summary>
        /// <param name="widget">Ui widget.</param>
        /// <param name="node">Xml node.</param>
        /// <param name="container">Markup container.</param>
        public static RectTransform Create(RectTransform widget, XmlNode node, MarkupContainer container)
        {
#if UNITY_EDITOR
            widget.name = "scrollView";
#endif
            float     amount;
            string    attrValue;
            Scrollbar horScroll = null;
            Scrollbar verScroll = null;

            var anchorMin     = Vector2.zero;
            var anchorMax     = Vector2.one;
            var offsetMin     = Vector3.zero;
            var offsetMax     = Vector3.zero;
            var needHorScroll = true;
            var needVerScroll = true;

            var theme = MarkupUtils.GetTheme(node, container);

            var scrollView = widget.gameObject.AddComponent <ScrollRect> ();

            // viewport.
            var viewport = MarkupUtils.CreateUiObject(ViewportName, widget);
            viewport.gameObject.AddComponent <RectMask2D> ();
            viewport.gameObject.AddComponent <NonVisualWidget> ().raycastTarget = false;
            viewport.anchorMin = Vector2.zero;
            viewport.anchorMax = Vector2.one;
            viewport.sizeDelta = Vector2.zero;
            viewport.pivot     = Vector2.up;

            // content.
            var content = MarkupUtils.CreateUiObject(ContentName, viewport);
            attrValue = node.GetAttribute(HashedContentSize);
            if (!string.IsNullOrEmpty(attrValue))
            {
                var parts = MarkupUtils.SplitAttrValue(attrValue);
                if (parts.Length > 0 && !string.IsNullOrEmpty(parts[0]))
                {
                    if (float.TryParse(parts[0], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount))
                    {
                        amount     *= 0.5f;
                        offsetMin.x = -amount;
                        offsetMax.x = amount;
                        anchorMin.x = 0.5f;
                        anchorMax.x = 0.5f;
                    }
                }
                if (parts.Length > 1 && !string.IsNullOrEmpty(parts[1]))
                {
                    if (float.TryParse(parts[1], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount))
                    {
                        amount     *= 0.5f;
                        offsetMin.y = -amount;
                        offsetMax.y = amount;
                        anchorMin.y = 0.5f;
                        anchorMax.y = 0.5f;
                    }
                }
            }
            content.anchorMin = anchorMin;
            content.anchorMax = anchorMax;
            content.offsetMin = offsetMin;
            content.offsetMax = offsetMax;

            attrValue = node.GetAttribute(HashedClamp);
            if (string.CompareOrdinal(attrValue, "true") == 0)
            {
                scrollView.movementType = ScrollRect.MovementType.Clamped;
            }

            attrValue = node.GetAttribute(HashedDrag);
            if (!string.IsNullOrEmpty(attrValue))
            {
                var parts = MarkupUtils.SplitAttrValue(attrValue);
                if (parts.Length > 0 && string.CompareOrdinal(parts[0], "false") == 0)
                {
                    scrollView.horizontal = false;
                }
                if (parts.Length > 1 && string.CompareOrdinal(parts[1], "false") == 0)
                {
                    scrollView.vertical = false;
                }
            }

            attrValue = node.GetAttribute(HashedBar);
            if (!string.IsNullOrEmpty(attrValue))
            {
                var parts = MarkupUtils.SplitAttrValue(attrValue);
                if (parts.Length > 0 && string.CompareOrdinal(parts[0], "false") == 0)
                {
                    needHorScroll = false;
                }
                if (parts.Length > 1 && string.CompareOrdinal(parts[1], "false") == 0)
                {
                    needVerScroll = false;
                }
            }

            if (needHorScroll)
            {
                horScroll = CreateScrollbar(widget, theme, true);
                MarkupUtils.SetNav(horScroll, node, container.UseNavigation);
            }
            if (needVerScroll)
            {
                verScroll = CreateScrollbar(widget, theme, false);
                MarkupUtils.SetNav(verScroll, node, container.UseNavigation);
            }

            attrValue = node.GetAttribute(HashedOnChange);
            if (!string.IsNullOrEmpty(attrValue))
            {
                widget.gameObject.AddComponent <NonVisualWidget> ();
                widget.gameObject.AddComponent <UiScrollViewAction> ().SetGroup(attrValue);
            }

            scrollView.content  = content;
            scrollView.viewport = viewport;
            scrollView.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
            scrollView.verticalScrollbarVisibility   = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
            scrollView.horizontalScrollbar           = horScroll;
            scrollView.verticalScrollbar             = verScroll;
            scrollView.decelerationRate = 0.01f;

            MarkupUtils.SetSize(widget, node);
            MarkupUtils.SetRotation(widget, node);
            MarkupUtils.SetOffset(widget, node);
            MarkupUtils.SetHidden(widget, node);

            scrollView.normalizedPosition = Vector2.up;

            return(content);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create "slider" node. If children supported - GameObject container for them should be returned.
        /// </summary>
        /// <param name="widget">Ui widget.</param>
        /// <param name="node">Xml node.</param>
        /// <param name="container">Markup container.</param>
        public static RectTransform Create(RectTransform widget, XmlNode node, MarkupContainer container)
        {
#if UNITY_EDITOR
            widget.name = "slider";
#endif
            var           slider = widget.gameObject.AddComponent <Slider> ();
            var           theme  = MarkupUtils.GetTheme(node, container);
            Image         img;
            RectTransform rt;

            var direction     = Slider.Direction.LeftToRight;
            var minValue      = 0f;
            var maxValue      = 1f;
            var useInts       = false;
            var dataValue     = 0f;
            var isInteractive = false;

            // background.
            rt           = MarkupUtils.CreateUiObject(BackgroundImageName, widget);
            img          = rt.gameObject.AddComponent <Image> ();
            img.sprite   = theme.GetSliderSprite(MarkupTheme.SliderState.Background);
            img.color    = theme.GetSliderColor(MarkupTheme.SliderState.Background);
            img.type     = Image.Type.Sliced;
            rt.anchorMin = Vector2.zero;
            rt.anchorMax = Vector2.one;
            rt.sizeDelta = Vector2.zero;

            // foreground.
            rt                = MarkupUtils.CreateUiObject(ForegroundImageName, widget);
            img               = rt.gameObject.AddComponent <Image> ();
            img.sprite        = theme.GetSliderSprite(MarkupTheme.SliderState.Foreground);
            img.color         = theme.GetSliderColor(MarkupTheme.SliderState.Foreground);
            img.type          = Image.Type.Sliced;
            img.raycastTarget = false;
            rt.sizeDelta      = Vector2.zero;
            slider.fillRect   = rt;

            string attrValue;
            attrValue = node.GetAttribute(HashedHandle);
            var useHandle = string.CompareOrdinal(attrValue, "true") == 0;
            if (useHandle)
            {
                var handle = MarkupUtils.CreateUiObject(null, widget);
                slider.handleRect = handle;

                rt  = MarkupUtils.CreateUiObject(HandleImageName, handle);
                img = rt.gameObject.AddComponent <Image> ();
                img.raycastTarget = false;
                img.sprite        = theme.GetSliderSprite(MarkupTheme.SliderState.Handle);
                img.color         = theme.GetSliderColor(MarkupTheme.SliderState.Handle);
                img.type          = Image.Type.Sliced;
                img.SetNativeSize();
                handle.sizeDelta = img.rectTransform.sizeDelta;
            }

            attrValue = node.GetAttribute(HashedRtl);
            if (string.CompareOrdinal(attrValue, "true") == 0)
            {
                direction = Slider.Direction.RightToLeft;
            }

            float amount;
            attrValue = node.GetAttribute(HashedRange);
            if (!string.IsNullOrEmpty(attrValue))
            {
                var parts = MarkupUtils.SplitAttrValue(attrValue);
                if (parts.Length > 0 && !string.IsNullOrEmpty(parts[0]))
                {
                    if (float.TryParse(parts[0], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount))
                    {
                        minValue = amount;
                    }
                }
                if (parts.Length > 1 && !string.IsNullOrEmpty(parts[1]))
                {
                    if (float.TryParse(parts[1], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount))
                    {
                        maxValue = amount;
                    }
                }
                if (parts.Length > 2 && string.CompareOrdinal(parts[2], "true") == 0)
                {
                    useInts = true;
                }
            }

            attrValue = node.GetAttribute(HashedValue);
            if (!string.IsNullOrEmpty(attrValue))
            {
                if (float.TryParse(attrValue, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount))
                {
                    dataValue = amount;
                }
            }

            attrValue = node.GetAttribute(HashedOnChange);
            if (!string.IsNullOrEmpty(attrValue))
            {
                widget.gameObject.AddComponent <UiSliderAction> ().SetGroup(attrValue);
                isInteractive = true;
            }

            slider.minValue     = minValue;
            slider.maxValue     = maxValue;
            slider.wholeNumbers = useInts;
            slider.value        = dataValue;
            slider.direction    = direction;

            slider.transition = Selectable.Transition.None;

            MarkupUtils.SetSize(widget, node);
            MarkupUtils.SetRotation(widget, node);
            MarkupUtils.SetOffset(widget, node);
            MarkupUtils.SetHidden(widget, node);
            MarkupUtils.SetNav(slider, node, container.UseNavigation);

            slider.interactable = useHandle && isInteractive;

            return(widget);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create "toggle" node. If children supported - GameObject container for them should be returned.
        /// </summary>
        /// <param name="widget">Ui widget.</param>
        /// <param name="node">Xml node.</param>
        /// <param name="container">Markup container.</param>
        public static RectTransform Create(RectTransform widget, XmlNode node, MarkupContainer container)
        {
#if UNITY_EDITOR
            widget.name = "toggle";
#endif
            var           tr = widget.transform;
            Image         img;
            RectTransform rt;
            Vector2       size;
            string        attrValue;

            var toggle = widget.gameObject.AddComponent <Toggle> ();
            var theme  = MarkupUtils.GetTheme(node, container);

            var isInteractive = false;

            // background.
            rt                   = MarkupUtils.CreateUiObject(BackgroundImageName, tr);
            img                  = rt.gameObject.AddComponent <Image> ();
            img.sprite           = theme.GetToggleSprite(MarkupTheme.ToggleState.Background);
            img.type             = Image.Type.Sliced;
            img.raycastTarget    = false;
            size                 = theme.GetToggleSize(MarkupTheme.ToggleState.Background);
            rt.anchorMin         = new Vector2(0f, 0.5f);
            rt.anchorMax         = new Vector2(0f, 0.5f);
            rt.offsetMin         = new Vector2(0f, -size.y * 0.5f);
            rt.offsetMax         = new Vector2(size.x, size.y * 0.5f);
            toggle.targetGraphic = img;

            // foreground.
            rt                = MarkupUtils.CreateUiObject(ForegroundImageName, rt);
            img               = rt.gameObject.AddComponent <Image> ();
            rt.anchorMin      = new Vector2(0.5f, 0.5f);
            rt.anchorMax      = new Vector2(0.5f, 0.5f);
            rt.sizeDelta      = theme.GetToggleSize(MarkupTheme.ToggleState.Foreground);
            img.sprite        = theme.GetToggleSprite(MarkupTheme.ToggleState.Foreground);
            img.type          = Image.Type.Sliced;
            img.raycastTarget = false;
            toggle.graphic    = img;

            // content.
            rt           = MarkupUtils.CreateUiObject(ContentName, tr);
            rt.anchorMin = Vector2.zero;
            rt.anchorMax = Vector2.one;
            rt.offsetMin = Vector2.right * size.x;
            rt.offsetMax = Vector2.zero;

            attrValue = node.GetAttribute(HashedGroup);
            if (!string.IsNullOrEmpty(attrValue))
            {
                var groupGo = container.GetNamedNode(attrValue);
                if ((object)groupGo != null)
                {
                    toggle.group = groupGo.GetComponent <ToggleGroup> ();
                }
            }

            attrValue = node.GetAttribute(HashedCheck);
            if (string.CompareOrdinal(attrValue, "true") == 0)
            {
                toggle.isOn = true;
            }

            attrValue = node.GetAttribute(HashedOnChange);
            if (!string.IsNullOrEmpty(attrValue))
            {
                widget.gameObject.AddComponent <NonVisualWidget> ();
                widget.gameObject.AddComponent <UiToggleAction> ().SetGroup(attrValue);
                isInteractive = true;
            }

            toggle.transition   = Selectable.Transition.None;
            toggle.interactable = isInteractive;

            MarkupUtils.SetSize(widget, node);
            MarkupUtils.SetRotation(widget, node);
            MarkupUtils.SetOffset(widget, node);
            MarkupUtils.SetHidden(widget, node);
            MarkupUtils.SetNav(toggle, node, container.UseNavigation);

            return(rt);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create "button" node. If children supported - GameObject container for them should be returned.
        /// </summary>
        /// <param name="widget">Ui widget.</param>
        /// <param name="node">Xml node.</param>
        /// <param name="container">Markup container.</param>
        public static RectTransform Create(RectTransform widget, XmlNode node, MarkupContainer container)
        {
#if UNITY_EDITOR
            widget.name = "button";
#endif
            var    btn = widget.gameObject.AddComponent <Button> ();
            var    img = widget.gameObject.AddComponent <Image> ();
            string attrValue;
            var    transition = Selectable.Transition.ColorTint;

            attrValue = node.GetAttribute(HashedBlend);
            switch (attrValue)
            {
            case "sprites":
                transition = Selectable.Transition.SpriteSwap;
                break;

            case "none":
                transition = Selectable.Transition.None;
                break;
            }

            var theme = MarkupUtils.GetTheme(node, container);

            switch (transition)
            {
            case Selectable.Transition.ColorTint:
                var colors = btn.colors;
                colors.normalColor      = theme.GetButtonColor(MarkupTheme.ButtonState.Normal);
                colors.pressedColor     = theme.GetButtonColor(MarkupTheme.ButtonState.Pressed);
                colors.highlightedColor = theme.GetButtonColor(MarkupTheme.ButtonState.Highlighted);
                colors.disabledColor    = theme.GetButtonColor(MarkupTheme.ButtonState.Disabled);
                btn.colors = colors;
                break;

            case Selectable.Transition.SpriteSwap:
                var sprites = btn.spriteState;
                sprites.pressedSprite     = theme.GetButtonSprite(MarkupTheme.ButtonState.Pressed);
                sprites.highlightedSprite = theme.GetButtonSprite(MarkupTheme.ButtonState.Highlighted);
                sprites.disabledSprite    = theme.GetButtonSprite(MarkupTheme.ButtonState.Disabled);
                btn.spriteState           = sprites;
                break;
            }

            img.sprite        = theme.GetButtonSprite(MarkupTheme.ButtonState.Normal);
            img.type          = Image.Type.Sliced;
            btn.targetGraphic = img;
            btn.transition    = transition;

            MarkupUtils.SetSize(widget, node);
            MarkupUtils.SetRotation(widget, node);
            MarkupUtils.SetOffset(widget, node);
            MarkupUtils.SetHidden(widget, node);
            MarkupUtils.SetNav(btn, node, container.UseNavigation);

            attrValue = node.GetAttribute(HashedDisabled);
            var disabled = string.CompareOrdinal(attrValue, "true") == 0;

            btn.interactable = !disabled && MarkupUtils.ValidateInteractive(widget, node, container.DragTreshold);

            return(widget);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create "input" node. If children supported - GameObject container for them should be returned.
        /// </summary>
        /// <param name="widget">Ui widget.</param>
        /// <param name="node">Xml node.</param>
        /// <param name="container">Markup container.</param>
        public static RectTransform Create(RectTransform widget, XmlNode node, MarkupContainer container)
        {
#if UNITY_EDITOR
            widget.name = "input";
#endif
            var    input = widget.gameObject.AddComponent <InputField> ();
            string attrValue;
            string font          = null;
            var    align         = TextAnchor.MiddleLeft;
            var    style         = FontStyle.Normal;
            var    size          = 24;
            var    isInteractive = false;

            var theme = MarkupUtils.GetTheme(node, container);

            var img = widget.gameObject.AddComponent <Image> ();
            img.type   = Image.Type.Sliced;
            img.sprite = theme.GetInputSprite();
            img.color  = theme.GetInputColor(MarkupTheme.InputState.Background);

            var ph     = MarkupUtils.CreateUiObject(null, widget);
            var phText = ph.gameObject.AddComponent <Text> ();
            phText.fontStyle = FontStyle.Italic;

            var txt    = MarkupUtils.CreateUiObject(null, widget);
            var inText = txt.gameObject.AddComponent <Text> ();
            inText.supportRichText = false;

            attrValue = node.GetAttribute(HashedFontName);
            if (!string.IsNullOrEmpty(attrValue))
            {
                font = attrValue;
            }

            attrValue = node.GetAttribute(HashedFontSize);
            if (!string.IsNullOrEmpty(attrValue))
            {
                int.TryParse(attrValue, out size);
            }

            attrValue = node.GetAttribute(HashedFontStyle);
            if (!string.IsNullOrEmpty(attrValue))
            {
                var parts = MarkupUtils.SplitAttrValue(attrValue);
                for (var i = 0; i < parts.Length; i++)
                {
                    switch (parts[i])
                    {
                    case "bold":
                        style |= FontStyle.Bold;
                        break;

                    case "italic":
                        style |= FontStyle.Italic;
                        break;
                    }
                }
            }

            attrValue = node.GetAttribute(HashedAlign);
            if (!string.IsNullOrEmpty(attrValue))
            {
                var parts    = MarkupUtils.SplitAttrValue(attrValue);
                var alignHor = 1;
                var alignVer = 3;
                for (var i = 0; i < parts.Length; i++)
                {
                    switch (parts[i])
                    {
                    case "left":
                        alignHor = 0;
                        break;

                    case "right":
                        alignHor = 2;
                        break;

                    case "top":
                        alignVer = 0;
                        break;

                    case "bottom":
                        alignVer = 6;
                        break;
                    }
                }
                align = (TextAnchor)(alignHor + alignVer);
            }

            attrValue = node.GetAttribute(HashedLocalize);
            if (!string.IsNullOrEmpty(attrValue))
            {
                phText.gameObject.AddComponent <TextLocalization> ().SetToken(attrValue);
            }
            else
            {
                phText.text = node.Value;
            }

            var margin    = theme.GetInputMargin();
            var marginMin = new Vector2(margin, margin);
            var marginMax = -marginMin;

            ph.anchorMin = Vector2.zero;
            ph.anchorMax = Vector2.one;
            ph.offsetMin = marginMin;
            ph.offsetMax = marginMax;

            txt.anchorMin = Vector2.zero;
            txt.anchorMax = Vector2.one;
            txt.offsetMin = marginMin;
            txt.offsetMax = marginMax;

            phText.raycastTarget = false;
            inText.raycastTarget = false;

            phText.alignment = align;
            inText.alignment = align;

            phText.font = container.GetFont(font);
            inText.font = phText.font;

            phText.color = theme.GetInputColor(MarkupTheme.InputState.Placeholder);
            if (!MarkupUtils.SetColor(inText, node))
            {
                inText.color = Color.black;
            }

            phText.fontStyle = theme.GetInputPlaceholderStyle();
            inText.fontStyle = style;

            phText.fontSize = size;
            inText.fontSize = size;

            input.targetGraphic  = img;
            input.transition     = Selectable.Transition.None;
            input.placeholder    = phText;
            input.textComponent  = inText;
            input.selectionColor = theme.GetInputColor(MarkupTheme.InputState.Selection);

            MarkupUtils.SetSize(widget, node);
            MarkupUtils.SetRotation(widget, node);
            MarkupUtils.SetOffset(widget, node);
            MarkupUtils.SetHidden(widget, node);
            MarkupUtils.SetNav(input, node, container.UseNavigation);

            attrValue = node.GetAttribute(HashedOnChange);
            if (!string.IsNullOrEmpty(attrValue))
            {
                widget.gameObject.AddComponent <UiInputAction> ().SetGroup(attrValue);
                isInteractive = true;
            }
            input.interactable = isInteractive;

            return(widget);
        }