Beispiel #1
0
        public void CopyFrom(RectStyle other)
        {
            color = other.color;

            enableGradient = other.enableGradient;
            gradientAngle  = other.gradientAngle;

            gradient?.Clear();

            if (other.gradient != null)
            {
                if (gradient == null)
                {
                    gradient = new Gradient();
                }

                foreach (var keyPoint in other.gradient)
                {
                    gradient.Add(keyPoint);
                }
            }

            borderMode  = other.borderMode;
            borderColor = other.borderColor;
            borderWidth = other.borderWidth;

            cornerRadius = other.cornerRadius;

            enableBoxShadow     = other.enableBoxShadow;
            boxShadowColor      = other.boxShadowColor;
            boxShadowBlurRadius = other.boxShadowBlurRadius;
            boxShadowOffset     = other.boxShadowOffset;
            boxShadowInner      = other.boxShadowInner;
        }
Beispiel #2
0
 public void ClearGrad()
 {
     Gradient.Clear();
 }
Beispiel #3
0
        public bool SetCss(string css)
        {
            if (css == null)
            {
                return(true);
            }

            // Get rid of comments
            css = _sCssComment.Replace(css, match => new string(match.Value
                                                                .Select(x => char.IsWhiteSpace(x) ? x : ' ')
                                                                .ToArray()));

            var newColor          = color;
            var newEnableGradient = enableGradient;
            var newGradient       = new Gradient();

            if (gradient != null)
            {
                foreach (var keyPoint in gradient)
                {
                    newGradient.Add(keyPoint);
                }
            }

            var newGradientAngle = gradientAngle;

            var newBorderMode  = borderMode;
            var newBorderColor = borderColor;
            var newBorderWidth = borderWidth;

            var newCornerRadius = cornerRadius;

            var valid      = true;
            var hasOpacity = false;

            foreach (Match match in _sCssStatement.Matches(css))
            {
                var command = match.Groups["command"].Value;
                var value   = match.Groups["value"].Value;

                switch (command)
                {
                case "background":
                {
                    var colorMatch = _sCssColor.Match(value);
                    if (colorMatch.Success && colorMatch.Index == 0)
                    {
                        newColor          = ParseColor(colorMatch, hasOpacity ? newColor.a : 1f);
                        newEnableGradient = false;
                        break;
                    }

                    var gradientMatch = _sCssLinearGradient.Match(value);
                    if (gradientMatch.Success && gradientMatch.Index == 0)
                    {
                        newColor          = Color.white;
                        newGradientAngle  = float.Parse(gradientMatch.Groups["angle"].Value);
                        newEnableGradient = true;

                        newGradient.Clear();

                        foreach (Capture capture in gradientMatch.Groups["point"].Captures)
                        {
                            var pointMatch = _sCssGradientPoint.Match(capture.Value);

                            newGradient.Add(new GradientKeyPoint
                                {
                                    color    = ParseColor(pointMatch.Groups["color"].Value),
                                    progress = pointMatch.Groups["percent"].Success
                                        ? float.Parse(pointMatch.Groups["percent"].Value) / 100f : 0f
                                });
                        }

                        break;
                    }

                    valid = false;
                    break;
                }

                case "opacity":
                {
                    float opacity;
                    if (float.TryParse(value, out opacity))
                    {
                        newColor.a = opacity;
                        hasOpacity = true;
                        break;
                    }

                    valid = false;
                    break;
                }

                case "border":
                {
                    var borderMatch = _sCssBorder.Match(value);
                    if (borderMatch.Success && borderMatch.Index == 0)
                    {
                        newBorderMode  = BorderMode.All;
                        newBorderColor = ParseColor(borderMatch.Groups["color"].Value);
                        newBorderWidth = ParsePixels(borderMatch.Groups["width"].Value);

                        valid = true;
                        break;
                    }

                    valid = false;
                    break;
                }

                case "border-radius":
                {
                    var pixelsMatch = _sCssPixels.Match(value);
                    if (pixelsMatch.Success && pixelsMatch.Index == 0)
                    {
                        newCornerRadius = ParsePixels(pixelsMatch.Value);
                        valid           = true;
                        break;
                    }

                    valid = false;
                    break;
                }
                }
            }

            if (valid)
            {
                color          = newColor;
                enableGradient = newEnableGradient;
                gradientAngle  = newGradientAngle;
                borderMode     = newBorderMode;
                borderColor    = newBorderColor;
                borderWidth    = newBorderWidth;
                cornerRadius   = newCornerRadius;
                gradient       = newGradient;
            }

            return(valid);
        }
Beispiel #4
0
        public void CopyFromLerped(RectStyle a, RectStyle b, float t)
        {
            color = Color.Lerp(a.color, b.color, t);

            if ((a.gradient?.keyPointCount ?? 0) != (b.gradient?.keyPointCount ?? 0))
            {
                // TODO
            }
            else if (a.gradient != null && b.gradient != null && a.gradient.keyPointCount > 0)
            {
                gradientAngle = Mathf.LerpAngle(a.gradientAngle, b.gradientAngle, t);

                if (gradient == null)
                {
                    gradient = new Gradient();
                }
                else
                {
                    gradient.Clear();
                }

                for (var i = 0; i < a.gradient.keyPointCount; ++i)
                {
                    gradient.Add(new GradientKeyPoint
                    {
                        color    = Color.Lerp(a.gradient[i].color, b.gradient[i].color, t),
                        progress = Mathf.Lerp(a.gradient[i].progress, b.gradient[i].progress, t)
                    });
                }
            }

            enableGradient = a.enableGradient || b.enableGradient;
            borderMode     = a.borderMode | b.borderMode;
            borderColor    = a.HasBorder != b.HasBorder
                ? a.HasBorder
                    ? a.borderColor
                    : b.borderColor
                : Color.Lerp(a.borderColor, b.borderColor, t);
            borderWidth = Mathf.Lerp(a.HasBorder ? a.borderWidth : 0f, b.HasBorder ? b.borderWidth : 0f, t);

            cornerRadius = Mathf.Lerp(a.cornerRadius, b.cornerRadius, t);

            var boxShadowColorA = a.enableBoxShadow ? a.boxShadowColor : b.boxShadowColor;
            var boxShadowColorB = b.enableBoxShadow ? b.boxShadowColor : a.boxShadowColor;

            if (!a.enableBoxShadow)
            {
                boxShadowColorA.a = 0f;
            }
            if (!b.enableBoxShadow)
            {
                boxShadowColorB.a = 0f;
            }

            enableBoxShadow     = a.enableBoxShadow || b.enableBoxShadow;
            boxShadowColor      = Color.Lerp(boxShadowColorA, boxShadowColorB, t);
            boxShadowBlurRadius = Mathf.Lerp(
                a.enableBoxShadow ? a.boxShadowBlurRadius : b.boxShadowBlurRadius,
                b.enableBoxShadow ? b.boxShadowBlurRadius : a.boxShadowBlurRadius, t);
            boxShadowOffset = Vector2.Lerp(
                a.enableBoxShadow ? a.boxShadowOffset : b.boxShadowOffset,
                b.enableBoxShadow ? b.boxShadowOffset : a.boxShadowOffset, t);
            boxShadowInner = Mathf.Lerp(
                a.enableBoxShadow ? a.boxShadowInner : b.boxShadowInner,
                b.enableBoxShadow ? b.boxShadowInner : a.boxShadowInner, t);
        }