Skip to content
35 changes: 22 additions & 13 deletions Pinta.Tools/Tools/SelectTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,13 @@ protected override void OnBuildToolBar (Gtk.Box tb)
workspace.SelectionHandler.BuildToolbar (tb, Settings);
}

private static PointD AdjustMousePosition (Document document, in PointD position)
{
double x = Math.Round (Math.Clamp (position.X, 0, document.ImageSize.Width));
double y = Math.Round (Math.Clamp (position.Y, 0, document.ImageSize.Height));
return new (x, y);
}

protected override void OnMouseDown (Document document, ToolMouseEventArgs e)
{
// Ignore extra button clicks while drawing
Expand All @@ -73,20 +80,20 @@ protected override void OnMouseDown (Document document, ToolMouseEventArgs e)
hist = new SelectionHistoryItem (workspace, Icon, Name);
hist.TakeSnapshot ();

if (!handle.BeginDrag (e.PointDouble, document.ImageSize)) {
// Start drawing a new rectangle.
combine_mode = PintaCore.Workspace.SelectionHandler.DetermineCombineMode (e);
if (handle.BeginDrag (e.PointDouble, document.ImageSize))
return;

double x = Math.Round (Math.Clamp (e.PointDouble.X, 0, document.ImageSize.Width));
double y = Math.Round (Math.Clamp (e.PointDouble.Y, 0, document.ImageSize.Height));
handle.Rectangle = new (x, y, 0.0, 0.0);
// Start drawing a new rectangle.
combine_mode = PintaCore.Workspace.SelectionHandler.DetermineCombineMode (e);

document.PreviousSelection = document.Selection.Clone ();
document.Selection.SelectionPolygons.Clear ();
PointD adjusted = AdjustMousePosition (document, e.PointDouble);
handle.Rectangle = new (adjusted.X, adjusted.Y, 0.0, 0.0);

if (!handle.BeginDrag (new PointD (x, y), document.ImageSize))
throw new InvalidOperationException ("Should be able to start drawing a new rectangle!");
}
document.PreviousSelection = document.Selection.Clone ();
document.Selection.SelectionPolygons.Clear ();

if (!handle.BeginDrag (adjusted, document.ImageSize))
throw new InvalidOperationException ("Should be able to start drawing a new rectangle!");
}

protected override void OnMouseMove (Document document, ToolMouseEventArgs e)
Expand All @@ -96,7 +103,8 @@ protected override void OnMouseMove (Document document, ToolMouseEventArgs e)
return;
}

handle.UpdateDrag (e.PointDouble, e.IsShiftPressed);
PointD adjusted = AdjustMousePosition (document, e.PointDouble);
handle.UpdateDrag (adjusted, e.IsShiftPressed);

ReDraw (document);

Expand All @@ -105,7 +113,8 @@ protected override void OnMouseMove (Document document, ToolMouseEventArgs e)

protected override void OnMouseUp (Document document, ToolMouseEventArgs e)
{
if (handle.HasDragged (e.PointDouble)) {
PointD adjusted = AdjustMousePosition (document, e.PointDouble);
if (handle.HasDragged (adjusted)) {
ReDraw (document);

SelectionModeHandler.PerformSelectionMode (document, combine_mode, document.Selection.SelectionPolygons);
Expand Down
Loading