Esempio n. 1
0
        void Canvas_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            var pt = e.GetPosition(Canvas);

            Canvas.Focus();
            Keyboard.Focus(Canvas);


            if (e.ChangedButton == MouseButton.Left)
            {
                switch (Mode)
                {
                //case XDrawingModes.Select:
                case XDrawingModes.Edit:
                    m_DragInfo = HitTest(pt);
                    if (!m_DragInfo.HasValue)
                    {
                        SelectedShape = null;
                    }
                    else
                    {
                        if (m_DragInfo.Value.Shape != SelectedShape)
                        {
                            SelectedShape = m_DragInfo.Value.Shape;
                        }
                        if (m_DragInfo.Value.ControlPoint != SelectedControlPoint)
                        {
                            if (m_DragInfo.Value.ControlPoint == null || m_DragInfo.Value.ControlPoint.IsSelectable)
                            {
                                SelectedControlPoint = m_DragInfo.Value.ControlPoint;
                            }
                        }
                        m_DragInfo.Value.DragObject.StartDrag();
                    }
                    break;

                case XDrawingModes.NewLine:
                case XDrawingModes.NewRect:
                case XDrawingModes.NewEllipse:
                case XDrawingModes.NewPath:
                case XDrawingModes.NewText:
                    SelectedShape = null;
                    XDrawingModes newMode = Mode;
                    switch (Mode)
                    {
                    case XDrawingModes.NewLine:
                        m_DragInfo = XDrawingLine.CreateNewByDrag(this, SnapToGrid(pt), out newMode);
                        break;

                    case XDrawingModes.NewRect:
                        m_DragInfo = XDrawingRectangle.CreateNewByDrag(this, SnapToGrid(pt), out newMode);
                        break;

                    case XDrawingModes.NewEllipse:
                        m_DragInfo = XDrawingEllipse.CreateNewByDrag(this, SnapToGrid(pt), out newMode);
                        break;

                    case XDrawingModes.NewPath:
                        m_DragInfo = XDrawingPath.CreateNewByDrag(this, SnapToGrid(pt), out newMode);
                        break;

                    case XDrawingModes.NewText:
                        m_DragInfo = XDrawingText.CreateNewByDrag(this, SnapToGrid(pt), out newMode);
                        break;
                    }
                    if (m_DragInfo.HasValue)
                    {
                        Mode = newMode;
                        SetValue(SelectedShapeProperty, m_DragInfo.Value.Shape);
                        m_Shapes.Add(m_DragInfo.Value.Shape);
                        Canvas.Children.Insert(Canvas.Children.IndexOf(m_ControlPointPath), m_DragInfo.Value.Shape.Path);
                        m_DragInfo.Value.DragObject.StartDrag();
                    }
                    break;
                }
                Canvas.CaptureMouse();
            }
            else if (e.ChangedButton == MouseButton.Right)
            {
                var hti = HitTest(pt);
                if (!hti.HasValue)
                {
                    SelectedShape = null;
                }
                else
                {
                    if (hti.Value.Shape != SelectedShape)
                    {
                        SelectedShape = hti.Value.Shape;
                    }
                    if (hti.Value.ControlPoint != SelectedControlPoint)
                    {
                        if (hti.Value.ControlPoint == null || hti.Value.ControlPoint.IsSelectable)
                        {
                            SelectedControlPoint = hti.Value.ControlPoint;
                        }
                    }
                }
            }
            e.Handled = true;
        }