Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Pinta.Tools/Handles/RectangleHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enum HandlePoint
/// <summary>
/// A handle for specifying a rectangular region.
/// </summary>
public class RectangleHandle : IToolHandle
public sealed class RectangleHandle : IToolHandle
{
private readonly IWorkspaceService workspace;

Expand Down
42 changes: 24 additions & 18 deletions Pinta.Tools/Tools/SelectTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Gtk;
using Pinta.Core;

namespace Pinta.Tools;
Expand All @@ -40,7 +37,7 @@ public abstract class SelectTool : BaseTool

private SelectionHistoryItem? hist = default;
private CombineMode combine_mode = default;
private Separator? mode_sep;
private Gtk.Separator? mode_sep;
private ToolBarDropDownButton? auto_scroll_button;

public override Gdk.Key ShortcutKey => new (Gdk.Constants.KEY_S);
Expand Down Expand Up @@ -72,6 +69,13 @@ protected override void OnBuildToolBar (Gtk.Box tb)
tb.Append (AutoScrollButton);
}

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 @@ -81,20 +85,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 @@ -104,7 +108,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 Down Expand Up @@ -136,7 +141,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) && handle.Rectangle.Width > 0 && handle.Rectangle.Height > 0) {
ReDraw (document);

SelectionModeHandler.PerformSelectionMode (document, combine_mode, document.Selection.SelectionPolygons);
Expand Down Expand Up @@ -247,7 +253,7 @@ private void LoadFromDocument (Document document)
ShowHandles (document.Selection.Visible && tools.CurrentTool == this);
}

private Separator Separator => mode_sep ??= GtkExtensions.CreateToolBarSeparator ();
private Gtk.Separator Separator => mode_sep ??= GtkExtensions.CreateToolBarSeparator ();

private ToolBarDropDownButton AutoScrollButton {
get {
Expand Down
Loading