/// <summary> /// Delete objects again. /// </summary> public override void Redo(DrawingCanvas drawingCanvas) { // Delete from list all objects kept in cloneList. // Use object IDs for deleting, don't beleive to objects order. int n = drawingCanvas.GraphicsList.Count; for (int i = n - 1; i >= 0; i--) { bool toDelete = false; GraphicsBase currentObject = (GraphicsBase)drawingCanvas.GraphicsList[i]; foreach (PropertiesGraphicsBase o in cloneList) { if (o.ID == currentObject.Id) { toDelete = true; break; } } if (toDelete) { drawingCanvas.GraphicsList.RemoveAt(i); } } }
/// <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 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); }
/// <summary> /// Delete added object /// </summary> public override void Undo(DrawingCanvas drawingCanvas) { // Find object to delete by its ID. // Don't use objects order in the list. GraphicsBase objectToDelete = null; // Remove object from the list foreach (GraphicsBase b in drawingCanvas.GraphicsList) { if (b.Id == newObjectClone.ID) { objectToDelete = b; break; } } if (objectToDelete != null) { drawingCanvas.GraphicsList.Remove(objectToDelete); } }
// Set selection order in graphicsList according to list of IDs private static void ChangeOrder(VisualCollection graphicsList, List <int> indexList) { List <GraphicsBase> tmpList = new List <GraphicsBase>(); // Read indexList, find every element in graphicsList by ID // and move it to tmpList. foreach (int id in indexList) { GraphicsBase objectToMove = null; foreach (GraphicsBase g in graphicsList) { if (g.Id == id) { objectToMove = g; break; } } if (objectToMove != null) { // Move tmpList.Add(objectToMove); graphicsList.Remove(objectToMove); } } // Now tmpList contains objects in correct order. // Read tmpList and add all its elements back to graphicsList. foreach (GraphicsBase g in tmpList) { graphicsList.Add(g); } }
/// <summary> /// Show context menu. /// </summary> void ShowContextMenu(MouseButtonEventArgs e) { // Change current selection if necessary Point point = e.GetPosition(this); GraphicsBase o = null; for (int i = graphicsList.Count - 1; i >= 0; i--) { if (((GraphicsBase)graphicsList[i]).MakeHitTest(point) == 0) { o = (GraphicsBase)graphicsList[i]; break; } } if (o != null) { if (!o.IsSelected) { UnselectAll(); } // Select clicked object o.IsSelected = true; } else { UnselectAll(); } UpdateState(); MenuItem item; /// Enable/disable menu items. foreach (object obj in contextMenu.Items) { item = obj as MenuItem; if (item != null) { ContextMenuCommand command = (ContextMenuCommand)item.Tag; switch (command) { case ContextMenuCommand.SelectAll: item.IsEnabled = CanSelectAll; break; case ContextMenuCommand.UnselectAll: item.IsEnabled = CanUnselectAll; break; case ContextMenuCommand.Delete: item.IsEnabled = CanDelete; break; case ContextMenuCommand.DeleteAll: item.IsEnabled = CanDeleteAll; break; case ContextMenuCommand.MoveToFront: item.IsEnabled = CanMoveToFront; break; case ContextMenuCommand.MoveToBack: item.IsEnabled = CanMoveToBack; break; case ContextMenuCommand.Undo: item.IsEnabled = CanUndo; break; case ContextMenuCommand.Redo: item.IsEnabled = CanRedo; break; case ContextMenuCommand.SerProperties: item.IsEnabled = CanSetProperties; break; } } } contextMenu.IsOpen = true; }
/// <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(); }
// Create this command with DrawObject instance added to the list public CommandAdd(GraphicsBase newObject) : base() { // Keep copy of added object this.newObjectClone = newObject.CreateSerializedObject(); }