Skip to content

Commit b82beb6

Browse files
committed
Refactor how the DocumentSelection bounds are tracked
- Store a rectangle rather than separate origin / end points. This is simpler, and for PR #1515 this will likely be extended further to also store an orientation - Clarify that the bounds are only used for interactive manipulation by tools, and might not reflect the selection's entire bounds when e.g. using intersection modes - Adjust the `TransformPoint()` signature to be easier to use
1 parent 37da4ad commit b82beb6

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

Pinta.Core/Classes/DocumentSelection.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,15 @@ internal DocumentSelection ()
4242
public List<List<IntPoint>> SelectionPolygons { get; set; } = [];
4343
public Clipper SelectionClipper { get; } = new ();
4444

45-
public PointD Origin { get; set; }
46-
public PointD End { get; set; }
45+
/// <summary>
46+
/// Bounding rectangle which is used by tools to display interactive
47+
/// handles for manipulating the selection.
48+
/// <remarks>
49+
/// This might not reflect the bounds of the entire selection, for example
50+
/// when creating a rectangle selection in "union" mode.
51+
/// </remarks>
52+
/// </summary>
53+
public RectangleD HandleBounds { get; set; }
4754

4855
private bool visible = true;
4956
public bool Visible {
@@ -94,8 +101,7 @@ public DocumentSelection Clone ()
94101
{
95102
return new () {
96103
SelectionPolygons = [.. SelectionPolygons],
97-
Origin = new PointD (Origin.X, Origin.Y),
98-
End = new PointD (End.X, End.Y),
104+
HandleBounds = HandleBounds,
99105
visible = visible,
100106
};
101107
}
@@ -166,15 +172,15 @@ public DocumentSelection Transform (Matrix transform)
166172
newPolygons.Add (newPolygon);
167173
}
168174

169-
var origin = Origin;
170-
var end = End;
171-
transform.TransformPoint (ref origin);
172-
transform.TransformPoint (ref end);
175+
// Note this currently doesn't behave well with rotations, since we only
176+
// store an axis-aligned bounding rectangle.
177+
RectangleD transformedBounds = RectangleD.FromPoints (
178+
transform.TransformPoint (HandleBounds.Location ()),
179+
transform.TransformPoint (HandleBounds.EndLocation ()));
173180

174181
return new () {
175182
SelectionPolygons = newPolygons,
176-
Origin = origin,
177-
End = end,
183+
HandleBounds = transformedBounds,
178184
visible = visible,
179185
};
180186
}
@@ -315,8 +321,7 @@ public void CreateRectangleSelection (RectangleD r)
315321
SelectionPolygons.Clear ();
316322
SelectionPolygons.Add (CreateRectanglePolygon (r));
317323

318-
Origin = new PointD (r.X, r.Y);
319-
End = new PointD (r.Right, r.Bottom);
324+
HandleBounds = r;
320325

321326
MarkDirty ();
322327
}
@@ -400,8 +405,7 @@ private static List<IntPoint> CreateRectanglePolygon (RectangleD r)
400405
public void Clear ()
401406
{
402407
SelectionPolygons.Clear ();
403-
Origin = new PointD (0, 0);
404-
End = new PointD (0, 0);
408+
HandleBounds = RectangleD.Zero;
405409
MarkDirty ();
406410
}
407411

Pinta.Core/Extensions/Cairo/CairoExtensions.Geometry.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,14 @@ public static Matrix Clone (this Matrix m)
293293
return result;
294294
}
295295

296-
public static void TransformPoint (
296+
public static PointD TransformPoint (
297297
this Matrix m,
298-
ref PointD p)
298+
in PointD p)
299299
{
300300
double newX = p.X;
301301
double newY = p.Y;
302302
m.TransformPoint (ref newX, ref newY);
303-
p = new PointD (newX, newY);
303+
return new (newX, newY);
304304
}
305305

306306
private static void GetRectangle (this Region region, int i, out CairoRectangleInt rect)

Pinta.Tools/Tools/SelectTool.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,7 @@ protected override void OnMouseUp (Document document, ToolMouseEventArgs e)
121121

122122
SelectionModeHandler.PerformSelectionMode (document, combine_mode, document.Selection.SelectionPolygons);
123123

124-
document.Selection.Origin = handle.Rectangle.Location ();
125-
document.Selection.End = handle.Rectangle.EndLocation ();
124+
document.Selection.HandleBounds = handle.Rectangle;
126125
document.Workspace.Invalidate (last_dirty.Union (dirty));
127126
last_dirty = dirty;
128127

@@ -228,7 +227,7 @@ private void AfterSelectionChange (object? sender, EventArgs event_args)
228227
private void LoadFromDocument (Document document)
229228
{
230229
DocumentSelection selection = document.Selection;
231-
handle.Rectangle = RectangleD.FromPoints (selection.Origin, selection.End);
230+
handle.Rectangle = selection.HandleBounds;
232231
ShowHandles (document.Selection.Visible && tools.CurrentTool == this);
233232
}
234233
}

0 commit comments

Comments
 (0)