01 
02 /*
03  *  JScripter Standard 1.0 - To Script In Java
04  *  Copyright (C) 2008-2011  J.J.Liu<jianjunliu@126.com> <http://www.jscripter.org>
05  *  
06  *  This program is free software: you can redistribute it and/or modify
07  *  it under the terms of the GNU Affero General Public License as published by
08  *  the Free Software Foundation, either version 3 of the License, or
09  *  (at your option) any later version.
10  *  
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Affero General Public License for more details.
15  *  
16  *  You should have received a copy of the GNU Affero General Public License
17  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 package jsx.ui.vect.canvas;
21 
22 import js.Js;
23 import jsx.Configurable;
24 import jsx.core.ObjectLikes;
25 import jsx.ui.dd.Mouse;
26 import jsx.ui.dd.event.DragMove;
27 import jsx.ui.dd.event.DragStop;
28 import jsx.ui.vect.Canvas;
29 
30 /**
31  * <p>Defines a drawing wrapper that interactively sketches on the wrapped canvas.</p>
32  * <p>A drawing tool of this class wraps a {@link Canvas} component and sketches in 
33  * accordance with the {@link Mouse} handle to which it listens mouse events.</p>
34  * <p>A {@link CanvasSketch} widget is {@link Configurable} and is also an event source 
35  * which may fire high level events.</p>
36  * 
37  * @author <a href="mailto:jianjunliu@126.com">J.J.Liu (Jianjun Liu)</a> at <a href="http://www.jscripter.org" target="_blank">http://www.jscripter.org</a>
38  */
39 public class CanvasSketch extends CanvasDraw
40 {
41     /**
42      * <p>A typical constructor that constructs a wrapper widget of this type and forces 
43      * constructors of subclasses to pass a {@link Canvas} widget.</p>
44      * <p>This constructor invokes the typical constructor of the superclass passing 
45      * the component wrapped by the specified {@link Canvas} widget that is set to the 
46      * configurable property {@link CanvasDraw#CANVAS} of the widget to be constructed.</p>
47      * @param c A canvas widget.
48      * @since 1.0
49      */
50     public CanvasSketch(Canvas c) {
51         super(c);
52     }
53 
54     /**
55      * <p>Performs an action on the dispatched event.</p>
56      * <p>If the current canvas drawing tool is in a drawing mode this method draws 
57      * a line on the draft canvas from the current position to the specified mouse move 
58      * event.</p>
59      * @param evt The event dispatched to this listener.
60      * @since 1.0
61      */
62     public void onEvent(DragMove evt) {
63         if (Js.be(ini(this).var(START))) {
64             lineTo(this, canvas(), evt);
65         }
66     }
67 
68     /**
69      * <p>Performs an action on the dispatched event.</p>
70      * <p>If the current canvas drawing tool is in a drawing mode this method draws 
71      * a line on the draft canvas from the current position to the specified mouse release 
72      * event, closes the current drawing path and exits the drawing mode.</p>
73      * @param evt The event dispatched to this listener.
74      * @since 1.0
75      */
76     public void onEvent(DragStop evt) {
77         if (Js.be(ini(this).var(START))) {
78             ObjectLikes.delete(ini(this), START);
79             Canvas c = canvas();
80             lineTo(this, c, evt);
81             Canvas.closePath(c);
82         }
83     }
84 }