private void DrawEllipse(SKCanvas canvas, EllipseDrawingFigure ellipse)
 {
     _paint.Color = new SKColor(0, 0, 0);
     canvas.DrawOval(ellipse.Rectangle.Margin(2), _paint);
     _paint.Color = new SKColor(255, 255, 255);
     canvas.DrawOval(ellipse.Rectangle, _paint);
     _paint.Color     = new SKColor(0, 0, 0);
     _paint.TextAlign = SKTextAlign.Center;
     _paint.TextSize  = GridWidth;
     canvas.DrawText(ellipse.Text, ellipse.Center, _paint);
 }
        private EllipseDrawingFigure[] CreateEllipses(SKPoint pt1, SKPoint pt2)
        {
            var left   = Math.Min(pt1.X, pt2.X);
            var top    = Math.Min(pt1.Y, pt2.Y);
            var right  = Math.Max(pt1.X, pt2.X);
            var bottom = Math.Max(pt1.Y, pt2.Y);

            var ellipses = new List <EllipseDrawingFigure>();

            for (var x = left; x <= right; x += EllipseWidth)
            {
                for (var y = top; y <= bottom; y += EllipseWidth)
                {
                    var ellipse = new EllipseDrawingFigure
                    {
                        Center = new SKPoint(x, y),
                        Width  = EllipseWidth,
                        Text   = (_completedEllipses.Count + ellipses.Count + 1).ToString(),
                    };
                    ellipses.Add(ellipse);
                }
            }
            return(ellipses.ToArray());
        }
        void OnTouchEffectAction(object sender, TouchActionEventArgs args)
        {
            switch (args.Type)
            {
            case TouchActionType.Pressed:
                bool isDragOperation = false;

                // Loop through the completed figures
                foreach (EllipseDrawingFigure fig in completedFigures.Reverse <EllipseDrawingFigure>())
                {
                    // Check if the finger is touching one of the ellipses
                    if (fig.IsInEllipse(ConvertToPixel(args.Location)))
                    {
                        // Tentatively assume this is a dragging operation
                        isDragOperation = true;

                        // Loop through all the figures currently being dragged
                        foreach (EllipseDrawingFigure draggedFigure in draggingFigures.Values)
                        {
                            // If there's a match, we'll need to dig deeper
                            if (fig == draggedFigure)
                            {
                                isDragOperation = false;
                                break;
                            }
                        }

                        if (isDragOperation)
                        {
                            fig.LastFingerLocation = args.Location;
                            draggingFigures.Add(args.Id, fig);
                            break;
                        }
                    }
                }

                if (isDragOperation)
                {
                    // Move the dragged ellipse to the end of completedFigures so it's drawn on top
                    EllipseDrawingFigure fig = draggingFigures[args.Id];
                    completedFigures.Remove(fig);
                    completedFigures.Add(fig);
                }
                else     // start making a new ellipse
                {
                    // Random bytes for random color
                    byte[] buffer = new byte[4];
                    random.NextBytes(buffer);

                    EllipseDrawingFigure figure = new EllipseDrawingFigure
                    {
                        Color      = new SKColor(buffer[0], buffer[1], buffer[2], buffer[3]),
                        StartPoint = ConvertToPixel(args.Location),
                        EndPoint   = ConvertToPixel(args.Location)
                    };
                    inProgressFigures.Add(args.Id, figure);
                }
                canvasView.InvalidateSurface();
                break;

            case TouchActionType.Moved:
                if (draggingFigures.ContainsKey(args.Id))
                {
                    EllipseDrawingFigure figure = draggingFigures[args.Id];
                    SKRect rect = figure.Rectangle;
                    rect.Offset(ConvertToPixel(new Point(args.Location.X - figure.LastFingerLocation.X,
                                                         args.Location.Y - figure.LastFingerLocation.Y)));
                    figure.Rectangle          = rect;
                    figure.LastFingerLocation = args.Location;
                }
                else if (inProgressFigures.ContainsKey(args.Id))
                {
                    EllipseDrawingFigure figure = inProgressFigures[args.Id];
                    figure.EndPoint = ConvertToPixel(args.Location);
                }
                canvasView.InvalidateSurface();
                break;

            case TouchActionType.Released:
                if (draggingFigures.ContainsKey(args.Id))
                {
                    draggingFigures.Remove(args.Id);
                }
                else if (inProgressFigures.ContainsKey(args.Id))
                {
                    EllipseDrawingFigure figure = inProgressFigures[args.Id];
                    figure.EndPoint = ConvertToPixel(args.Location);
                    completedFigures.Add(figure);
                    inProgressFigures.Remove(args.Id);
                }
                canvasView.InvalidateSurface();
                break;

            case TouchActionType.Cancelled:
                if (draggingFigures.ContainsKey(args.Id))
                {
                    draggingFigures.Remove(args.Id);
                }
                if (inProgressFigures.ContainsKey(args.Id))
                {
                    inProgressFigures.Remove(args.Id);
                }
                canvasView.InvalidateSurface();
                break;
            }
        }