Skip to content

Commit

Permalink
Bundled position parameters/variables (#607)
Browse files Browse the repository at this point in the history
* Bundled position parameters in `PasteAction.Paste()`

* In `BaseEditEngine`

* - In `EllipseEngine.CreatePoints()`

* Implemented requested changes
  • Loading branch information
Lehonti authored Dec 16, 2023
1 parent c6092c6 commit 9afaf22
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 35 deletions.
19 changes: 13 additions & 6 deletions Pinta.Tools/Editable/EditEngines/BaseEditEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,15 +1351,22 @@ protected void CalculateModifiedCurrentPoint ()
//Don't bother calculating a modified point if there is no selected shape.
if (selEngine != null) {
if (ShapeType != ShapeTypes.OpenLineCurveSeries && selEngine.ControlPoints.Count == 4) {

// Constrain to a square / circle.

var origin = selEngine.ControlPoints[(SelectedPointIndex + 2) % 4].Position;

var dx = current_point.X - origin.X;
var dy = current_point.Y - origin.Y;
var length = Math.Max (Math.Abs (dx), Math.Abs (dy));
dx = length * Math.Sign (dx);
dy = length * Math.Sign (dy);
current_point = new PointD (origin.X + dx, origin.Y + dy);
PointD d = current_point - origin;

var length = Math.Max (Math.Abs (d.X), Math.Abs (d.Y));

d = new PointD (
X: length * Math.Sign (d.X),
Y: length * Math.Sign (d.Y)
);

current_point = origin + d;

} else {
// Calculate the modified position of currentPoint such that the angle between the adjacent point
// (if any) and currentPoint is snapped to the closest angle out of a certain number of angles.
Expand Down
49 changes: 27 additions & 22 deletions Pinta.Tools/Editable/Shapes/EllipseEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,20 +199,25 @@ private IEnumerable<GeneratedPoint> CreatePoints ()
//that will be calculated and stored into the Polygon collection.
double tInterval = .02d;

double rx = width / 2d; //1/2 of the bounding Rectangle Width.
double ry = height / 2d; //1/2 of the bounding Rectangle Height.
double cx = topLeft.X + rx; //The middle of the bounding Rectangle, horizontally speaking.
double cy = topLeft.Y + ry; //The middle of the bounding Rectangle, vertically speaking.
double c1 = 0.5522847498307933984022516322796d; //tan(pi / 8d) * 4d / 3d ~= 0.5522847498307933984022516322796d
double r_x = width / 2d; //1/2 of the bounding Rectangle Width.
double r_y = height / 2d; //1/2 of the bounding Rectangle Height.

//The middle of the bounding Rectangle...
PointD c = new (
X: topLeft.X + r_x, // ...Horizontally speaking
Y: topLeft.Y + r_y // ...Vertically speaking
);

const double c_1 = 0.5522847498307933984022516322796d; //tan(pi / 8d) * 4d / 3d ~= 0.5522847498307933984022516322796d

foreach (
var p in
calculateCurvePoints (
tInterval,
cx + rx, cy,
cx + rx, cy - c1 * ry,
cx + c1 * rx, cy - ry,
cx, cy - ry,
c.X + r_x, c.Y,
c.X + r_x, c.Y - c_1 * r_y,
c.X + c_1 * r_x, c.Y - r_y,
c.X, c.Y - r_y,
3
)
) yield return p;
Expand All @@ -221,10 +226,10 @@ var p in
var p in
calculateCurvePoints (
tInterval,
cx, cy - ry,
cx - c1 * rx, cy - ry,
cx - rx, cy - c1 * ry,
cx - rx, cy,
c.X, c.Y - r_y,
c.X - c_1 * r_x, c.Y - r_y,
c.X - r_x, c.Y - c_1 * r_y,
c.X - r_x, c.Y,
0
)
) yield return p;
Expand All @@ -233,10 +238,10 @@ var p in
var p in
calculateCurvePoints (
tInterval,
cx - rx, cy,
cx - rx, cy + c1 * ry,
cx - c1 * rx, cy + ry,
cx, cy + ry,
c.X - r_x, c.Y,
c.X - r_x, c.Y + c_1 * r_y,
c.X - c_1 * r_x, c.Y + r_y,
c.X, c.Y + r_y,
1
)
) yield return p;
Expand All @@ -245,16 +250,16 @@ var p in
var p in
calculateCurvePoints (
tInterval,
cx, cy + ry,
cx + c1 * rx, cy + ry,
cx + rx, cy + c1 * ry,
cx + rx, cy,
c.X, c.Y + r_y,
c.X + c_1 * r_x, c.Y + r_y,
c.X + r_x, c.Y + c_1 * r_y,
c.X + r_x, c.Y,
2
)
) yield return p;

// Close the curve.
yield return new GeneratedPoint (new PointD (cx + rx, cy), 3);
yield return new GeneratedPoint (new PointD (c.X + r_x, c.Y), 3);
}

/// <summary>
Expand Down
18 changes: 12 additions & 6 deletions Pinta/Actions/Edit/PasteAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ private void Activated (object sender, EventArgs e)
// Paste into the active document.
// The 'false' argument indicates that paste should be
// performed into the current (not a new) layer.
Paste (doc, false, (int) canvasPos.X, (int) canvasPos.Y);
Paste (
doc: doc,
toNewLayer: false,
pastePosition: canvasPos.ToInt ()
);
}

/// <summary>
Expand All @@ -72,7 +76,7 @@ private void Activated (object sender, EventArgs e)
/// <param name="y">Optional. Location within image to paste to.
/// Position will be adjusted if pasted image would hang
/// over right or bottom edges of canvas.</param>
public static async void Paste (Document doc, bool toNewLayer, int x = 0, int y = 0)
public static async void Paste (Document doc, bool toNewLayer, PointI pastePosition = new ())
{
// Create a compound history item for recording several
// operations so that they can all be undone/redone together.
Expand Down Expand Up @@ -116,8 +120,10 @@ public static async void Paste (Document doc, bool toNewLayer, int x = 0, int y

// If the pasted image would fall off bottom- or right-
// side of image, adjust paste position
x = Math.Max (0, Math.Min (x, canvas_size.Width - cb_image.Width));
y = Math.Max (0, Math.Min (y, canvas_size.Height - cb_image.Height));
pastePosition = new PointI (
X: Math.Clamp (pastePosition.X, 0, canvas_size.Width - cb_image.Width),
Y: Math.Clamp (pastePosition.Y, 0, canvas_size.Height - cb_image.Height)
);

// If requested, create a new layer, make it the current
// layer and record it's creation in the history
Expand All @@ -137,13 +143,13 @@ public static async void Paste (Document doc, bool toNewLayer, int x = 0, int y
g.Paint ();

doc.Layers.SelectionLayer.Transform.InitIdentity ();
doc.Layers.SelectionLayer.Transform.Translate (x, y);
doc.Layers.SelectionLayer.Transform.Translate (pastePosition.X, pastePosition.Y);

PintaCore.Tools.SetCurrentTool ("MoveSelectedTool");

var old_selection = doc.Selection.Clone ();

doc.Selection.CreateRectangleSelection (new RectangleD (x, y, cb_image.Width, cb_image.Height));
doc.Selection.CreateRectangleSelection (new RectangleD ((PointD) pastePosition, cb_image.Width, cb_image.Height));
doc.Selection.Visible = true;

doc.Workspace.Invalidate ();
Expand Down
6 changes: 5 additions & 1 deletion Pinta/Actions/Edit/PasteIntoNewLayerAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ private void Activated (object sender, EventArgs e)
// Paste into the active document.
// The 'true' argument indicates that paste should be
// performed into a new layer.
PasteAction.Paste (doc, true, (int) canvasPos.X, (int) canvasPos.Y);
PasteAction.Paste (
doc: doc,
toNewLayer: true,
pastePosition: canvasPos.ToInt ()
);
}
}

0 comments on commit 9afaf22

Please sign in to comment.