1 | dojo.provide("dojox.drawing.tools.Path"); |
---|
2 | |
---|
3 | dojox.drawing.tools.Path = dojox.drawing.util.oo.declare( |
---|
4 | // summary: |
---|
5 | // Class for a drawable Path |
---|
6 | // |
---|
7 | dojox.drawing.stencil.Path, |
---|
8 | function(){ |
---|
9 | // summary: constructor |
---|
10 | |
---|
11 | this.pathMode = ""; |
---|
12 | this.currentPathMode = ""; |
---|
13 | this._started = false; |
---|
14 | this.oddEvenClicks = 0; |
---|
15 | |
---|
16 | }, |
---|
17 | { |
---|
18 | draws:true, |
---|
19 | onDown: function(obj){ |
---|
20 | if(!this._started){ |
---|
21 | this.onStartPath(obj); |
---|
22 | } |
---|
23 | |
---|
24 | }, |
---|
25 | |
---|
26 | makeSubPath: function(_closePath){ |
---|
27 | if(_closePath){ |
---|
28 | if(this.currentPathMode=="Q"){ |
---|
29 | this.points.push({ |
---|
30 | x:this.points[0].x, |
---|
31 | y:this.points[0].y |
---|
32 | }); |
---|
33 | } |
---|
34 | this.points.push({t:"Z"}); |
---|
35 | this.render(); |
---|
36 | } |
---|
37 | this.currentPathMode = ""; |
---|
38 | this.pathMode = "M"; |
---|
39 | }, |
---|
40 | |
---|
41 | onStartPath: function(obj){ |
---|
42 | this._started = true; |
---|
43 | this.revertRenderHit = this.renderHit; |
---|
44 | this.renderHit = false; |
---|
45 | this.closePath = false; |
---|
46 | |
---|
47 | |
---|
48 | this.mouse.setEventMode("PathEdit"); |
---|
49 | |
---|
50 | this.closePoint = {x:obj.x, y:obj.y}; |
---|
51 | |
---|
52 | this._kc1 = this.connect(this.keys, "onEsc", this, function(){ |
---|
53 | this.onCompletePath(false); |
---|
54 | }); |
---|
55 | |
---|
56 | this._kc2 = this.connect(this.keys, "onKeyUp", this, function(evt){ |
---|
57 | |
---|
58 | switch(evt.letter){ |
---|
59 | case "c": |
---|
60 | this.onCompletePath(true); break; |
---|
61 | case "l": this.pathMode = "L"; break; |
---|
62 | case "m": this.makeSubPath(false); break; |
---|
63 | case "q": this.pathMode = "Q"; break; |
---|
64 | case "s": this.pathMode = "S"; break; |
---|
65 | case "z": this.makeSubPath(true); break; |
---|
66 | } |
---|
67 | |
---|
68 | //console.log("KEY:", evt.letter); |
---|
69 | }); |
---|
70 | }, |
---|
71 | |
---|
72 | onCompletePath:function(_closePath){ |
---|
73 | this.remove(this.closeGuide, this.guide); |
---|
74 | var box = this.getBounds(); |
---|
75 | if(box.w<this.minimumSize && box.h<this.minimumSize){ |
---|
76 | this.remove(this.hit, this.shape, this.closeGuide); |
---|
77 | this._started = false; |
---|
78 | this.mouse.setEventMode(""); |
---|
79 | this.setPoints([]); |
---|
80 | return; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | if(_closePath){ |
---|
85 | if(this.currentPathMode=="Q"){ |
---|
86 | this.points.push({ |
---|
87 | x:this.points[0].x, |
---|
88 | y:this.points[0].y |
---|
89 | }); |
---|
90 | } |
---|
91 | this.closePath = true; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | this.renderHit = this.revertRenderHit; |
---|
96 | this.renderedOnce = true; |
---|
97 | this.onRender(this); |
---|
98 | this.disconnect([this._kc1, this._kc2]); |
---|
99 | this.mouse.setEventMode(""); |
---|
100 | this.render(); |
---|
101 | //console.log(this.stringPath); |
---|
102 | }, |
---|
103 | |
---|
104 | onUp: function(/*EventObject*/obj){ |
---|
105 | //console.log(" Path UP", obj.mid, "within:", obj.withinCanvas) |
---|
106 | |
---|
107 | |
---|
108 | if(!this._started || !obj.withinCanvas){ return; } |
---|
109 | |
---|
110 | |
---|
111 | if(this.points.length>2 && this.closeRadius>this.util.distance(obj.x, obj.y, this.closePoint.x, this.closePoint.y)){ |
---|
112 | this.onCompletePath(true); |
---|
113 | |
---|
114 | }else { |
---|
115 | var p = { |
---|
116 | x:obj.x, |
---|
117 | y:obj.y |
---|
118 | }; |
---|
119 | this.oddEvenClicks++; |
---|
120 | if(this.currentPathMode != this.pathMode){ |
---|
121 | if(this.pathMode=="Q"){ |
---|
122 | p.t = "Q"; |
---|
123 | this.oddEvenClicks = 0; |
---|
124 | }else if(this.pathMode=="L"){ |
---|
125 | p.t = "L"; |
---|
126 | }else if(this.pathMode=="M"){ |
---|
127 | p.t = "M"; |
---|
128 | this.closePoint = {x:obj.x, y:obj.y}; |
---|
129 | } |
---|
130 | this.currentPathMode = this.pathMode; |
---|
131 | } |
---|
132 | |
---|
133 | |
---|
134 | this.points.push(p); |
---|
135 | if(this.points.length>1){ |
---|
136 | this.remove(this.guide); |
---|
137 | this.render(); |
---|
138 | } |
---|
139 | |
---|
140 | } |
---|
141 | |
---|
142 | //console.log(this.stringPath); |
---|
143 | }, |
---|
144 | createGuide: function(obj){ |
---|
145 | if(!this.points.length){ return; } |
---|
146 | var realPoints = [].concat(this.points); |
---|
147 | |
---|
148 | var pt = { |
---|
149 | x:obj.x, |
---|
150 | y:obj.y |
---|
151 | }; |
---|
152 | if(this.currentPathMode=="Q" && this.oddEvenClicks % 2){ |
---|
153 | // On a Q curve, every other click needs to be a |
---|
154 | // straight line - the inbetween Q coords don't render |
---|
155 | pt.t = "L"; // this is not permanent |
---|
156 | } |
---|
157 | |
---|
158 | this.points.push(pt); |
---|
159 | |
---|
160 | this.render(); |
---|
161 | this.points = realPoints; |
---|
162 | |
---|
163 | |
---|
164 | var dist = this.util.distance(obj.x, obj.y, this.closePoint.x, this.closePoint.y); |
---|
165 | if(this.points.length>1){ |
---|
166 | if(dist<this.closeRadius && !this.closeGuide){ |
---|
167 | var c = { |
---|
168 | cx:this.closePoint.x, |
---|
169 | cy:this.closePoint.y, |
---|
170 | rx:this.closeRadius, |
---|
171 | ry:this.closeRadius |
---|
172 | } |
---|
173 | this.closeGuide = this.container.createEllipse(c) |
---|
174 | .setFill(this.closeColor); |
---|
175 | |
---|
176 | }else if(dist>this.closeRadius && this.closeGuide){ |
---|
177 | this.remove(this.closeGuide); |
---|
178 | this.closeGuide = null; |
---|
179 | } |
---|
180 | } |
---|
181 | |
---|
182 | }, |
---|
183 | |
---|
184 | onMove: function(obj){ |
---|
185 | if(!this._started){ return; } |
---|
186 | this.createGuide(obj); |
---|
187 | }, |
---|
188 | onDrag: function(obj){ |
---|
189 | if(!this._started){ return; } |
---|
190 | this.createGuide(obj); |
---|
191 | } |
---|
192 | } |
---|
193 | ); |
---|
194 | |
---|
195 | dojox.drawing.tools.Path.setup = { |
---|
196 | // summary: See Base ToolsSetup |
---|
197 | // |
---|
198 | name:"dojox.drawing.tools.Path", |
---|
199 | tooltip:"Path Tool", |
---|
200 | iconClass:"iconLine" |
---|
201 | }; |
---|
202 | |
---|
203 | dojox.drawing.register(dojox.drawing.tools.Path.setup, "tool"); |
---|