| 1 |
/* |
| 2 |
|
| 3 |
Licensed to the Apache Software Foundation (ASF) under one or more |
| 4 |
contributor license agreements. See the NOTICE file distributed with |
| 5 |
this work for additional information regarding copyright ownership. |
| 6 |
The ASF licenses this file to You under the Apache License, Version 2.0 |
| 7 |
(the "License"); you may not use this file except in compliance with |
| 8 |
the License. You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
|
| 18 |
*/ |
| 19 |
|
| 20 |
var svgns = "http://www.w3.org/2000/svg"; |
| 21 |
var xlinkns = "http://www.w3.org/1999/xlink"; |
| 22 |
|
| 23 |
function Pile(board, x, y, w, h, dx, dy, pdx, pdy) { |
| 24 |
this.board = board; |
| 25 |
this.doc = board.doc; |
| 26 |
this.index = board.thePiles.length; |
| 27 |
|
| 28 |
board.thePiles.push(this); |
| 29 |
this.g = this.doc.createElementNS(svgns, "g"); |
| 30 |
this.g.setAttribute("id", "pile-"+this.index); |
| 31 |
this.g.addEventListener("mousedown", new PileMouseDownHandler(this), false); |
| 32 |
this.g.addEventListener("click", new PileClickHandler(this), false); |
| 33 |
|
| 34 |
this.base = this.doc.createElementNS(svgns, "rect"); |
| 35 |
this.base.setAttribute("x", ""+x); |
| 36 |
this.base.setAttribute("y", ""+y); |
| 37 |
this.base.setAttribute("width", ""+w); |
| 38 |
this.base.setAttribute("height", ""+h); |
| 39 |
this.base.setAttribute("style", "visibility:hidden; pointer-events:fill"); |
| 40 |
this.g.appendChild(this.base); |
| 41 |
|
| 42 |
board.boardGroup.appendChild(this.g); |
| 43 |
this.x = x; |
| 44 |
this.y = y; |
| 45 |
this.width = w; |
| 46 |
this.height = h; |
| 47 |
this.dx = dx; |
| 48 |
this.dy = dy; |
| 49 |
this.pdx = pdx; |
| 50 |
this.pdy = pdy; |
| 51 |
this.cards = new Array(); |
| 52 |
var me = this; |
| 53 |
} |
| 54 |
|
| 55 |
Pile.prototype.addCard = function(card) { |
| 56 |
if (card.pile) { |
| 57 |
card.pile.removeCard(card); |
| 58 |
} |
| 59 |
var len = this.cards.length; |
| 60 |
if ((this.pdx != 0) || (this.pdy !=0)) { |
| 61 |
for (var i=0; i<len; i++) { |
| 62 |
this.cards[i].setPos(this.x+this.pdx*len+this.dx*i, |
| 63 |
this.y+this.pdy*len+this.dy*i); |
| 64 |
} |
| 65 |
} |
| 66 |
card.pile = this; |
| 67 |
this.cards[len] = card; |
| 68 |
card.rect.style.setProperty("pointer-events", "fill", ""); |
| 69 |
card.setPos(this.x+this.pdx*len+this.dx*len, |
| 70 |
this.y+this.pdy*len+this.dy*len); |
| 71 |
this.g.appendChild(card.getElem()); |
| 72 |
} |
| 73 |
|
| 74 |
Pile.prototype.removeCard = function(card) { |
| 75 |
if (this != card.pile) return; |
| 76 |
var len = this.cards.length; |
| 77 |
for (var i=len-1; i>=0; i--) { |
| 78 |
var c = this.cards[i]; |
| 79 |
if (c == card) { |
| 80 |
this.cards.splice(i, 1); |
| 81 |
len = this.cards.length; |
| 82 |
for (var j=i; j<len; j++) { |
| 83 |
c = this.cards[j]; |
| 84 |
c.setPos(this.x+this.dx*j, this.y+this.dy*j); |
| 85 |
} |
| 86 |
card.pile = null; |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
if ((this.pdx != 0) || (this.pdy !=0)) { |
| 91 |
var len = this.cards.length; |
| 92 |
var x = this.x+this.pdx*(len-1); |
| 93 |
var y = this.y+this.pdy*(len-1); |
| 94 |
for (var i=0; i<len; i++) { |
| 95 |
this.cards[i].setPos(x+this.dx*i, y+this.dy*i); |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
Pile.prototype.moveCardTo = function(card, step) { |
| 101 |
if (card.pile) { |
| 102 |
card.pile.removeCard(card); |
| 103 |
} |
| 104 |
var len = this.cards.length; |
| 105 |
card.pile = this; |
| 106 |
this.cards.push(card); |
| 107 |
card.rect.style.setProperty("pointer-events", "fill", ""); |
| 108 |
this.board.moveGroup.appendChild(card.getElem()); |
| 109 |
var x = this.x+this.pdx*len; |
| 110 |
var y = this.y+this.pdy*len; |
| 111 |
card.moveTo(x+this.dx*len, y+this.dy*len, step); |
| 112 |
} |
| 113 |
|
| 114 |
Pile.prototype.moveCardsTo = function(cards, step) { |
| 115 |
var xlocs = new Array(); |
| 116 |
var ylocs = new Array(); |
| 117 |
for (var i=0; i<cards.length; i++) { |
| 118 |
xlocs[i] = cards[i].x; |
| 119 |
ylocs[i] = cards[i].y; |
| 120 |
} |
| 121 |
|
| 122 |
for (var i=0; i<cards.length; i++) { |
| 123 |
var card = cards[i]; |
| 124 |
if (card.pile) { |
| 125 |
card.pile.removeCard(card); |
| 126 |
} |
| 127 |
card.setPos(xlocs[i], ylocs[i]); |
| 128 |
var len = this.cards.length; |
| 129 |
card.pile = this; |
| 130 |
this.cards.push(card); |
| 131 |
card.rect.style.setProperty("pointer-events", "fill", ""); |
| 132 |
this.board.moveGroup.appendChild(card.getElem()); |
| 133 |
var x = this.x+this.pdx*len; |
| 134 |
var y = this.y+this.pdy*len; |
| 135 |
card.moveTo(x+this.dx*len, y+this.dy*len, step); |
| 136 |
this.moveCardTo(card, step); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
function moveCardsToPiles(cards, piles, step) { |
| 141 |
var xlocs = new Array(); |
| 142 |
var ylocs = new Array(); |
| 143 |
for (var i=0; i<cards.length; i++) { |
| 144 |
xlocs[i] = cards[i].x; |
| 145 |
ylocs[i] = cards[i].y; |
| 146 |
} |
| 147 |
|
| 148 |
for (var i=0; i<cards.length; i++) { |
| 149 |
var card = cards[i]; |
| 150 |
var pile = piles[i]; |
| 151 |
if (card.pile) { |
| 152 |
card.pile.removeCard(card); |
| 153 |
} |
| 154 |
card.setPos(xlocs[i], ylocs[i]); |
| 155 |
var len = pile.cards.length; |
| 156 |
card.pile = pile; |
| 157 |
pile.cards.push(card); |
| 158 |
card.rect.style.setProperty("pointer-events", "fill", ""); |
| 159 |
pile.board.moveGroup.appendChild(card.getElem()); |
| 160 |
var x = pile.x+pile.pdx*len; |
| 161 |
var y = pile.y+pile.pdy*len; |
| 162 |
card.moveTo(x+pile.dx*len, y+pile.dy*len, step); |
| 163 |
pile.moveCardTo(card, step); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
Pile.prototype.size = function() { |
| 170 |
return this.cards.length; |
| 171 |
} |
| 172 |
|
| 173 |
Pile.prototype.getTopCard = function() { |
| 174 |
if (this.cards.length == 0) return null; |
| 175 |
var c = this.cards.pop(); |
| 176 |
c.pile = null; |
| 177 |
if ((this.pdx != 0) || (this.pdy !=0)) { |
| 178 |
var len = this.cards.length; |
| 179 |
var x = this.x+this.pdx*(len-1); |
| 180 |
var y = this.y+this.pdy*(len-1); |
| 181 |
for (var i=0; i<len; i++) { |
| 182 |
this.cards[i].setPos(x+this.dx*i, y+this.dy*i); |
| 183 |
} |
| 184 |
} |
| 185 |
return c; |
| 186 |
} |
| 187 |
|
| 188 |
Pile.prototype.checkTopCard = function() { |
| 189 |
if (this.cards.length == 0) return null; |
| 190 |
return this.cards[this.cards.length-1]; |
| 191 |
} |
| 192 |
|
| 193 |
Pile.prototype.checkCard = function(index) { |
| 194 |
return this.cards[index]; |
| 195 |
} |
| 196 |
|
| 197 |
Pile.prototype.nextCard = function(card) { |
| 198 |
var sz = this.cards.length-1; |
| 199 |
for (var i=0; i<sz; i++) { |
| 200 |
if (this.cards[i] == card) |
| 201 |
return this.cards[i+1]; |
| 202 |
} |
| 203 |
return null; |
| 204 |
} |
| 205 |
|
| 206 |
Pile.prototype.setDragCheck = function(dragCheck) { |
| 207 |
this.dragCheck = dragCheck; |
| 208 |
} |
| 209 |
|
| 210 |
Pile.prototype.setDropCheck = function(dropCheck) { |
| 211 |
this.dropCheck = dropCheck; |
| 212 |
} |
| 213 |
|
| 214 |
Pile.prototype.setClick = function(click) { |
| 215 |
this.click = click; |
| 216 |
} |
| 217 |
|
| 218 |
Pile.prototype.setDoubleClick = function(doubleClick) { |
| 219 |
this.doubleClick = doubleClick; |
| 220 |
} |
| 221 |
|
| 222 |
function PileClickHandler(pile) { |
| 223 |
this.pile = pile; |
| 224 |
this.tgt = null; |
| 225 |
this.clickX =0; |
| 226 |
this.clickY =0; |
| 227 |
this.clickCnt =0; |
| 228 |
} |
| 229 |
|
| 230 |
PileClickHandler.prototype.handleEvent = function(evt) { |
| 231 |
if (this.tgt != evt.target) { |
| 232 |
this.tgt = evt.target; |
| 233 |
this.clickX = evt.clientX; |
| 234 |
this.clickY = evt.clientY; |
| 235 |
this.clickCnt = 1; |
| 236 |
if (this.pile.click) |
| 237 |
this.pile.click(this.pile, evt); |
| 238 |
return; |
| 239 |
} |
| 240 |
|
| 241 |
var dx = evt.clientX-this.clickX; |
| 242 |
var dy = evt.clientY-this.clickY; |
| 243 |
if (dx*dx+dy*dy > 9) { |
| 244 |
this.clickX = evt.clientX; |
| 245 |
this.clickY = evt.clientY; |
| 246 |
this.clickCnt = 1; |
| 247 |
if (this.pile.click) |
| 248 |
this.pile.click(this.pile, evt); |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
this.clickX = evt.clientX; |
| 253 |
this.clickY = evt.clientY; |
| 254 |
this.clickCnt++; |
| 255 |
|
| 256 |
if (this.clickCnt > 2) { |
| 257 |
this.clickCnt = 1; |
| 258 |
if (this.pile.click) |
| 259 |
this.pile.click(this.pile, evt); |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
if (this.pile.doubleClick) { |
| 264 |
this.pile.doubleClick(this.pile, evt); |
| 265 |
} |
| 266 |
}; |
| 267 |
|
| 268 |
function PileMouseDownHandler(pile) { |
| 269 |
this.pile = pile; |
| 270 |
} |
| 271 |
|
| 272 |
PileMouseDownHandler.prototype.handleEvent = function(evt) { |
| 273 |
if (!this.pile.dragCheck) return; |
| 274 |
var tgt = evt.target; |
| 275 |
var e = tgt; |
| 276 |
var p = e.parentNode; |
| 277 |
while (p && (p != this.pile.g)) { |
| 278 |
e = p; |
| 279 |
p = e.parentNode; |
| 280 |
} |
| 281 |
|
| 282 |
if (!p) return; |
| 283 |
var c, i; |
| 284 |
var num = this.pile.cards.length; |
| 285 |
for (i=num-1; i>=0; i--) { |
| 286 |
c = this.pile.cards[i]; |
| 287 |
if (e == c.elem) |
| 288 |
break; |
| 289 |
c = null; |
| 290 |
} |
| 291 |
if (!c) return; |
| 292 |
|
| 293 |
var g = this.pile.dragCheck(this.pile, c, i); |
| 294 |
if (!g) return; |
| 295 |
var cards = this.pile.cards.splice(i, num-i); |
| 296 |
|
| 297 |
new CardMoveManager(g, this.pile, cards, |
| 298 |
evt.clientX, evt.clientY); |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
/** |
| 303 |
* Finds the pile associated with elem, if any. |
| 304 |
* returns null if elem is not associated with a pile. |
| 305 |
* otherwise it returns the associated Pile object. |
| 306 |
*/ |
| 307 |
function findPile(board, elem) { |
| 308 |
var root = document.getRootElement(); |
| 309 |
var tgt = elem |
| 310 |
var destPile = null; |
| 311 |
while (tgt != root) { |
| 312 |
var id; |
| 313 |
try { id = tgt.getAttribute("id"); } catch (x) { break; } |
| 314 |
if (id && id.substring(0, 5) == "pile-") { |
| 315 |
var idx = parseFloat(id.substring(5)); |
| 316 |
destPile = board.thePiles[idx]; |
| 317 |
break; |
| 318 |
} |
| 319 |
tgt = tgt.parentNode; |
| 320 |
} |
| 321 |
return destPile; |
| 322 |
} |
| 323 |
|
| 324 |
function CardMoveManager(group, pile, cards, x, y) { |
| 325 |
this.pile = pile; |
| 326 |
this.board = pile.board; |
| 327 |
this.cards = cards; |
| 328 |
this.group = group; |
| 329 |
this.parent = group.parentNode; |
| 330 |
this.root = this.group.getOwnerDocument().getRootElement(); |
| 331 |
this.startPt = localPt(this.parent, x, y); |
| 332 |
this.fmm = new ForwardMouseMove(this); |
| 333 |
this.fmu = new ForwardMouseUp(this); |
| 334 |
this.root.addEventListener("mousemove", this.fmm, false); |
| 335 |
this.root.addEventListener("mouseup", this.fmu, false); |
| 336 |
this.moved = false; |
| 337 |
}; |
| 338 |
|
| 339 |
CardMoveManager.prototype.mousemove = function(evt) { |
| 340 |
var pt = localPt(this.parent, evt.clientX, evt.clientY); |
| 341 |
var dx = pt.x-this.startPt.x; |
| 342 |
var dy = pt.y-this.startPt.y; |
| 343 |
if (!this.moved && ((dx*dx+dy*dy) < 4)) |
| 344 |
return; |
| 345 |
|
| 346 |
if (!this.moved) { |
| 347 |
this.moved = true; |
| 348 |
// Now move cards |
| 349 |
for (var i=0; i<this.cards.length; i++) { |
| 350 |
this.cards[i].pile = null; |
| 351 |
this.cards[i].rect.style.removeProperty("pointer-events"); |
| 352 |
this.group.appendChild(this.cards[i].elem); |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
this.group.setAttribute("transform", "translate(" + dx + ", " + dy + ")"); |
| 357 |
}; |
| 358 |
|
| 359 |
CardMoveManager.prototype.mouseup = function(evt) { |
| 360 |
this.root.removeEventListener("mousemove", this.fmm, false); |
| 361 |
this.root.removeEventListener("mouseup", this.fmu, false); |
| 362 |
this.group.setAttribute("transform",""); |
| 363 |
if (!this.moved) { |
| 364 |
for (var i=0; i<this.cards.length; i++) { |
| 365 |
this.pile.cards.push(this.cards[i]); |
| 366 |
} |
| 367 |
return; |
| 368 |
} |
| 369 |
|
| 370 |
var pt = localPt(this.parent, evt.clientX, evt.clientY); |
| 371 |
var dx = pt.x-this.startPt.x; |
| 372 |
var dy = pt.y-this.startPt.y; |
| 373 |
|
| 374 |
var moveInfo = false; |
| 375 |
var destPile = findPile(this.board, evt.target); |
| 376 |
if (destPile && destPile.dropCheck) { |
| 377 |
moveInfo = destPile.dropCheck(this.pile, destPile, this.cards); |
| 378 |
} |
| 379 |
|
| 380 |
switch(moveInfo) { |
| 381 |
case true: |
| 382 |
// Drop succeeded but no move info. |
| 383 |
this.pile.board.notifyMoveDone(); |
| 384 |
return; |
| 385 |
case false: |
| 386 |
// Drop failed move back to original. |
| 387 |
for (var i=0; i<this.cards.length; i++) { |
| 388 |
var c = this.cards[i]; |
| 389 |
c.setPos(c.x+dx, c.y+dy); |
| 390 |
} |
| 391 |
this.pile.moveCardsTo(this.cards, 40); |
| 392 |
return; |
| 393 |
default: |
| 394 |
// Drop succeed record move info. |
| 395 |
this.pile.board.saveMove(moveInfo); |
| 396 |
this.pile.board.notifyMoveDone(); |
| 397 |
return; |
| 398 |
} |
| 399 |
}; |