Beispiel #1
0
        static YogaNode CreateYogaNode(XmlElement elem, YogaConfig conf)
        {
            YogaNode node = new YogaNode(conf);

            node.Note = elem.GetAttribute("note");
            //parse style
            Dictionary <string, string> style = ParseStyle(elem.GetAttribute("style"));

            switch (elem.Name)
            {
            default: throw new NotSupportedException();

            case "box":
                break;

            case "row":
                node.FlexDirection = YogaFlexDirection.Row;
                break;

            case "column":
                node.FlexDirection = YogaFlexDirection.Column;
                break;
            }
            foreach (var kv in style)
            {
                switch (kv.Key)
                {
                default: throw new NotSupportedException();

                case "width":
                    node.Width = ParseYogaValue(kv.Value);
                    break;

                case "height":
                    node.Height = ParseYogaValue(kv.Value);
                    break;

                case "padding":
                    node.Padding = ParseYogaValue(kv.Value);
                    break;

                case "align-items":
                    node.AlignItems = GetYogaAlign(kv.Value);
                    break;

                case "align-self":
                    node.AlignSelf = GetYogaAlign(kv.Value);
                    break;

                case "align-content":
                    node.AlignContent = GetYogaAlign(kv.Value);
                    break;

                case "margin-horizontal":
                    node.MarginHorizontal = ParseYogaValue(kv.Value);
                    break;

                case "flex":
                    node.Flex = float.Parse(kv.Value);
                    break;

                case "flex-shrink":
                    node.FlexShrink = float.Parse(kv.Value);
                    break;

                case "flex-glow":
                    node.FlexGrow = float.Parse(kv.Value);
                    break;

                case "max-width":
                    node.MaxWidth = ParseYogaValue(kv.Value);
                    break;

                case "max-height":
                    node.MaxHeight = ParseYogaValue(kv.Value);
                    break;

                case "min-width":
                    node.MinWidth = ParseYogaValue(kv.Value);
                    break;

                case "min-height":
                    node.MinHeight = ParseYogaValue(kv.Value);
                    break;

                case "aspect-ratio":
                    node.MinHeight = float.Parse(kv.Value);
                    break;

                case "overflow":
                    node.Overflow = GetOverflow(kv.Value);
                    break;

                case "flex-basis":
                    node.FlexBasis = ParseYogaValue(kv.Value);
                    break;

                case "wrap":
                    node.Wrap = GetYogaWrap(kv.Value);
                    break;

                case "position-type":
                    node.PositionType = GetYogaPosition(kv.Value);
                    break;

                case "justify-content":
                    node.JustifyContent = GetJustify(kv.Value);
                    break;

                case "flex-direction":
                    node.FlexDirection = GetFlexDirection(kv.Value);
                    break;

                case "style-direction":
                    node.StyleDirection = GetYogaDirection(kv.Value);
                    break;
                }
            }


            foreach (XmlNode ch in elem)
            {
                if (ch is XmlElement childElem)
                {
                    node.Append(CreateYogaNode(childElem, conf));
                }
            }
            return(node);
        }
Beispiel #2
0
        void Init2_1()
        {
            YogaConfig config = new YogaConfig();

            _rootNode = new YogaNode(config)
            {
                FlexDirection = YogaFlexDirection.Column,
                Width         = _rootPanel.Width,
                Height        = _rootPanel.Height,
                Padding       = 20,
                AlignItems    = YogaAlign.Stretch
            };

            YogaNode root_child0 = _rootNode.Append(new YogaNode(config)
            {
                FlexDirection  = YogaFlexDirection.Row,
                Width          = _rootNode.Width,
                Height         = 100,
                AlignItems     = YogaAlign.Center,
                AlignSelf      = YogaAlign.Center,
                Flex           = 1,
                FlexShrink     = 1,
                StyleDirection = YogaDirection.RightToLeft,
                Note           = "yellow",
            });

            YogaNode c1r0_child0 = root_child0.Append(new YogaNode(config)
            {
                Width      = 100,
                Height     = 100,
                FlexShrink = 1,
                Note       = "blue",
            });

            YogaNode c1r0_child1 = root_child0.Append(new YogaNode(config)
            {
                Width            = 100,
                Height           = 100,
                MarginHorizontal = 20,
                FlexGrow         = 1,
                FlexShrink       = 1,
                Note             = "blue",
            });

            YogaNode c1r0_child2 = root_child0.Append(new YogaNode(config)
            {
                Width      = 100,
                Height     = 100,
                FlexShrink = 1,
                Note       = "blue",
            });


            YogaNode root_child1 = _rootNode.Append(new YogaNode(config)
            {
                Width            = 100,
                Height           = 100,
                MarginHorizontal = 20,
                FlexGrow         = 1,
                FlexShrink       = 1,
                Note             = "red",
            });

            //YogaNode root_child2 = new YogaNode(config);
            //root_child2.Width = 100;
            //root_child2.Height = 100;
            //root_child2.FlexShrink = 1;
            //root.Insert(2, root_child2);

            _rootNode.StyleDirection = YogaDirection.LeftToRight;
            _rootNode.CalculateLayout();

            foreach (YogaNode child in _rootNode)
            {
                _rootPanel.Add(CreateBoxFromYogaNode(child, (y_node, b_node) =>
                {
                    switch (y_node.Note)
                    {
                    case "yellow": b_node.BackColor = Color.Yellow; break;

                    case "blue": b_node.BackColor = Color.Blue; break;

                    case "red": b_node.BackColor = Color.Red; break;
                    }
                }));
            }

            _rootPanel.UpdateLayout();
        }