mirror of
https://github.com/greenshot/greenshot
synced 2025-08-19 21:13:23 -07:00
BUG-2203: Improving on the previous fixes, by locking to make sure things do not run parallel. This might risk a deadlock though.
This commit is contained in:
parent
b85f9e66a3
commit
fcab0a0f79
1 changed files with 99 additions and 74 deletions
|
@ -36,6 +36,9 @@ namespace Greenshot.Drawing {
|
||||||
public class FreehandContainer : DrawableContainer {
|
public class FreehandContainer : DrawableContainer {
|
||||||
private static readonly float [] PointOffset = {0.5f, 0.25f, 0.75f};
|
private static readonly float [] PointOffset = {0.5f, 0.25f, 0.75f};
|
||||||
|
|
||||||
|
[NonSerialized]
|
||||||
|
private readonly object _freehandPathLock = new object();
|
||||||
|
|
||||||
[NonSerialized]
|
[NonSerialized]
|
||||||
private GraphicsPath freehandPath = new GraphicsPath();
|
private GraphicsPath freehandPath = new GraphicsPath();
|
||||||
private Rectangle myBounds = Rectangle.Empty;
|
private Rectangle myBounds = Rectangle.Empty;
|
||||||
|
@ -104,14 +107,20 @@ namespace Greenshot.Drawing {
|
||||||
if (GeometryHelper.Distance2D(previousPoint.X, previousPoint.Y, mouseX, mouseY) >= 2*EditorConfig.FreehandSensitivity) {
|
if (GeometryHelper.Distance2D(previousPoint.X, previousPoint.Y, mouseX, mouseY) >= 2*EditorConfig.FreehandSensitivity) {
|
||||||
capturePoints.Add(new Point(mouseX, mouseY));
|
capturePoints.Add(new Point(mouseX, mouseY));
|
||||||
}
|
}
|
||||||
if (GeometryHelper.Distance2D(lastMouse.X, lastMouse.Y, mouseX, mouseY) >= EditorConfig.FreehandSensitivity) {
|
if (GeometryHelper.Distance2D(lastMouse.X, lastMouse.Y, mouseX, mouseY) < EditorConfig.FreehandSensitivity)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
//path.AddCurve(new Point[]{lastMouse, new Point(mouseX, mouseY)});
|
//path.AddCurve(new Point[]{lastMouse, new Point(mouseX, mouseY)});
|
||||||
freehandPath.AddLine(lastMouse, new Point(mouseX, mouseY));
|
|
||||||
lastMouse = new Point(mouseX, mouseY);
|
lastMouse = new Point(mouseX, mouseY);
|
||||||
|
lock (_freehandPathLock)
|
||||||
|
{
|
||||||
|
freehandPath.AddLine(lastMouse, new Point(mouseX, mouseY));
|
||||||
// Only re-calculate the bounds & redraw when we added something to the path
|
// Only re-calculate the bounds & redraw when we added something to the path
|
||||||
myBounds = Rectangle.Round(freehandPath.GetBounds());
|
myBounds = Rectangle.Round(freehandPath.GetBounds());
|
||||||
Invalidate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Invalidate();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,25 +139,34 @@ namespace Greenshot.Drawing {
|
||||||
/// Here we recalculate the freehand path by smoothing out the lines with Beziers.
|
/// Here we recalculate the freehand path by smoothing out the lines with Beziers.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private void RecalculatePath() {
|
private void RecalculatePath() {
|
||||||
|
lock (_freehandPathLock)
|
||||||
|
{
|
||||||
isRecalculated = true;
|
isRecalculated = true;
|
||||||
// Dispose the previous path, if we have one
|
// Dispose the previous path, if we have one
|
||||||
freehandPath?.Dispose();
|
freehandPath?.Dispose();
|
||||||
freehandPath = new GraphicsPath();
|
freehandPath = new GraphicsPath();
|
||||||
|
|
||||||
// Here we can put some cleanup... like losing all the uninteresting points.
|
// Here we can put some cleanup... like losing all the uninteresting points.
|
||||||
if (capturePoints.Count >= 3) {
|
if (capturePoints.Count >= 3)
|
||||||
|
{
|
||||||
int index = 0;
|
int index = 0;
|
||||||
while ((capturePoints.Count - 1) % 3 != 0) {
|
while ((capturePoints.Count - 1) % 3 != 0)
|
||||||
|
{
|
||||||
// duplicate points, first at 50% than 25% than 75%
|
// duplicate points, first at 50% than 25% than 75%
|
||||||
capturePoints.Insert((int)(capturePoints.Count*PointOffset[index]), capturePoints[(int)(capturePoints.Count*PointOffset[index++])]);
|
capturePoints.Insert((int)(capturePoints.Count * PointOffset[index]), capturePoints[(int)(capturePoints.Count * PointOffset[index++])]);
|
||||||
}
|
}
|
||||||
freehandPath.AddBeziers(capturePoints.ToArray());
|
freehandPath.AddBeziers(capturePoints.ToArray());
|
||||||
} else if (capturePoints.Count == 2) {
|
}
|
||||||
|
else if (capturePoints.Count == 2)
|
||||||
|
{
|
||||||
freehandPath.AddLine(capturePoints[0], capturePoints[1]);
|
freehandPath.AddLine(capturePoints[0], capturePoints[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recalculate the bounds
|
// Recalculate the bounds
|
||||||
myBounds = Rectangle.Round(freehandPath.GetBounds());
|
myBounds = Rectangle.Round(freehandPath.GetBounds());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -164,34 +182,41 @@ namespace Greenshot.Drawing {
|
||||||
|
|
||||||
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
|
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
|
||||||
Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR);
|
Color lineColor = GetFieldValueAsColor(FieldType.LINE_COLOR);
|
||||||
using (Pen pen = new Pen(lineColor)) {
|
using (var pen = new Pen(lineColor)) {
|
||||||
pen.Width = lineThickness;
|
pen.Width = lineThickness;
|
||||||
if (pen.Width > 0) {
|
if (!(pen.Width > 0))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
// Make sure the lines are nicely rounded
|
// Make sure the lines are nicely rounded
|
||||||
pen.EndCap = LineCap.Round;
|
pen.EndCap = LineCap.Round;
|
||||||
pen.StartCap = LineCap.Round;
|
pen.StartCap = LineCap.Round;
|
||||||
pen.LineJoin = LineJoin.Round;
|
pen.LineJoin = LineJoin.Round;
|
||||||
|
|
||||||
// Move to where we need to draw
|
// Move to where we need to draw
|
||||||
graphics.TranslateTransform(Left,Top);
|
graphics.TranslateTransform(Left, Top);
|
||||||
if (isRecalculated && Selected && renderMode == RenderMode.EDIT) {
|
lock (_freehandPathLock)
|
||||||
DrawSelectionBorder(graphics, pen);
|
{
|
||||||
|
if (isRecalculated && Selected && renderMode == RenderMode.EDIT)
|
||||||
|
{
|
||||||
|
DrawSelectionBorder(graphics, pen, freehandPath);
|
||||||
}
|
}
|
||||||
graphics.DrawPath(pen, freehandPath);
|
graphics.DrawPath(pen, freehandPath);
|
||||||
|
}
|
||||||
|
|
||||||
// Move back, otherwise everything is shifted
|
// Move back, otherwise everything is shifted
|
||||||
graphics.TranslateTransform(-Left,-Top);
|
graphics.TranslateTransform(-Left,-Top);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Draw a selectionborder around the freehand path
|
/// Draw a selectionborder around the freehand path
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="graphics"></param>
|
/// <param name="graphics">Graphics</param>
|
||||||
/// <param name="linePen"></param>
|
/// <param name="linePen">Pen</param>
|
||||||
protected void DrawSelectionBorder(Graphics graphics, Pen linePen) {
|
/// <param name="path">GraphicsPath</param>
|
||||||
using (Pen selectionPen = (Pen) linePen.Clone()) {
|
protected static void DrawSelectionBorder(Graphics graphics, Pen linePen, GraphicsPath path) {
|
||||||
using (GraphicsPath selectionPath = (GraphicsPath) freehandPath.Clone()) {
|
using (var selectionPen = (Pen) linePen.Clone()) {
|
||||||
|
using (var selectionPath = (GraphicsPath)path.Clone()) {
|
||||||
selectionPen.Width += 5;
|
selectionPen.Width += 5;
|
||||||
selectionPen.Color = Color.FromArgb(120, Color.LightSeaGreen);
|
selectionPen.Color = Color.FromArgb(120, Color.LightSeaGreen);
|
||||||
graphics.DrawPath(selectionPen, selectionPath);
|
graphics.DrawPath(selectionPen, selectionPath);
|
||||||
|
@ -221,38 +246,38 @@ namespace Greenshot.Drawing {
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// FreehandContainer are regarded equal if they are of the same type and their paths are equal.
|
/// FreehandContainer are regarded equal if they are of the same type and their paths are equal.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="obj"></param>
|
/// <param name="obj">object</param>
|
||||||
/// <returns></returns>
|
/// <returns>bool</returns>
|
||||||
public override bool Equals(object obj) {
|
public override bool Equals(object obj) {
|
||||||
bool ret = false;
|
bool ret = false;
|
||||||
if(obj != null && GetType() == obj.GetType()) {
|
if (obj == null || GetType() != obj.GetType())
|
||||||
FreehandContainer other = obj as FreehandContainer;
|
{
|
||||||
if(other != null && freehandPath.Equals(other.freehandPath)) {
|
return false;
|
||||||
ret = true;
|
|
||||||
}
|
}
|
||||||
|
var other = obj as FreehandContainer;
|
||||||
|
if (other != null && Equals(freehandPath, other.freehandPath)) {
|
||||||
|
ret = true;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override int GetHashCode() {
|
public override int GetHashCode() {
|
||||||
if (freehandPath == null)
|
lock (_freehandPathLock)
|
||||||
{
|
{
|
||||||
return 0;
|
return freehandPath?.GetHashCode() ?? 0;
|
||||||
}
|
}
|
||||||
return freehandPath.GetHashCode();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override bool ClickableAt(int x, int y) {
|
public override bool ClickableAt(int x, int y) {
|
||||||
bool returnValue = base.ClickableAt(x, y);
|
bool returnValue = base.ClickableAt(x, y);
|
||||||
if (returnValue) {
|
if (returnValue) {
|
||||||
if (freehandPath == null)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
|
int lineThickness = GetFieldValueAsInt(FieldType.LINE_THICKNESS);
|
||||||
using (var pen = new Pen(Color.White)) {
|
using (var pen = new Pen(Color.White)) {
|
||||||
pen.Width = lineThickness + 10;
|
pen.Width = lineThickness + 10;
|
||||||
returnValue = freehandPath.IsOutlineVisible(x-Left,y-Top, pen);
|
lock (_freehandPathLock)
|
||||||
|
{
|
||||||
|
returnValue = freehandPath.IsOutlineVisible(x - Left, y - Top, pen);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return returnValue;
|
return returnValue;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue