Ejemplo n.º 1
0
        private static YogaValue ToYogaValue(JValue value)
        {
            if (value == null || value.Type == JTokenType.Null || value.Type == JTokenType.Undefined)
            {
                return(YogaValue.Undefined());
            }

            if (value.Type == JTokenType.String)
            {
                var s = value.Value <string>();

                if (s == "auto")
                {
                    return(YogaValue.Auto());
                }

                if (s.EndsWith("%"))
                {
                    return(YogaValue.Percent(float.Parse(s.Substring(0, s.Length - 1))));
                }

                throw new InvalidOperationException(
                          Invariant($"Unknown value: '{s}'"));
            }

            return(YogaValue.Point(value.Value <float>()));
        }
Ejemplo n.º 2
0
        internal static YogaValue ToYogaValue(this StyleLength styleValue)
        {
            if (styleValue.keyword == StyleKeyword.Auto)
            {
                return(YogaValue.Auto());
            }

            // For max-width and max-height
            if (styleValue.keyword == StyleKeyword.None)
            {
                return(float.NaN);
            }

            var length = styleValue.value;

            switch (length.unit)
            {
            case LengthUnit.Pixel:
                return(YogaValue.Point(length.value));

            case LengthUnit.Percent:
                return(YogaValue.Percent(length.value));

            default:
                Debug.LogAssertion($"Unexpected unit '{length.unit}'");
                return(float.NaN);
            }
        }
Ejemplo n.º 3
0
        internal static YogaValue ToYogaValue(this Length length)
        {
            if (length.IsAuto())
            {
                return(YogaValue.Auto());
            }

            // For max-width and max-height
            if (length.IsNone())
            {
                return(float.NaN);
            }

            switch (length.unit)
            {
            case LengthUnit.Pixel:
                return(YogaValue.Point(length.value));

            case LengthUnit.Percent:
                return(YogaValue.Percent(length.value));

            default:
                Debug.LogAssertion($"Unexpected unit '{length.unit}'");
                return(float.NaN);
            }
        }
Ejemplo n.º 4
0
        private static void Main()
        {
            var parent = new YogaNode
            {
                Width          = YogaValue.Point(100),
                Height         = YogaValue.Point(100),
                FlexDirection  = YogaFlexDirection.Row,
                JustifyContent = YogaJustify.SpaceAround,
                AlignItems     = YogaAlign.Center
            };

            var childA = new YogaNode
            {
                Width  = YogaValue.Point(25),
                Height = YogaValue.Point(50)
            };

            var childB = new YogaNode
            {
                Width     = YogaValue.Point(25),
                Height    = YogaValue.Point(50),
                AlignSelf = YogaAlign.FlexEnd
            };

            parent.AddChild(childA);
            parent.AddChild(childB);

            parent.CalculateLayout();

            Console.WriteLine($"{parent.LayoutX} {parent.LayoutY} {parent.LayoutWidth} {parent.LayoutHeight}");
            Console.WriteLine($"{childA.LayoutX} {childA.LayoutY} {childA.LayoutWidth} {childA.LayoutHeight}");
            Console.WriteLine($"{childB.LayoutX} {childB.LayoutY} {childB.LayoutWidth} {childB.LayoutHeight}");
        }
        /// <summary>
        /// Set a corner radius value.
        /// </summary>
        /// <param name="cornerType">
        ///     One of <see cref="TopLeft" />, <see cref="TopRight" />, <see cref="BottomRight" />,
        ///     <see cref="BottomLeft" />, <see cref="All" />.
        /// </param>
        /// <param name="value">the value for specified corner type.</param>
        public void Set(int cornerType, double?value)
        {
            if (cornerType != TopLeft && cornerType != TopRight && cornerType != BottomRight &&
                cornerType != BottomLeft && cornerType != All)
            {
                throw new ArgumentOutOfRangeException(nameof(cornerType));
            }
            var yogaValue = value.HasValue ? YogaValue.Point((float)value) : YogaValue.Undefined();

            _edgeSpacing.Set(cornerType, yogaValue);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Set a thickness value.
        /// </summary>
        /// <param name="thicknessType">
        ///     One of <see cref="Top" />, <see cref="Right" />, <see cref="Bottom" />,
        ///     <see cref="Left" />, <see cref="All" />.
        /// </param>
        /// <param name="value">the value for specified thickness type.</param>
        public void Set(int thicknessType, double?value)
        {
            if (thicknessType != Top && thicknessType != Right && thicknessType != Bottom &&
                thicknessType != Left && thicknessType != All)
            {
                throw new ArgumentOutOfRangeException(nameof(thicknessType));
            }
            var yogaValue = value.HasValue ? YogaValue.Point((float)value) : YogaValue.Undefined();

            _edgeSpacing.Set(thicknessType, yogaValue);
        }
Ejemplo n.º 7
0
        YogaValue DrawYogaValue(YogaValue value, GUIStyle style = null, params GUILayoutOption[] options)
        {
            var str      = "";
            var valueStr = IsNegativeZero(value.Value) ? "-0" : $"{value.Value}";

            if (value.Unit == YogaUnit.Auto)
            {
                str = "auto";
            }
            else if (value.Unit == YogaUnit.Percent)
            {
                str = $"{valueStr}%";
            }
            else if (value.Unit == YogaUnit.Point)
            {
                str = $"{valueStr}";
            }

            var res = EditorGUILayout.DelayedTextField(str, style ?? GUI.skin.textField, options);

            if (res == "auto")
            {
                return(YogaValue.Undefined());
            }

            var trimmed = new Regex("[^\\d\\.-]").Replace(res, "");

            var canParse = float.TryParse(trimmed, out var fval);

            if (trimmed == "-" || trimmed == "-0")
            {
                fval = -0f;
            }


            if (trimmed.Length > 0 && (canParse || trimmed == "-"))
            {
                if (res.EndsWith("%"))
                {
                    return(YogaValue.Percent(fval));
                }
                return(YogaValue.Point(fval));
            }

            return(YogaValue.Undefined());
        }
Ejemplo n.º 8
0
 public static YogaValue?NormalizeYogaValue(object value)
 {
     if (value == null)
     {
         return(YogaValue.Undefined());
     }
     else if (value is YogaValue c)
     {
         return(c);
     }
     else if (value is double d)
     {
         return(YogaValue.Point((float)d));
     }
     else if (value is int i)
     {
         return(YogaValue.Point(i));
     }
     else if (value is float v)
     {
         return(YogaValue.Point(v));
     }
     else if (value is string s)
     {
         if (s == "auto")
         {
             return(YogaValue.Auto());
         }
         else if (s.EndsWith("%"))
         {
             return(YogaValue.Percent(float.Parse(s.Replace("%", ""))));
         }
         else
         {
             return(YogaValue.Point(float.Parse(s)));
         }
     }
     else
     {
         return(value as YogaValue?);
     }
 }
Ejemplo n.º 9
0
 public static YogaValue StyleLengthToYogaValue(StyleLength value)
 {
     if (value.keyword == StyleKeyword.Auto)
     {
         return(YogaValue.Auto());
     }
     if (value.keyword == StyleKeyword.Null || value.keyword == StyleKeyword.None || value.keyword == StyleKeyword.Initial)
     {
         return(YogaValue.Undefined());
     }
     if (value.value.unit == LengthUnit.Percent)
     {
         return(YogaValue.Percent(value.value.value));
     }
     if (value.value.unit == LengthUnit.Pixel)
     {
         return(YogaValue.Point(value.value.value));
     }
     return(YogaValue.Undefined());
 }
Ejemplo n.º 10
0
        internal static YogaValue ToYogaValue(this StyleLength styleValue)
        {
            bool      flag = styleValue.keyword == StyleKeyword.Auto;
            YogaValue result;

            if (flag)
            {
                result = YogaValue.Auto();
            }
            else
            {
                bool flag2 = styleValue.keyword == StyleKeyword.None;
                if (flag2)
                {
                    result = float.NaN;
                }
                else
                {
                    Length     value      = styleValue.value;
                    LengthUnit unit       = value.unit;
                    LengthUnit lengthUnit = unit;
                    if (lengthUnit != LengthUnit.Pixel)
                    {
                        if (lengthUnit != LengthUnit.Percent)
                        {
                            Debug.LogAssertion(string.Format("Unexpected unit '{0}'", value.unit));
                            result = float.NaN;
                        }
                        else
                        {
                            result = YogaValue.Percent(value.value);
                        }
                    }
                    else
                    {
                        result = YogaValue.Point(value.value);
                    }
                }
            }
            return(result);
        }
Ejemplo n.º 11
0
        public object FromString(string value)
        {
            if (value == "auto")
            {
                return(YogaValue.Auto());
            }
            else if (value.EndsWith("%"))
            {
                if (float.TryParse(value.Replace("%", ""), NumberStyles.Float, culture, out var parsedValue))
                {
                    return(YogaValue.Percent(parsedValue));
                }
                return(SpecialNames.CantParse);
            }

            if (float.TryParse(PxRegex.Replace(value, ""), NumberStyles.Float, culture, out var parsedValue2))
            {
                return(YogaValue.Point(parsedValue2));
            }
            return(SpecialNames.CantParse);
        }
Ejemplo n.º 12
0
 static YogaValue ParseYogaValue(string value)
 {
     if (value.EndsWith("%"))
     {
         return(YogaValue.Percent(float.Parse(value.Substring(0, value.Length - 1))));
     }
     else if (value.EndsWith("pt"))
     {
         return(YogaValue.Point(float.Parse(value.Substring(0, value.Length - 2))));
     }
     else if (float.TryParse(value, out float result))
     {
         return(YogaValue.Point(result));
     }
     else if (value == "auto")
     {
         return(YogaValue.Auto);
     }
     else
     {
         return(YogaValue.Undefined);
     }
 }
Ejemplo n.º 13
0
 public object Convert(object value)
 {
     if (value == null)
     {
         return(YogaValue.Undefined());
     }
     else if (value is YogaValue c)
     {
         return(c);
     }
     else if (value is double d)
     {
         return(YogaValue.Point((float)d));
     }
     else if (value is int i)
     {
         return(YogaValue.Point(i));
     }
     else if (value is float v)
     {
         return(YogaValue.Point(v));
     }
     return(FromString(value?.ToString()));
 }
Ejemplo n.º 14
0
 public static YogaValue Pt(this int value)
 {
     return(YogaValue.Point(value));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Instantiates a <see cref="EdgeSpacing"/>
 /// </summary>
 public EdgeSpacing()
     : this(YogaValue.Point(0))
 {
 }
Ejemplo n.º 16
0
    protected override void OnUpdate()
    {
        Entities.ForEach((Entity e, ref UiRenderBounds uiRenderBounds) =>
        {
            if (!nodes.TryGetValue(e, out var yn))
            {
                var yogaNode = new YogaNode(YogaConfig.Default)
                {
                    Data   = e,
                    Margin = YogaValue.Point(10),
                    Flex   = 1,
                };
                nodes.Add(e, yogaNode);
                _root.AddChild(yogaNode);
            }
        });

//        YogaNode n = new YogaNode(YogaConfig.Default)
//        {
//            Data = "root",
//            Display = YogaDisplay.Flex,
//            FlexDirection = YogaFlexDirection.Column,
//        };
//        n.Width = YogaValue.Point(400);
//        n.Height = YogaValue.Point(200);
//        n.AddChild(new YogaNode(YogaConfig.Default)
//        {
//            Data = "child",
//            Margin = YogaValue.Point(10),
//            Flex = 1,
////            Width = YogaValue.Percent(100),
////            Height = YogaValue.Percent(100),
//        });n.AddChild(new YogaNode(YogaConfig.Default)
//        {
//            Data = "child",
//            Margin = YogaValue.Point(10),
//            Flex = 1,
////            Width = YogaValue.Percent(100),
////            Height = YogaValue.Percent(100),
//        });
        var r = Screen.safeArea;

        _root.Width  = YogaValue.Point(r.width);
        _root.Height = YogaValue.Point(r.height);
        _root.CalculateLayout(Single.NaN, Single.NaN);
//        Dump(_root, 0);
        Entities.ForEach((Entity e, ref UiRenderBounds uiRenderBounds) =>
        {
            var yn     = nodes[e];
            var layout = yn.GetLayoutRect();
            uiRenderBounds.Value.Center  = new float3(layout.center.x, layout.center.y, 0);
            uiRenderBounds.Value.Extents = new float3(layout.width / 2, layout.height / 2, 0);
        });

        void Dump(YogaNode yn, int indent)
        {
            Debug.Log($"{new string(' ', 2 * indent)}{yn.Data}: {yn.GetLayoutRect()}");
            foreach (YogaNode child in yn)
            {
                Dump(child, indent + 1);
            }
        }
    }