/// <summary> /// Add new object to drawing canvas. /// Function is called when user left-clicks drawing canvas, /// and one of ToolObject-derived tools is active. /// </summary> protected static void AddNewObject(DrawingCanvas drawingCanvas, GraphicsBase o) { HelperFunctions.UnselectAll(drawingCanvas); o.IsSelected = true; o.Clip = new RectangleGeometry(new Rect(0, 0, drawingCanvas.ActualWidth, drawingCanvas.ActualHeight)); drawingCanvas.GraphicsList.Add(o); drawingCanvas.CaptureMouse(); }
/// <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(); }