Keep aspect ratio when resizing

This commit is contained in:
Julien Richard 2022-01-26 09:08:55 +01:00
commit 1c74700c61
4 changed files with 32 additions and 15 deletions

View file

@ -109,7 +109,8 @@ namespace Greenshot.Editor.Drawing.Adorners
_boundsAfterResize.Height = _boundsBeforeResize.Height;
// calculate scaled rectangle
ScaleHelper.Scale(ref _boundsAfterResize, Position, new PointF(mouseEventArgs.X, mouseEventArgs.Y), ScaleHelper.GetScaleOptions());
var scaleOptions = (Owner as IHasScaleOptions)?.GetScaleOptions() ?? ScaleHelper.GetScaleOptions();
ScaleHelper.Scale(ref _boundsAfterResize, Position, new PointF(mouseEventArgs.X, mouseEventArgs.Y), scaleOptions);
// apply scaled bounds to this DrawableContainer
Owner.ApplyBounds(_boundsAfterResize);

View file

@ -512,13 +512,14 @@ namespace Greenshot.Editor.Drawing
{
Invalidate();
// reset "workrbench" rectangle to current bounds
// reset "workbench" rectangle to current bounds
_boundsAfterResize.X = _boundsBeforeResize.Left;
_boundsAfterResize.Y = _boundsBeforeResize.Top;
_boundsAfterResize.Width = x - _boundsAfterResize.Left;
_boundsAfterResize.Height = y - _boundsAfterResize.Top;
ScaleHelper.Scale(_boundsBeforeResize, x, y, ref _boundsAfterResize, GetAngleRoundProcessor());
var scaleOptions = (this as IHasScaleOptions)?.GetScaleOptions() ?? null;
ScaleHelper.Scale(_boundsBeforeResize, x, y, ref _boundsAfterResize, GetAngleRoundProcessor(), scaleOptions);
// apply scaled bounds to this DrawableContainer
ApplyBounds(_boundsAfterResize);

View file

@ -33,6 +33,7 @@ using System.Windows.Media.Imaging;
using Greenshot.Base.Core;
using Greenshot.Base.Interfaces.Drawing;
using Greenshot.Editor.Controls;
using Greenshot.Editor.Drawing.Adorners;
using Greenshot.Editor.Helpers;
using Image = System.Drawing.Image;
using Matrix = System.Drawing.Drawing2D.Matrix;
@ -44,7 +45,7 @@ namespace Greenshot.Editor.Drawing
/// Description of EmojiContainer.
/// </summary>
[Serializable]
public class EmojiContainer : DrawableContainer, IEmojiContainer
public class EmojiContainer : DrawableContainer, IEmojiContainer, IHasScaleOptions
{
[NonSerialized] private static EmojiContainer _currentContainer;
[NonSerialized] private static ElementHost _emojiPickerHost;
@ -135,7 +136,10 @@ namespace Greenshot.Editor.Drawing
private void Init()
{
CreateDefaultAdorners();
Adorners.Add(new ResizeAdorner(this, Positions.TopLeft));
Adorners.Add(new ResizeAdorner(this, Positions.TopRight));
Adorners.Add(new ResizeAdorner(this, Positions.BottomLeft));
Adorners.Add(new ResizeAdorner(this, Positions.BottomRight));
PropertyChanged += OnPropertyChanged;
}
@ -223,6 +227,11 @@ namespace Greenshot.Editor.Drawing
_cachedImage?.Dispose();
_cachedImage = null;
}
public ScaleHelper.ScaleOptions GetScaleOptions()
{
return ScaleHelper.ScaleOptions.Rational;
}
}
internal static class PickerExtensions

View file

@ -26,6 +26,11 @@ using Greenshot.Editor.Drawing;
namespace Greenshot.Editor.Helpers
{
public interface IHasScaleOptions
{
ScaleHelper.ScaleOptions GetScaleOptions();
}
/// <summary>
/// Offers a few helper functions for scaling/aligning an element with another element
/// </summary>
@ -280,23 +285,23 @@ namespace Greenshot.Editor.Helpers
return newSize;
}
public static void Scale(Rectangle boundsBeforeResize, int cursorX, int cursorY, ref RectangleF boundsAfterResize)
public static void Scale(Rectangle boundsBeforeResize, int cursorX, int cursorY, ref RectangleF boundsAfterResize, ScaleOptions? options)
{
Scale(boundsBeforeResize, cursorX, cursorY, ref boundsAfterResize, null);
Scale(boundsBeforeResize, cursorX, cursorY, ref boundsAfterResize, null, options);
}
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, ScaleOptions? options)
{
Scale(boundsBeforeResize, Positions.TopLeft, cursorX, cursorY, ref boundsAfterResize, angleRoundBehavior);
Scale(boundsBeforeResize, Positions.TopLeft, cursorX, cursorY, ref boundsAfterResize, angleRoundBehavior, options);
}
public static void Scale(Rectangle boundsBeforeResize, Positions gripperPosition, int cursorX, int cursorY, ref RectangleF boundsAfterResize,
IDoubleProcessor angleRoundBehavior)
IDoubleProcessor angleRoundBehavior, ScaleOptions? options)
{
ScaleOptions opts = GetScaleOptions();
options ??= GetScaleOptions();
bool rationalScale = (opts & ScaleOptions.Rational) == ScaleOptions.Rational;
bool centeredScale = (opts & ScaleOptions.Centered) == ScaleOptions.Centered;
bool rationalScale = (options & ScaleOptions.Rational) == ScaleOptions.Rational;
bool centeredScale = (options & ScaleOptions.Centered) == ScaleOptions.Centered;
if (rationalScale)
{
@ -324,12 +329,13 @@ namespace Greenshot.Editor.Helpers
}
}
/// <param name="drawableContainer"></param>
/// <returns>the current ScaleOptions depending on modifier keys held down</returns>
public static ScaleOptions GetScaleOptions()
{
bool anchorAtCenter = (Control.ModifierKeys & Keys.Control) != 0;
bool maintainAspectRatio = (Control.ModifierKeys & Keys.Shift) != 0;
ScaleOptions opts = ScaleOptions.Default;
var opts = ScaleOptions.Default;
if (anchorAtCenter) opts |= ScaleOptions.Centered;
if (maintainAspectRatio) opts |= ScaleOptions.Rational;
return opts;