Exemple #1
0
 public void TryParseTest()
 {
     Assert.IsTrue(Relative.TryParse("50%", out float value, out bool relative));
     Assert.IsTrue(relative);
     Assert.AreEqual(0.5f, value);
 }
Exemple #2
0
        public static bool TryParse(string input, out Sides sides)
        {
            // If the given string is empty, return with an empty padding.
            if (string.IsNullOrWhiteSpace(input))
            {
                sides = new Sides(0, SideMask.None); return(false);
            }

            // Split the input into sides.
            string[] paddingSides = input.Split(',');

            // Keep track of what sides should be relative.
            SideMask relativeSides = SideMask.None;

            // Parse the first value.
            if (!Relative.TryParse(paddingSides[0], out float top, out bool topRelative))
            {
                sides = new Sides(0, SideMask.None); return(false);
            }

            // If the first value is the only value, return a padding with all sides set to the value.
            if (paddingSides.Length == 1)
            {
                sides = new Sides(top, topRelative ? SideMask.All : SideMask.None); return(true);
            }

            // Parse the right side.
            if (!Relative.TryParse(paddingSides[1], out float right, out bool rightRelative))
            {
                sides = new Sides(0, SideMask.None); return(false);
            }

            // If there are 2 given sides, use them for the vertical and horizontal sides.
            if (paddingSides.Length == 2)
            {
                // Set the bitflag based on which sides are relative.
                if (rightRelative)
                {
                    relativeSides |= SideMask.Horizontal;
                }
                if (topRelative)
                {
                    relativeSides |= SideMask.Vertical;
                }

                // Return a relative padding with the parsed values and sides.
                sides = new Sides(right, top, relativeSides);
                return(true);
            }

            // Parse the bottom side.
            if (!Relative.TryParse(paddingSides[2], out float bottom, out bool bottomRelative))
            {
                sides = new Sides(0, SideMask.None); return(false);
            }

            // If there are 3 given sides, use them for the top, bottom, and horizontal sides.
            if (paddingSides.Length == 3)
            {
                // Set the bitflag based on which sides are relative.
                if (rightRelative)
                {
                    relativeSides |= SideMask.Horizontal;
                }
                if (topRelative)
                {
                    relativeSides |= SideMask.Top;
                }
                if (bottomRelative)
                {
                    relativeSides |= SideMask.Bottom;
                }

                // Return a new relative padding with the parsed values and sides.
                sides = new Sides(top, right, bottom, relativeSides);
                return(true);
            }

            // Parse the left side.
            if (!Relative.TryParse(paddingSides[3], out float left, out bool leftRelative))
            {
                sides = new Sides(0, SideMask.None); return(false);
            }

            // Set the bitflag based on which sides are relative.
            if (rightRelative)
            {
                relativeSides |= SideMask.Right;
            }
            if (leftRelative)
            {
                relativeSides |= SideMask.Left;
            }
            if (topRelative)
            {
                relativeSides |= SideMask.Top;
            }
            if (bottomRelative)
            {
                relativeSides |= SideMask.Bottom;
            }

            // Return a relative padding with all sides set.
            sides = new Sides(top, right, bottom, left, relativeSides);
            return(true);
        }