/// <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; drawingCanvas.SelectedObject = o; 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; drawingCanvas.SelectedObject = movedObject; // 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(); }
// Create this command with DrawObject instance added to the list public CommandAdd(GraphicsBase newObject) : base() { // Keep copy of added object this.newObjectClone = newObject.CreateSerializedObject(); }
/// <summary> /// Handle mouse up. /// Return to normal state. /// </summary> public override void OnMouseUp(DrawingCanvas drawingCanvas, MouseButtonEventArgs e) { if ( ! drawingCanvas.IsMouseCaptured ) { drawingCanvas.Cursor = HelperFunctions.DefaultCursor; selectMode = SelectionMode.None; return; } if (resizedObject != null) { // after resizing resizedObject.Normalize(); // Special case for text if (resizedObject is GraphicsText) { ((GraphicsText)resizedObject).UpdateRectangle(); } resizedObject = null; } if ( selectMode == SelectionMode.GroupSelection ) { GraphicsSelectionRectangle r = (GraphicsSelectionRectangle)drawingCanvas[drawingCanvas.Count - 1]; r.Normalize(); Rect rect = r.Rectangle; drawingCanvas.GraphicsList.Remove(r); foreach(GraphicsBase g in drawingCanvas.GraphicsList) { if ( g.IntersectsWith(rect) ) { g.IsSelected = true; } } } drawingCanvas.ReleaseMouseCapture(); drawingCanvas.Cursor = HelperFunctions.DefaultCursor; selectMode = SelectionMode.None; AddChangeToHistory(drawingCanvas); }