/// <summary>
 /// Delete selected stroke / shape
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="routedEventArgs"></param>
 private void OnDeleteStroke(object sender, RoutedEventArgs routedEventArgs)
 {
     m_inkCanvas.Strokes.Remove(m_inkCanvas.GetSelectedStrokes());
     for (int i = 0; i < m_inkCanvas.GetSelectedElements().Count; i++)
     {
         m_inkCanvas.Children.Remove(m_inkCanvas.GetSelectedElements()[i]);
     }
 }
Exemple #2
0
        private void InkCanvasOnSelectionChanged(object sender, EventArgs eventArgs)
        {
            _selectedElements = new Collection <UIElement>();
            _selectedStrokes  = new StrokeCollection();
            _originPoints.Clear();
            _rotatePoint = new Point();
            LastAngle    = 0;
            _workAreaModel.SelectionChanged();
            _selectedElements = _inkCanvas.GetSelectedElements();
            _selectedStrokes  = _inkCanvas.GetSelectedStrokes();
            if (!IsSelected)
            {
                return;
            }
            var selectionBounds = ModelStorage.WorkAreaModel.CurrentInkCanvas.GetSelectionBounds();

            if (selectionBounds.IsEmpty)
            {
                return;
            }
            _rotatePoint = new Point(selectionBounds.X + (selectionBounds.Width / 2),
                                     selectionBounds.Y + (selectionBounds.Height / 2));
            foreach (var element in _selectedElements)
            {
                var translatePoint = element.TranslatePoint(new Point(0, 0), _inkCanvas);
                var x = element.DesiredSize.Width.Equals(0)
                            ? 0
                            : (_rotatePoint.X - translatePoint.X) / element.DesiredSize.Width;
                var y = element.DesiredSize.Height.Equals(0)
                            ? 0
                            : (_rotatePoint.Y - translatePoint.Y) / element.DesiredSize.Height;
                _originPoints.Add(element, new Point(x, y));
            }
            LastAngle = 0;
        }
Exemple #3
0
        public void CopyInkCanvasObjects(InkCanvas inkCanvas, InkCanvas clipboardCanvas)
        {
            var selectionBounds = inkCanvas.GetSelectionBounds();

            if (selectionBounds.IsEmpty)
            {
                return;
            }
            Clear(clipboardCanvas);

            var selectedElements = inkCanvas.GetSelectedElements().ToList();
            var selectedStrokes  = new StrokeCollection(inkCanvas.GetSelectedStrokes());

            ClearSelection(inkCanvas);

            var elements = new List <UIElement>();

            WPFHelper.CloneElementsTo(elements, selectedElements.GetEnumerator(), clipboardCanvas);
            WPFHelper.CloneStrokesTo(clipboardCanvas.Strokes, selectedStrokes);
            elements.ForEach(element => clipboardCanvas.Children.Add(element));
            inkCanvas.Select(selectedStrokes, selectedElements);

            var dataObject = new DataObject();

            dataObject.SetData(ClipboardIdFormat, _clipboardStateId = Guid.NewGuid());
            dataObject.SetData(DataFormats.Bitmap, ImageHelper.GetCroppedImageFromInkCanvas(selectionBounds, inkCanvas));
            Clipboard.SetDataObject(dataObject);
        }
Exemple #4
0
 public void Start(InkCanvas inkCanvas)
 {
     ClearRepetitionCollections();
     _selectedElements            = new List <UIElement>(inkCanvas.GetSelectedElements());
     _selectedStrokes             = new StrokeCollection(inkCanvas.GetSelectedStrokes());
     inkCanvas.EditingMode        = InkCanvasEditingMode.None;
     _startPoint                  = new Point(-100, -100);
     ProcessRepeateSelectionState = RepeateSelectionState.WaitingStartPoint;
 }
Exemple #5
0
        public void SelectCanvas(int canvasIndex)
        {
            InkCanvas oldInkCanvas = _currentInkCanvas;

            _currentInkCanvas = (InkCanvas)_canvasList.Children[canvasIndex];

            //copy images & strokes
            if (oldInkCanvas != null)
            {
                foreach (Image uIElement in oldInkCanvas.GetSelectedElements().OfType <Image>())
                {
                    oldInkCanvas.Children.Remove(uIElement);
                    _currentInkCanvas.Children.Add(uIElement);
                }
                foreach (Stroke stroke in oldInkCanvas.GetSelectedStrokes())
                {
                    if (Keyboard.IsKeyDown(Key.LeftShift))
                    {
                        _currentInkCanvas.Strokes.Add(stroke.Clone());
                    }
                    if (Keyboard.IsKeyDown(Key.LeftAlt))
                    {
                        oldInkCanvas.Strokes.Remove(stroke);
                        _currentInkCanvas.Strokes.Add(stroke);
                    }
                }
            }

            //hide all canvases
            foreach (InkCanvas inkCanvas in _canvasList.Children)
            {
                inkCanvas.Opacity          = .2;
                inkCanvas.IsHitTestVisible = false;
            }

            _currentInkCanvas.IsHitTestVisible = true;
            _currentInkCanvas.Opacity          = 1;
            //SetMode(InkCanvasEditingMode.Select, CustomMode.select);
            _currentInkCanvas.Select(new StrokeCollection());
            if (null != oldInkCanvas)
            {
                oldInkCanvas.Select(new StrokeCollection());
            }
        }
Exemple #6
0
        public void SelectCanvas(int i)
        {
            InkCanvas _oldInkCanvas = _InkCanvas;

            _InkCanvas = (InkCanvas)_CanvasList.Children[i];

            if (_oldInkCanvas != null)
            {
                foreach (Image _UIElement in _oldInkCanvas.GetSelectedElements().OfType <Image>().ToList())
                {
                    _oldInkCanvas.Children.Remove(_UIElement);
                    _InkCanvas.Children.Add(_UIElement);
                }
                foreach (Stroke _Stroke in _oldInkCanvas.GetSelectedStrokes())
                {
                    if (Keyboard.IsKeyDown(Key.LeftShift))
                    {
                        _InkCanvas.Strokes.Add(_Stroke.Clone());
                    }
                    if (Keyboard.IsKeyDown(Key.LeftAlt))
                    {
                        _oldInkCanvas.Strokes.Remove(_Stroke);
                        _InkCanvas.Strokes.Add(_Stroke);
                    }
                }
            }
            foreach (InkCanvas _InkCanvas1 in _CanvasList.Children)
            {
                _InkCanvas1.Opacity          = .2;
                _InkCanvas1.IsHitTestVisible = false;
            }

            _InkCanvas.IsHitTestVisible = true;
            _InkCanvas.Opacity          = 1;
            //SetMode(InkCanvasEditingMode.Select, CustomMode.select);
            _InkCanvas.Select(new StrokeCollection());
            if (null != _oldInkCanvas)
            {
                _oldInkCanvas.Select(new StrokeCollection());
            }
        }
Exemple #7
0
 public static void SelectionChanged(InkCanvas inkCanvas, ICollection <StrokeCollection> groupedStrokesCollections)
 {
     lock (SelectionChangedInProgress)
     {
         if (SelectionChangedInProgress.Contains(inkCanvas))
         {
             return;
         }
         SelectionChangedInProgress.Add(inkCanvas);
     }
     try
     {
         var selectedStrokes         = inkCanvas.GetSelectedStrokes();
         var uniqueStrokesCollection = new HashSet <Stroke>(selectedStrokes);
         foreach (var groupedStrokesCollection in groupedStrokesCollections)
         {
             if (!groupedStrokesCollection.Any(uniqueStrokesCollection.Contains))
             {
                 continue;
             }
             foreach (var stroke in groupedStrokesCollection)
             {
                 uniqueStrokesCollection.Add(stroke);
             }
         }
         inkCanvas.Select(new StrokeCollection(uniqueStrokesCollection), inkCanvas.GetSelectedElements());
     }
     finally
     {
         lock (SelectionChangedInProgress)
         {
             if (SelectionChangedInProgress.Contains(inkCanvas))
             {
                 SelectionChangedInProgress.Remove(inkCanvas);
             }
         }
     }
 }
Exemple #8
0
        /// <summary>
        /// OnPreviewMouseDown is called before InkCanvas sees the Down.  We use this
        /// handler to hit test any selection and initiate a drag and drop operation
        /// if the user clicks on the selection to move it.
        /// </summary>
        private void OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            InkCanvas inkCanvas    = (InkCanvas)sender;
            Point     downPosition = e.GetPosition(inkCanvas);

            //see if we hit the selection, and not a grab handle
            if (inkCanvas.HitTestSelection(downPosition) == InkCanvasSelectionHitResult.Selection)
            {
                //clone the selected strokes so we can transform them without affecting the actual selection
                StrokeCollection selectedStrokes = inkCanvas.GetSelectedStrokes();

                List <UIElement> selectedElements = new List <UIElement>(inkCanvas.GetSelectedElements());
                DataObject       dataObject       = new DataObject();

                //copy ink to the clipboard if we have any.  we do this even if there are no
                //strokes since clonedStrokes will be used by the Xaml clipboard code below
                StrokeCollection clonedStrokes = selectedStrokes.Clone();
                if (clonedStrokes.Count > 0)
                {
                    //translate the strokes relative to the down position
                    Matrix translation = new Matrix();
                    translation.Translate(-downPosition.X, -downPosition.Y);
                    clonedStrokes.Transform(translation, false);

                    //save the strokes to a dataobject to use during the dragdrop operation
                    MemoryStream ms = new MemoryStream();
                    clonedStrokes.Save(ms);
                    ms.Position = 0;
                    dataObject.SetData(StrokeCollection.InkSerializedFormat, ms);

                    //we don't close the MemoryStream here, we'll do it in OnDrop
                }

                //Now we're going to add Xaml to the dragdrop dataobject.  We'll create an
                //InkCanvas and add any selected strokes and elements in the selection to it
                InkCanvas inkCanvasForDragDrop = new InkCanvas();
                foreach (UIElement childElement in selectedElements)
                {
                    //we can't add elements in the selection to the InkCanvas that
                    //represents selection (since they are already parented to the
                    //inkCanvas that has the selection) so we need to clone them.
                    //To clone each element, we need to convert it to Xaml and back again
                    string    childXaml          = XamlWriter.Save(childElement);
                    UIElement clonedChildElement =
                        (UIElement)XamlReader.Load(new XmlTextReader(new StringReader(childXaml)));

                    //adjust top and left relative to the down position
                    double childLeft = InkCanvas.GetLeft(clonedChildElement);
                    InkCanvas.SetLeft(clonedChildElement, childLeft - downPosition.X);

                    double childTop = InkCanvas.GetTop(clonedChildElement);
                    InkCanvas.SetTop(clonedChildElement, childTop - downPosition.Y);

                    inkCanvasForDragDrop.Children.Add(clonedChildElement);
                }

                //last, add the cloned strokes in case our drop location only supports Xaml
                //this preserves both the ink and the selected elements
                inkCanvasForDragDrop.Strokes = clonedStrokes;

                //now copy the Xaml for the InkCanvas that represents selection to the clipboard
                string inkCanvasXaml = XamlWriter.Save(inkCanvasForDragDrop);
                dataObject.SetData(DataFormats.Xaml, inkCanvasXaml);

                //The call to DragDrop.DoDragDrop will block until the drag and drop operation is completed
                //once it does, 'effects' will have been updated by OnDragOver getting called
                //if we're moving the strokes and elements, we'll remove them.  If we're copying them
                //(the CTRL key is pressed) we won't remove them.
                DragDropEffects effects =
                    DragDrop.DoDragDrop(inkCanvas, dataObject, DragDropEffects.Move | DragDropEffects.Copy);

                if (effects == DragDropEffects.Move)
                {
                    inkCanvas.Strokes.Remove(selectedStrokes);

                    foreach (UIElement childElement in selectedElements)
                    {
                        inkCanvas.Children.Remove(childElement);
                    }
                }
            }
        }