private void RemoveAdorner(ref DrawBondAdorner adorner) { var layer = AdornerLayer.GetAdornerLayer(CurrentEditor); layer.Remove(adorner); adorner = null; }
/// /// what happens when we move the mouse /// private void CurrentEditor_PreviewMouseMove(object sender, MouseEventArgs e) { Bond existingBond = null; if (_adorner != null) { RemoveAdorner(ref _adorner); } var targetedVisual = CurrentEditor.ActiveVisual; string bondOrder = EditViewModel.CurrentBondOrder; //check to see if we have already got an atom remembered if (_currentAtomVisual != null) { Point?lastPos; if (Dragging(e)) { CurrentStatus = "[Shift] = unlock length; [Ctrl] = unlock angle; [Esc] = cancel."; //are we already on top of an atom? if (targetedVisual is GroupVisual gv) { CurrentEditor.Cursor = Cursors.No; lastPos = null; } else if (targetedVisual is AtomVisual atomUnderCursor) { CurrentEditor.Cursor = CursorUtils.Pencil; //if so. snap to the atom's position lastPos = atomUnderCursor.Position; //if we are stroking over an existing bond //then draw a double bond adorner existingBond = _lastAtomVisual.ParentAtom.BondBetween(atomUnderCursor.ParentAtom); if (_lastAtomVisual != null && existingBond != null) { if (existingBond.Order == OrderSingle) { bondOrder = OrderDouble; } else if (existingBond.Order == OrderDouble) { bondOrder = OrderTriple; } else if (existingBond.Order == OrderTriple) { bondOrder = OrderSingle; } } } else //or dangling over free space? { CurrentEditor.Cursor = CursorUtils.Pencil; lastPos = e.GetPosition(CurrentEditor); var angleBetween = Vector.AngleBetween(_lastAtomVisual?.ParentAtom?.BalancingVector() ?? BasicGeometry.ScreenNorth, BasicGeometry.ScreenNorth); //snap a bond into position lastPos = _angleSnapper.SnapBond(lastPos.Value, e, angleBetween); } if (lastPos != null) { _adorner = new DrawBondAdorner(CurrentEditor, BondThickness) { Stereo = EditViewModel.CurrentStereo, BondOrder = bondOrder, ExistingBond = existingBond }; _adorner.StartPoint = _currentAtomVisual.Position; _adorner.EndPoint = lastPos.Value; } } } else { if (targetedVisual is GroupVisual gv) //can't draw on a group { CurrentStatus = "Ungroup before attempting to draw."; CurrentEditor.Cursor = Cursors.No; } else if (targetedVisual is AtomVisual av) { CurrentEditor.Cursor = CursorUtils.Pencil; if (EditViewModel.SelectedElement != av.ParentAtom.Element) { CurrentStatus = "Click to set element."; } else { CurrentStatus = "Click to sprout chain"; } } else if (targetedVisual is BondVisual bv) { CurrentEditor.Cursor = CursorUtils.Pencil; CurrentStatus = "Click to modify bond"; } } }