Rework for paste from clipboard

- location is determined by the same code for images and text;
- use visible area to determine the location.
This commit is contained in:
Killy 2020-04-27 19:15:08 +03:00
parent 3d39241f82
commit 169dbdccec
4 changed files with 93 additions and 50 deletions

View file

@ -302,33 +302,6 @@ namespace Greenshot.Drawing
} }
} }
public void AlignToParent(HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment) {
if (_parent == null)
{
return;
}
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
if (horizontalAlignment == HorizontalAlignment.Left) {
Left = lineThickness/2;
}
if (horizontalAlignment == HorizontalAlignment.Right) {
Left = _parent.Width - Width - lineThickness/2;
}
if (horizontalAlignment == HorizontalAlignment.Center) {
Left = (_parent.Width / 2) - (Width / 2) - lineThickness/2;
}
if (verticalAlignment == VerticalAlignment.TOP) {
Top = lineThickness/2;
}
if (verticalAlignment == VerticalAlignment.BOTTOM) {
Top = _parent.Height - Height - lineThickness/2;
}
if (verticalAlignment == VerticalAlignment.CENTER) {
Top = (_parent.Height / 2) - (Height / 2) - lineThickness/2;
}
}
public virtual bool InitContent() { return true; } public virtual bool InitContent() { return true; }
public virtual void OnDoubleClick() {} public virtual void OnDoubleClick() {}

View file

@ -305,6 +305,8 @@ namespace Greenshot.Drawing
[NonSerialized] [NonSerialized]
private Matrix _zoomMatrix = new Matrix(1, 0, 0, 1, 0, 0); private Matrix _zoomMatrix = new Matrix(1, 0, 0, 1, 0, 0);
[NonSerialized] [NonSerialized]
private Matrix _inverseZoomMatrix = new Matrix(1, 0, 0, 1, 0, 0);
[NonSerialized]
private float _zoomFactor = 1.0f; private float _zoomFactor = 1.0f;
public float ZoomFactor public float ZoomFactor
{ {
@ -313,6 +315,7 @@ namespace Greenshot.Drawing
{ {
_zoomFactor = value; _zoomFactor = value;
_zoomMatrix = new Matrix(_zoomFactor, 0, 0, _zoomFactor, 0, 0); _zoomMatrix = new Matrix(_zoomFactor, 0, 0, _zoomFactor, 0, 0);
_inverseZoomMatrix = new Matrix(1f / _zoomFactor, 0, 0, 1f / _zoomFactor, 0, 0);
UpdateSize(); UpdateSize();
} }
} }
@ -803,9 +806,9 @@ namespace Greenshot.Drawing
return cursorContainer; return cursorContainer;
} }
public ITextContainer AddTextContainer(string text, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, FontFamily family, float size, bool italic, bool bold, bool shadow, int borderSize, Color color, Color fillColor) public ITextContainer AddTextContainer(string text, int x, int y, FontFamily family, float size, bool italic, bool bold, bool shadow, int borderSize, Color color, Color fillColor)
{ {
TextContainer textContainer = new TextContainer(this) {Text = text}; TextContainer textContainer = new TextContainer(this) {Text = text, Left = x, Top = y};
textContainer.SetFieldValue(FieldType.FONT_FAMILY, family.Name); textContainer.SetFieldValue(FieldType.FONT_FAMILY, family.Name);
textContainer.SetFieldValue(FieldType.FONT_BOLD, bold); textContainer.SetFieldValue(FieldType.FONT_BOLD, bold);
textContainer.SetFieldValue(FieldType.FONT_ITALIC, italic); textContainer.SetFieldValue(FieldType.FONT_ITALIC, italic);
@ -816,8 +819,6 @@ namespace Greenshot.Drawing
textContainer.SetFieldValue(FieldType.SHADOW, shadow); textContainer.SetFieldValue(FieldType.SHADOW, shadow);
// Make sure the Text fits // Make sure the Text fits
textContainer.FitToText(); textContainer.FitToText();
// Align to Surface
textContainer.AlignToParent(horizontalAlignment, verticalAlignment);
//AggregatedProperties.UpdateElement(textContainer); //AggregatedProperties.UpdateElement(textContainer);
AddElement(textContainer); AddElement(textContainer);
@ -1817,43 +1818,72 @@ namespace Greenshot.Drawing
} }
else if (ClipboardHelper.ContainsImage(clipboard)) else if (ClipboardHelper.ContainsImage(clipboard))
{ {
int x = 10; Point pasteLocation = GetPasteLocation(0.1f, 0.1f);
int y = 10;
// FEATURE-995: Added a check for the current mouse cursor location, to paste the image on that location.
var mousePositionOnControl = PointToClient(MousePosition);
if (ClientRectangle.Contains(mousePositionOnControl))
{
x = mousePositionOnControl.X;
y = mousePositionOnControl.Y;
}
foreach (Image clipboardImage in ClipboardHelper.GetImages(clipboard)) foreach (Image clipboardImage in ClipboardHelper.GetImages(clipboard))
{ {
if (clipboardImage != null) if (clipboardImage != null)
{ {
DeselectAllElements(); DeselectAllElements();
IImageContainer container = AddImageContainer(clipboardImage as Bitmap, x, y); IImageContainer container = AddImageContainer(clipboardImage as Bitmap, pasteLocation.X, pasteLocation.Y);
SelectElement(container); SelectElement(container);
clipboardImage.Dispose(); clipboardImage.Dispose();
x += 10; pasteLocation.X += 10;
y += 10; pasteLocation.Y += 10;
} }
} }
} }
else if (ClipboardHelper.ContainsText(clipboard)) else if (ClipboardHelper.ContainsText(clipboard))
{ {
Point pasteLocation = GetPasteLocation(0.4f, 0.4f);
string text = ClipboardHelper.GetText(clipboard); string text = ClipboardHelper.GetText(clipboard);
if (text != null) if (text != null)
{ {
DeselectAllElements(); DeselectAllElements();
ITextContainer textContainer = AddTextContainer(text, HorizontalAlignment.Center, VerticalAlignment.CENTER, ITextContainer textContainer = AddTextContainer(text, pasteLocation.X, pasteLocation.Y,
FontFamily.GenericSansSerif, 12f, false, false, false, 2, Color.Black, Color.Transparent); FontFamily.GenericSansSerif, 12f, false, false, false, 2, Color.Black, Color.Transparent);
SelectElement(textContainer); SelectElement(textContainer);
} }
} }
} }
/// <summary>
/// Find a location to paste elements.
/// If mouse is over the surface - use it's position, otherwise use the visible area.
/// Return a point in image coordinate space.
/// </summary>
/// <param name="horizontalRatio">0.0f for the left edge of visible area, 1.0f for the right edge of visible area.</param>
/// <param name="verticalRatio">0.0f for the top edge of visible area, 1.0f for the bottom edge of visible area.</param>
private Point GetPasteLocation(float horizontalRatio = 0.5f, float verticalRatio = 0.5f)
{
var point = PointToClient(MousePosition);
var rc = GetVisibleRectangle();
if (!rc.Contains(point))
{
point = new Point(
rc.Left + (int)(rc.Width * horizontalRatio),
rc.Top + (int)(rc.Height * verticalRatio)
);
}
return ToImageCoordinates(point);
}
/// <summary>
/// Get the rectangle bounding the part of this Surface currently visible in the editor (in surface coordinate space).
/// </summary>
private Rectangle GetVisibleRectangle()
{
var bounds = Bounds;
var clientArea = Parent.ClientRectangle;
return new Rectangle(
Math.Max(0, -bounds.Left),
Math.Max(0, -bounds.Top),
clientArea.Width,
clientArea.Height
);
}
/// <summary> /// <summary>
/// Duplicate all the selecteded elements /// Duplicate all the selecteded elements
/// </summary> /// </summary>
@ -2127,7 +2157,38 @@ namespace Greenshot.Drawing
{ {
Point[] points = { rc.Location, rc.Location + rc.Size }; Point[] points = { rc.Location, rc.Location + rc.Size };
_zoomMatrix.TransformPoints(points); _zoomMatrix.TransformPoints(points);
return new Rectangle(points[0].X, points[0].Y, points[1].X - points[0].X, points[1].Y - points[1].Y); return new Rectangle(
points[0].X,
points[0].Y,
points[1].X - points[0].X,
points[1].Y - points[0].Y
);
}
}
public Point ToImageCoordinates(Point point)
{
Point[] points = { point };
_inverseZoomMatrix.TransformPoints(points);
return points[0];
}
public Rectangle ToImageCoordinates(Rectangle rc)
{
if (_inverseZoomMatrix.IsIdentity)
{
return rc;
}
else
{
Point[] points = { rc.Location, rc.Location + rc.Size };
_inverseZoomMatrix.TransformPoints(points);
return new Rectangle(
points[0].X,
points[0].Y,
points[1].X - points[0].X,
points[1].Y - points[0].Y
);
} }
} }
} }

View file

@ -102,7 +102,6 @@ namespace GreenshotPlugin.Interfaces.Drawing
get; get;
set; set;
} }
void AlignToParent(HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment);
void Invalidate(); void Invalidate();
bool ClickableAt(int x, int y); bool ClickableAt(int x, int y);
void MoveBy(int x, int y); void MoveBy(int x, int y);

View file

@ -84,8 +84,8 @@ namespace GreenshotPlugin.Interfaces
/// The TextContainer will be "re"sized to the text size. /// The TextContainer will be "re"sized to the text size.
/// </summary> /// </summary>
/// <param name="text">String to show</param> /// <param name="text">String to show</param>
/// <param name="horizontalAlignment">Left, Center, Right</param> /// <param name="x">Where to put the container, X coordinate in the Image coordinate space</param>
/// <param name="verticalAlignment">TOP, CENTER, BOTTOM</param> /// <param name="y">Where to put the container, Y coordinate in the Image coordinate space</param>
/// <param name="family">FontFamily</param> /// <param name="family">FontFamily</param>
/// <param name="size">Font Size in float</param> /// <param name="size">Font Size in float</param>
/// <param name="italic">bool true if italic</param> /// <param name="italic">bool true if italic</param>
@ -94,7 +94,7 @@ namespace GreenshotPlugin.Interfaces
/// <param name="borderSize">size of border (0 for none)</param> /// <param name="borderSize">size of border (0 for none)</param>
/// <param name="color">Color of string</param> /// <param name="color">Color of string</param>
/// <param name="fillColor">Color of background (e.g. Color.Transparent)</param> /// <param name="fillColor">Color of background (e.g. Color.Transparent)</param>
ITextContainer AddTextContainer(string text, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, FontFamily family, float size, bool italic, bool bold, bool shadow, int borderSize, Color color, Color fillColor); ITextContainer AddTextContainer(string text, int x, int y, FontFamily family, float size, bool italic, bool bold, bool shadow, int borderSize, Color color, Color fillColor);
IImageContainer AddImageContainer(Image image, int x, int y); IImageContainer AddImageContainer(Image image, int x, int y);
ICursorContainer AddCursorContainer(Cursor cursor, int x, int y); ICursorContainer AddCursorContainer(Cursor cursor, int x, int y);
@ -208,6 +208,16 @@ namespace GreenshotPlugin.Interfaces
/// </summary> /// </summary>
/// <param name="rc">A rectangle in the coordinate space of the image.</param> /// <param name="rc">A rectangle in the coordinate space of the image.</param>
Rectangle ToSurfaceCoordinates(Rectangle rc); Rectangle ToSurfaceCoordinates(Rectangle rc);
/// <summary>
/// Translate a point from surface coorditate space to image coordinate space.
/// </summary>
/// <param name="point">A point in the coordinate space of the surface.</param>
Point ToImageCoordinates(Point point);
/// <summary>
/// Translate a rectangle from surface coorditate space to image coordinate space.
/// </summary>
/// <param name="rc">A rectangle in the coordinate space of the surface.</param>
Rectangle ToImageCoordinates(Rectangle rc);
void MakeUndoable(IMemento memento, bool allowMerge); void MakeUndoable(IMemento memento, bool allowMerge);
} }