void _paintSide(Canvas canvas, Size size, _GlowController controller, AxisDirection axisDirection,
                        GrowthDirection growthDirection)
        {
            if (controller == null)
            {
                return;
            }

            switch (GrowthDirectionUtils.applyGrowthDirectionToAxisDirection(axisDirection, growthDirection))
            {
            case AxisDirection.up:
                controller.paint(canvas, size);
                break;

            case AxisDirection.down:
                canvas.save();
                canvas.translate(0.0f, size.height);
                canvas.scale(1.0f, -1.0f);
                controller.paint(canvas, size);
                canvas.restore();
                break;

            case AxisDirection.left:
                canvas.save();
                canvas.rotate(piOver2);
                canvas.scale(1.0f, -1.0f);
                controller.paint(canvas, new Size(size.height, size.width));
                canvas.restore();
                break;

            case AxisDirection.right:
                canvas.save();
                canvas.translate(size.width, 0.0f);
                canvas.rotate(piOver2);
                controller.paint(canvas, new Size(size.height, size.width));
                canvas.restore();
                break;
            }
        }
Beispiel #2
0
        public override void paint(Canvas canvas, Size size)
        {
            canvas.scale(scale ?? 1.0f, scale ?? 1.0f);
            if (shouldMirror == true)
            {
                canvas.rotate(Mathf.PI);
                canvas.translate(-size.width, -size.height);
            }

            float clampedProgress = progress.value.clamp(0.0f, 1.0f);

            foreach (_PathFrames path in paths)
            {
                path.paint(canvas, color, uiPathFactory, clampedProgress);
            }
        }
        public void paint(Canvas canvas, Size size)
        {
            if (_glowOpacity.value == 0.0f)
            {
                return;
            }

            float  baseGlowScale = size.width > size.height ? size.height / size.width : 1.0f;
            float  radius        = size.width * 3.0f / 2.0f;
            float  height        = Mathf.Min(size.height, size.width * _widthToHeightFactor);
            float  scaleY        = _glowSize.value * baseGlowScale;
            Rect   rect          = Rect.fromLTWH(0.0f, 0.0f, size.width, height);
            Offset center        = new Offset((size.width / 2.0f) * (0.5f + _displacement), height - radius);
            Paint  paint         = new Paint();

            paint.color = color.withOpacity(_glowOpacity.value);
            canvas.save();
            canvas.scale(1.0f, scaleY);
            canvas.clipRect(rect);
            canvas.drawCircle(center, radius, paint);
            canvas.restore();
        }
Beispiel #4
0
        public static void paintImage(
            Canvas canvas               = null,
            Rect rect                   = null,
            Image image                 = null,
            float scale                 = 1.0f,
            ColorFilter colorFilter     = null,
            BoxFit?fit                  = null,
            Alignment alignment         = null,
            Rect centerSlice            = null,
            ImageRepeat repeat          = ImageRepeat.noRepeat,
            bool flipHorizontally       = false,
            bool invertColors           = false,
            FilterQuality filterQuality = FilterQuality.low
            )
        {
            D.assert(canvas != null);
            D.assert(rect != null);
            D.assert(image != null);
            alignment = alignment ?? Alignment.center;

            if (rect.isEmpty)
            {
                return;
            }

            Size   outputSize  = rect.size;
            Size   inputSize   = new Size(image.width, image.height);
            Offset sliceBorder = null;

            if (centerSlice != null)
            {
                sliceBorder = new Offset(
                    centerSlice.left + inputSize.width - centerSlice.right,
                    centerSlice.top + inputSize.height - centerSlice.bottom
                    );
                outputSize -= sliceBorder;
                inputSize  -= sliceBorder;
            }

            fit = fit ?? (centerSlice == null ? BoxFit.scaleDown : BoxFit.fill);
            D.assert(centerSlice == null || (fit != BoxFit.none && fit != BoxFit.cover),
                     () => $"centerSlice was used with a BoxFit {fit} that is not supported.");
            FittedSizes fittedSizes     = FittedSizes.applyBoxFit(fit.Value, inputSize / scale, outputSize);
            Size        sourceSize      = fittedSizes.source * scale;
            Size        destinationSize = fittedSizes.destination;

            if (centerSlice != null)
            {
                outputSize      += sliceBorder;
                destinationSize += sliceBorder;
                D.assert(sourceSize == inputSize,
                         () =>
                         $"centerSlice was used with a BoxFit {fit} that does not guarantee that the image is fully visible.");
            }

            if (repeat != ImageRepeat.noRepeat && destinationSize == outputSize)
            {
                repeat = ImageRepeat.noRepeat;
            }

            Paint paint = new Paint();

            if (colorFilter != null)
            {
                paint.colorFilter = colorFilter;
            }

            if (sourceSize != destinationSize)
            {
                paint.filterQuality = filterQuality;
            }

            paint.invertColors = invertColors;

            float  halfWidthDelta  = (outputSize.width - destinationSize.width) / 2.0f;
            float  halfHeightDelta = (outputSize.height - destinationSize.height) / 2.0f;
            float  dx = halfWidthDelta + (flipHorizontally ? -alignment.x : alignment.x) * halfWidthDelta;
            float  dy = halfHeightDelta + alignment.y * halfHeightDelta;
            Offset destinationPosition = rect.topLeft.translate(dx, dy);
            Rect   destinationRect     = destinationPosition & destinationSize;
            bool   needSave            = repeat != ImageRepeat.noRepeat || flipHorizontally;

            if (needSave)
            {
                canvas.save();
            }

            if (flipHorizontally)
            {
                float dxInside = -(rect.left + rect.width / 2.0f);
                canvas.translate(-dxInside, 0.0f);
                canvas.scale(-1.0f, 1.0f);
                canvas.translate(dxInside, 0.0f);
            }

            if (repeat != ImageRepeat.noRepeat)
            {
                canvas.clipRect(rect);
            }

            if (centerSlice == null)
            {
                Rect sourceRect = alignment.inscribe(
                    sourceSize, Offset.zero & inputSize
                    );
                if (repeat == ImageRepeat.noRepeat)
                {
                    canvas.drawImageRect(image, sourceRect, destinationRect, paint);
                }
                else
                {
                    foreach (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
                    {
                        canvas.drawImageRect(image, sourceRect, tileRect, paint);
                    }
                }
            }
            else
            {
                if (repeat == ImageRepeat.noRepeat)
                {
                    canvas.drawImageNine(image, centerSlice, destinationRect, paint);
                }
                else
                {
                    foreach (Rect tileRect in _generateImageTileRects(rect, destinationRect, repeat))
                    {
                        canvas.drawImageNine(image, centerSlice, tileRect, paint);
                    }
                }
            }

            if (needSave)
            {
                canvas.restore();
            }
        }
        void _drawValueIndicator(
            RenderBox parentBox,
            Canvas canvas,
            Offset center,
            Paint paint,
            float scale,
            TextPainter labelPainter
            )
        {
            canvas.save();
            canvas.translate(center.dx, center.dy);

            float textScaleFactor = labelPainter.height / _labelTextDesignSize;
            float overallScale    = scale * textScaleFactor;

            canvas.scale(overallScale, overallScale);
            float inverseTextScale = textScaleFactor != 0 ? 1.0f / textScaleFactor : 0.0f;
            float labelHalfWidth   = labelPainter.width / 2.0f;

            float halfWidthNeeded = Mathf.Max(
                0.0f,
                inverseTextScale * labelHalfWidth - (_topLobeRadius - _labelPadding)
                );

            float shift = this._getIdealOffset(parentBox, halfWidthNeeded, overallScale, center);
            float leftWidthNeeded;
            float rightWidthNeeded;

            if (shift < 0.0)
            {
                shift = Mathf.Max(shift, -halfWidthNeeded);
            }
            else
            {
                shift = Mathf.Min(shift, halfWidthNeeded);
            }

            rightWidthNeeded = halfWidthNeeded + shift;
            leftWidthNeeded  = halfWidthNeeded - shift;

            Path   path          = new Path();
            Offset bottomLobeEnd = this._addBottomLobe(path);

            float neckTriangleBase = _topNeckRadius - bottomLobeEnd.dx;

            float leftAmount  = Mathf.Max(0.0f, Mathf.Min(1.0f, leftWidthNeeded / neckTriangleBase));
            float rightAmount = Mathf.Max(0.0f, Mathf.Min(1.0f, rightWidthNeeded / neckTriangleBase));

            float  leftTheta      = (1.0f - leftAmount) * _thirtyDegrees;
            float  rightTheta     = (1.0f - rightAmount) * _thirtyDegrees;
            Offset neckLeftCenter = new Offset(
                -neckTriangleBase,
                _topLobeCenter.dy + Mathf.Cos(leftTheta) * _neckTriangleHypotenuse
                );
            Offset neckRightCenter = new Offset(
                neckTriangleBase,
                _topLobeCenter.dy + Mathf.Cos(rightTheta) * _neckTriangleHypotenuse
                );

            float leftNeckArcAngle  = _ninetyDegrees - leftTheta;
            float rightNeckArcAngle = Mathf.PI + _ninetyDegrees - rightTheta;

            float  neckStretchBaseline = bottomLobeEnd.dy - Mathf.Max(neckLeftCenter.dy, neckRightCenter.dy);
            float  t           = Mathf.Pow(inverseTextScale, 3.0f);
            float  stretch     = (neckStretchBaseline * t).clamp(0.0f, 10.0f * neckStretchBaseline);
            Offset neckStretch = new Offset(0.0f, neckStretchBaseline - stretch);

            D.assert(() => {
                if (!_debuggingLabelLocation)
                {
                    return(true);
                }

#pragma warning disable 0162
                Offset leftCenter  = _topLobeCenter - new Offset(leftWidthNeeded, 0.0f) + neckStretch;
                Offset rightCenter = _topLobeCenter + new Offset(rightWidthNeeded, 0.0f) + neckStretch;
                Rect valueRect     = Rect.fromLTRB(
                    leftCenter.dx - _topLobeRadius,
                    leftCenter.dy - _topLobeRadius,
                    rightCenter.dx + _topLobeRadius,
                    rightCenter.dy + _topLobeRadius
                    );
                Paint outlinePaint       = new Paint();
                outlinePaint.color       = new Color(0xffff0000);
                outlinePaint.style       = PaintingStyle.stroke;
                outlinePaint.strokeWidth = 1.0f;
                canvas.drawRect(valueRect, outlinePaint);
                return(true);

#pragma warning restore 0162
            });

            _addArc(
                path,
                neckLeftCenter + neckStretch,
                _topNeckRadius,
                0.0f,
                -leftNeckArcAngle
                );
            _addArc(
                path,
                _topLobeCenter - new Offset(leftWidthNeeded, 0.0f) + neckStretch,
                _topLobeRadius,
                _ninetyDegrees + leftTheta,
                _twoSeventyDegrees
                );
            _addArc(
                path,
                _topLobeCenter + new Offset(rightWidthNeeded, 0.0f) + neckStretch,
                _topLobeRadius,
                _twoSeventyDegrees,
                _twoSeventyDegrees + Mathf.PI - rightTheta
                );
            _addArc(
                path,
                neckRightCenter + neckStretch,
                _topNeckRadius,
                rightNeckArcAngle,
                Mathf.PI
                );
            canvas.drawPath(path, paint);

            canvas.save();
            canvas.translate(shift, -_distanceBetweenTopBottomCenters + neckStretch.dy);
            canvas.scale(inverseTextScale, inverseTextScale);
            labelPainter.paint(canvas, Offset.zero - new Offset(labelHalfWidth, labelPainter.height / 2.0f));
            canvas.restore();
            canvas.restore();
        }