/// <summary> /// Display proper cursor to corresponding thumb /// </summary> /// <param name="thumbPosition">Thumb position</param> /// <returns></returns> private static Cursor GetCursor(ThumbPosition thumbPosition) { switch (thumbPosition) { case ThumbPosition.TopLeft: return(Cursors.SizeNWSE); case ThumbPosition.TopMiddle: return(Cursors.SizeNS); case ThumbPosition.TopRight: return(Cursors.SizeNESW); case ThumbPosition.RightMiddle: return(Cursors.SizeWE); case ThumbPosition.BottomRight: return(Cursors.SizeNWSE); case ThumbPosition.BottomMiddle: return(Cursors.SizeNS); case ThumbPosition.BottomLeft: return(Cursors.SizeNESW); case ThumbPosition.LeftMiddle: return(Cursors.SizeWE); case ThumbPosition.Center: return(Cursors.Cross); default: return(null); } }
/// <summary> /// Thumb factory /// </summary> /// <param name="thumbPosition">Thumb positions</param> /// <param name="canvas">Parent UI element that we will attach thumb as child</param> /// <param name="size">Size of thumb</param> /// <returns></returns> public static ThumbCrop CreateThumb(ThumbPosition thumbPosition, Canvas canvas, double size) { ThumbCrop customThumb = new ThumbCrop(size); customThumb.Cursor = GetCursor(thumbPosition); customThumb.Visibility = Visibility.Hidden; canvas.Children.Add(customThumb); return(customThumb); }
/// <summary> /// Display proper cursor to corresponding thumb /// </summary> /// <param name="thumbPosition">Thumb position</param> /// <returns></returns> private static Cursor GetCursor(ThumbPosition thumbPosition) { return(thumbPosition switch { (ThumbPosition.TopLeft) => Cursors.SizeNWSE, (ThumbPosition.TopMiddle) => Cursors.SizeNS, (ThumbPosition.TopRight) => Cursors.SizeNESW, (ThumbPosition.RightMiddle) => Cursors.SizeWE, (ThumbPosition.BottomRight) => Cursors.SizeNWSE, (ThumbPosition.BottomMiddle) => Cursors.SizeNS, (ThumbPosition.BottomLeft) => Cursors.SizeNESW, (ThumbPosition.LeftMiddle) => Cursors.SizeWE, _ => null, });
private static bool IsCornerThumb(ThumbPosition thumbPosition) { switch (thumbPosition) { case ThumbPosition.Top: case ThumbPosition.Bottom: case ThumbPosition.Left: case ThumbPosition.Right: return(false); case ThumbPosition.UpperLeft: case ThumbPosition.UpperRight: case ThumbPosition.LowerLeft: case ThumbPosition.LowerRight: return(true); } return(false); }
private Thumb CreateThumb(ThumbPosition position) { var thumb = new Thumb(); thumb.Tag = position; thumb.DragDelta += Thumb_DragDelta; switch (position) { case ThumbPosition.Top: case ThumbPosition.Bottom: thumb.Cursor = Cursors.SizeNS; break; case ThumbPosition.Left: case ThumbPosition.Right: thumb.Cursor = Cursors.SizeWE; break; case ThumbPosition.BottomLeft: case ThumbPosition.TopRight: thumb.Cursor = Cursors.SizeNESW; break; case ThumbPosition.BottomRight: case ThumbPosition.TopLeft: thumb.Cursor = Cursors.SizeNWSE; break; case ThumbPosition.Center: thumb.Cursor = Cursors.ScrollAll; break; } return(thumb); }
public Point GetCanvasThumbPosition(ThumbPosition thumbPosition) { return TransformToAncestor(OwnerCanvas).Transform(GetThumbPosition(thumbPosition)); }
public bool HandleCollisions(Point position, Element caller, ThumbPosition sourcePosition, out Element targetElement, out ThumbPosition targetPosition) { double distance; targetElement = ElementNearestToPoint(position, caller, out distance, out targetPosition); if (Math.Abs(distance)<= 20 && (caller.IsConnectionAllowedTo(targetPosition, targetElement))) { targetElement.CircleAdorner.SetBackground(targetPosition, (SolidColorBrush)FindResource("ConnectorAvailable")); LinkCurve(caller.GetLink(sourcePosition).From, caller, sourcePosition, targetElement, targetPosition); return true; } return false; }
public bool ExistsLink(ThumbPosition thumbPosition) { switch (thumbPosition) { case ThumbPosition.Left: return LeftLink.TargetElement != null; case ThumbPosition.Top: return TopLink.TargetElement != null; case ThumbPosition.Right: return RightLink.TargetElement != null; case ThumbPosition.Bottom: return BottomLink.TargetElement != null; default: return false; } }
void UpdateCurve(ThumbPosition thumbPosition, Point currentPosition) { Point startBezierPoint, endBezierPoint; switch (thumbPosition) { case ThumbPosition.Top: startBezierPoint = new Point(startPoint.X, startPoint.Y - GridCanvas.BezierYOffset); endBezierPoint = new Point(currentPosition.X, currentPosition.Y - GridCanvas.BezierYOffset); break; default: case ThumbPosition.Bottom: startBezierPoint = new Point(startPoint.X, startPoint.Y + GridCanvas.BezierYOffset); endBezierPoint = new Point(currentPosition.X, currentPosition.Y - GridCanvas.BezierYOffset); break; case ThumbPosition.Right: startBezierPoint = new Point(startPoint.X + GridCanvas.BezierYOffset, startPoint.Y); endBezierPoint = new Point(currentPosition.X - GridCanvas.BezierYOffset, currentPosition.Y); break; case ThumbPosition.Left: startBezierPoint = new Point(startPoint.X - GridCanvas.BezierYOffset, startPoint.Y); endBezierPoint = new Point(currentPosition.X + GridCanvas.BezierYOffset, currentPosition.Y); break; } BezierSegment bezierSegment = new BezierSegment(startBezierPoint, endBezierPoint, currentPosition, true); Path[] curves = ownerCanvas.Curves.Values.Where( path => path.Name.StartsWith(ownerElement.GetLink(thumbPosition).From)).ToArray(); foreach (PathGeometry curveGeometry in curves.Select(curve => (PathGeometry)curve.Data)) { if (curveGeometry.Figures[0].Segments.Count == 0) curveGeometry.Figures[0].Segments.Add(bezierSegment); else curveGeometry.Figures[0].Segments[0] = bezierSegment; } }
public void AddEffect(ThumbPosition position, Effect effect) { Thumb control; switch (position) { default: case ThumbPosition.Left: control = left; break; case ThumbPosition.Top: control = top; break; case ThumbPosition.Right: control = right; break; case ThumbPosition.Bottom: control = bottom; break; } control.Effect = effect; }
public bool HasConnectionsAllowedFrom(ThumbPosition source) { return Connections.ContainsKey(source); }
static Point ComputeBezierSegmentPoint(ThumbPosition position, Point thumbLocation) { switch (position) { default: case ThumbPosition.Top: return new Point(thumbLocation.X, thumbLocation.Y - BezierYOffset); case ThumbPosition.Bottom: return new Point(thumbLocation.X, thumbLocation.Y + BezierYOffset); case ThumbPosition.Right: return new Point(thumbLocation.X+BezierYOffset, thumbLocation.Y); case ThumbPosition.Left: return new Point(thumbLocation.X - BezierYOffset, thumbLocation.Y); } }
public void RemoveLink(ThumbPosition thumbPosition) { Link emptyLink = new Link(this, thumbPosition); switch (thumbPosition) { case ThumbPosition.Left: LeftLink = emptyLink; break; default: case ThumbPosition.Top: TopLink = emptyLink; break; case ThumbPosition.Right: RightLink = emptyLink; break; case ThumbPosition.Bottom: BottomLink = emptyLink; break; } }
/// <summary> /// Update cropped area. /// </summary> /// <param name="position">The control point</param> /// <param name="diffPos">Position offset</param> private void UpdateCroppedRect(ThumbPosition position, Point diffPos) { if (diffPos == default(Point) || !IsValidRect(CanvasRect)) { return; } double radian = 0d, diffPointRadian = 0d; if (KeepAspectRatio) { radian = Math.Atan(UsedAspectRatio); diffPointRadian = Math.Atan(diffPos.X / diffPos.Y); } var startPoint = new Point(_startX, _startY); var endPoint = new Point(_endX, _endY); var currentSelectedRect = startPoint.ToRect(endPoint); switch (position) { case ThumbPosition.Top: if (KeepAspectRatio) { var originSizeChange = new Point(-diffPos.Y * UsedAspectRatio, -diffPos.Y); var safeChange = GetSafeSizeChangeWhenKeepAspectRatio(_restrictedSelectRect, position, currentSelectedRect, originSizeChange, UsedAspectRatio); startPoint.X += -safeChange.X / 2; endPoint.X += safeChange.X / 2; startPoint.Y += -safeChange.Y; } else { startPoint.Y += diffPos.Y; } break; case ThumbPosition.Bottom: if (KeepAspectRatio) { var originSizeChange = new Point(diffPos.Y * UsedAspectRatio, diffPos.Y); var safeChange = GetSafeSizeChangeWhenKeepAspectRatio(_restrictedSelectRect, position, currentSelectedRect, originSizeChange, UsedAspectRatio); startPoint.X += -safeChange.X / 2; endPoint.X += safeChange.X / 2; endPoint.Y += safeChange.Y; } else { endPoint.Y += diffPos.Y; } break; case ThumbPosition.Left: if (KeepAspectRatio) { var originSizeChange = new Point(-diffPos.X, -diffPos.X / UsedAspectRatio); var safeChange = GetSafeSizeChangeWhenKeepAspectRatio(_restrictedSelectRect, position, currentSelectedRect, originSizeChange, UsedAspectRatio); startPoint.Y += -safeChange.Y / 2; endPoint.Y += safeChange.Y / 2; startPoint.X += -safeChange.X; } else { startPoint.X += diffPos.X; } break; case ThumbPosition.Right: if (KeepAspectRatio) { var originSizeChange = new Point(diffPos.X, diffPos.X / UsedAspectRatio); var safeChange = GetSafeSizeChangeWhenKeepAspectRatio(_restrictedSelectRect, position, currentSelectedRect, originSizeChange, UsedAspectRatio); startPoint.Y += -safeChange.Y / 2; endPoint.Y += safeChange.Y / 2; endPoint.X += safeChange.X; } else { endPoint.X += diffPos.X; } break; case ThumbPosition.UpperLeft: if (KeepAspectRatio) { var effectiveLength = diffPos.Y / Math.Cos(diffPointRadian) * Math.Cos(diffPointRadian - radian); var originSizeChange = new Point(-effectiveLength * Math.Sin(radian), -effectiveLength * Math.Cos(radian)); var safeChange = GetSafeSizeChangeWhenKeepAspectRatio(_restrictedSelectRect, position, currentSelectedRect, originSizeChange, UsedAspectRatio); diffPos.X = -safeChange.X; diffPos.Y = -safeChange.Y; } startPoint.X += diffPos.X; startPoint.Y += diffPos.Y; break; case ThumbPosition.UpperRight: if (KeepAspectRatio) { diffPointRadian = -diffPointRadian; var effectiveLength = diffPos.Y / Math.Cos(diffPointRadian) * Math.Cos(diffPointRadian - radian); var originSizeChange = new Point(-effectiveLength * Math.Sin(radian), -effectiveLength * Math.Cos(radian)); var safeChange = GetSafeSizeChangeWhenKeepAspectRatio(_restrictedSelectRect, position, currentSelectedRect, originSizeChange, UsedAspectRatio); diffPos.X = safeChange.X; diffPos.Y = -safeChange.Y; } endPoint.X += diffPos.X; startPoint.Y += diffPos.Y; break; case ThumbPosition.LowerLeft: if (KeepAspectRatio) { diffPointRadian = -diffPointRadian; var effectiveLength = diffPos.Y / Math.Cos(diffPointRadian) * Math.Cos(diffPointRadian - radian); var originSizeChange = new Point(effectiveLength * Math.Sin(radian), effectiveLength * Math.Cos(radian)); var safeChange = GetSafeSizeChangeWhenKeepAspectRatio(_restrictedSelectRect, position, currentSelectedRect, originSizeChange, UsedAspectRatio); diffPos.X = -safeChange.X; diffPos.Y = safeChange.Y; } startPoint.X += diffPos.X; endPoint.Y += diffPos.Y; break; case ThumbPosition.LowerRight: if (KeepAspectRatio) { var effectiveLength = diffPos.Y / Math.Cos(diffPointRadian) * Math.Cos(diffPointRadian - radian); var originSizeChange = new Point(effectiveLength * Math.Sin(radian), effectiveLength * Math.Cos(radian)); var safeChange = GetSafeSizeChangeWhenKeepAspectRatio(_restrictedSelectRect, position, currentSelectedRect, originSizeChange, UsedAspectRatio); diffPos.X = safeChange.X; diffPos.Y = safeChange.Y; } endPoint.X += diffPos.X; endPoint.Y += diffPos.Y; break; } if (!IsSafeRect(startPoint, endPoint, MinSelectSize)) { if (KeepAspectRatio) { if ((endPoint.Y - startPoint.Y) < (_endY - _startY) || (endPoint.X - startPoint.X) < (_endX - _startX)) { return; } } else { var safeRect = GetSafeRect(startPoint, endPoint, MinSelectSize, position); safeRect.Intersect(_restrictedSelectRect); startPoint = new Point(safeRect.X, safeRect.Y); endPoint = new Point(safeRect.X + safeRect.Width, safeRect.Y + safeRect.Height); } } var isEffectiveRegion = IsSafePoint(_restrictedSelectRect, startPoint) && IsSafePoint(_restrictedSelectRect, endPoint); var selectedRect = startPoint.ToRect(endPoint); if (!isEffectiveRegion) { if (!IsCornerThumb(position) && TryGetContainedRect(_restrictedSelectRect, ref selectedRect)) { startPoint = new Point(selectedRect.Left, selectedRect.Top); endPoint = new Point(selectedRect.Right, selectedRect.Bottom); } else { return; } } selectedRect.Union(CanvasRect); if (selectedRect != CanvasRect) { var croppedRect = _inverseImageTransform.TransformBounds(startPoint.ToRect(endPoint)); croppedRect.Intersect(_restrictedCropRect); _currentCroppedRect = croppedRect; var viewportRect = GetUniformRect(CanvasRect, selectedRect.Width / selectedRect.Height); var viewportImgRect = _inverseImageTransform.TransformBounds(selectedRect); UpdateImageLayoutWithViewport(viewportRect, viewportImgRect); } else { UpdateSelectedRect(startPoint, endPoint); } }
public void LinkCurve(string curveId, Element source, ThumbPosition sourcePosition, Element target, ThumbPosition targetPosition) { foreach (Path curve in Curves.Values.Where(curve => curve.Name.StartsWith(curveId))) { Point thumbLocation = target.GetCanvasThumbPosition(targetPosition); PathGeometry curveGeometry = (PathGeometry)curve.Data; PathFigure curveFigure = curveGeometry.Figures[0]; BezierSegment bezierSegment = (BezierSegment)curveFigure.Segments[0]; bezierSegment.Point2 = ComputeBezierSegmentPoint(targetPosition, thumbLocation); bezierSegment.Point3 = thumbLocation; } }
/// <summary> /// Gets a rectangle with a minimum size limit. /// </summary> /// <param name="startPoint">The point on the upper left corner.</param> /// <param name="endPoint">The point on the lower right corner.</param> /// <param name="minSize">The minimum size.</param> /// <param name="position">The control point.</param> /// <returns>The right rectangle.</returns> private static Rect GetSafeRect(Point startPoint, Point endPoint, Size minSize, ThumbPosition position) { var checkPoint = new Point(startPoint.X + minSize.Width, startPoint.Y + minSize.Height); switch (position) { case ThumbPosition.Top: if (checkPoint.Y > endPoint.Y) { startPoint.Y = endPoint.Y - minSize.Height; } break; case ThumbPosition.Bottom: if (checkPoint.Y > endPoint.Y) { endPoint.Y = startPoint.Y + minSize.Height; } break; case ThumbPosition.Left: if (checkPoint.X > endPoint.X) { startPoint.X = endPoint.X - minSize.Width; } break; case ThumbPosition.Right: if (checkPoint.X > endPoint.X) { endPoint.X = startPoint.X + minSize.Width; } break; case ThumbPosition.UpperLeft: if (checkPoint.X > endPoint.X) { startPoint.X = endPoint.X - minSize.Width; } if (checkPoint.Y > endPoint.Y) { startPoint.Y = endPoint.Y - minSize.Height; } break; case ThumbPosition.UpperRight: if (checkPoint.X > endPoint.X) { endPoint.X = startPoint.X + minSize.Width; } if (checkPoint.Y > endPoint.Y) { startPoint.Y = endPoint.Y - minSize.Height; } break; case ThumbPosition.LowerLeft: if (checkPoint.X > endPoint.X) { startPoint.X = endPoint.X - minSize.Width; } if (checkPoint.Y > endPoint.Y) { endPoint.Y = startPoint.Y + minSize.Height; } break; case ThumbPosition.LowerRight: if (checkPoint.X > endPoint.X) { endPoint.X = startPoint.X + minSize.Width; } if (checkPoint.Y > endPoint.Y) { endPoint.Y = startPoint.Y + minSize.Height; } break; } return(new Rect(startPoint, endPoint)); }
private static Point GetSafeSizeChangeWhenKeepAspectRatio(Rect targetRect, ThumbPosition thumbPosition, Rect selectedRect, Point originSizeChange, double aspectRatio) { var safeWidthChange = originSizeChange.X; var safeHeightChange = originSizeChange.Y; var maxWidthChange = 0d; var maxHeightChange = 0d; switch (thumbPosition) { case ThumbPosition.Top: maxWidthChange = targetRect.Width - selectedRect.Width; maxHeightChange = selectedRect.Top - targetRect.Top; break; case ThumbPosition.Bottom: maxWidthChange = targetRect.Width - selectedRect.Width; maxHeightChange = targetRect.Bottom - selectedRect.Bottom; break; case ThumbPosition.Left: maxWidthChange = selectedRect.Left - targetRect.Left; maxHeightChange = targetRect.Height - selectedRect.Height; break; case ThumbPosition.Right: maxWidthChange = targetRect.Right - selectedRect.Right; maxHeightChange = targetRect.Height - selectedRect.Height; break; case ThumbPosition.UpperLeft: maxWidthChange = selectedRect.Left - targetRect.Left; maxHeightChange = selectedRect.Top - targetRect.Top; break; case ThumbPosition.UpperRight: maxWidthChange = targetRect.Right - selectedRect.Right; maxHeightChange = selectedRect.Top - targetRect.Top; break; case ThumbPosition.LowerLeft: maxWidthChange = selectedRect.Left - targetRect.Left; maxHeightChange = targetRect.Bottom - selectedRect.Bottom; break; case ThumbPosition.LowerRight: maxWidthChange = targetRect.Right - selectedRect.Right; maxHeightChange = targetRect.Bottom - selectedRect.Bottom; break; } if (originSizeChange.X > maxWidthChange) { safeWidthChange = maxWidthChange; safeHeightChange = safeWidthChange / aspectRatio; } if (originSizeChange.Y > maxHeightChange) { safeHeightChange = maxHeightChange; safeWidthChange = safeHeightChange * aspectRatio; } return(new Point(safeWidthChange, safeHeightChange)); }
public void UnhighlightConnectors(Element element, ThumbPosition sourcePosition) { if (!element.HasConnectionsAllowedFrom(sourcePosition)) return; var connectors = (from e in UnconnectedElements let c = element.Connections[sourcePosition] where c.TargetTypes.Contains(e.GetType()) select new { Element = e, ThumbPosition = c.TargetPosition }); foreach (var connector in connectors) { connector.Element.CircleAdorner.SetBackground(connector.ThumbPosition, (SolidColorBrush)FindResource("ConnectorUnconnected")); } }
Element ElementNearestToPoint(Point position, Element caller, out double minDistance, out ThumbPosition nearestThumbPosition) { minDistance = double.PositiveInfinity; Element hitElement = null; nearestThumbPosition = ThumbPosition.NotSet; foreach (Element element in UnconnectedElements) { if(element == caller) continue; foreach (ThumbPosition thumbPosition in element.AvailableIncomingConnectors) { Point elementLocation = element.GetCanvasThumbPosition(thumbPosition); double distance = position.Distance(elementLocation); if(distance < minDistance) { minDistance = distance; hitElement = element; nearestThumbPosition = thumbPosition; } } } return hitElement; }
public Link GetLink(ThumbPosition thumbPosition) { switch (thumbPosition) { case ThumbPosition.Left: return LeftLink; case ThumbPosition.Top: return TopLink; case ThumbPosition.Right: return RightLink; case ThumbPosition.Bottom: return BottomLink; default: return default(Link); } }
public void EstablishLink(string curveId, Element source, ThumbPosition sourcePosition, Element target, ThumbPosition targetPosition) { Link sourceLink = new Link(source, sourcePosition, target, targetPosition, true); source.SetLink(sourcePosition, sourceLink); linkList.Add(sourceLink); Path curve = Curves.Values.First(c => c.Name == curveId + "_i"); curve.Stroke = (Brush)FindResource("ConnectedLink"); source.CircleAdorner.SetBackground(sourcePosition, Brushes.LightGreen); target.CircleAdorner.SetBackground(targetPosition, Brushes.LightGreen); OnLinkEstablished(new LinkArgs(source, target)); }
public Point GetThumbPosition(ThumbPosition thumbPosition) { switch (thumbPosition) { case ThumbPosition.Left: return new Point(0, ActualHeight / 2); case ThumbPosition.Top: return new Point(ActualWidth / 2, 0); case ThumbPosition.Right: return new Point(ActualWidth, ActualHeight / 2); default: case ThumbPosition.Bottom: return new Point(ActualWidth / 2, ActualHeight); } }
// Helper method to instantiate the corner Thumbs, set the Cursor property, // set some appearance properties, and add the elements to the visual tree. void BuildAdornerThumb(ref Thumb cornerThumb, Cursor customizedCursor, ThumbPosition thumbPosition) { if (cornerThumb != null) return; bool multiConnector = ownerElement.Connections.ContainsKey(thumbPosition) ? (ownerElement.Connections[thumbPosition].MultiConnector ? true : false) : false; string resourceType = multiConnector ? "MultiConnector" : thumbPosition == ThumbPosition.Top ? "IncomingConnector" : "SingleConnector"; cornerThumb = new Thumb { Name = ownerElement.Name + "_" + thumbPosition, Cursor = customizedCursor, Style = (Style)FindResource(resourceType), Tag = thumbPosition, IsHitTestVisible = true }; cornerThumb.PreviewMouseLeftButtonDown += ThumbMouseLeftButtonDown; cornerThumb.PreviewMouseMove += ThumbMouseMove; cornerThumb.PreviewMouseLeftButtonUp += ThumbPreviewMouseLeftButtonUp; // Set some arbitrary visual characteristics. visualChildren.Add(cornerThumb); }
public bool IsConnectionAllowedTo(ThumbPosition targetPosition, Element targetElement) { return Connections.Any(c => c.Value.TargetPosition == targetPosition && c.Value.TargetTypes.Contains(targetElement.GetType())); }
public void UpdateCurveTarget(string id, Element target, ThumbPosition thumbPosition) { var curves = Children.OfType<Path>(); curves.Count().ToString(); foreach (Path curve in Curves.Values.Where(curve => curve.Name.StartsWith(id))) { Point thumbLocation = target.GetCanvasThumbPosition(thumbPosition); PathGeometry curveGeometry = (PathGeometry)curve.Data; PathFigure curveFigure = curveGeometry.Figures[0]; BezierSegment bezierSegment = (BezierSegment)curveFigure.Segments[0]; bezierSegment.Point2 = ComputeBezierSegmentPoint(thumbPosition, thumbLocation); bezierSegment.Point3 = thumbLocation; } }
public void SetLink(ThumbPosition thumbPosition, Link link) { switch (thumbPosition) { case ThumbPosition.Left: LeftLink = link; break; case ThumbPosition.Top: TopLink = link; break; case ThumbPosition.Right: RightLink = link; break; case ThumbPosition.Bottom: BottomLink = link; break; default: return; } }
public void SetBackground(ThumbPosition position, Brush brush) { Thumb control; switch (position) { default: case ThumbPosition.Left: control = left; break; case ThumbPosition.Top: control = top; break; case ThumbPosition.Right: control = right; break; case ThumbPosition.Bottom: control = bottom; break; } control.Background = brush; }
/// <summary> /// Update cropped area. /// </summary> /// <param name="position">The control point</param> /// <param name="diffPos">Position offset</param> private void UpdateCroppedRect(ThumbPosition position, Point diffPos) { if (diffPos == default(Point) || !IsValidRect(CanvasRect)) { return; } double radian = 0d, diffPointRadian = 0d, effectiveLength = 0d; if (KeepAspectRatio) { radian = Math.Atan(UsedAspectRatio); diffPointRadian = Math.Atan(diffPos.X / diffPos.Y); } var startPoint = new Point(_startX, _startY); var endPoint = new Point(_endX, _endY); switch (position) { case ThumbPosition.Top: startPoint.Y += diffPos.Y; if (KeepAspectRatio) { var changeX = diffPos.Y * UsedAspectRatio; startPoint.X += changeX / 2; endPoint.X -= changeX / 2; } break; case ThumbPosition.Bottom: endPoint.Y += diffPos.Y; if (KeepAspectRatio) { var changeX = diffPos.Y * UsedAspectRatio; startPoint.X -= changeX / 2; endPoint.X += changeX / 2; } break; case ThumbPosition.Left: startPoint.X += diffPos.X; if (KeepAspectRatio) { var changeY = diffPos.X / UsedAspectRatio; startPoint.Y += changeY / 2; endPoint.Y -= changeY / 2; } break; case ThumbPosition.Right: endPoint.X += diffPos.X; if (KeepAspectRatio) { var changeY = diffPos.X / UsedAspectRatio; startPoint.Y -= changeY / 2; endPoint.Y += changeY / 2; } break; case ThumbPosition.UpperLeft: if (KeepAspectRatio) { effectiveLength = diffPos.Y / Math.Cos(diffPointRadian) * Math.Cos(diffPointRadian - radian); diffPos.X = effectiveLength * Math.Sin(radian); diffPos.Y = effectiveLength * Math.Cos(radian); } startPoint.X += diffPos.X; startPoint.Y += diffPos.Y; break; case ThumbPosition.UpperRight: if (KeepAspectRatio) { diffPointRadian = -diffPointRadian; effectiveLength = diffPos.Y / Math.Cos(diffPointRadian) * Math.Cos(diffPointRadian - radian); diffPos.X = -effectiveLength *Math.Sin(radian); diffPos.Y = effectiveLength * Math.Cos(radian); } endPoint.X += diffPos.X; startPoint.Y += diffPos.Y; break; case ThumbPosition.LowerLeft: if (KeepAspectRatio) { diffPointRadian = -diffPointRadian; effectiveLength = diffPos.Y / Math.Cos(diffPointRadian) * Math.Cos(diffPointRadian - radian); diffPos.X = -effectiveLength *Math.Sin(radian); diffPos.Y = effectiveLength * Math.Cos(radian); } startPoint.X += diffPos.X; endPoint.Y += diffPos.Y; break; case ThumbPosition.LowerRight: if (KeepAspectRatio) { effectiveLength = diffPos.Y / Math.Cos(diffPointRadian) * Math.Cos(diffPointRadian - radian); diffPos.X = effectiveLength * Math.Sin(radian); diffPos.Y = effectiveLength * Math.Cos(radian); } endPoint.X += diffPos.X; endPoint.Y += diffPos.Y; break; } if (!IsSafeRect(startPoint, endPoint, MinSelectSize)) { if (KeepAspectRatio) { if ((endPoint.Y - startPoint.Y) < (_endY - _startY) || (endPoint.X - startPoint.X) < (_endX - _startX)) { return; } } else { var safeRect = GetSafeRect(startPoint, endPoint, MinSelectSize, position); safeRect.Intersect(_restrictedSelectRect); startPoint = new Point(safeRect.X, safeRect.Y); endPoint = new Point(safeRect.X + safeRect.Width, safeRect.Y + safeRect.Height); } } var isEffectiveRegion = IsSafePoint(_restrictedSelectRect, startPoint) && IsSafePoint(_restrictedSelectRect, endPoint); if (!isEffectiveRegion) { return; } var selectedRect = new Rect(startPoint, endPoint); selectedRect.Union(CanvasRect); if (selectedRect != CanvasRect) { var croppedRect = _inverseImageTransform.TransformBounds( new Rect(startPoint, endPoint)); croppedRect.Intersect(_restrictedCropRect); _currentCroppedRect = croppedRect; var viewportRect = GetUniformRect(CanvasRect, selectedRect.Width / selectedRect.Height); var viewportImgRect = _inverseImageTransform.TransformBounds(selectedRect); UpdateImageLayoutWithViewport(viewportRect, viewportImgRect); } else { UpdateSelectedRect(startPoint, endPoint); } }
public void SetVisibility(ThumbPosition position, bool value) { Thumb control; switch (position) { default: case ThumbPosition.Left: control = left; break; case ThumbPosition.Top: control = top; break; case ThumbPosition.Right: control = right; break; case ThumbPosition.Bottom: control = bottom; break; } control.Visibility = value ? Visibility.Visible : Visibility.Collapsed; }