Skip to content

Commit 9a874c7

Browse files
committed
- further refactoring of how zoom and cursors work together
- fix bug in slash brush where the angle could be perceived as slightly off due to zero-area correction - fix bug in cursor creation where at high zoom levels the slash brush cursor was no longer correctly drawn
1 parent ed14b61 commit 9a874c7

File tree

16 files changed

+41
-66
lines changed

16 files changed

+41
-66
lines changed

Pinta.Core/Classes/BaseTool.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace Pinta.Core;
3737
public abstract class BaseTool
3838
{
3939
private readonly IToolService tools;
40-
private readonly IWorkspaceService workspace;
40+
protected readonly IWorkspaceService workspace;
4141

4242
protected IResourceService Resources { get; }
4343
protected ISettingsService Settings { get; }
@@ -55,11 +55,9 @@ protected BaseTool (IServiceProvider services)
5555
CurrentCursor = DefaultCursor;
5656

5757
// Update cursor when active document changes
58-
workspace.ActiveDocumentChanged += (_, _) => {
59-
if (tools.CurrentTool == this)
60-
SetCursor (CurrentCursor);
61-
};
62-
58+
workspace.ActiveDocumentChanged += (_, _) => RefreshCursorIfActive ();
59+
// Update cursor on zoom
60+
workspace.ViewSizeChanged += (_, _) => RefreshCursorIfActive ();
6361
// Give tools a chance to save their settings on application quit
6462
Settings.SaveSettingsBeforeQuit += (_, _)
6563
=> OnSaveSettings (Settings);
@@ -92,12 +90,6 @@ public virtual Cursor? DefaultCursor
9290
/// </summary>
9391
public Cursor? CurrentCursor { get; private set; }
9492

95-
/// <summary>
96-
/// Specifies whether this application needs to update this tool's
97-
/// cursor after a zoom operation.
98-
/// </summary>
99-
public virtual bool CursorChangesOnZoom
100-
=> false;
10193

10294
/// <summary>
10395
/// Specifies whether the tool manipulates selections.
@@ -170,14 +162,6 @@ public virtual bool UseAlphaBlending {
170162
}
171163
}
172164

173-
public virtual void OnCanvasZoom ()
174-
{
175-
if (CursorChangesOnZoom) {
176-
//The current tool's cursor changes when the zoom changes.
177-
SetCursor (DefaultCursor);
178-
}
179-
}
180-
181165
/// <summary>
182166
/// Called when the tool is selected from the toolbox.
183167
/// </summary>
@@ -335,6 +319,18 @@ public void SetCursor (Cursor? cursor)
335319
workspace.ActiveWorkspace.Canvas.Cursor = cursor;
336320
}
337321

322+
private void RefreshCursorIfActive ()
323+
{
324+
if (tools.CurrentTool == this) {
325+
RefreshCursor ();
326+
}
327+
}
328+
329+
protected virtual void RefreshCursor ()
330+
{
331+
SetCursor (DefaultCursor);
332+
}
333+
338334
#region Toolbar
339335
private ToolBoxButton? tool_item;
340336
private ToolBarDropDownButton? antialiasing_button;

Pinta.Core/Classes/Document.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public Document (
8080
Selection = new DocumentSelection ();
8181

8282
Layers = new DocumentLayers (tools, this);
83-
Workspace = new DocumentWorkspace (actions, tools, this);
83+
Workspace = new DocumentWorkspace (actions, this);
8484
IsDirty = false;
8585
HasBeenSavedInSession = false;
8686
ImageSize = size;

Pinta.Core/Classes/DocumentWorkspace.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public sealed class DocumentWorkspace
3333
private readonly Document document;
3434

3535
private readonly ActionManager actions;
36-
private readonly ToolManager tools;
3736

3837
private enum ZoomType
3938
{
@@ -44,11 +43,9 @@ private enum ZoomType
4443

4544
internal DocumentWorkspace (
4645
ActionManager actions,
47-
ToolManager tools,
4846
Document document)
4947
{
5048
this.actions = actions;
51-
this.tools = tools;
5249

5350
this.document = document;
5451

@@ -116,7 +113,6 @@ public double Scale {
116113
ViewSize = GetNewViewSize (document.ImageSize, value);
117114

118115
Invalidate ();
119-
tools.CurrentTool?.OnCanvasZoom ();
120116
}
121117
}
122118

Pinta.Core/Extensions/GdkExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public static Gdk.Texture CreateIconWithShape (
130130
RectangleI initialShapeBBox = new (
131131
imgToShapeX - Math.Max (halfOfShapeWidth, halfOfShapeHeight),
132132
imgToShapeY - Math.Max (halfOfShapeWidth, halfOfShapeHeight),
133-
clampedWidth,
134-
clampedHeight);
133+
Math.Max (clampedWidth, clampedHeight),
134+
Math.Max (clampedWidth, clampedHeight));
135135

136136
// Inflate shape bounding box to allow for anti-aliasing
137137
RectangleI inflatedBBox = initialShapeBBox.Inflated (2, 2);
@@ -186,7 +186,7 @@ public static Gdk.Texture CreateIconWithShape (
186186
shapeRect = shapeRect.Inflated (-1, -1);
187187
PointD[] pointsOfInflatedRotatedRectangle = RotateRectangle (shapeRect, shapeAngle);
188188
g.DrawPolygonal (new ReadOnlySpan<PointD> (pointsOfRotatedRectangle), outerColor, LineCap.Butt);
189-
g.DrawPolygonal (new ReadOnlySpan<PointD> (pointsOfInflatedRotatedRectangle), innerColor, LineCap.Butt);
189+
g.DrawPolygonal (new ReadOnlySpan<PointD> ([.. pointsOfInflatedRotatedRectangle, pointsOfInflatedRotatedRectangle[0]]), innerColor, LineCap.Butt);
190190
}
191191
break;
192192
}

Pinta.Core/Managers/WorkspaceManager.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public interface IWorkspaceService
5050
public event EventHandler? LayerAdded;
5151
public event EventHandler? LayerRemoved;
5252
public event EventHandler? SelectedLayerChanged;
53+
public event EventHandler? ViewSizeChanged;
5354
public event PropertyChangedEventHandler? LayerPropertyChanged;
5455
}
5556

@@ -207,6 +208,7 @@ public void ActivateDocument (Document document)
207208
document.Layers.LayerRemoved += Document_LayerRemoved;
208209
document.Layers.SelectedLayerChanged += Document_SelectedLayerChanged;
209210
document.Layers.LayerPropertyChanged += Document_LayerPropertyChanged;
211+
document.Workspace.ViewSizeChanged += Document_ViewSizeChanged;
210212

211213
open_documents.Add (document);
212214

@@ -236,6 +238,11 @@ private void Document_LayerAdded (object? sender, IndexEventArgs e)
236238
LayerAdded?.Invoke (sender, e);
237239
}
238240

241+
private void Document_ViewSizeChanged (object? sender, EventArgs ev)
242+
{
243+
ViewSizeChanged?.Invoke (sender, ev);
244+
}
245+
239246
public void CloseDocument (Document document)
240247
{
241248
int index = open_documents.IndexOf (document);
@@ -263,6 +270,7 @@ public void CloseDocument (Document document)
263270
document.Layers.LayerRemoved -= Document_LayerRemoved;
264271
document.Layers.SelectedLayerChanged -= Document_SelectedLayerChanged;
265272
document.Layers.LayerPropertyChanged -= Document_LayerPropertyChanged;
273+
document.Workspace.ViewSizeChanged -= Document_ViewSizeChanged;
266274
document.Close ();
267275

268276
OnDocumentClosed (new DocumentEventArgs (document));
@@ -453,6 +461,7 @@ private Task ShowFilePermissionErrorDialog (
453461
public event EventHandler? LayerRemoved;
454462
public event EventHandler? SelectedLayerChanged;
455463
public event PropertyChangedEventHandler? LayerPropertyChanged;
464+
public event EventHandler? ViewSizeChanged;
456465

457466
public event EventHandler<DocumentEventArgs>? DocumentActivated;
458467
public event EventHandler<DocumentEventArgs>? DocumentClosed;

Pinta.Tools/Brushes/SlashBrush.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ present logic...
8686
new_bottom.X * old_bottom.Y - new_bottom.Y * old_bottom.X +
8787
old_bottom.X * old_top.Y - old_bottom.Y * old_top.X));
8888

89-
if (area < 2) {
89+
if (area < 2 && (last_pos.X != current_pos.X || last_pos.Y != current_pos.Y)) {
9090
old_top = OffsetPoint (old_top, -1, 1, angle + 90);
9191
new_top = OffsetPoint (new_top, -1, 1, angle + 90);
9292
old_bottom = OffsetPoint (old_bottom, 1, 1, angle + 90);

Pinta.Tools/Tools/CloneStampTool.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ public CloneStampTool (IServiceProvider services) : base (services)
4747
public override string Icon => Pinta.Resources.Icons.ToolCloneStamp;
4848
// Translators: {0} is 'Ctrl', or a platform-specific key such as 'Command' on macOS.
4949
public override string StatusBarText => Translations.GetString ("{0} + left click to set origin, left click to paint.", system_manager.CtrlLabel ());
50-
public override bool CursorChangesOnZoom => true;
5150
public override Gdk.Key ShortcutKey => new (Gdk.Constants.KEY_L);
5251
public override int Priority => 47;
5352
protected override bool ShowAntialiasingButton => true;

Pinta.Tools/Tools/ColorPickerTool.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ public ColorPickerTool (IServiceProvider services) : base (services)
4848
public override string Name => Translations.GetString ("Color Picker");
4949
public override string Icon => Pinta.Resources.Icons.ToolColorPicker;
5050
public override string StatusBarText => Translations.GetString ("Left click to set primary color.\nRight click to set secondary color.");
51-
public override bool CursorChangesOnZoom => true;
5251
public override Gdk.Key ShortcutKey => new (Gdk.Constants.KEY_K);
5352
public override int Priority => 33;
5453
private int SampleSize => SampleSizeDropDown.SelectedItem.GetTagOrDefault (1);

Pinta.Tools/Tools/EraserTool.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ public override string Icon
5656
public override string StatusBarText
5757
=> Translations.GetString ("Left click to erase to transparent, right click to erase to secondary color. ");
5858

59-
public override bool CursorChangesOnZoom
60-
=> true;
61-
6259
public override Gdk.Key ShortcutKey
6360
=> new (Gdk.Constants.KEY_E);
6461

Pinta.Tools/Tools/LassoSelectTool.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,8 @@
3333

3434
namespace Pinta.Tools;
3535

36-
public sealed class LassoSelectTool : BaseTool
36+
public sealed class LassoSelectTool (IServiceProvider services) : BaseTool (services)
3737
{
38-
private readonly IWorkspaceService workspace;
39-
4038
private bool is_dragging = false;
4139
private CombineMode combine_mode;
4240
private SelectionHistoryItem? hist;
@@ -47,11 +45,6 @@ public sealed class LassoSelectTool : BaseTool
4745
private Label? lasso_mode_label;
4846
private ToolBarDropDownButton? lasso_mode_buttom;
4947

50-
public LassoSelectTool (IServiceProvider services) : base (services)
51-
{
52-
workspace = services.GetService<IWorkspaceService> ();
53-
}
54-
5548
public override string Name => Translations.GetString ("Lasso Select");
5649
public override string Icon => Pinta.Resources.Icons.ToolSelectLasso;
5750
public override string StatusBarText => Translations.GetString ("In Freeform mode, click and drag to draw the outline for a selection area." +

0 commit comments

Comments
 (0)