Exemplo n.º 1
0
        // Touch move event handler.
        /// <summary>
        /// 在move过程判断具体的手势(zoom,pan,rotate)
        /// </summary>
        /// <param name="drawingCanvas"></param>
        /// <param name="e"></param>
        public void TouchMoveHandler(DrawingCanvas drawingCanvas, TouchEventArgs e)
        {
            GraphicsPolyLine polyLine;

            if (_activeStrokes.TryGetValue(e.TouchDevice.Id, out polyLine))
            {
                TouchPoint touchPoint = e.GetTouchPoint(drawingCanvas);
                Point      p          = touchPoint.Position;
                polyLine.AddPoint(p);
                if (e.TouchDevice.Id == GestureData.FirstDeviceId)
                {
                    GestureData.FirstFingerCurPoint = p;
                }
                if (e.TouchDevice.Id == GestureData.SecondDeviceId)
                {
                    GestureData.SecFingerCurPoint = p;
                }
                //polyLine.AddToCanvas(drawingCanvas);
                //判断出具体手势,然后根据手势,在case中按照相应的比例绘画图像。
                if (GestureData.IsGesture)
                {
                    GestureId gestureId   = HelperFunctions.getGestureId(drawingCanvas, e);
                    bool      hasSelected = HelperFunctions.hasSelected(drawingCanvas);
                    System.Diagnostics.Debug.WriteLine("hasSelected:{0}", hasSelected);
                    switch (gestureId)
                    {
                    case GestureId.ZOOM:
                        if (hasSelected)
                        {
                            foreach (GraphicsBase b in drawingCanvas.GraphicsList)
                            {
                                if (b.IsOption)
                                {
                                    b.Zoom(GestureData.ZoomScale, GestureData.StartCenterPointInTwoFingers);
                                }
                            }
                        }
                        else
                        {
                            foreach (GraphicsBase b in drawingCanvas.GraphicsList)
                            {
                                b.Zoom(GestureData.ZoomScale, GestureData.StartCenterPointInTwoFingers);
                            }
                        }
                        break;

                    case GestureId.PAN:
                        if (hasSelected)
                        {
                            foreach (GraphicsBase b in drawingCanvas.GraphicsList)
                            {
                                if (b.IsOption)
                                {
                                    b.Move(GestureData.Panx, GestureData.Pany);
                                }
                            }
                        }
                        else
                        {
                            foreach (GraphicsBase b in drawingCanvas.GraphicsList)
                            {
                                b.Move(GestureData.Panx, GestureData.Pany);
                            }
                        }
                        break;

                    case GestureId.RORATE:
                        if (hasSelected)
                        {
                            foreach (GraphicsBase b in drawingCanvas.GraphicsList)
                            {
                                if (b.IsOption)
                                {
                                    b.Rotate(GestureData.RotateAngle, GestureData.StartCenterPointInTwoFingers);
                                }
                            }
                        }
                        else
                        {
                            foreach (GraphicsBase b in drawingCanvas.GraphicsList)
                            {
                                b.Rotate(GestureData.RotateAngle, GestureData.StartCenterPointInTwoFingers);
                            }
                        }
                        break;

                    case GestureId.ERASE:
                        break;

                    default:
                        break;
                    }
                }
            }
            System.Diagnostics.Debug.WriteLine("Touchmove");
        }
Exemplo n.º 2
0
        /// <summary>
        /// Handle mouse down.
        /// Start moving, resizing or group selection.
        /// </summary>
        public override void OnMouseDown(DrawingCanvas drawingCanvas, MouseButtonEventArgs e)
        {
            commandChangeState = null;
            wasMove            = false;


            Point point = e.GetPosition(drawingCanvas);

            selectMode = SelectionMode.None;

            GraphicsBase o;
            GraphicsBase movedObject = null;
            int          handleNumber;

            // Test for resizing (only if control is selected, cursor is on the handle)
            for (int i = drawingCanvas.GraphicsList.Count - 1; i >= 0; i--)
            {
                o = drawingCanvas[i];

                if (o.IsSelected)
                {
                    handleNumber = o.MakeHitTest(point);

                    if (handleNumber > 0)
                    {
                        selectMode = SelectionMode.Size;

                        // keep resized object in class member
                        resizedObject       = o;
                        resizedObjectHandle = handleNumber;

                        // Since we want to resize only one object, unselect all other objects
                        HelperFunctions.UnselectAll(drawingCanvas);
                        o.IsSelected = true;

                        commandChangeState = new CommandChangeState(drawingCanvas);

                        break;
                    }
                }
            }

            // Test for move (cursor is on the object)
            if (selectMode == SelectionMode.None)
            {
                for (int i = drawingCanvas.GraphicsList.Count - 1; i >= 0; i--)
                {
                    o = drawingCanvas[i];

                    if (o.MakeHitTest(point) == 0)
                    {
                        movedObject = o;
                        break;
                    }
                }

                if (movedObject != null)
                {
                    selectMode = SelectionMode.Move;

                    // Unselect all if Ctrl is not pressed and clicked object is not selected yet
                    if (Keyboard.Modifiers != ModifierKeys.Control && !movedObject.IsSelected)
                    {
                        HelperFunctions.UnselectAll(drawingCanvas);
                    }

                    // Select clicked object
                    movedObject.IsSelected = true;

                    // Set move cursor
                    drawingCanvas.Cursor = Cursors.SizeAll;

                    commandChangeState = new CommandChangeState(drawingCanvas);
                }
            }

            // Click on background
            if (selectMode == SelectionMode.None)
            {
                // Unselect all if Ctrl is not pressed
                if (Keyboard.Modifiers != ModifierKeys.Control)
                {
                    HelperFunctions.UnselectAll(drawingCanvas);
                }

                // Group selection. Create selection rectangle.
                GraphicsSelectionRectangle r = new GraphicsSelectionRectangle(
                    point.X, point.Y,
                    point.X + 1, point.Y + 1,
                    drawingCanvas.ActualScale);

                r.Clip = new RectangleGeometry(new Rect(0, 0, drawingCanvas.ActualWidth, drawingCanvas.ActualHeight));

                drawingCanvas.GraphicsList.Add(r);

                selectMode = SelectionMode.GroupSelection;
            }


            lastPoint = point;

            // Capture mouse until MouseUp event is received
            drawingCanvas.CaptureMouse();
        }
Exemplo n.º 3
0
        // Touch down event handler.
        /// <summary>
        /// 输入一个点,判定是否为手势,是则不需要在继续判断,手指离开时,将手势标识设置为false
        /// 如果不是则继续判断是否为手势.
        /// </summary>
        /// <param name="drawingCanvas"></param>
        /// <param name="e"></param>
        public void TouchDownHandler(DrawingCanvas drawingCanvas, TouchEventArgs e)
        {
            GestureData.Fingers++;//记录手指数
            GraphicsPolyLine polyLine;
            TouchPoint       touchPoint = e.GetTouchPoint(drawingCanvas);
            Point            p          = touchPoint.Position;

            if (GestureData.Fingers == 1)//记录第一个手指的基本信息,后后续手势判断使用
            {
                if (!GestureData.IsSignFoFingerOne)
                {
                    GestureData.IsTouch               = true;
                    GestureData.FirstDeviceId         = e.TouchDevice.Id;
                    GestureData.FirstFingerBeginPoint = p;
                    GestureData.FirstFingerCurPoint   = p;
                    GestureData.IsSignFoFingerOne     = true;
                }
            }

            HelperFunctions.IsGesture(drawingCanvas, e);//判断是否为手势

            //如果是手势,则将第一个手指画的几何型的Candraw属性置为false,使得后续不在画出来
            if (GestureData.IsGesture && GestureData.Fingers == 2 && _activeStrokes.TryGetValue(GestureData.FirstDeviceId, out polyLine))
            {
                polyLine.CanDraw = false;
            }

            if (_activeStrokes.TryGetValue(e.TouchDevice.Id, out polyLine))
            {
                FinishStroke(polyLine);
                return;
            }

            //当手指的个数大于2时,将第一个手指与第二个手指的canDraw属性置回true
            if (!GestureData.IsGesture && GestureData.Fingers > 2)
            {
                if (_activeStrokes.TryGetValue(GestureData.FirstDeviceId, out polyLine))
                {
                    polyLine.CanDraw = true;
                }
                if (_activeStrokes.TryGetValue(GestureData.SecondDeviceId, out polyLine))
                {
                    polyLine.CanDraw = true;
                }
            }

            // Create new stroke, add point and assign a color to it.
            GraphicsPolyLine newPolyLine = new GraphicsPolyLine(
                new Point[]
            {
                p,
                new Point(p.X + 1, p.Y + 1)
            },
                drawingCanvas.LineWidth,
                drawingCanvas.ObjectColor,
                drawingCanvas.ActualScale);

            newPolyLine.DeviceID = e.TouchDevice.Id;
            //是手势并且是第二个手指,将其画的几何的CanDraw置为false,不用画
            if (GestureData.IsGesture && GestureData.Fingers == 2)
            {
                if (!GestureData.IsSignForFingerTwo)
                {
                    GestureData.SecondDeviceId               = e.TouchDevice.Id;
                    GestureData.SecFingerBeginPoint          = p;
                    GestureData.StartDistInTwoFingers        = HelperFunctions.CalcDistanceSquare(GestureData.FirstFingerBeginPoint, GestureData.SecFingerBeginPoint);
                    GestureData.StartCenterPointInTwoFingers = HelperFunctions.getCenterPoint(GestureData.FirstFingerBeginPoint, GestureData.SecFingerBeginPoint);
                    GestureData.SecFingerCurPoint            = p;
                    newPolyLine.CanDraw            = false;
                    GestureData.IsSignForFingerTwo = true;
                    if (GestureData.FirstFingerBeginPoint.X < GestureData.SecFingerBeginPoint.X)
                    {
                        GestureData.MaxPositionX = true;
                    }
                }
            }
            //防止已经是手势了,但因为用户不小心使得其他手指不小心碰到了触控,需要使得该误碰的手写内容不画出来
            if (GestureData.IsGesture && GestureData.Fingers > 2)
            {
                newPolyLine.CanDraw = false;
            }
            AddNewObject(drawingCanvas, newPolyLine);

            // Add new stroke to the collection of strokes in drawing.
            _activeStrokes[newPolyLine.DeviceID] = newPolyLine;

            System.Diagnostics.Debug.WriteLine("Touchdown:{0}", GestureData.IsGesture);
        }