| 1 |
/* |
| 2 |
* Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 |
* contributor license agreements. See the NOTICE file distributed with |
| 4 |
* this work for additional information regarding copyright ownership. |
| 5 |
* The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 |
* (the "License"); you may not use this file except in compliance with |
| 7 |
* the License. You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
var svgns = "http://www.w3.org/2000/svg"; |
| 18 |
var xlinkns = "http://www.w3.org/1999/xlink"; |
| 19 |
|
| 20 |
var SUITE_DIAMOND = 1; |
| 21 |
var SUITE_CLUB = 2; |
| 22 |
var SUITE_HEART = 3; |
| 23 |
var SUITE_SPADE = 4; |
| 24 |
|
| 25 |
var MOVING = new Array(); |
| 26 |
|
| 27 |
function Card(doc, value, suite, deck, faceup) { |
| 28 |
this.value = value; |
| 29 |
this.suite = suite; |
| 30 |
this.deck = deck; |
| 31 |
|
| 32 |
this.elem = doc.createElementNS(svgns, "g"); |
| 33 |
this.svg = doc.createElementNS(svgns, "svg"); |
| 34 |
this.svg.setAttribute("x", "0"); |
| 35 |
this.svg.setAttribute("y", "0"); |
| 36 |
this.svg.setAttribute("width", "100"); |
| 37 |
this.svg.setAttribute("height", "100"); |
| 38 |
this.elem.appendChild(this.svg); |
| 39 |
|
| 40 |
var id; |
| 41 |
switch(suite) { |
| 42 |
case SUITE_DIAMOND: id = "d"; break; |
| 43 |
case SUITE_CLUB: id = "c"; break; |
| 44 |
case SUITE_HEART: id = "h"; break; |
| 45 |
case SUITE_SPADE: id = "s"; break; |
| 46 |
} |
| 47 |
id += value; |
| 48 |
this.id = id; |
| 49 |
this.card = doc.createElementNS(svgns, "use"); |
| 50 |
this.faceup = faceup; |
| 51 |
if (this.faceup) { |
| 52 |
this.card.setAttributeNS(xlinkns, "xlink:href", |
| 53 |
deck + "#" + id); |
| 54 |
} else { |
| 55 |
this.card.setAttributeNS(xlinkns, "xlink:href", |
| 56 |
deck + "#card-back"); |
| 57 |
} |
| 58 |
this.card.setAttribute("x", "0"); |
| 59 |
this.card.setAttribute("y", "0"); |
| 60 |
this.card.setAttribute("width", "100%"); |
| 61 |
this.card.setAttribute("height", "100%"); |
| 62 |
this.svg.appendChild(this.card); |
| 63 |
this.rect = doc.createElementNS(svgns, "rect"); |
| 64 |
this.rect.setAttribute("x", "0"); |
| 65 |
this.rect.setAttribute("y", "0"); |
| 66 |
this.rect.setAttribute("width", "100%"); |
| 67 |
this.rect.setAttribute("height", "100%"); |
| 68 |
this.rect.setAttribute("style", "visibility:hidden; pointer-events:fill"); |
| 69 |
this.svg.appendChild(this.rect); |
| 70 |
|
| 71 |
return this; |
| 72 |
} |
| 73 |
|
| 74 |
Card.prototype.flipCard = function(faceup) { |
| 75 |
if (this.faceup == faceup) return; |
| 76 |
if (faceup) { |
| 77 |
this.card.setAttributeNS(xlinkns, "xlink:href", |
| 78 |
this.deck + "#" + this.id); |
| 79 |
} else { |
| 80 |
this.card.setAttributeNS(xlinkns, "xlink:href", |
| 81 |
this.deck + "#card-back"); |
| 82 |
} |
| 83 |
this.faceup = faceup; |
| 84 |
} |
| 85 |
|
| 86 |
Card.prototype.getValue = function() { |
| 87 |
return this.value; |
| 88 |
} |
| 89 |
|
| 90 |
Card.prototype.getSuite = function() { |
| 91 |
return this.suite; |
| 92 |
} |
| 93 |
|
| 94 |
Card.prototype.getElem = function() { |
| 95 |
return this.elem; |
| 96 |
} |
| 97 |
|
| 98 |
Card.prototype.suiteMatch = function(c) { |
| 99 |
return c.suite == this.suite; |
| 100 |
} |
| 101 |
|
| 102 |
Card.prototype.isRed = function() { |
| 103 |
return ((this.suite == SUITE_HEART) || (this.suite == SUITE_DIAMOND)); |
| 104 |
} |
| 105 |
Card.prototype.isBlack = function() { |
| 106 |
return ((this.suite == SUITE_CLUB) || (this.suite == SUITE_SPADE)); |
| 107 |
} |
| 108 |
|
| 109 |
Card.prototype.colorMatch = function(c) { |
| 110 |
return c.isRed() == this.isRed(); |
| 111 |
} |
| 112 |
|
| 113 |
Card.prototype.valueMatch = function(c) { |
| 114 |
return c.value == this.value; |
| 115 |
} |
| 116 |
|
| 117 |
Card.prototype.valueOneHigher = function(c) { |
| 118 |
return c.value-1 == this.value; |
| 119 |
} |
| 120 |
|
| 121 |
Card.prototype.valueOneLower = function(c) { |
| 122 |
return c.value+1 == this.value; |
| 123 |
} |
| 124 |
|
| 125 |
Card.prototype.asString = function() { |
| 126 |
return this.id; |
| 127 |
} |
| 128 |
|
| 129 |
Card.prototype.setPos = function(x, y) { |
| 130 |
if ((this.x == x) && (this.y == y)) return; |
| 131 |
this.x = x; |
| 132 |
this.y = y; |
| 133 |
this.elem.setAttribute("transform", "translate("+x+","+y+")"); |
| 134 |
} |
| 135 |
|
| 136 |
Card.prototype.setSize = function(w, h) { |
| 137 |
this.w = w; |
| 138 |
this.h = h; |
| 139 |
this.svg.setAttribute("width", ""+w); |
| 140 |
this.svg.setAttribute("height", ""+h); |
| 141 |
} |
| 142 |
|
| 143 |
Card.prototype.moveTo = function(x, y, step) { |
| 144 |
this.destX = x; |
| 145 |
this.destY = y; |
| 146 |
this.dx = (x - this.x); |
| 147 |
this.dy = (y - this.y); |
| 148 |
var dist = Math.sqrt(this.dx*this.dx+this.dy*this.dy); |
| 149 |
var steps = Math.floor(dist/step); |
| 150 |
if (steps < 1) steps = 1; |
| 151 |
this.dx /= steps; |
| 152 |
this.dy /= steps; |
| 153 |
if (this.moving) |
| 154 |
return; |
| 155 |
|
| 156 |
if (!this.updateDisplay()) |
| 157 |
return; |
| 158 |
|
| 159 |
this.moving = true; |
| 160 |
MOVING.push(this); |
| 161 |
if (MOVING.length == 1) { |
| 162 |
setTimeout("moveCards()", 50); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
Card.prototype.updateDisplay = function() { |
| 167 |
var deltaX = (this.destX-this.x); |
| 168 |
var newX = this.x + this.dx; |
| 169 |
var register = false; |
| 170 |
if (deltaX < 0) { |
| 171 |
if (((deltaX*1.01) <= this.dx) && |
| 172 |
((deltaX*0.99) >= this.dx)) { |
| 173 |
newX = this.destX; |
| 174 |
} else { |
| 175 |
register = true; |
| 176 |
} |
| 177 |
} else { |
| 178 |
if (((deltaX*1.01) >= this.dx) && |
| 179 |
((deltaX*0.99) <= this.dx)) { |
| 180 |
newX = this.destX; |
| 181 |
} else { |
| 182 |
register = true; |
| 183 |
} |
| 184 |
} |
| 185 |
var deltaY = (this.destY-this.y); |
| 186 |
var newY = this.y + this.dy; |
| 187 |
if (deltaY < 0) { |
| 188 |
if (((deltaY*1.01) <= this.dy) && |
| 189 |
((deltaY*0.99) >= this.dy)) { |
| 190 |
newY = this.destY; |
| 191 |
} else { |
| 192 |
register = true; |
| 193 |
} |
| 194 |
} else { |
| 195 |
if (((deltaY*1.01) >= this.dy) && |
| 196 |
((deltaY*0.99) <= this.dy)) { |
| 197 |
newY = this.destY; |
| 198 |
} else { |
| 199 |
register = true; |
| 200 |
} |
| 201 |
} |
| 202 |
this.setPos(newX, newY); |
| 203 |
if (!register) { |
| 204 |
var nc = this.pile.nextCard(this); |
| 205 |
while (nc) { |
| 206 |
var ncelem = nc?nc.elem:null; |
| 207 |
if (ncelem.parentNode == this.pile.g) { |
| 208 |
this.pile.g.insertBefore(this.elem, ncelem); |
| 209 |
break; |
| 210 |
} |
| 211 |
nc = this.pile.nextCard(nc); |
| 212 |
} |
| 213 |
if (!nc) |
| 214 |
this.pile.g.appendChild(this.elem); |
| 215 |
} |
| 216 |
this.moving = register; |
| 217 |
return register; |
| 218 |
} |
| 219 |
|
| 220 |
function moveCards() { |
| 221 |
var register = false; |
| 222 |
var num = MOVING.length; |
| 223 |
for (var i=0; i<num; i++) { |
| 224 |
var card = MOVING.shift(); |
| 225 |
if (card.updateDisplay()) { |
| 226 |
MOVING.push(card); |
| 227 |
register = true; |
| 228 |
} else { |
| 229 |
} |
| 230 |
} |
| 231 |
if (register) { |
| 232 |
setTimeout("moveCards()", 50); |
| 233 |
} |
| 234 |
} |