~mantas/pinta/stable

« back to all changes in this revision

Viewing changes to Pinta.Core/Tools/PencilTool.cs

  • Committer: Iain Lane
  • Date: 2010-02-19 15:33:30 UTC
  • Revision ID: git-v1:92cc09614a4e2801bfe39cd971f6b31e694a500b
ImportedĀ UpstreamĀ versionĀ 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 
 
2
// PencilTool.cs
 
3
//  
 
4
// Author:
 
5
//       Jonathan Pobst <monkey@jpobst.com>
 
6
// 
 
7
// Copyright (c) 2010 Jonathan Pobst
 
8
// 
 
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:
 
15
// 
 
16
// The above copyright notice and this permission notice shall be included in
 
17
// all copies or substantial portions of the Software.
 
18
// 
 
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
 
25
// THE SOFTWARE.
 
26
 
 
27
using System;
 
28
using Cairo;
 
29
using Gtk;
 
30
 
 
31
namespace Pinta.Core
 
32
{
 
33
        public class PencilTool : BaseTool
 
34
        {
 
35
                private static Point point_empty = new Point (-500, -500);
 
36
                
 
37
                private Point last_point = point_empty;
 
38
                
 
39
                private ImageSurface undo_surface;
 
40
                private bool surface_modified;
 
41
 
 
42
                public PencilTool ()
 
43
                {
 
44
                }
 
45
 
 
46
                #region Properties
 
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; } }
 
51
                #endregion
 
52
 
 
53
                #region Mouse Handlers
 
54
                protected override void OnMouseDown (Gtk.DrawingArea canvas, Gtk.ButtonPressEventArgs args, Cairo.PointD point)
 
55
                {
 
56
                        surface_modified = false;
 
57
                        undo_surface = PintaCore.Layers.CurrentLayer.Surface.Clone ();
 
58
                }
 
59
 
 
60
                protected override void OnMouseMove (object o, Gtk.MotionNotifyEventArgs args, Cairo.PointD point)
 
61
                {
 
62
                        Color tool_color;
 
63
                        
 
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;
 
68
                        else {
 
69
                                last_point = point_empty;
 
70
                                return;
 
71
                        }
 
72
                                
 
73
                        DrawingArea drawingarea1 = (DrawingArea)o;
 
74
                        
 
75
                        int x = (int)point.X;
 
76
                        int y = (int)point.Y;
 
77
                        
 
78
                        if (last_point.Equals (point_empty)) {
 
79
                                last_point = new Point (x, y);
 
80
                                return;
 
81
                        }
 
82
                        
 
83
                        if (PintaCore.Workspace.PointInCanvas (point))
 
84
                                surface_modified = true;
 
85
 
 
86
                        ImageSurface surf = PintaCore.Layers.CurrentLayer.Surface;
 
87
                        
 
88
                        using (Context g = new Context (surf)) {
 
89
                                g.AppendPath (PintaCore.Layers.SelectionPath);
 
90
                                g.FillRule = FillRule.EvenOdd;
 
91
                                g.Clip ();
 
92
                                
 
93
                                g.Antialias = Antialias.None;
 
94
                                
 
95
                                g.MoveTo (last_point.X, last_point.Y);
 
96
                                g.LineTo (x, y);
 
97
 
 
98
                                g.Color = tool_color;
 
99
                                g.LineWidth = 1;
 
100
                                g.LineCap = LineCap.Square;
 
101
                                
 
102
                                g.Stroke ();
 
103
                        }
 
104
                        
 
105
                        Gdk.Rectangle r = GetRectangleFromPoints (last_point, new Point (x, y));
 
106
 
 
107
                        PintaCore.Workspace.Invalidate (r);
 
108
                        
 
109
                        last_point = new Point (x, y);
 
110
                }
 
111
                
 
112
                protected override void OnMouseUp (Gtk.DrawingArea canvas, Gtk.ButtonReleaseEventArgs args, Cairo.PointD point)
 
113
                {
 
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 ();
 
118
 
 
119
                        surface_modified = false;
 
120
                }
 
121
                #endregion
 
122
 
 
123
                #region Private Methods
 
124
                private Gdk.Rectangle GetRectangleFromPoints (Point a, Point b)
 
125
                {
 
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;
 
130
                        
 
131
                        return new Gdk.Rectangle (x, y, w, h);
 
132
                }
 
133
                #endregion
 
134
        }
 
135
}