void DoDragFigureMouseUp(DTkViewer dv, DMouseButton btn, DPoint pt) { if (btn == DMouseButton.Left) { // drag figure end event if ((mouseHitTest == DHitTest.Body || mouseHitTest == DHitTest.SelectRect) && !currentFigure.Locked && DragFigureEnd != null) DragFigureEnd(null, currentFigure, dv.EngineToClient(pt)); // nullify currentFigure currentFigure = null; // transition TransitionTo(SelectDefault); } }
void DoSelectDefaultMouseUp(DTkViewer dv, DMouseButton btn, DPoint pt) { DHitTest hitTest; List<Figure> children = new List<Figure>(); IGlyph glyph; if (btn == DMouseButton.Left) { Figure f = figureHandler.HitTestFigures(pt, out hitTest, children, out glyph); if (hitTest == DHitTest.Glyph) { if (children.Count > 0) glyph.CallClicked(children[0], dv.EngineToClient(pt)); else glyph.CallClicked(f, dv.EngineToClient(pt)); } else if ((hitTest == DHitTest.Body || hitTest == DHitTest.SelectRect) && FigureClick != null) { foreach (Figure child in children) if (child.ClickEvent) FigureClick(null, child, dv.EngineToClient(pt)); if (f.ClickEvent) FigureClick(null, f, dv.EngineToClient(pt)); if (FiguresDeselectOnSingleClick && !FigureSelectToggleToSelection) { figureHandler.SelectFigures(new List<Figure>(new Figure[] { f }), false); dv.Update(); } } } else if (btn == DMouseButton.Right) { Figure f = figureHandler.HitTestSelect(pt, out hitTest, null, out glyph, figureSelectToggleToSelection); dv.SetCursor(DCursor.Default); dv.Update(); if (ContextClick != null) ContextClick(null, f, dv.EngineToClient(pt)); } // nullify current figure for DoSelectDefault -> DHsmSignals.MouseMove: currentFigure = null; }
void DoDragFigureMouseMove(DTkViewer dv, DPoint pt) { // rectangular area to update with paint event DRect updateRect = new DRect(); // move selected figures switch (mouseHitTest) { case DHitTest.Body: System.Diagnostics.Trace.Assert(currentFigure != null, "currentFigure is null"); // drag figure event if (DragFigureEvt != null) DragFigureEvt(null, currentFigure, dv.EngineToClient(pt)); // if figure drag op is cancelled then quit this function if (cancelledFigureDrag) { cancelledFigureDrag = false; return; } // bound pt to canvas BoundPtToPage(pt); // initial update rect updateRect = GetBoundingBox(currentFigure); foreach (Figure f in figureHandler.SelectedFigures) updateRect = updateRect.Union(GetBoundingBox(f)); // apply x/y delta to figures DPoint dPos = CalcDragDelta(pt); if (gridSnapPosition && grid > 0) { DPoint o = GridSnapOffset(currentFigure.X + dPos.X, currentFigure.Y + dPos.Y); dPos.X += o.X; dPos.Y += o.Y; pt.X += o.X; pt.Y += o.Y; } if (dPos.X != 0 || dPos.Y != 0) foreach (Figure f in figureHandler.SelectedFigures) if (!f.Locked) { f.X += dPos.X; f.Y += dPos.Y; } // store drag pt for reference later (eg. next mousemove event) dragPt = pt; // final update rect foreach (Figure f in figureHandler.SelectedFigures) updateRect = updateRect.Union(GetBoundingBox(f)); break; case DHitTest.SelectRect: goto case DHitTest.Body; case DHitTest.Resize: System.Diagnostics.Trace.Assert(currentFigure != null, "currentFigure is null"); // bound pt to canvas BoundPtToPage(pt); // alert figure we are going to resize it currentFigure.BeforeResize(); // inital update rect updateRect = GetBoundingBox(currentFigure); // translate point onto the same rotated plane as the figure pt = currentFigure.RotatePointToFigure(pt); // apply width/height delta to figure DPoint dSize = CalcSizeDelta(pt, currentFigure, LockingAspectRatio || currentFigure.LockAspectRatio); if (lockInitialAspectRatio && !(figureLockAspectRatioMode == DHsmLockAspectRatioMode.Always || currentFigure.LockAspectRatio)) { DPoint dSizeUnlocked = CalcSizeDelta(pt, currentFigure, false); if (figureLockAspectRatioMode == DHsmLockAspectRatioMode.Never || Math.Abs(dSizeUnlocked.X - dSize.X) >= unlockInitalAspectRatioThreshold || Math.Abs(dSizeUnlocked.Y - dSize.Y) >= unlockInitalAspectRatioThreshold) { lockInitialAspectRatio = false; dSize = dSizeUnlocked; } } if (currentFigure.Width > 0 && currentFigure.Width + dSize.X < currentFigure.MinSize) { dSize.X = currentFigure.MinSize - currentFigure.Width; if (LockingAspectRatio || currentFigure.LockAspectRatio) dSize.Y = (currentFigure.Height / currentFigure.Width) * dSize.X; } if (currentFigure.Height > 0 && currentFigure.Height + dSize.Y < currentFigure.MinSize) { dSize.Y = currentFigure.MinSize - currentFigure.Height; if (LockingAspectRatio || currentFigure.LockAspectRatio) dSize.X = (currentFigure.Width / currentFigure.Height) * dSize.Y; } DRect oldRect = currentFigure.Rect; currentFigure.Width += dSize.X; currentFigure.Height += dSize.Y; // snap resize if (gridSnapResize && grid > 0) { DPoint o2 = GridSnapOffset(currentFigure.Width, currentFigure.Height); currentFigure.Width += o2.X; currentFigure.Height += o2.Y; } DGeom.UpdateRotationPosition(currentFigure, oldRect, currentFigure.Rect); // final update rect updateRect = updateRect.Union(GetBoundingBox(currentFigure)); // alert figure we have finished resizing currentFigure.AfterResize(); // debug message #if DEBUG DoDebugMessage(string.Format("{0} {1}", dSize.X, dSize.Y)); #endif break; case DHitTest.RepositionPoint: System.Diagnostics.Trace.Assert(currentFigure != null, "currentFigure is null"); // bound pt to canvas BoundPtToPage(pt); // inital update rect updateRect = GetBoundingBox(currentFigure); // get our reposition point interface IRepositionPoint rp = (IRepositionPoint)currentFigure; // setup points DPoint newPoint; newPoint = new DPoint(pt.X, pt.Y); SetPointDelegate setPoint = delegate(DPoint point) { // snap point to grid if (gridSnapLines && grid > 0) { DPoint o3 = GridSnapOffset(point.X, point.Y); point.X += o3.X; point.Y += o3.Y; } // now set point rp.RepositionPoint = point; }; GetRotationalSnapDelegate getRotationalSnap = delegate(double angleRemainder) { if (angleRemainder < figureSnapRange) return -angleRemainder; else if (angleRemainder > figureSnapAngle - figureSnapRange) return figureSnapAngle - angleRemainder; else return 0; }; if (rp.AnglePoint != null) { // find the current angle of the line and the remainder when divided by the snap angle double currentAngle = DGeom.AngleBetweenPoints(rp.RepositionPoint, rp.AnglePoint); double ar = currentAngle % figureSnapAngle; // reposition line double newAngle; switch (FigureSnapAngleMode) { case DHsmSnapAngleMode.Always: // slide point along snap angle newAngle = DGeom.AngleBetweenPoints(newPoint, rp.AnglePoint); ar = newAngle % figureSnapAngle; if (ar < figureSnapAngle / 2) setPoint(DGeom.RotatePoint(newPoint, rp.AnglePoint, -ar)); else setPoint(DGeom.RotatePoint(newPoint, rp.AnglePoint, figureSnapAngle - ar)); break; case DHsmSnapAngleMode.Default: if (ar == 0) { // line is snapped, test if new angle will unsnap the line newAngle = DGeom.AngleBetweenPoints(newPoint, rp.AnglePoint); ar = newAngle % figureSnapAngle; if (ar > figureSnapRange && ar < figureSnapAngle - figureSnapRange) // unsnapped, set new point setPoint(newPoint); else { // slide point along snap angle newPoint = DGeom.RotatePoint(newPoint, rp.AnglePoint, getRotationalSnap(ar)); setPoint(newPoint); } } else { // set new point setPoint(newPoint); // test whether to snap our line newAngle = DGeom.AngleBetweenPoints(newPoint, rp.AnglePoint); ar = newAngle % figureSnapAngle; double rotationalSnap = getRotationalSnap(ar); // snap it if (rotationalSnap != 0) setPoint(DGeom.RotatePoint(newPoint, rp.AnglePoint, rotationalSnap)); } break; case DHsmSnapAngleMode.Never: // set new point setPoint(newPoint); break; } } else setPoint(newPoint); // final update rect updateRect = updateRect.Union(GetBoundingBox(currentFigure)); break; case DHitTest.Rotate: System.Diagnostics.Trace.Assert(currentFigure != null, "currentFigure is null"); // initial update rect updateRect = GetBoundingBox(currentFigure); // apply rotation to figure double newRot = GetRotationOfPointComparedToFigure(currentFigure, pt) - dragRot; double r = newRot % figureSnapAngle; switch (figureSnapAngleMode) { case DHsmSnapAngleMode.Always: if (r < figureSnapAngle / 2) currentFigure.Rotation = newRot - r; else currentFigure.Rotation = newRot + figureSnapAngle - r; break; case DHsmSnapAngleMode.Default: if (r < figureSnapRange) currentFigure.Rotation = newRot - r; else if (r > figureSnapAngle - figureSnapRange) currentFigure.Rotation = newRot + figureSnapAngle - r; else currentFigure.Rotation = newRot; break; case DHsmSnapAngleMode.Never: currentFigure.Rotation = newRot; break; } // final update rect updateRect = updateRect.Union(GetBoundingBox(currentFigure)); #if DEBUG // debug message DoDebugMessage((currentFigure.Rotation * 180 / Math.PI).ToString()); #endif break; } // update drawing dv.Update(updateRect); }
void DoSelectDefaultMouseDown(DTkViewer dv, DMouseButton btn, DPoint pt) { if (btn == DMouseButton.Left) { // find and select clicked figure List<Figure> children = new List<Figure>(); IGlyph glyph; Figure f = figureHandler.HitTestSelect(pt, out mouseHitTest, children, out glyph, figureSelectToggleToSelection); // update selected figures if (glyph != null) { } else if (f != null) { // set drag infomation currentFigure = f; switch (mouseHitTest) { case DHitTest.Body: // store drag point dragPt = pt; if (!f.Locked) { // drag figure start event if (DragFigureStart != null) DragFigureStart(null, f, dv.EngineToClient(pt)); } break; case DHitTest.SelectRect: goto case DHitTest.Body; case DHitTest.Resize: dragPt = new DPoint(0, 0); dragPt = CalcSizeDelta(f.RotatePointToFigure(pt), f, LockingAspectRatio || f.LockAspectRatio); lockInitialAspectRatio = figureLockAspectRatioMode == DHsmLockAspectRatioMode.Default; break; case DHitTest.RepositionPoint: break; case DHitTest.Rotate: dragRot = GetRotationOfPointComparedToFigure(f, pt) - f.Rotation; if (dragRot > Math.PI) dragRot = dragRot - (Math.PI * 2); break; case DHitTest.Context: if (FigureContextClick != null) FigureContextClick(null, f, dv.EngineToClient(pt)); break; case DHitTest.Lock: if (FigureLockClick != null) FigureLockClick(null, f, dv.EngineToClient(pt)); break; } } else { if (!figureSelectToggleToSelection) figureHandler.ClearSelected(); dragPt = pt; // mouseHitTest = DHitTest.None // transition TransitionTo(DragSelect); } // update drawing dv.Update(); } }