mirror of
https://github.com/greenshot/greenshot
synced 2025-08-19 13:10:00 -07:00
Fix Inconsistent Scale Behavior when Scaling Objects with Shift Modifier (#300)
* Fix Objects Growing near Infinity when Scaling w Aspect Ratio Maintained * Refactor adjustCoordsForRationalScale to Get Rid of Code Duplication and Improve Comprehensibility * Rename getNewSizeForRationalScale to GetNewSizeForRationalScale for Consistency
This commit is contained in:
parent
56df0dbca5
commit
4a959621ff
1 changed files with 338 additions and 317 deletions
|
@ -192,62 +192,83 @@ namespace Greenshot.Helpers {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Adjusts resizeHandleCoords so that aspect ratio is kept after resizing a given rectangle with provided arguments
|
/// Adjusts resizeHandleCoords so that aspect ratio is kept after resizing a given rectangle with provided arguments.
|
||||||
|
/// An adjustment can always be done in two ways, e.g. *in*crease width until fit or *de*crease height until fit.
|
||||||
|
/// To avoid objects growing near infinity unexpectedly in certain combinations, the adjustment will choose the
|
||||||
|
/// option resulting in the smaller rectangle.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="originalRectangle">bounds of the current rectangle</param>
|
/// <param name="originalRectangle">bounds of the current rectangle</param>
|
||||||
/// <param name="resizeHandlePosition">position of the handle/gripper being used for resized, see Position</param>
|
/// <param name="resizeHandlePosition">position of the handle/gripper being used for resized, see Position</param>
|
||||||
/// <param name="resizeHandleCoords">coordinates of the used handle/gripper, adjusted coordinates will be written to this reference</param>
|
/// <param name="resizeHandleCoords">coordinates of the used handle/gripper, adjusted coordinates will be written to this reference</param>
|
||||||
private static void AdjustCoordsForRationalScale(RectangleF originalRectangle, Positions resizeHandlePosition, ref PointF resizeHandleCoords) {
|
private static void AdjustCoordsForRationalScale(RectangleF originalRectangle, Positions resizeHandlePosition, ref PointF resizeHandleCoords) {
|
||||||
float originalRatio = originalRectangle.Width / originalRectangle.Height;
|
SizeF selectedRectangle, newSize;
|
||||||
float newWidth, newHeight, newRatio;
|
|
||||||
switch(resizeHandlePosition) {
|
switch(resizeHandlePosition) {
|
||||||
|
|
||||||
case Positions.TopLeft:
|
case Positions.TopLeft:
|
||||||
newWidth = originalRectangle.Right - resizeHandleCoords.X;
|
selectedRectangle = new SizeF(originalRectangle.Right - resizeHandleCoords.X, originalRectangle.Bottom - resizeHandleCoords.Y);
|
||||||
newHeight = originalRectangle.Bottom - resizeHandleCoords.Y;
|
newSize = GetNewSizeForRationalScale(originalRectangle.Size, selectedRectangle);
|
||||||
newRatio = newWidth / newHeight;
|
resizeHandleCoords.X = originalRectangle.Right - newSize.Width;
|
||||||
if(newRatio > originalRatio) { // FIXME
|
resizeHandleCoords.Y = originalRectangle.Bottom - newSize.Height;
|
||||||
resizeHandleCoords.X = originalRectangle.Right - newHeight * originalRatio;
|
|
||||||
} else if(newRatio < originalRatio) {
|
|
||||||
resizeHandleCoords.Y = originalRectangle.Bottom - newWidth / originalRatio;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Positions.TopRight:
|
case Positions.TopRight:
|
||||||
newWidth = resizeHandleCoords.X - originalRectangle.Left;
|
selectedRectangle = new SizeF(resizeHandleCoords.X - originalRectangle.Left, originalRectangle.Bottom - resizeHandleCoords.Y);
|
||||||
newHeight = originalRectangle.Bottom - resizeHandleCoords.Y;
|
newSize = GetNewSizeForRationalScale(originalRectangle.Size, selectedRectangle);
|
||||||
newRatio = newWidth / newHeight;
|
resizeHandleCoords.X = originalRectangle.Left + newSize.Width;
|
||||||
if(newRatio > originalRatio) { // FIXME
|
resizeHandleCoords.Y = originalRectangle.Bottom - newSize.Height;
|
||||||
resizeHandleCoords.X = newHeight * originalRatio + originalRectangle.Left;
|
|
||||||
} else if(newRatio < originalRatio) {
|
|
||||||
resizeHandleCoords.Y = originalRectangle.Bottom - newWidth / originalRatio;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Positions.BottomLeft:
|
case Positions.BottomLeft:
|
||||||
newWidth = originalRectangle.Right - resizeHandleCoords.X;
|
selectedRectangle = new SizeF(originalRectangle.Right - resizeHandleCoords.X, resizeHandleCoords.Y - originalRectangle.Top);
|
||||||
newHeight = resizeHandleCoords.Y - originalRectangle.Top;
|
newSize = GetNewSizeForRationalScale(originalRectangle.Size, selectedRectangle);
|
||||||
newRatio = newWidth / newHeight;
|
resizeHandleCoords.X = originalRectangle.Right - newSize.Width;
|
||||||
if(newRatio > originalRatio) {
|
resizeHandleCoords.Y = originalRectangle.Top + newSize.Height;
|
||||||
resizeHandleCoords.X = originalRectangle.Right - newHeight * originalRatio;
|
|
||||||
} else if(newRatio < originalRatio) {
|
|
||||||
resizeHandleCoords.Y = newWidth / originalRatio + originalRectangle.Top;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case Positions.BottomRight:
|
case Positions.BottomRight:
|
||||||
newWidth = resizeHandleCoords.X - originalRectangle.Left;
|
selectedRectangle = new SizeF(resizeHandleCoords.X - originalRectangle.Left, resizeHandleCoords.Y - originalRectangle.Top);
|
||||||
newHeight = resizeHandleCoords.Y - originalRectangle.Top;
|
newSize = GetNewSizeForRationalScale(originalRectangle.Size, selectedRectangle);
|
||||||
newRatio = newWidth / newHeight;
|
resizeHandleCoords.X = originalRectangle.Left + newSize.Width;
|
||||||
if(newRatio > originalRatio) {
|
resizeHandleCoords.Y = originalRectangle.Top + newSize.Height;
|
||||||
resizeHandleCoords.X = newHeight * originalRatio + originalRectangle.Left;
|
|
||||||
} else if(newRatio < originalRatio) {
|
|
||||||
resizeHandleCoords.Y = newWidth / originalRatio + originalRectangle.Top;
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// For an original size, and a selected size, returns the the largest possible size that
|
||||||
|
/// * has the same aspect ratio as the original
|
||||||
|
/// * fits into selected size
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="originalSize">size to be considered for keeping aspect ratio</param>
|
||||||
|
/// <param name="selectedSize">selection size (i.e. the size we'd produce if we wouldn't keep aspect ratio)</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private static SizeF GetNewSizeForRationalScale(SizeF originalSize, SizeF selectedSize)
|
||||||
|
{
|
||||||
|
SizeF newSize = selectedSize;
|
||||||
|
float originalRatio = originalSize.Width / originalSize.Height;
|
||||||
|
float selectedRatio = selectedSize.Width / selectedSize.Height;
|
||||||
|
// will fix orientation if the scaling causes size to be flipped in any direction
|
||||||
|
int flippedRatioSign = Math.Sign(selectedRatio) * Math.Sign(originalRatio);
|
||||||
|
if (Math.Abs(selectedRatio) > Math.Abs(originalRatio))
|
||||||
|
{
|
||||||
|
// scaled rectangle (ratio) would be wider than original
|
||||||
|
// keep height and tweak width to maintain aspect ratio
|
||||||
|
newSize.Width = selectedSize.Height * originalRatio * flippedRatioSign;
|
||||||
|
}
|
||||||
|
else if (Math.Abs(selectedRatio) < Math.Abs(originalRatio))
|
||||||
|
{
|
||||||
|
// scaled rectangle (ratio) would be taller than original
|
||||||
|
// keep width and tweak height to maintain aspect ratio
|
||||||
|
newSize.Height = selectedSize.Width / originalRatio * flippedRatioSign;
|
||||||
|
}
|
||||||
|
return newSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void Scale(Rectangle boundsBeforeResize, int cursorX, int cursorY, ref RectangleF boundsAfterResize) {
|
||||||
|
Scale(boundsBeforeResize, cursorX, cursorY, ref boundsAfterResize, null);
|
||||||
|
}
|
||||||
|
|
||||||
public static void Scale(Rectangle boundsBeforeResize, int cursorX, int cursorY, ref RectangleF boundsAfterResize, IDoubleProcessor angleRoundBehavior) {
|
public static void Scale(Rectangle boundsBeforeResize, int cursorX, int cursorY, ref RectangleF boundsAfterResize, IDoubleProcessor angleRoundBehavior) {
|
||||||
|
|
||||||
Scale(boundsBeforeResize, Positions.TopLeft, cursorX, cursorY, ref boundsAfterResize, angleRoundBehavior);
|
Scale(boundsBeforeResize, Positions.TopLeft, cursorX, cursorY, ref boundsAfterResize, angleRoundBehavior);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue