forked from PintaProject/Pinta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPencilTool.cs
192 lines (161 loc) · 6.08 KB
/
PencilTool.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// PencilTool.cs
//
// Author:
// Jonathan Pobst <[email protected]>
//
// Copyright (c) 2010 Jonathan Pobst
//
// 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 Gtk;
using Pinta.Core;
using Mono.Unix;
namespace Pinta.Tools
{
public class PencilTool : BaseTool
{
private Point last_point = point_empty;
private ImageSurface undo_surface;
private bool surface_modified;
protected uint mouse_button;
public PencilTool ()
{
}
#region Properties
public override string Name { get { return Catalog.GetString ("Pencil"); } }
public override string Icon { get { return "Tools.Pencil.png"; } }
public override string StatusBarText { get { return Catalog.GetString ("Left click to draw freeform one-pixel wide lines with the primary color. Right click to use the secondary color."); } }
public override Gdk.Cursor DefaultCursor { get { return new Gdk.Cursor (PintaCore.Chrome.Canvas.Display, PintaCore.Resources.GetIcon ("Cursor.Pencil.png"), 7, 24); } }
public override Gdk.Key ShortcutKey { get { return Gdk.Key.P; } }
public override int Priority { get { return 29; } }
#endregion
#region Mouse Handlers
protected override void OnMouseDown (Gtk.DrawingArea canvas, Gtk.ButtonPressEventArgs args, Cairo.PointD point)
{
// If we are already drawing, ignore any additional mouse down events
if (mouse_button > 0)
return;
surface_modified = false;
undo_surface = PintaCore.Workspace.ActiveDocument.CurrentUserLayer.Surface.Clone ();
mouse_button = args.Event.Button;
Color tool_color;
if (mouse_button == 1)
{
tool_color = PintaCore.Palette.PrimaryColor;
}
else if (mouse_button == 3)
{
tool_color = PintaCore.Palette.SecondaryColor;
}
else
{
last_point = point_empty;
return;
}
Draw (canvas, tool_color, point, true);
}
protected override void OnMouseMove (object o, Gtk.MotionNotifyEventArgs args, Cairo.PointD point)
{
Color tool_color;
if (mouse_button == 1)
{
tool_color = PintaCore.Palette.PrimaryColor;
}
else if (mouse_button == 3)
{
tool_color = PintaCore.Palette.SecondaryColor;
}
else
{
last_point = point_empty;
return;
}
Draw ((DrawingArea) o, tool_color, point, false);
}
private void Draw (DrawingArea drawingarea1, Color tool_color, Cairo.PointD point, bool first_pixel)
{
int x = (int)point.X;
int y = (int) point.Y;
if (last_point.Equals (point_empty)) {
last_point = new Point (x, y);
if (!first_pixel)
return;
}
Document doc = PintaCore.Workspace.ActiveDocument;
if (doc.Workspace.PointInCanvas (point))
surface_modified = true;
ImageSurface surf = doc.CurrentUserLayer.Surface;
if (first_pixel && doc.Workspace.PointInCanvas (point)) {
// Does Cairo really not support a single-pixel-long single-pixel-wide line?
surf.Flush ();
int shiftedX = (int)point.X;
int shiftedY = (int)point.Y;
ColorBgra source = surf.GetColorBgraUnchecked (shiftedX, shiftedY);
source = UserBlendOps.NormalBlendOp.ApplyStatic (source, tool_color.ToColorBgra ());
surf.SetColorBgra (source, shiftedX, shiftedY);
surf.MarkDirty ();
} else {
using (Context g = new Context (surf)) {
g.AppendPath (doc.Selection.SelectionPath);
g.FillRule = FillRule.EvenOdd;
g.Clip ();
g.Antialias = Antialias.None;
// Adding 0.5 forces cairo into the correct square:
// See https://bugs.launchpad.net/bugs/672232
g.MoveTo (last_point.X + 0.5, last_point.Y + 0.5);
g.LineTo (x + 0.5, y + 0.5);
g.SetSourceColor (tool_color);
g.LineWidth = 1;
g.LineCap = LineCap.Square;
g.Stroke ();
}
}
Gdk.Rectangle r = GetRectangleFromPoints (last_point, new Point (x, y));
doc.Workspace.Invalidate (doc.ClampToImageSize (r));
last_point = new Point (x, y);
}
protected override void OnMouseUp (Gtk.DrawingArea canvas, Gtk.ButtonReleaseEventArgs args, Cairo.PointD point)
{
Document doc = PintaCore.Workspace.ActiveDocument;
if (undo_surface != null)
{
if (surface_modified)
doc.History.PushNewItem(new SimpleHistoryItem(Icon, Name, undo_surface, doc.CurrentUserLayerIndex));
else if (undo_surface != null)
(undo_surface as IDisposable).Dispose();
}
surface_modified = false;
undo_surface = null;
mouse_button = 0;
}
#endregion
#region Private Methods
private Gdk.Rectangle GetRectangleFromPoints (Point a, Point b)
{
int x = Math.Min (a.X, b.X) - 2 - 2;
int y = Math.Min (a.Y, b.Y) - 2 - 2;
int w = Math.Max (a.X, b.X) - x + (2 * 2) + 4;
int h = Math.Max (a.Y, b.Y) - y + (2 * 2) + 4;
return new Gdk.Rectangle (x, y, w, h);
}
#endregion
}
}