|
28
|
1 |
(function ($) { |
|
|
2 |
|
|
|
3 |
$.fn._vs.draw = { |
|
|
4 |
|
|
|
5 |
settings:{ |
|
|
6 |
draw:{ |
|
|
7 |
trail:1, |
|
|
8 |
showLayout:false |
|
|
9 |
} |
|
|
10 |
}, |
|
|
11 |
|
|
|
12 |
update:function(_this){ |
|
|
13 |
/* refresh rate of canvas (show trail) */ |
|
|
14 |
//console.log(_this.ctx) |
|
|
15 |
if(this.settings.draw.trail==1) { |
|
|
16 |
_this.ctx.clearRect(0, 0, _this.ctx.canvas.clientWidth, _this.ctx.canvas.clientHeight); |
|
|
17 |
}else{ |
|
|
18 |
debugDrawChart(0, |
|
|
19 |
0, |
|
|
20 |
ctx.canvas.clientWidth, |
|
|
21 |
ctx.canvas.clientHeight, |
|
|
22 |
"rgba(255,255,255,"+this.settings.draw.trail+")", |
|
|
23 |
ctx); |
|
|
24 |
} |
|
|
25 |
|
|
|
26 |
/* Draw body(s) from box2d */ |
|
|
27 |
for( var b = _this.world.GetBodyList() ; b ; b = b.GetNext()) { |
|
|
28 |
for (var s = b.GetFixtureList(); s != null; s = s.GetNext()) { |
|
|
29 |
this.drawShape(_this,s); |
|
|
30 |
} |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
/* Show wireframe mode */ |
|
|
34 |
if(this.settings.draw.showLayout==true){ |
|
|
35 |
this.debugDrawChart(chart.position.x, |
|
|
36 |
chart.position.y, |
|
|
37 |
chart.position.width, |
|
|
38 |
chart.position.height, |
|
|
39 |
"rgba(255,0,0,0.2)", |
|
|
40 |
ctx); |
|
|
41 |
} |
|
|
42 |
}, |
|
|
43 |
debugDrawChart :function (x,y,w,h,color,ctx) { |
|
|
44 |
ctx.save(); |
|
|
45 |
ctx.translate(0,0); |
|
|
46 |
ctx.fillStyle = color; |
|
|
47 |
ctx.beginPath(); |
|
|
48 |
ctx.rect(x,y,w,h); |
|
|
49 |
ctx.closePath(); |
|
|
50 |
ctx.strokeStyle ="#000" |
|
|
51 |
ctx.lineWidth = 0.5; |
|
|
52 |
ctx.stroke(); |
|
|
53 |
ctx.restore(); |
|
|
54 |
}, |
|
|
55 |
clippedBackgroundImage:function( ctx, img,x,y ){ |
|
|
56 |
ctx.save(); |
|
|
57 |
ctx.clip(); |
|
|
58 |
//console.log(img) |
|
|
59 |
ctx.drawImage(img, |
|
|
60 |
x-(img.height/2), |
|
|
61 |
y-(img.width/2), |
|
|
62 |
img.width, |
|
|
63 |
img.height); |
|
|
64 |
ctx.restore(); |
|
|
65 |
}, |
|
|
66 |
haveTexture:function(s){ |
|
|
67 |
var result = false |
|
|
68 |
if(typeof(s.m_userData.texture)!="undefined"){ |
|
|
69 |
if(typeof(s.m_userData.texture.img)!="undefined"){ |
|
|
70 |
if(s.m_userData.texture.img.complete){ |
|
|
71 |
result = true |
|
|
72 |
} |
|
|
73 |
} |
|
|
74 |
} |
|
|
75 |
return result |
|
|
76 |
}, |
|
|
77 |
|
|
|
78 |
drawShape: function (_this,s) { |
|
|
79 |
var b = s.GetBody(); |
|
|
80 |
var position = b.GetPosition(); |
|
|
81 |
var angle = b.GetAngle(); |
|
|
82 |
var radiusCoef = 9; |
|
|
83 |
var radiusCoefMax=10 |
|
|
84 |
var scale = _this.settings.options.scale |
|
|
85 |
|
|
|
86 |
// add x and y to userData |
|
|
87 |
s.m_userData.x = b.GetWorldCenter().x*scale |
|
|
88 |
s.m_userData.y = b.GetWorldCenter().y*scale |
|
|
89 |
|
|
|
90 |
// |
|
|
91 |
// Call back draw |
|
|
92 |
if(typeof(s.m_userData.callback)!="undefined"){ |
|
|
93 |
if(typeof(s.m_userData.callback.draw)=="function"){ |
|
|
94 |
var t = _this.select('ID',s.m_userData.ID) |
|
|
95 |
s.m_userData.callback.draw(t) |
|
|
96 |
} |
|
|
97 |
} |
|
|
98 |
|
|
|
99 |
switch (s.GetType()){ |
|
|
100 |
case 0: // round |
|
|
101 |
|
|
|
102 |
switch (s.m_userData){ |
|
|
103 |
case null: |
|
|
104 |
_this.ctx.fillStyle = "rgba(255,0,0,1)"; |
|
|
105 |
break; |
|
|
106 |
default: |
|
|
107 |
_this.ctx.fillStyle = s.m_userData.fillStyle; |
|
|
108 |
break |
|
|
109 |
} |
|
|
110 |
|
|
|
111 |
var radius = s.m_shape.m_radius |
|
|
112 |
|
|
|
113 |
// round token |
|
|
114 |
if(_this.settings.sedimentation.token.visible==true){ |
|
|
115 |
|
|
|
116 |
_this.ctx.save(); |
|
|
117 |
_this.ctx.translate(position.x*scale, position.y*scale); |
|
|
118 |
_this.ctx.rotate(angle); |
|
|
119 |
_this.ctx.beginPath(); |
|
|
120 |
var h = (radius/radiusCoefMax*radiusCoef)*scale |
|
|
121 |
|
|
|
122 |
//console.log(s.m_userData.strokeStyle) |
|
|
123 |
if(typeof(s.m_userData.strokeStyle)!="undefined"){ |
|
|
124 |
_this.ctx.strokeStyle = s.m_userData.strokeStyle |
|
|
125 |
} else{ |
|
|
126 |
_this.ctx.strokeStyle = "rgba(0,0,0,0)" |
|
|
127 |
} |
|
|
128 |
|
|
|
129 |
if(typeof(s.m_userData.lineWidth)!="undefined"){ |
|
|
130 |
_this.ctx.lineWidth = s.m_userData.lineWidth |
|
|
131 |
} else { |
|
|
132 |
_this.ctx.lineWidth = 0 |
|
|
133 |
} |
|
|
134 |
|
|
|
135 |
_this.ctx.arc(0, 0,h, 0, Math.PI*2, true); |
|
|
136 |
|
|
|
137 |
if(this.haveTexture(s)){ |
|
|
138 |
this.clippedBackgroundImage(_this.ctx,s.m_userData.texture.img |
|
|
139 |
,0 |
|
|
140 |
,0); |
|
|
141 |
} |
|
|
142 |
_this.ctx.closePath(); |
|
|
143 |
|
|
|
144 |
if(_this.settings.options.layout==true){ |
|
|
145 |
_this.ctx.strokeStyle = "#000" |
|
|
146 |
_this.ctx.lineWidth = 0.5 |
|
|
147 |
_this.ctx.stroke(); |
|
|
148 |
}else{ |
|
|
149 |
if(!this.haveTexture(s))_this.ctx.fill(); |
|
|
150 |
_this.ctx.stroke(); |
|
|
151 |
|
|
|
152 |
} |
|
|
153 |
|
|
|
154 |
_this.ctx.restore(); |
|
|
155 |
|
|
|
156 |
} |
|
|
157 |
|
|
|
158 |
|
|
|
159 |
break |
|
|
160 |
case 1: // vertice (polygon and squares ...) |
|
|
161 |
|
|
|
162 |
//if(s.m_userData.type != "wall" && s.m_userData.type != "lift")console.log("draw",s.m_userData) |
|
|
163 |
|
|
|
164 |
switch (s.m_userData){ |
|
|
165 |
case null: |
|
|
166 |
_this.ctx.fillStyle = "rgba(255,0,0,1)"; |
|
|
167 |
break; |
|
|
168 |
default: |
|
|
169 |
_this.ctx.fillStyle = s.m_userData.fillStyle; |
|
|
170 |
break |
|
|
171 |
} |
|
|
172 |
|
|
|
173 |
var width = s.m_shape.m_vertices[0].x*scale |
|
|
174 |
var height = s.m_shape.m_vertices[0].y*scale |
|
|
175 |
var posx = position.x*scale-s.m_shape.m_vertices[0].x*scale |
|
|
176 |
var posy = position.y*scale-s.m_shape.m_vertices[0].y*scale |
|
|
177 |
|
|
|
178 |
_this.ctx.save(); |
|
|
179 |
_this.ctx.translate(position.x*scale, position.y*scale); |
|
|
180 |
_this.ctx.rotate(angle); |
|
|
181 |
_this.ctx.beginPath(); |
|
|
182 |
|
|
|
183 |
//if(s.m_userData.ID==1 ){ console.log(s.m_userData.lineWidth) } |
|
|
184 |
//if(typeof(s.m_userData.fillStyle)!="undefined") _this.ctx.fillStyle = s.m_userData.fillStyle |
|
|
185 |
if(typeof(s.m_userData.strokeStyle)!="undefined"){ _this.ctx.strokeStyle = s.m_userData.strokeStyle |
|
|
186 |
} else{ _this.ctx.strokeStyle = s.m_userData.fillStyle} |
|
|
187 |
|
|
|
188 |
if(typeof(s.m_userData.lineWidth)!="undefined"){ _this.ctx.lineWidth = s.m_userData.lineWidth |
|
|
189 |
} else{ _this.ctx.lineWidth = 0} |
|
|
190 |
|
|
|
191 |
|
|
|
192 |
for (var i = 0; i < s.m_shape.m_vertices.length; i++) { |
|
|
193 |
var points = s.m_shape.m_vertices; |
|
|
194 |
//var this = {x:0,y:0} |
|
|
195 |
_this.ctx.moveTo(( points[0].x) * scale, (points[0].y) * scale); |
|
|
196 |
for (var j = 1; j < points.length; j++) { |
|
|
197 |
_this.ctx.lineTo((points[j].x ) * scale, (points[j].y ) * scale); |
|
|
198 |
} |
|
|
199 |
_this.ctx.lineTo(( points[0].x) * scale, ( points[0].y) * scale); |
|
|
200 |
} |
|
|
201 |
_this.ctx.closePath(); |
|
|
202 |
_this.ctx.fill(); |
|
|
203 |
|
|
|
204 |
// pour le debug mode |
|
|
205 |
if(_this.settings.options.layout==true){ |
|
|
206 |
_this.ctx.lineWidth = 0.1; |
|
|
207 |
_this.ctx.strokeStyle ="rgb(0,0,0)" |
|
|
208 |
_this.ctx.stroke(); |
|
|
209 |
|
|
|
210 |
// incomming points Drawer |
|
|
211 |
//for (var i = _this.settings.sedimentation.incoming.point.length - 1; i >= 0; i--) { |
|
|
212 |
// |
|
|
213 |
//_this.settings.sedimentation.incoming.point[i].y |
|
|
214 |
// draw green |
|
|
215 |
//_this.ctx.font = '40px Arial'; |
|
|
216 |
//_this.ctx.fillText("x", _this.settings.sedimentation.incoming.point[i].x, _this.settings.sedimentation.incoming.point[i].y); |
|
|
217 |
//_this.ctx.fillStyle = "rgb(0,250,0,0.5)"; |
|
|
218 |
|
|
|
219 |
//}; |
|
|
220 |
|
|
|
221 |
}else{ |
|
|
222 |
_this.ctx.stroke(); |
|
|
223 |
} |
|
|
224 |
_this.ctx.restore(); |
|
|
225 |
|
|
|
226 |
break; |
|
|
227 |
case 2: |
|
|
228 |
|
|
|
229 |
break; |
|
|
230 |
_this.ctx.fillStyle = "rgb(0,0,0)"; |
|
|
231 |
} |
|
|
232 |
|
|
|
233 |
} |
|
|
234 |
} |
|
|
235 |
|
|
|
236 |
})(jQuery); |