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
34 changes: 33 additions & 1 deletion Pinta.Core/Classes/BasePaintBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using Cairo;

Expand Down Expand Up @@ -65,6 +64,10 @@ public abstract class BasePaintBrush
/// </summary>
public virtual List<ToolOption> Options { get; protected set; } = [];

public delegate void CursorChangeHandler ();

public event CursorChangeHandler? OnCursorChanged;

public void DoMouseUp ()
{
OnMouseUp ();
Expand All @@ -83,6 +86,25 @@ public RectangleI DoMouseMove (
return OnMouseMove (g, surface, strokeArgs);
}

/// <summary>
/// Notify the paint brush that the tool's line width has changed so that it
/// can refresh its cursor.
/// </summary>
/// <param name="lineWidth">New line width set in brush tool</param>
public virtual void UpdateLineWidth (int lineWidth)
{
CursorChanged ();
}

/// <summary>
/// Get the cursor appropriate for the brush's current settings.
/// </summary>
/// <returns>Cursor returned by the brush, or null if the tool should use the default cursor.</returns>
public virtual Gdk.Cursor? GetCursor ()
Comment thread
spaghetti22 marked this conversation as resolved.
{
return null;
}

/// <summary>
/// Event handler called when the mouse is released.
/// </summary>
Expand All @@ -109,4 +131,14 @@ protected abstract RectangleI OnMouseMove (
Context g,
ImageSurface surface,
BrushStrokeArgs strokeArgs);


/// <summary>
/// Notify listeners that the brush's cursor has been changed (most likely
/// in response to the user having changed a setting).
/// </summary>
protected void CursorChanged ()
{
OnCursorChanged?.Invoke ();
}
}
2 changes: 1 addition & 1 deletion Pinta.Core/Classes/BaseTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected BaseTool (IServiceProvider services)
// Update cursor when active document changes
workspace.ActiveDocumentChanged += (_, _) => {
if (tools.CurrentTool == this)
SetCursor (DefaultCursor);
SetCursor (CurrentCursor);
};

// Give tools a chance to save their settings on application quit
Expand Down
2 changes: 1 addition & 1 deletion Pinta.Core/Classes/DocumentWorkspace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public double Scale {

if (tools.CurrentTool?.CursorChangesOnZoom == true) {
//The current tool's cursor changes when the zoom changes.
tools.CurrentTool.SetCursor (tools.CurrentTool.DefaultCursor);
tools.CurrentTool.SetCursor (tools.CurrentTool.CurrentCursor);
}
}
}
Expand Down
57 changes: 48 additions & 9 deletions Pinta.Core/Extensions/GdkExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Cairo;
Expand Down Expand Up @@ -92,6 +94,21 @@ public static Gdk.Texture CreateIconWithShape (
int imgToShapeY,
out int shapeX,
out int shapeY)
{
Gdk.Texture result = CreateIconWithShape (imgName, shape, shapeWidth, shapeWidth, 0, imgToShapeX, imgToShapeY, out shapeX, out shapeY);
return result;
}

public static Gdk.Texture CreateIconWithShape (
string imgName,
CursorShape shape,
int shapeWidth,
int shapeHeight,
int shapeAngle,
int imgToShapeX,
int imgToShapeY,
out int shapeX,
out int shapeY)
{
Gdk.Texture img = PintaCore.Resources.GetIcon (imgName);

Expand All @@ -102,17 +119,19 @@ public static Gdk.Texture CreateIconWithShape (

int clampedWidth = (int) Math.Min (800d, shapeWidth * zoom);
int halfOfShapeWidth = clampedWidth / 2;
int clampedHeight = (int) Math.Min (800d, shapeHeight * zoom);
int halfOfShapeHeight = clampedHeight / 2;

// Calculate bounding boxes around the both image and shape
// relative to the image top-left corner.

RectangleI imgBBox = new (0, 0, img.Width, img.Height);

RectangleI initialShapeBBox = new (
imgToShapeX - halfOfShapeWidth,
imgToShapeY - halfOfShapeWidth,
imgToShapeX - Math.Max (halfOfShapeWidth, halfOfShapeHeight),
imgToShapeY - Math.Max (halfOfShapeWidth, halfOfShapeHeight),
clampedWidth,
clampedWidth);
clampedHeight);

// Inflate shape bounding box to allow for anti-aliasing
RectangleI inflatedBBox = initialShapeBBox.Inflated (2, 2);
Expand All @@ -138,15 +157,15 @@ public static Gdk.Texture CreateIconWithShape (
using Context g = new (i);

// Don't show shape if shapeWidth less than 3,
if (clampedWidth > 3) {
if (clampedHeight > 3) {

int diam = Math.Max (1, clampedWidth - 2);

RectangleD shapeRect = new (
shapeX - halfOfShapeWidth,
shapeY - halfOfShapeWidth,
shapeY - halfOfShapeHeight,
diam,
diam);
clampedHeight);

Color outerColor = new (255, 255, 255, 0.75);
Color innerColor = new (0, 0, 0);
Expand All @@ -158,9 +177,17 @@ public static Gdk.Texture CreateIconWithShape (
g.DrawEllipse (shapeRect, innerColor, 1);
break;
case CursorShape.Rectangle:
g.DrawRectangle (shapeRect, outerColor, 1);
shapeRect = shapeRect.Inflated (-1, -1);
g.DrawRectangle (shapeRect, innerColor, 1);
if (shapeAngle == 0) {
g.DrawRectangle (shapeRect, outerColor, 1);
shapeRect = shapeRect.Inflated (-1, -1);
g.DrawRectangle (shapeRect, innerColor, 1);
} else {
PointD[] pointsOfRotatedRectangle = RotateRectangle (shapeRect, shapeAngle);
shapeRect = shapeRect.Inflated (-1, -1);
PointD[] pointsOfInflatedRotatedRectangle = RotateRectangle (shapeRect, shapeAngle);
g.DrawPolygonal (new ReadOnlySpan<PointD> (pointsOfRotatedRectangle), outerColor, LineCap.Butt);
g.DrawPolygonal (new ReadOnlySpan<PointD> (pointsOfInflatedRotatedRectangle), innerColor, LineCap.Butt);
}
break;
}
}
Expand Down Expand Up @@ -284,4 +311,16 @@ public static Gdk.RGBA ToGdkRGBA (this Cairo.Color color)
Alpha = (float) color.A
};
}

private static PointD[] RotateRectangle (RectangleD rectangle, int angle_in_degrees)
Comment thread
spaghetti22 marked this conversation as resolved.
{
float angle_in_radians = float.DegreesToRadians (-angle_in_degrees);
Matrix3x2D rotation = Matrix3x2D.CreateRotation (new RadiansAngle (angle_in_radians), rectangle.GetCenter ());
return [
rectangle.Location().Transformed(rotation),
new PointD(rectangle.Location().X, rectangle.EndLocation().Y).Transformed(rotation),
rectangle.EndLocation().Transformed(rotation),
new PointD(rectangle.EndLocation().X, rectangle.Location().Y).Transformed(rotation)
];
}
}
158 changes: 158 additions & 0 deletions Pinta.Tools/Brushes/SlashBrush.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
//
// SlashBrush.cs
//
// Author:
// Paul Korecky <https://github.com/spaghetti22>
//
// Copyright (c) 2026 Paul Korecky
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using Cairo;
using Pinta.Core;

namespace Pinta.Tools.Brushes;

internal sealed class SlashBrush : BasePaintBrush
{
public override string Name
=> Translations.GetString ("Slash");

private int line_width;
private int angle;

private const string AngleSettingName = "slash-brush-angle";

public SlashBrush (ISettingsService settingsService)
{
IntegerOption angleOption = new IntegerOption (
AngleSettingName,
0,
180,
45,
Translations.GetString ("Angle")
);
angleOption.OnValueChanged += an => {
angle = an;
CursorChanged ();
};
angleOption.LoadValueFromSettings (settingsService);
Options = [angleOption];
}

protected override RectangleI OnMouseMove (
Context g,
ImageSurface surface,
BrushStrokeArgs strokeArgs)
{
line_width = (int) g.LineWidth;

PointD last_pos = strokeArgs.LastPosition.ToDouble ();
PointD current_pos = strokeArgs.CurrentPosition.ToDouble ();

PointD old_top = OffsetPoint (last_pos, -1, line_width / 2, angle);
PointD old_bottom = OffsetPoint (last_pos, 1, line_width / 2, angle);
PointD new_top = OffsetPoint (current_pos, -1, line_width / 2, angle);
PointD new_bottom = OffsetPoint (current_pos, 1, line_width / 2, angle);

/*
We want to avoid situations where no area is drawn because (for
example) the user selected an angle of 0 and 180 and moved the
mouse straight up or down. This code covers most such cases, but it
is currently still possible to create such cases by moving the
mouse very fast, so there is still some room for refinement of the
present logic...
*/

double area = Math.Abs (0.5 * (old_top.X * new_top.Y - old_top.Y * new_top.X +
new_top.X * new_bottom.Y - new_top.Y * new_bottom.X +
new_bottom.X * old_bottom.Y - new_bottom.Y * old_bottom.X +
old_bottom.X * old_top.Y - old_bottom.Y * old_top.X));

if (area < 2) {
old_top = OffsetPoint (old_top, -1, 1, angle + 90);
new_top = OffsetPoint (new_top, -1, 1, angle + 90);
old_bottom = OffsetPoint (old_bottom, 1, 1, angle + 90);
new_bottom = OffsetPoint (new_bottom, 1, 1, angle + 90);
}

g.MoveTo (old_top.X, old_top.Y);
g.LineTo (new_top.X, new_top.Y);
g.LineTo (new_bottom.X, new_bottom.Y);
g.LineTo (old_bottom.X, old_bottom.Y);

g.Fill ();

/*
Anti-aliasing creates ugly effects because it anti-aliases at all
edges of each rectangle, even the ones that are adjacent to the next
one in the path, creating gaps in what should be a continuous line.

Workaround: If anti-aliasing is enabled, create a non-antialiased
stroke at the end to draw over the gap.
*/

if (g.Antialias != Antialias.None) {
Antialias previous_antialias = g.Antialias;
g.Antialias = Antialias.None;
PointD antialias_correction_top = OffsetPoint (new_top, 1, 2, angle);
PointD antialias_correction_bottom = OffsetPoint (new_bottom, -1, 2, angle);
g.MoveTo (antialias_correction_top.X, antialias_correction_top.Y);
g.LineTo (antialias_correction_bottom.X, antialias_correction_bottom.Y);
g.LineWidth = 2;
g.Stroke ();
g.Antialias = previous_antialias;
g.LineWidth = line_width;
}

RectangleI dirty = g.StrokeExtents ().ToInt ();

return dirty;
}

public override void UpdateLineWidth (int lineWidth)
{
line_width = lineWidth;
CursorChanged ();
}

public override Gdk.Cursor GetCursor ()
{
/*
If we do not override a 0 angle with 180, the logic in GdkExtensions will
step into the "rectangle" path which will look inconsistent (unfilled) to all
other angles, so call a 0 angle with 180 for cursor creation so that it looks
consistent.
*/
var icon = GdkExtensions.CreateIconWithShape ("Cursor.Paintbrush.png",
CursorShape.Rectangle, 2, line_width, angle == 0 ? 180 : angle, 8, 24,
out var iconOffsetX, out var iconOffsetY);

return Gdk.Cursor.NewFromTexture (icon, iconOffsetX, iconOffsetY, null);
}

private PointD OffsetPoint (PointD point, double multiplier, double offset, float angle_in_degrees)
{
double offsetX = offset * Math.Sin (Single.DegreesToRadians (angle_in_degrees));
double offsetY = offset * Math.Cos (Single.DegreesToRadians (angle_in_degrees));
return new (Math.Round (point.X + multiplier * offsetX), Math.Round (point.Y + multiplier * offsetY));
}

}
1 change: 1 addition & 0 deletions Pinta.Tools/CoreToolsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public void Initialize ()
PintaCore.PaintBrushes.AddPaintBrush (new Brushes.GridBrush ());
PintaCore.PaintBrushes.AddPaintBrush (new Brushes.PlainBrush (PintaCore.Workspace));
PintaCore.PaintBrushes.AddPaintBrush (new Brushes.SplatterBrush (services.GetService<ISettingsService> ()));
PintaCore.PaintBrushes.AddPaintBrush (new Brushes.SlashBrush (services.GetService<ISettingsService> ()));
PintaCore.PaintBrushes.AddPaintBrush (new Brushes.SquaresBrush ());

PintaCore.Tools.AddTool (new MoveSelectedTool (services));
Expand Down
10 changes: 8 additions & 2 deletions Pinta.Tools/Tools/BaseBrushTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ protected BaseBrushTool (IServiceProvider services) : base (services)
Palette = services.GetService<IPaletteService> ();

BrushWidthSpinButton.TooltipText = Translations.GetString ("Change brush width. Shortcut keys: [ ]");
// Change the cursor when the BrushWidth is changed.
BrushWidthSpinButton.OnValueChanged += (_, _) => SetCursor (DefaultCursor);
BrushWidthSpinButton.OnValueChanged += (_, _) => OnBrushWidthChanged ();
}

protected override bool ShowAntialiasingButton => true;
Expand Down Expand Up @@ -113,6 +112,13 @@ protected override void OnSaveSettings (ISettingsService settings)
settings.PutSetting (SettingNames.BrushWidth (this), brush_width.GetValueAsInt ());
}

protected virtual void OnBrushWidthChanged ()
{
// Change the cursor when the BrushWidth is changed.
SetCursor (DefaultCursor);
}


private SpinButton? brush_width;
private Label? brush_width_label;

Expand Down
Loading
Loading