Small fixes to the VectorGraphicsContainer, to align it with the possible coming EmojiContainer. Still having issues with Scaling code, this needs to be fixed. [skip ci]

This commit is contained in:
Robin Krom 2022-04-14 11:58:52 +02:00
parent 8880578f77
commit 94591e5b14
No known key found for this signature in database
GPG key ID: BCC01364F1371490
5 changed files with 75 additions and 39 deletions

View file

@ -6,15 +6,15 @@
<ItemGroup>
<PackageReference Include="Dapplo.HttpExtensions.JsonNet" Version="1.0.18" />
<PackageReference Include="Dapplo.Windows.Clipboard" Version="1.0.21" />
<PackageReference Include="Dapplo.Windows.Dpi" Version="1.0.21" />
<PackageReference Include="Dapplo.Windows.Gdi32" Version="1.0.21" />
<PackageReference Include="Dapplo.Windows.Icons" Version="1.0.21" />
<PackageReference Include="Dapplo.Windows.Kernel32" Version="1.0.21" />
<PackageReference Include="Dapplo.Windows.Multimedia" Version="1.0.21" />
<PackageReference Include="Dapplo.Windows.Clipboard" Version="1.0.25" />
<PackageReference Include="Dapplo.Windows.Dpi" Version="1.0.25" />
<PackageReference Include="Dapplo.Windows.Gdi32" Version="1.0.25" />
<PackageReference Include="Dapplo.Windows.Icons" Version="1.0.25" />
<PackageReference Include="Dapplo.Windows.Kernel32" Version="1.0.25" />
<PackageReference Include="Dapplo.Windows.Multimedia" Version="1.0.25" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.42" />
<PackageReference Include="log4net" version="2.0.14" />
<PackageReference Include="Svg" Version="3.4.1" />
<PackageReference Include="Svg" Version="3.4.2" />
<Reference Include="Accessibility" />
<Reference Include="CustomMarshalers" />
</ItemGroup>

View file

@ -250,7 +250,7 @@ namespace Greenshot.Editor.Drawing
break;
}
}
_boundsAfterResize = ScaleHelper.Scale(_boundsBeforeResize, x, y, GetAngleRoundProcessor());
_boundsAfterResize = ScaleHelper.Scale(_boundsBeforeResize, Positions.TopLeft, x, y, GetAngleRoundProcessor());
// apply scaled bounds to this DrawableContainer
ApplyBounds(_boundsAfterResize);

View file

@ -533,7 +533,7 @@ namespace Greenshot.Editor.Drawing
// reset "workbench" rectangle to current bounds
_boundsAfterResize = new NativeRectFloat(_boundsBeforeResize.Left, _boundsBeforeResize.Top, x - _boundsAfterResize.Left, y - _boundsAfterResize.Top);
_boundsAfterResize = ScaleHelper.Scale(_boundsAfterResize, x, y, GetAngleRoundProcessor());
_boundsAfterResize = ScaleHelper.Scale(_boundsAfterResize, Positions.TopLeft, x, y, GetAngleRoundProcessor());
// apply scaled bounds to this DrawableContainer
ApplyBounds(_boundsAfterResize);

View file

@ -25,6 +25,7 @@ using System.Drawing.Drawing2D;
using System.Runtime.Serialization;
using Greenshot.Base.Interfaces;
using Greenshot.Base.Interfaces.Drawing;
using Greenshot.Editor.Drawing.Adorners;
namespace Greenshot.Editor.Drawing
{
@ -35,28 +36,54 @@ namespace Greenshot.Editor.Drawing
[Serializable]
public abstract class VectorGraphicsContainer : DrawableContainer
{
protected int RotationAngle;
private int _rotationAngle;
protected int RotationAngle
{
get => _rotationAngle;
set => _rotationAngle = value;
}
/// <summary>
/// This is the cached version of the bitmap, pre-rendered to save performance
/// Do not serialized, it can be rebuild with some other information.
/// Do not serialized, it can be rebuild with other information.
/// </summary>
[NonSerialized] private Image _cachedImage;
/// <summary>
/// Constructor takes care of calling Init
/// </summary>
/// <param name="parent">ISurface</param>
public VectorGraphicsContainer(ISurface parent) : base(parent)
{
Init();
}
/// <summary>
/// Make sure Init is called after deserializing
/// </summary>
/// <param name="streamingContext">StreamingContext</param>
protected override void OnDeserialized(StreamingContext streamingContext)
{
base.OnDeserialized(streamingContext);
Init();
}
private void Init()
/// <summary>
/// Init is called after creating the instance, and from OnDeserialized
/// This is the place to generate your adorners
/// </summary>
protected virtual void Init()
{
CreateDefaultAdorners();
// Check if the adorners are already defined!
if (Adorners.Count > 0)
{
return;
}
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));
}
/// <summary>
@ -96,7 +123,10 @@ namespace Greenshot.Editor.Drawing
}
_cachedImage ??= ComputeBitmap();
if (_cachedImage == null)
{
return;
}
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.CompositingQuality = CompositingQuality.HighQuality;
@ -106,9 +136,16 @@ namespace Greenshot.Editor.Drawing
graphics.DrawImage(_cachedImage, Bounds);
}
/// <summary>
/// Implement this to compute the new bitmap according to the size of the container
/// </summary>
/// <returns>Image</returns>
protected abstract Image ComputeBitmap();
private void ResetCachedBitmap()
/// <summary>
/// Dispose of the cached bitmap, forcing the code to regenerate it
/// </summary>
protected void ResetCachedBitmap()
{
_cachedImage?.Dispose();
_cachedImage = null;

View file

@ -58,7 +58,7 @@ namespace Greenshot.Editor.Helpers
/// <param name="currentSize">the size of the element to be resized</param>
/// <param name="targetSize">the target size of the element</param>
/// <param name="crop">in case the aspect ratio of currentSize and targetSize differs: shall the scaled size fit into targetSize (i.e. that one of its dimensions is smaller - false) or vice versa (true)</param>
/// <returns>a new SizeF object indicating the width and height the element should be scaled to</returns>
/// <returns>NativeSizeFloat object indicating the width and height the element should be scaled to</returns>
public static NativeSizeFloat GetScaledSize(NativeSizeFloat currentSize, NativeSizeFloat targetSize, bool crop)
{
float wFactor = targetSize.Width / currentSize.Width;
@ -71,10 +71,10 @@ namespace Greenshot.Editor.Helpers
/// <summary>
/// calculates the position of an element depending on the desired alignment within a RectangleF
/// </summary>
/// <param name="currentRect">the bounds of the element to be aligned</param>
/// <param name="targetRect">the rectangle reference for alignment of the element</param>
/// <param name="currentRect">NativeRectFloat the bounds of the element to be aligned</param>
/// <param name="targetRect">NativeRectFloat with the rectangle for alignment of the element</param>
/// <param name="alignment">the System.Drawing.ContentAlignment value indicating how the element is to be aligned should the width or height differ from targetSize</param>
/// <returns>a new RectangleF object with Location aligned aligned to targetRect</returns>
/// <returns>NativeRectFloat object with Location aligned aligned to targetRect</returns>
public static NativeRectFloat GetAlignedRectangle(NativeRectFloat currentRect, NativeRectFloat targetRect, ContentAlignment alignment)
{
var newRect = new NativeRectFloat(targetRect.Location, currentRect.Size);
@ -96,9 +96,9 @@ namespace Greenshot.Editor.Helpers
/// <summary>
/// Calculates target size of a given rectangle scaled by dragging one of its handles (corners)
/// </summary>
/// <param name="originalRectangle">bounds of the current rectangle</param>
/// <param name="resizeHandlePosition">position of the handle/gripper being used for resized, see constants in Gripper.cs, e.g. Gripper.POSITION_TOP_LEFT</param>
/// <param name="resizeHandleCoords">coordinates of the used handle/gripper</param>
/// <param name="originalRectangle">NativeRectFloat bounds of the current rectangle</param>
/// <param name="resizeHandlePosition">Positions with the position of the handle/gripper being used for resized, see constants in Gripper.cs, e.g. Gripper.POSITION_TOP_LEFT</param>
/// <param name="resizeHandleCoords">NativePointFloat coordinates of the used handle/gripper</param>
/// <param name="options">ScaleOptions to use when scaling</param>
/// <returns>NativeRectFloat scaled originalRectangle</returns>
public static NativeRectFloat Scale(NativeRectFloat originalRectangle, Positions resizeHandlePosition, NativePointFloat resizeHandleCoords, ScaleOptions? options)
@ -134,9 +134,9 @@ namespace Greenshot.Editor.Helpers
/// <summary>
/// Calculates target size of a given rectangle scaled by dragging one of its handles (corners)
/// </summary>
/// <param name="originalRectangle">bounds of the current rectangle</param>
/// <param name="resizeHandlePosition">position of the handle/gripper being used for resized, see constants in Gripper.cs, e.g. Gripper.POSITION_TOP_LEFT</param>
/// <param name="resizeHandleCoords">coordinates of the used handle/gripper</param>
/// <param name="originalRectangle">NativeRectFloat bounds of the current rectangle</param>
/// <param name="resizeHandlePosition">Positions with the position of the handle/gripper being used for resized, see constants in Gripper.cs, e.g. Gripper.POSITION_TOP_LEFT</param>
/// <param name="resizeHandleCoords">NativePointFloat with coordinates of the used handle/gripper</param>
/// <returns>NativeRectFloat with the scaled originalRectangle</returns>
private static NativeRectFloat Scale(NativeRectFloat originalRectangle, Positions resizeHandlePosition, NativePointFloat resizeHandleCoords)
{
@ -158,9 +158,9 @@ namespace Greenshot.Editor.Helpers
/// To avoid objects growing near infinity unexpectedly in certain combinations, the adjustment will choose the
/// option resulting in the smaller rectangle.
/// </summary>
/// <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="resizeHandleCoords">coordinates of the used handle/gripper</param>
/// <param name="originalRectangle">NativeRectFloat with the bounds of the current rectangle</param>
/// <param name="resizeHandlePosition">Positions with the position of the handle/gripper being used for resized, see Position</param>
/// <param name="resizeHandleCoords">NativePointFloat with coordinates of the used handle/gripper</param>
/// <returns>NativePointFloat with the adjusted coordinates</returns>
private static NativePointFloat AdjustCoordsForRationalScale(NativeRectFloat originalRectangle, Positions resizeHandlePosition, NativePointFloat resizeHandleCoords)
{
@ -203,7 +203,7 @@ namespace Greenshot.Editor.Helpers
/// </summary>
/// <param name="originalSize">NativeSizeFloat to be considered for keeping aspect ratio</param>
/// <param name="selectedSize">NativeSizeFloat selection size (i.e. the size we'd produce if we wouldn't keep aspect ratio)</param>
/// <returns></returns>
/// <returns>NativeSizeFloat</returns>
private static NativeSizeFloat GetNewSizeForRationalScale(NativeSizeFloat originalSize, NativeSizeFloat selectedSize)
{
NativeSizeFloat newSize = selectedSize;
@ -227,16 +227,15 @@ namespace Greenshot.Editor.Helpers
return newSize;
}
public static NativeRectFloat Scale(NativeRect boundsBeforeResize, int cursorX, int cursorY)
{
return Scale(boundsBeforeResize, cursorX, cursorY, null);
}
public static NativeRectFloat Scale(NativeRect boundsBeforeResize, int cursorX, int cursorY, IDoubleProcessor angleRoundBehavior)
{
return Scale(boundsBeforeResize, Positions.TopLeft, cursorX, cursorY, angleRoundBehavior);
}
/// <summary>
/// Scale the boundsBeforeResize with the specified position and new location, using the angle angleRoundBehavior
/// </summary>
/// <param name="boundsBeforeResize">NativeRect</param>
/// <param name="gripperPosition">Positions</param>
/// <param name="cursorX">int</param>
/// <param name="cursorY">int</param>
/// <param name="angleRoundBehavior">IDoubleProcessor</param>
/// <returns>NativeRectFloat</returns>
public static NativeRectFloat Scale(NativeRect boundsBeforeResize, Positions gripperPosition, int cursorX, int cursorY, IDoubleProcessor angleRoundBehavior)
{
ScaleOptions opts = GetScaleOptions();