// Replace objects in graphicsList with objects from list private void ReplaceObjects(GraphicsList graphicsList, List <DrawObject> list) { for (int i = 0; i < graphicsList.Count; i++) { DrawObject replacement = list.FirstOrDefault(o => o.ID == graphicsList[i].ID); if (replacement != null) { graphicsList.Replace(i, replacement); } } }
/// <summary> /// Add new object to draw area. Function is called when user left-clicks draw area, and one of ToolObject-derived tools is active. /// </summary> /// <param name="drawArea"> </param> /// <param name="o"> </param> protected void AddNewObject(TransparentCanvas drawArea, DrawObject o) { drawArea.GraphicsList.UnselectAll(); o.Selected = true; drawArea.GraphicsList.Add(o); drawArea.Capture = true; drawArea.Refresh(); drawArea.SetDirty(); }
public void Redo(GraphicsList list) { // Delete from list all objects kept in cloneList int n = list.Count; for (int i = n - 1; i >= 0; i--) { DrawObject objectToDelete = list[i]; bool toDelete = _cloneList.Any(o => objectToDelete.ID == o.ID); if (toDelete) { list.RemoveAt(i); } } }
public void Draw(Graphics g) { int n = _graphicsList.Count; // Enumerate list in reverse order to get first // object on the top of Z-order. for (int i = n - 1; i >= 0; i--) { DrawObject o = _graphicsList[i]; o.Draw(g); if (o.Selected) { o.DrawTracker(g); } } }
/// <summary> /// Left mouse button is pressed /// </summary> /// <param name="drawArea"> </param> /// <param name="e"> </param> public override void OnMouseDown(TransparentCanvas drawArea, MouseEventArgs e) { _commandChangeState = null; _wasMove = false; _selectMode = SelectionMode.None; Point point = new Point(e.X, e.Y); // Test for resizing (only if control is selected, cursor is on the handle) foreach (DrawObject o in drawArea.GraphicsList.Selection) { int handleNumber = o.HitTest(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 drawArea.GraphicsList.UnselectAll(); o.Selected = true; _commandChangeState = new CommandChangeState(drawArea.GraphicsList); break; } } // Test for move (cursor is on the object) if (_selectMode == SelectionMode.None) { int n1 = drawArea.GraphicsList.Count; DrawObject o = null; for (int i = 0; i < n1; i++) { if (drawArea.GraphicsList[i].HitTest(point) == 0) { o = drawArea.GraphicsList[i]; break; } } if (o != null) { _selectMode = SelectionMode.Move; // Unselect all if Ctrl is not pressed and clicked object is not selected yet if ((Control.ModifierKeys & Keys.Control) == 0 && !o.Selected) { drawArea.GraphicsList.UnselectAll(); } // Select clicked object o.Selected = true; _commandChangeState = new CommandChangeState(drawArea.GraphicsList); drawArea.Cursor = Cursors.SizeAll; } } // Net selection if (_selectMode == SelectionMode.None) { // click on background if ((Control.ModifierKeys & Keys.Control) == 0) { drawArea.GraphicsList.UnselectAll(); } _selectMode = SelectionMode.NetSelection; } _lastPoint.X = e.X; _lastPoint.Y = e.Y; _startPoint.X = e.X; _startPoint.Y = e.Y; drawArea.Capture = true; drawArea.Refresh(); if (_selectMode == SelectionMode.NetSelection) { // Draw selection rectangle in initial position ControlPaint.DrawReversibleFrame( drawArea.RectangleToScreen(DrawRectangle.GetNormalizedRectangle(_startPoint, _lastPoint)), Color.Black, FrameStyle.Dashed); } }
/// <summary> /// Right mouse button is released /// </summary> /// <param name="drawArea"> </param> /// <param name="e"> </param> public override void OnMouseUp(TransparentCanvas drawArea, MouseEventArgs e) { if (_selectMode == SelectionMode.NetSelection) { // Remove old selection rectangle ControlPaint.DrawReversibleFrame( drawArea.RectangleToScreen(DrawRectangle.GetNormalizedRectangle(_startPoint, _lastPoint)), Color.Black, FrameStyle.Dashed); // Make group selection drawArea.GraphicsList.SelectInRectangle( DrawRectangle.GetNormalizedRectangle(_startPoint, _lastPoint)); _selectMode = SelectionMode.None; } if (_resizedObject != null) { // after resizing _resizedObject.Normalize(); _resizedObject = null; } drawArea.Capture = false; drawArea.Refresh(); if (_commandChangeState != null && _wasMove) { // Keep state after moving/resizing and add command to history _commandChangeState.NewState(drawArea.GraphicsList); drawArea.AddCommandToHistory(_commandChangeState); _commandChangeState = null; } }
/// <summary> /// Right-click handler /// </summary> /// <param name="e"> </param> private void OnContextMenu(MouseEventArgs e) { // Change current selection if necessary var point = new Point(e.X, e.Y); int n = GraphicsList.Count; DrawObject o = null; for (int i = 0; i < n; i++) { if (GraphicsList[i].HitTest(point) == 0) { o = GraphicsList[i]; break; } } if (o != null) { if (!o.Selected) { GraphicsList.UnselectAll(); } // Select clicked object o.Selected = true; } else { GraphicsList.UnselectAll(); } Refresh(); // in the case selection was changed // Show context menu. // Context menu items are filled from owner form Edit menu items. _contextMenu = new ContextMenuStrip(); // int nItems = owner.ContextParent.DropDownItems.Count; // Read Edit items and move them to context menu. // Since every move reduces number of items, read them in reverse order. // To get items in direct order, insert each of them to beginning. // for (int i = nItems - 1; i >= 0; i--) // { // m_ContextMenu.Items.Insert(0, owner.ContextParent.DropDownItems[i]); // } // Show context menu for owner form, so that it handles items selection. // Convert point from this window coordinates to owner's coordinates. point.X += Left; point.Y += Top; //m_ContextMenu.Show(owner, point); //Owner.SetStateOfControls(); // enable/disable menu items // Context menu is shown, but owner's Edit menu is now empty. // Subscribe to context menu Closed event and restore items there. _contextMenu.Closed += delegate { if (_contextMenu != null) // precaution { // nItems = m_ContextMenu.Items.Count; // for (int k = nItems - 1; k >= 0; k--) // { // owner.ContextParent.DropDownItems.Insert(0, m_ContextMenu.Items[k]); // } } }; }
/// <summary> /// Replace object in specified place. Used for Undo. /// </summary> public void Replace(int index, DrawObject obj) { if (index >= 0 && index < _graphicsList.Count) { _graphicsList.RemoveAt(index); _graphicsList.Insert(index, obj); } }
/// <summary> /// Insert object to specified place. Used for Undo. /// </summary> public void Insert(int index, DrawObject obj) { if (index >= 0 && index < _graphicsList.Count) { _graphicsList.Insert(index, obj); } }
public void Add(DrawObject obj) { // insert to the top of z-order _graphicsList.Insert(0, obj); }
/// <summary> /// Copy fields from this instance to cloned instance drawObject. Called from Clone functions of derived classes. /// </summary> protected void FillDrawObjectFields(DrawObject drawObject) { drawObject._selected = _selected; drawObject._color = _color; drawObject._penWidth = _penWidth; drawObject.ID = ID; }