/// <summary> /// Apply new font stretch /// </summary> public static bool ApplyFontStretch(DrawingCanvas drawingCanvas, FontStretch value, bool addToHistory) { CommandChangeState command = new CommandChangeState(drawingCanvas); bool wasChange = false; foreach (GraphicsBase g in drawingCanvas.Selection) { GraphicsText gt = g as GraphicsText; if (gt != null) { if (gt.TextFontStretch != value) { gt.TextFontStretch = value; wasChange = true; } } } if (wasChange && addToHistory) { command.NewState(drawingCanvas); drawingCanvas.AddCommandToHistory(command); } return(wasChange); }
/// <summary> /// Apply new line width /// </summary> public static bool ApplyLineWidth(DrawingCanvas drawingCanvas, double value, bool addToHistory) { CommandChangeState command = new CommandChangeState(drawingCanvas); bool wasChange = false; // LineWidth is set for all objects except of GraphicsText. // Though GraphicsText has this property, it should remain constant. foreach (GraphicsBase g in drawingCanvas.Selection) { if (g is GraphicsRectangle || g is GraphicsEllipse || g is GraphicsLine || g is GraphicsPolyLine) { if (g.LineWidth != value) { g.LineWidth = value; wasChange = true; } } } if (wasChange && addToHistory) { command.NewState(drawingCanvas); drawingCanvas.AddCommandToHistory(command); } return(wasChange); }
/// <summary> /// Add change to history. /// Called after finishing moving/resizing. /// </summary> public void AddChangeToHistory(DrawingCanvas drawingCanvas) { if (commandChangeState != null && wasMove) { // Keep state after moving/resizing and add command to history commandChangeState.NewState(drawingCanvas); drawingCanvas.AddCommandToHistory(commandChangeState); commandChangeState = null; } }
/// <summary> /// Apply currently active properties to selected objects /// </summary> public static void ApplyProperties(DrawingCanvas drawingCanvas) { // Apply every property. // Call every Apply* function with addToHistory = false. // History is updated here and not in called functions. CommandChangeState command = new CommandChangeState(drawingCanvas); bool wasChange = false; // Line Width if (ApplyLineWidth(drawingCanvas, drawingCanvas.LineWidth, false)) { wasChange = true; } // Color if (ApplyColor(drawingCanvas, drawingCanvas.ObjectColor, false)) { wasChange = true; } // Font properties if (ApplyFontFamily(drawingCanvas, drawingCanvas.TextFontFamilyName, false)) { wasChange = true; } if (ApplyFontSize(drawingCanvas, drawingCanvas.TextFontSize, false)) { wasChange = true; } if (ApplyFontStretch(drawingCanvas, drawingCanvas.TextFontStretch, false)) { wasChange = true; } if (ApplyFontStyle(drawingCanvas, drawingCanvas.TextFontStyle, false)) { wasChange = true; } if (ApplyFontWeight(drawingCanvas, drawingCanvas.TextFontWeight, false)) { wasChange = true; } if (wasChange) { command.NewState(drawingCanvas); drawingCanvas.AddCommandToHistory(command); } }
/// <summary> /// Apply new color /// </summary> public static bool ApplyColor(DrawingCanvas drawingCanvas, Color value, bool addToHistory) { CommandChangeState command = new CommandChangeState(drawingCanvas); bool wasChange = false; foreach (GraphicsBase g in drawingCanvas.Selection) { if (g.ObjectColor != value) { g.ObjectColor = value; wasChange = true; } } if (wasChange && addToHistory) { command.NewState(drawingCanvas); drawingCanvas.AddCommandToHistory(command); } return(wasChange); }
/// <summary> /// Hide in-place editing textbox. /// Called from TextTool, when user pressed Enter or Esc, /// or from this class, when user clicks on the canvas. /// /// graphicsText passed to this function can be new text added by /// ToolText, or existing text opened for editing. /// If ToolText.OldText is empty, this is new object. /// If not, this is existing object. /// </summary> internal void HideTextbox(GraphicsText graphicsText) { if (toolText.TextBox == null) { return; } graphicsText.IsSelected = true; // restore selection which was removed for better textbox appearance if (toolText.TextBox.Text.Trim().Length == 0) { // Textbox is empty: remove text object. if (!String.IsNullOrEmpty(toolText.OldText)) // existing text was edited { // Since text object is removed now, // Add Delete command to the history undoManager.AddCommandToHistory(new CommandDelete(this)); } // Remove empty text object graphicsList.Remove(graphicsText); } else { if (!String.IsNullOrEmpty(toolText.OldText)) // existing text was edited { if (toolText.TextBox.Text.Trim() != toolText.OldText) // text was really changed { // Create command CommandChangeState command = new CommandChangeState(this); // Make change in the text object graphicsText.Text = toolText.TextBox.Text.Trim(); graphicsText.UpdateRectangle(); // Keep state after change and add command to the history command.NewState(this); undoManager.AddCommandToHistory(command); } } else // new text was added { // Make change in the text object graphicsText.Text = toolText.TextBox.Text.Trim(); graphicsText.UpdateRectangle(); // Add command to the history undoManager.AddCommandToHistory(new CommandAdd(graphicsText)); } } // Remove textbox and set it to null. this.Children.Remove(toolText.TextBox); toolText.TextBox = null; // This enables back all ApplicationCommands, // which are disabled while textbox is active. this.Focus(); }
/// <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(); }