From a2f8703759491d8f29394f3daa339bbab362c1c2 Mon Sep 17 00:00:00 2001 From: Gokhan Kurt Date: Mon, 15 Jul 2024 20:02:23 +0300 Subject: [PATCH] support yoga 3.1.0 percent gaps --- .../UGUI/Components/UGUIComponent.cs | 4 +-- .../Styling/Properties/LayoutProperties.cs | 4 +-- Runtime/Styling/Shorthands/AllShorthands.cs | 4 ++- Runtime/Yoga/Native.cs | 3 +++ Runtime/Yoga/YogaNode.cs | 27 ++++++++++++------- 5 files changed, 28 insertions(+), 14 deletions(-) diff --git a/Runtime/Frameworks/UGUI/Components/UGUIComponent.cs b/Runtime/Frameworks/UGUI/Components/UGUIComponent.cs index 8022cf5b..326c8ac1 100644 --- a/Runtime/Frameworks/UGUI/Components/UGUIComponent.cs +++ b/Runtime/Frameworks/UGUI/Components/UGUIComponent.cs @@ -222,8 +222,8 @@ protected void ApplyYogaValues() Layout.Top = StylingHelpers.GetStyleLength(computed, LayoutProperties.Top); Layout.Bottom = StylingHelpers.GetStyleLength(computed, LayoutProperties.Bottom); - Layout.RowGap = StylingHelpers.GetStyleFloat(computed, LayoutProperties.RowGap); - Layout.ColumnGap = StylingHelpers.GetStyleFloat(computed, LayoutProperties.ColumnGap); + Layout.RowGap = StylingHelpers.GetStyleLength(computed, LayoutProperties.RowGap); + Layout.ColumnGap = StylingHelpers.GetStyleLength(computed, LayoutProperties.ColumnGap); Layout.BorderLeftWidth = StylingHelpers.GetStyleFloatDouble(computed, LayoutProperties.BorderLeftWidth, LayoutProperties.BorderWidth); Layout.BorderRightWidth = StylingHelpers.GetStyleFloatDouble(computed, LayoutProperties.BorderRightWidth, LayoutProperties.BorderWidth); diff --git a/Runtime/Styling/Properties/LayoutProperties.cs b/Runtime/Styling/Properties/LayoutProperties.cs index 68fb0daa..49b2f659 100644 --- a/Runtime/Styling/Properties/LayoutProperties.cs +++ b/Runtime/Styling/Properties/LayoutProperties.cs @@ -59,8 +59,8 @@ public static class LayoutProperties public static readonly LayoutProperty BorderEndWidth = new LayoutProperty("BorderEndWidth", true, converter: AllConverters.LengthConverter); public static readonly LayoutProperty Order = new LayoutProperty("order", true); - public static readonly LayoutProperty RowGap = new LayoutProperty("RowGap", true, 0f, converter: AllConverters.LengthConverter); - public static readonly LayoutProperty ColumnGap = new LayoutProperty("ColumnGap", true, 0f, converter: AllConverters.LengthConverter); + public static readonly LayoutProperty RowGap = new LayoutProperty("RowGap", true); + public static readonly LayoutProperty ColumnGap = new LayoutProperty("ColumnGap", true); public static Dictionary PropertyMap = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { diff --git a/Runtime/Styling/Shorthands/AllShorthands.cs b/Runtime/Styling/Shorthands/AllShorthands.cs index f25b015a..07cbec29 100644 --- a/Runtime/Styling/Shorthands/AllShorthands.cs +++ b/Runtime/Styling/Shorthands/AllShorthands.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using Yoga; namespace ReactUnity.Styling.Shorthands { @@ -35,7 +36,8 @@ internal static class AllShorthands internal static readonly StyleShorthand Animation = new AnimationShorthand("animation"); internal static readonly StyleShorthand Audio = new AudioShorthand("audio"); internal static readonly StyleShorthand Transform = new TransformShorthand("transform"); - internal static readonly StyleShorthand Gap = new XYShorthand("gap", LayoutProperties.RowGap, LayoutProperties.ColumnGap); + internal static readonly StyleShorthand Gap = new XYShorthand("gap", LayoutProperties.RowGap, LayoutProperties.ColumnGap); + internal static readonly Dictionary Map = new Dictionary(StringComparer.InvariantCultureIgnoreCase) { { "all", All }, diff --git a/Runtime/Yoga/Native.cs b/Runtime/Yoga/Native.cs index 4f97ac27..8bb3a5fa 100644 --- a/Runtime/Yoga/Native.cs +++ b/Runtime/Yoga/Native.cs @@ -317,6 +317,9 @@ internal static class Native [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] public static extern void YGNodeStyleSetGap(YGNodeHandle node, YogaGutter gutter, float gapLength); + [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] + public static extern void YGNodeStyleSetGapPercent(YGNodeHandle node, YogaGutter gutter, float percent); + [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] public static extern float YGNodeStyleGetGap(YGNodeHandle node, YogaGutter gutter); diff --git a/Runtime/Yoga/YogaNode.cs b/Runtime/Yoga/YogaNode.cs index 8f846f7b..5006b496 100644 --- a/Runtime/Yoga/YogaNode.cs +++ b/Runtime/Yoga/YogaNode.cs @@ -296,22 +296,31 @@ public float AspectRatio } } - public float Gap + public YogaValue Gap { - get => Native.YGNodeStyleGetGap(_ygNode, YogaGutter.All); - set => Native.YGNodeStyleSetGap(_ygNode, YogaGutter.All, value); + set + { + if (value.Unit == YogaUnit.Percent) Native.YGNodeStyleSetGapPercent(_ygNode, YogaGutter.All, value.Value); + else Native.YGNodeStyleSetGap(_ygNode, YogaGutter.All, value.Value); + } } - public float ColumnGap + public YogaValue ColumnGap { - get => Native.YGNodeStyleGetGap(_ygNode, YogaGutter.Column); - set => Native.YGNodeStyleSetGap(_ygNode, YogaGutter.Column, value); + set + { + if (value.Unit == YogaUnit.Percent) Native.YGNodeStyleSetGapPercent(_ygNode, YogaGutter.Column, value.Value); + else Native.YGNodeStyleSetGap(_ygNode, YogaGutter.Column, value.Value); + } } - public float RowGap + public YogaValue RowGap { - get => Native.YGNodeStyleGetGap(_ygNode, YogaGutter.Row); - set => Native.YGNodeStyleSetGap(_ygNode, YogaGutter.Row, value); + set + { + if (value.Unit == YogaUnit.Percent) Native.YGNodeStyleSetGapPercent(_ygNode, YogaGutter.Row, value.Value); + else Native.YGNodeStyleSetGap(_ygNode, YogaGutter.Row, value.Value); + } } public float LayoutLeft => Native.YGNodeLayoutGetLeft(_ygNode);