pictcode / app / webroot / js / pictcode / sprite_drawer.js @ 5ec4ad9d
履歴 | 表示 | アノテート | ダウンロード (9.225 KB)
1 |
define(["image_manager"], function(imageManager){ |
---|---|
2 |
|
3 |
var edOuterSize = {width:1000, height:800};//スプライト配置可能領域のサイズ、独自のスプライトの座標系 |
4 |
var edInnerSize = {width:700, height:560};//画面表示領域のサイズ、独自のスプライトの座標系 |
5 |
var dpInnerSize = {width:100, height:80}; |
6 |
var edSpriteOriginalSize = 250;//エディタ上のスプライトのサイズ |
7 |
var edCenter = {x:0, y:0};//UIエディタの中心座標 |
8 |
var edOuterLeftTop = {x:-edOuterSize.width/2, y:edOuterSize.height/2}; |
9 |
var edInnerLeftTop = {x:-edInnerSize.width/2, y:edInnerSize.height/2}; |
10 |
|
11 |
var edKnobSize = 50; |
12 |
var cvKnobRect = {x:0, y:0, width:-1, height:-1}; |
13 |
|
14 |
//Canvasへの描画
|
15 |
function drawSprites(canvas, canvasContext, sprites){ |
16 |
var cvSize ={width:canvas.width, height:canvas.height};//canvasの座標系 |
17 |
var cvCenter = {x:canvas.width/2, y:canvas.height/2}; |
18 |
|
19 |
canvasContext.clearRect(0, 0, canvas.width, canvas.height); |
20 |
|
21 |
//CanvasとUIエディタの長さの単位の比
|
22 |
var ceRatio;
|
23 |
if (canvas.isUI) {
|
24 |
ceRatio = cvSize.width/edOuterSize.width; |
25 |
}else{
|
26 |
ceRatio = cvSize.width/edInnerSize.width; |
27 |
}; |
28 |
|
29 |
//背景画像の描画
|
30 |
if (canvas.isUI && canvas.canvasBackgroundImage) {
|
31 |
canvasContext.drawImage( |
32 |
canvas.canvasBackgroundImage, |
33 |
(edOuterSize.width-edInnerSize.width)/2*ceRatio,
|
34 |
(edOuterSize.height-edInnerSize.height)/2*ceRatio,
|
35 |
canvas.width*edInnerSize.width/edOuterSize.width, |
36 |
canvas.height*edInnerSize.height/edOuterSize.height |
37 |
); |
38 |
}else{
|
39 |
canvasContext.save(); |
40 |
canvasContext.fillStyle = '#4682b4';
|
41 |
canvasContext.fillRect(0, 0, canvas.width, canvas.height); |
42 |
canvasContext.restore(); |
43 |
}; |
44 |
|
45 |
//スプライト描画
|
46 |
var targetSprite = null; |
47 |
_.each( sprites, function(sprite){
|
48 |
if(sprite.type == "ghost"){ |
49 |
return;
|
50 |
}; |
51 |
var edSpriteSize = getSpriteSize(sprite);
|
52 |
var edSpriteLeftTop = {x:sprite.position.x-edSpriteSize.width/2, y:sprite.position.y+edSpriteSize.height/2}; |
53 |
var cvSpriteLeftTop;
|
54 |
if (canvas.isUI) {
|
55 |
cvSpriteLeftTop = {x:(edSpriteLeftTop.x - edOuterLeftTop.x)*ceRatio, y:(edOuterLeftTop.y - edSpriteLeftTop.y)*ceRatio}; |
56 |
}else{
|
57 |
cvSpriteLeftTop = {x:(edSpriteLeftTop.x - edInnerLeftTop.x)*ceRatio, y:(edInnerLeftTop.y - edSpriteLeftTop.y)*ceRatio}; |
58 |
}; |
59 |
var cvSpriteSize = {width:edSpriteSize.width*ceRatio, height:edSpriteSize.height*ceRatio}; |
60 |
var cvSpriteCenter = {x:cvSpriteLeftTop.x+cvSpriteSize.width/2, y:cvSpriteLeftTop.y+cvSpriteSize.height/2}; |
61 |
|
62 |
sprite.drawnRect = {x:cvSpriteLeftTop.x, y:cvSpriteLeftTop.y, width:cvSpriteSize.width, height:cvSpriteSize.height}; |
63 |
|
64 |
//スプライトの回転、描画
|
65 |
canvasContext.save(); |
66 |
canvasContext.translate(cvSpriteCenter.x, cvSpriteCenter.y); |
67 |
canvasContext.rotate(sprite.angle); |
68 |
if (sprite.mirrored) {
|
69 |
canvasContext.transform(-1, 0, 0, 1, 0, 0); |
70 |
}; |
71 |
canvasContext.translate(-cvSpriteCenter.x, -cvSpriteCenter.y); |
72 |
canvasContext.globalAlpha = sprite.opacity; |
73 |
if (sprite.images) {
|
74 |
canvasContext.drawImage(sprite.images[sprite.animationCount], sprite.drawnRect.x, sprite.drawnRect.y, sprite.drawnRect.width, sprite.drawnRect.height); |
75 |
}; |
76 |
canvasContext.restore(); |
77 |
|
78 |
if (sprite.text || sprite.text == 0) { |
79 |
var textSize = Math.round(cvSpriteSize.width*0.25); |
80 |
canvasContext.save(); |
81 |
canvasContext.font = "bold "+ textSize + "px " + "sans-serif"; |
82 |
canvasContext.fillStyle = "black";
|
83 |
canvasContext.textAlign = "center";
|
84 |
canvasContext.textBaseline = "middle";
|
85 |
canvasContext.fillText(sprite.text, cvSpriteCenter.x, cvSpriteCenter.y+cvSpriteSize.height*0.05);
|
86 |
canvasContext.restore(); |
87 |
}; |
88 |
|
89 |
if (sprite.isTarget && canvas.isUI) {
|
90 |
targetSprite = sprite; |
91 |
//十字線の描画
|
92 |
canvasContext.save(); |
93 |
canvasContext.lineWidth = 4;
|
94 |
canvasContext.strokeStyle = "#ff0000";
|
95 |
canvasContext.setLineDash([5, 5]); |
96 |
if (targetSprite.position.x > -edInnerSize.width/2 && targetSprite.position.x < edInnerSize.width/2) { |
97 |
canvasContext.beginPath(); |
98 |
canvasContext.moveTo(cvSpriteCenter.x, (edOuterSize.height-edInnerSize.height)/2*ceRatio);
|
99 |
canvasContext.lineTo(cvSpriteCenter.x, (edOuterSize.height+edInnerSize.height)/2*ceRatio);
|
100 |
canvasContext.stroke(); |
101 |
} |
102 |
if (targetSprite.position.y > -edInnerSize.height/2 && targetSprite.position.y < edInnerSize.height/2) { |
103 |
canvasContext.beginPath(); |
104 |
canvasContext.moveTo((edOuterSize.width-edInnerSize.width)/2*ceRatio, cvSpriteCenter.y);
|
105 |
canvasContext.lineTo((edOuterSize.width+edInnerSize.width)/2*ceRatio, cvSpriteCenter.y);
|
106 |
canvasContext.stroke(); |
107 |
} |
108 |
canvasContext.restore(); |
109 |
}; |
110 |
}); |
111 |
//境界の描画
|
112 |
canvasContext.save(); |
113 |
canvasContext.lineWidth = 6;
|
114 |
canvasContext.strokeStyle = "#8b4513";
|
115 |
canvasContext.strokeRect(0, 0, canvas.width, canvas.height); |
116 |
if(canvas.isUI){
|
117 |
canvasContext.strokeRect( |
118 |
(edOuterSize.width-edInnerSize.width)/2*ceRatio,
|
119 |
(edOuterSize.height-edInnerSize.height)/2*ceRatio,
|
120 |
canvas.width*edInnerSize.width/edOuterSize.width, |
121 |
canvas.height*edInnerSize.height/edOuterSize.height |
122 |
); |
123 |
} |
124 |
canvasContext.restore(); |
125 |
|
126 |
if (targetSprite && canvas.isUI) {
|
127 |
//四角い点線の描画
|
128 |
canvasContext.save(); |
129 |
canvasContext.lineWidth = 6;
|
130 |
canvasContext.strokeStyle = "#ff0000";
|
131 |
canvasContext.setLineDash([10, 10]); |
132 |
canvasContext.translate(targetSprite.drawnRect.x+targetSprite.drawnRect.width/2, targetSprite.drawnRect.y+targetSprite.drawnRect.height/2); |
133 |
canvasContext.rotate(targetSprite.angle); |
134 |
canvasContext.translate(-(targetSprite.drawnRect.x+targetSprite.drawnRect.width/2), -(targetSprite.drawnRect.y+targetSprite.drawnRect.height/2)); |
135 |
canvasContext.strokeRect(targetSprite.drawnRect.x, targetSprite.drawnRect.y, targetSprite.drawnRect.width, targetSprite.drawnRect.height); |
136 |
canvasContext.restore(); |
137 |
|
138 |
//ツマミの描画
|
139 |
var cvKnobSize = edKnobSize * ceRatio;
|
140 |
var rX = targetSprite.drawnRect.width/2; |
141 |
var rY = targetSprite.drawnRect.height/2; |
142 |
var cvKnobDistance = Math.sqrt(rX*rX + rY*rY);
|
143 |
cvKnobRect = { |
144 |
x:targetSprite.drawnRect.x + targetSprite.drawnRect.width/2 + (Math.cos(targetSprite.angle)*rX-Math.sin(targetSprite.angle)*rY) - cvKnobSize/2, |
145 |
y:targetSprite.drawnRect.y + targetSprite.drawnRect.height/2 + (Math.cos(targetSprite.angle)*rY+Math.sin(targetSprite.angle)*rX) - cvKnobSize/2, |
146 |
width:cvKnobSize,
|
147 |
height:cvKnobSize
|
148 |
}; |
149 |
canvasContext.drawImage(imageManager.images.ui_001, cvKnobRect.x, cvKnobRect.y, cvKnobRect.width, cvKnobRect.height); |
150 |
//座標の描画
|
151 |
var textSize = Math.round(canvas.width*0.03); |
152 |
canvasContext.save(); |
153 |
canvasContext.font = "bold "+ textSize + "px " + "sans-serif"; |
154 |
canvasContext.fillStyle = "black";
|
155 |
if (targetSprite.position.x > -edInnerSize.width/2 && targetSprite.position.x < edInnerSize.width/2) { |
156 |
var pX = Math.round((targetSprite.position.x + edInnerSize.width/2)*dpInnerSize.width/edInnerSize.width); |
157 |
canvasContext.fillText(pX, targetSprite.drawnRect.x+targetSprite.drawnRect.width/2-textSize*0.7, (edOuterSize.height+edInnerSize.height)/2*ceRatio+textSize); |
158 |
}; |
159 |
if (targetSprite.position.y > -edInnerSize.height/2 && targetSprite.position.y < edInnerSize.height/2) { |
160 |
var pY = Math.round((targetSprite.position.y + edInnerSize.height/2)*dpInnerSize.height/edInnerSize.height); |
161 |
canvasContext.fillText(pY, (edOuterSize.width-edInnerSize.width)/2*ceRatio-textSize*1.6, targetSprite.drawnRect.y + targetSprite.drawnRect.height/2+textSize*0.4); |
162 |
}; |
163 |
canvasContext.restore(); |
164 |
}; |
165 |
} |
166 |
|
167 |
function getPositionInfo(sprites, x, y){ |
168 |
|
169 |
var isOnSprite = false; |
170 |
var targetSprite;
|
171 |
|
172 |
if (cvKnobRect.x<x && cvKnobRect.x+cvKnobRect.width>x && cvKnobRect.y<y && cvKnobRect.y+cvKnobRect.height>y) {
|
173 |
return{target:"knob"}; |
174 |
} |
175 |
|
176 |
for (var i=sprites.length-1; i>=0; i--){ |
177 |
var sprite = sprites[i];
|
178 |
if (sprite.type == "ghost") {continue;}; |
179 |
if (sprite.drawnRect.x<x && sprite.drawnRect.x+sprite.drawnRect.width>x && sprite.drawnRect.y<y && sprite.drawnRect.y+sprite.drawnRect.height>y) {
|
180 |
isOnSprite = true;
|
181 |
targetSprite = sprite; |
182 |
break;
|
183 |
}else{
|
184 |
|
185 |
}; |
186 |
}; |
187 |
|
188 |
if (isOnSprite) {
|
189 |
return{target:"sprite", sprite:targetSprite}; |
190 |
}else{
|
191 |
return{target:"canvas"}; |
192 |
}; |
193 |
} |
194 |
|
195 |
function getSpriteSize(sprite){ |
196 |
var edSpriteSize;
|
197 |
if (sprite.type == "background") { |
198 |
edSpriteSize = {width:edInnerSize.width*sprite.scale, height:edInnerSize.height*sprite.scale}; |
199 |
} else{
|
200 |
edSpriteSize = { |
201 |
width:edSpriteOriginalSize/(sprite.aspectRatio+1)*sprite.scale, |
202 |
height:edSpriteOriginalSize*sprite.aspectRatio/(sprite.aspectRatio+1)*sprite.scale |
203 |
}; |
204 |
}; |
205 |
return edSpriteSize;
|
206 |
} |
207 |
|
208 |
return {
|
209 |
drawSprites:drawSprites,
|
210 |
getPositionInfo:getPositionInfo,
|
211 |
editorOuterSize:edOuterSize,
|
212 |
editorInnerSize:edInnerSize,
|
213 |
dislayInnerSize:dpInnerSize,
|
214 |
getSpriteSize:getSpriteSize,
|
215 |
}; |
216 |
}); |