private void OnTouch(object sender, SKTouchEventArgs e)
        {
            SKPoint absoluteTouchLocation = new SKPoint((int)Math.Floor(e.Location.X), (int)Math.Floor(e.Location.Y));

            SKPoint aspectTouchLocation = anArtBoard.MatrixToAspectCoordinates().MapPoint(absoluteTouchLocation);

            switch (e.ActionType)
            {
            case SKTouchAction.Pressed:
                // the user added a finger
                aBezierHandle = new BezierHandle(aspectTouchLocation);
                bezierHandlePreviewCanvasView.InvalidateSurface();
                break;

            case SKTouchAction.Moved:
                // the user moved a finger
                if (e.InContact)
                {
                    aBezierHandle.MouseControlPoint = aspectTouchLocation;
                    if (aBezierHandle.Magnitude <= bezierHandleThresholdSlider.Value)
                    {
                        aBezierHandle.MouseControlPoint = aBezierHandle.AnchorPoint;
                    }
                }
                bezierHandlePreviewCanvasView.InvalidateSurface();
                break;

            case SKTouchAction.Released:
                // the user removed a finger
                aBezierHandle.MouseControlPoint = aspectTouchLocation;
                if (aBezierHandle.Magnitude <= bezierHandleThresholdSlider.Value)
                {
                    aBezierHandle.MouseControlPoint = aBezierHandle.AnchorPoint;
                }
                bezierHandlePreviewCanvasView.InvalidateSurface();
                break;

            case SKTouchAction.Cancelled:
                // the user removed a finger
                aBezierHandle = null;
                bezierHandlePreviewCanvasView.InvalidateSurface();
                break;
            }

            // the location (in pixels) of the finger on the screen
            //var pos = e.Location;

            // set Handled to true if we handled the event
            // if we don't, then parent views may also respond
            e.Handled = true;
        }
        private void OnPaintCanvas(object sender, SKPaintSurfaceEventArgs e)
        {
            SKImageInfo info    = e.Info;
            SKSurface   surface = e.Surface;
            SKCanvas    canvas  = surface.Canvas;

            int surfaceWidth  = info.Width;
            int surfaceHeight = info.Height;

            SKPoint surfaceSize = new SKPoint(surfaceWidth, surfaceHeight);


            //絵画開始
            //背景色を塗る
            canvas.Clear(PaintColors.OutOfRangeBackground.ToSKColor());

            //アートボードの絵画
            anArtBoard.SizeRecalculation(surfaceSize);

            using (SKPaint paint = new SKPaint())
            {
                paint.Style = SKPaintStyle.Fill;
                paint.Color = PaintColors.ArtBoardBackground.ToSKColor();
                canvas.DrawRect(anArtBoard.AbsoluteRect, paint);
            }


            radius = Math.Min(anArtBoard.Size.X, anArtBoard.Size.Y) / 2.0f * 0.7f;



            anArtBoard.InProgressPath.StrokeColor = PaintColors.Black;
            anArtBoard.InProgressPath.StrokeSize  = 5;
            anArtBoard.InProgressPath.StrokeJoin  = SKStrokeJoin.Round;
            anArtBoard.InProgressPath.StrokeCap   = SKStrokeCap.Round;
            anArtBoard.InProgressPath.Screentone  = PaintPatterns.CheckeredPattern(PaintColors.Blue.ToSKColor());

            anArtBoard.BezierHandleList.Clear();
            anArtBoard.BezierHandleList.Add(
                new BezierHandle(
                    anArtBoard.AbsolutePosition + new SKPoint(anArtBoard.Size.X / 2, anArtBoard.Size.Y / 2 - radius),
                    anArtBoard.AbsolutePosition + new SKPoint(anArtBoard.Size.X / 2 + radius * controlPointCoefficient, anArtBoard.Size.Y / 2 - radius)));
            anArtBoard.BezierHandleList.Add(
                new BezierHandle(
                    anArtBoard.AbsolutePosition + new SKPoint(anArtBoard.Size.X / 2 + radius, anArtBoard.Size.Y / 2),
                    anArtBoard.AbsolutePosition + new SKPoint(anArtBoard.Size.X / 2 + radius, anArtBoard.Size.Y / 2 + radius * controlPointCoefficient)));
            anArtBoard.BezierHandleList.Add(
                new BezierHandle(
                    anArtBoard.AbsolutePosition + new SKPoint(anArtBoard.Size.X / 2, anArtBoard.Size.Y / 2 + radius),
                    anArtBoard.AbsolutePosition + new SKPoint(anArtBoard.Size.X / 2 - radius * controlPointCoefficient, anArtBoard.Size.Y / 2 + radius)));
            anArtBoard.BezierHandleList.Add(
                new BezierHandle(
                    anArtBoard.AbsolutePosition + new SKPoint(anArtBoard.Size.X / 2 - radius, anArtBoard.Size.Y / 2),
                    anArtBoard.AbsolutePosition + new SKPoint(anArtBoard.Size.X / 2 - radius, anArtBoard.Size.Y / 2 - radius * controlPointCoefficient)));


            //絶対座標からアスペクト座標に変換
            foreach (var bezierHandle in anArtBoard.BezierHandleList)
            {
                bezierHandle.AnchorPoint       = anArtBoard.MatrixToAspectCoordinates().MapPoint(bezierHandle.AnchorPoint);
                bezierHandle.MouseControlPoint = anArtBoard.MatrixToAspectCoordinates().MapPoint(bezierHandle.MouseControlPoint);
            }


            //暫定Path絵画
            if (anArtBoard.BezierHandleList.Count >= 2)
            {
                anArtBoard.InProgressPath.Reset();

                //bezierHandleList -> inProgressPath
                anArtBoard.GenerateBezierPath(pathPreview);
                anArtBoard.InProgressPath.Transform(this.anArtBoard.InverseMatrixToAbsoluteCoordinates());
                canvas.DrawPath(anArtBoard.InProgressPath, anArtBoard.InProgressPath.GetFillPaint());
                canvas.DrawPath(anArtBoard.InProgressPath, anArtBoard.InProgressPath.GetStrokePaint());
            }



            //コントロールポイントを絵画
            SKPaint aBezierHandlePaint = new SKPaint()
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Stroke,
                Color       = PaintColors.Lasso.ToSKColor(),
                StrokeWidth = 2,
                StrokeJoin  = SKStrokeJoin.Round,
                StrokeCap   = SKStrokeCap.Round
            };

            SKPaint aBezierHandleAnchorPaint = new SKPaint()
            {
                IsAntialias = true,
                Style       = SKPaintStyle.Fill,
                Color       = PaintColors.Lasso.ToSKColor()
            };



            using (SKPath path = new SKPath())
            {
                foreach (var aBezierHandle in anArtBoard.BezierHandleList)
                {
                    SKPoint anchorPoint          = anArtBoard.InverseMatrixToAbsoluteCoordinates().MapPoint(aBezierHandle.AnchorPoint);
                    SKPoint mouseControlPoint    = anArtBoard.InverseMatrixToAbsoluteCoordinates().MapPoint(aBezierHandle.MouseControlPoint);
                    SKPoint theOtherControlPoint = anArtBoard.InverseMatrixToAbsoluteCoordinates().MapPoint(aBezierHandle.TheOtherControlPoint);


                    path.MoveTo(anchorPoint);
                    path.LineTo(mouseControlPoint);

                    canvas.DrawCircle(anchorPoint, 4, aBezierHandleAnchorPaint);


                    path.MoveTo(anchorPoint);
                    path.LineTo(theOtherControlPoint);
                }

                canvas.DrawPath(path, aBezierHandlePaint);
            }
        }
Пример #3
0
        //https://github.com/mono/SkiaSharp/releases/tag/v1.58.1
        private void OnTouch(object sender, SKTouchEventArgs e)
        {
            //絶対座標
            SKPoint absoluteTouchLocation = new SKPoint((int)Math.Floor(e.Location.X), (int)Math.Floor(e.Location.Y));
            //アスペクト座標に変換
            SKPoint aspectTouchLocation = anArtBoard.MatrixToAspectCoordinates().MapPoint(absoluteTouchLocation);

            // the ID of the finger that toucged the screen
            var pointerId = e.Id;



            switch (e.ActionType)
            {
            case SKTouchAction.Pressed:
                // the user added a finger
                inProgressPathUndoButton.IsEnabled = false;
                openPathButton.IsEnabled           = false;
                closePathButton.IsEnabled          = false;

                anArtBoard.BezierHandleList.Add(new BezierHandle(aspectTouchLocation));
                canvasView.InvalidateSurface();
                break;

            case SKTouchAction.Moved:
                // the user moved a finger
                if (e.InContact)
                {
                    if (anArtBoard.BezierHandleList.Count == 0)
                    {
                        break;
                    }

                    inProgressPathUndoButton.IsEnabled = false;
                    openPathButton.IsEnabled           = false;
                    closePathButton.IsEnabled          = false;

                    anArtBoard.BezierHandleList.Last().MouseControlPoint = aspectTouchLocation;
                    if (anArtBoard.BezierHandleList.Last().Magnitude <= SaveProperties.BezierHandleThreshold)
                    {
                        anArtBoard.BezierHandleList.Last().MouseControlPoint = anArtBoard.BezierHandleList.Last().AnchorPoint;
                    }
                }

                canvasView.InvalidateSurface();
                break;

            case SKTouchAction.Released:
                // the user removed a finger
                if (anArtBoard.BezierHandleList.Count == 0)
                {
                    break;
                }

                inProgressPathUndoButton.IsEnabled = true;
                openPathButton.IsEnabled           = true;
                closePathButton.IsEnabled          = true;

                anArtBoard.BezierHandleList.Last().MouseControlPoint = aspectTouchLocation;
                if (anArtBoard.BezierHandleList.Last().Magnitude <= SaveProperties.BezierHandleThreshold)
                {
                    anArtBoard.BezierHandleList.Last().MouseControlPoint = anArtBoard.BezierHandleList.Last().AnchorPoint;
                }

                canvasView.InvalidateSurface();
                break;

            case SKTouchAction.Cancelled:
                // the user removed a finger
                inProgressPathUndoButton.IsEnabled = true;
                openPathButton.IsEnabled           = true;
                closePathButton.IsEnabled          = true;

                anArtBoard.BezierHandleList.RemoveAt(anArtBoard.BezierHandleList.Count - 1);
                canvasView.InvalidateSurface();
                break;
            }

            // the location (in pixels) of the finger on the screen
            //var pos = e.Location;

            // set Handled to true if we handled the event
            // if we don't, then parent views may also respond
            e.Handled = true;
        }