5
// Jonathan Pobst <monkey@jpobst.com>
7
// Copyright (c) 2010 Jonathan Pobst
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
public class PencilTool : BaseTool
35
private static Point point_empty = new Point (-500, -500);
37
private Point last_point = point_empty;
39
private ImageSurface undo_surface;
40
private bool surface_modified;
47
public override string Name { get { return "Pencil"; } }
48
public override string Icon { get { return "Tools.Pencil.png"; } }
49
public override string StatusBarText { get { return "Left click to draw freeform, one-pixel wide lines with the primary color, right click to use the secondary color"; } }
50
public override bool Enabled { get { return true; } }
53
#region Mouse Handlers
54
protected override void OnMouseDown (Gtk.DrawingArea canvas, Gtk.ButtonPressEventArgs args, Cairo.PointD point)
56
surface_modified = false;
57
undo_surface = PintaCore.Layers.CurrentLayer.Surface.Clone ();
60
protected override void OnMouseMove (object o, Gtk.MotionNotifyEventArgs args, Cairo.PointD point)
64
if ((args.Event.State & Gdk.ModifierType.Button1Mask) == Gdk.ModifierType.Button1Mask)
65
tool_color = PintaCore.Palette.PrimaryColor;
66
else if ((args.Event.State & Gdk.ModifierType.Button3Mask) == Gdk.ModifierType.Button3Mask)
67
tool_color = PintaCore.Palette.SecondaryColor;
69
last_point = point_empty;
73
DrawingArea drawingarea1 = (DrawingArea)o;
78
if (last_point.Equals (point_empty)) {
79
last_point = new Point (x, y);
83
if (PintaCore.Workspace.PointInCanvas (point))
84
surface_modified = true;
86
ImageSurface surf = PintaCore.Layers.CurrentLayer.Surface;
88
using (Context g = new Context (surf)) {
89
g.AppendPath (PintaCore.Layers.SelectionPath);
90
g.FillRule = FillRule.EvenOdd;
93
g.Antialias = Antialias.None;
95
g.MoveTo (last_point.X, last_point.Y);
100
g.LineCap = LineCap.Square;
105
Gdk.Rectangle r = GetRectangleFromPoints (last_point, new Point (x, y));
107
PintaCore.Workspace.Invalidate (r);
109
last_point = new Point (x, y);
112
protected override void OnMouseUp (Gtk.DrawingArea canvas, Gtk.ButtonReleaseEventArgs args, Cairo.PointD point)
114
if (surface_modified)
115
PintaCore.History.PushNewItem (new SimpleHistoryItem (Icon, Name, undo_surface, PintaCore.Layers.CurrentLayerIndex));
116
else if (undo_surface != null)
117
(undo_surface as IDisposable).Dispose ();
119
surface_modified = false;
123
#region Private Methods
124
private Gdk.Rectangle GetRectangleFromPoints (Point a, Point b)
126
int x = Math.Min (a.X, b.X) - 2 - 2;
127
int y = Math.Min (a.Y, b.Y) - 2 - 2;
128
int w = Math.Max (a.X, b.X) - x + (2 * 2) + 4;
129
int h = Math.Max (a.Y, b.Y) - y + (2 * 2) + 4;
131
return new Gdk.Rectangle (x, y, w, h);