| 1 | <script> |
| 2 | import { getSquareId, getSquareFromId } from "@/utils/squareId"; |
| 3 | import { ArrayFun } from "@/utils/array"; |
| 4 | import { store } from "@/store"; |
| 5 | export default { |
| 6 | name: "my-board", |
| 7 | // Last move cannot be guessed from here, and is required for highlights. |
| 8 | // vr: object to check moves, print board... |
| 9 | // userColor is left undefined for an external observer |
| 10 | props: [ |
| 11 | "vr", |
| 12 | "lastMove", |
| 13 | "analyze", |
| 14 | "score", |
| 15 | "incheck", |
| 16 | "orientation", |
| 17 | "userColor", |
| 18 | "vname" |
| 19 | ], |
| 20 | data: function() { |
| 21 | return { |
| 22 | mobileBrowser: ("ontouchstart" in window), |
| 23 | possibleMoves: [], //filled after each valid click/dragstart |
| 24 | choices: [], //promotion pieces, or checkered captures... (as moves) |
| 25 | containerPos: null, |
| 26 | selectedPiece: null, //moving piece (or clicked piece) |
| 27 | start: null, //pixels coordinates + id of starting square (click or drag) |
| 28 | startArrow: null, |
| 29 | movingArrow: null, |
| 30 | arrows: [], //object of {start: x,y / end: x,y} |
| 31 | circles: {}, //object of squares' ID --> true (TODO: use a set?) |
| 32 | click: "", |
| 33 | clickTime: 0, |
| 34 | settings: store.state.settings |
| 35 | }; |
| 36 | }, |
| 37 | render(h) { |
| 38 | if (!this.vr) { |
| 39 | // Return empty div of class 'game' to avoid error when setting size |
| 40 | return h( |
| 41 | "div", |
| 42 | { "class": { game: true } } |
| 43 | ); |
| 44 | } |
| 45 | const [sizeX, sizeY] = [V.size.x, V.size.y]; |
| 46 | // Precompute hints squares to facilitate rendering |
| 47 | let hintSquares = ArrayFun.init(sizeX, sizeY, false); |
| 48 | this.possibleMoves.forEach(m => { |
| 49 | hintSquares[m.end.x][m.end.y] = true; |
| 50 | }); |
| 51 | // Also precompute in-check squares |
| 52 | let incheckSq = ArrayFun.init(sizeX, sizeY, false); |
| 53 | this.incheck.forEach(sq => { |
| 54 | incheckSq[sq[0]][sq[1]] = true; |
| 55 | }); |
| 56 | |
| 57 | let lm = this.lastMove; |
| 58 | // Precompute lastMove highlighting squares |
| 59 | const lmHighlights = {}; |
| 60 | if (!!lm) { |
| 61 | if (!Array.isArray(lm)) lm = [lm]; |
| 62 | lm.forEach(m => { |
| 63 | if (V.OnBoard(m.start.x, m.start.y)) |
| 64 | lmHighlights[m.start.x + sizeX * m.start.y] = true; |
| 65 | if (V.OnBoard(m.end.x, m.end.y)) |
| 66 | lmHighlights[m.end.x + sizeX * m.end.y] = true; |
| 67 | }); |
| 68 | } |
| 69 | const showLight = ( |
| 70 | this.settings.highlight && |
| 71 | ["all","highlight"].includes(V.ShowMoves) |
| 72 | ); |
| 73 | const showCheck = ( |
| 74 | this.settings.highlight && |
| 75 | ["all","highlight","byrow"].includes(V.ShowMoves) |
| 76 | ); |
| 77 | const orientation = !V.CanFlip ? "w" : this.orientation; |
| 78 | // Ensure that squares colors do not change when board is flipped |
| 79 | const lightSquareMod = (sizeX + sizeY) % 2; |
| 80 | const showPiece = (x, y) => { |
| 81 | return ( |
| 82 | this.vr.board[x][y] != V.EMPTY && |
| 83 | (!this.vr.enlightened || this.analyze || this.score != "*" || |
| 84 | (!!this.userColor && this.vr.enlightened[this.userColor][x][y])) |
| 85 | ); |
| 86 | }; |
| 87 | const inHighlight = (x, y) => { |
| 88 | return showLight && !!lmHighlights[x + sizeX * y]; |
| 89 | }; |
| 90 | const inShadow = (x, y) => { |
| 91 | return ( |
| 92 | !this.analyze && |
| 93 | this.score == "*" && |
| 94 | this.vr.enlightened && |
| 95 | (!this.userColor || !this.vr.enlightened[this.userColor][x][y]) |
| 96 | ); |
| 97 | }; |
| 98 | // Create board element (+ reserves if needed by variant) |
| 99 | let elementArray = []; |
| 100 | const gameDiv = h( |
| 101 | "div", |
| 102 | { |
| 103 | attrs: { id: "gamePosition" }, |
| 104 | "class": { |
| 105 | game: true, |
| 106 | clearer: true |
| 107 | } |
| 108 | }, |
| 109 | [...Array(sizeX).keys()].map(i => { |
| 110 | const ci = orientation == "w" ? i : sizeX - i - 1; |
| 111 | return h( |
| 112 | "div", |
| 113 | { |
| 114 | "class": { |
| 115 | row: true |
| 116 | }, |
| 117 | style: { opacity: this.choices.length > 0 ? "0.5" : "1" } |
| 118 | }, |
| 119 | [...Array(sizeY).keys()].map(j => { |
| 120 | const cj = orientation == "w" ? j : sizeY - j - 1; |
| 121 | const squareId = "sq-" + ci + "-" + cj; |
| 122 | let elems = []; |
| 123 | if (showPiece(ci, cj)) { |
| 124 | let pieceSpecs = { |
| 125 | "class": { |
| 126 | piece: true, |
| 127 | ghost: |
| 128 | !!this.selectedPiece && |
| 129 | this.selectedPiece.parentNode.id == squareId |
| 130 | }, |
| 131 | attrs: { |
| 132 | src: |
| 133 | "/images/pieces/" + |
| 134 | this.vr.getPpath( |
| 135 | this.vr.board[ci][cj], |
| 136 | // Extra args useful for some variants: |
| 137 | this.userColor, |
| 138 | this.score, |
| 139 | this.orientation) + |
| 140 | V.IMAGE_EXTENSION |
| 141 | } |
| 142 | }; |
| 143 | if (this.arrows.length == 0) |
| 144 | pieceSpecs["style"] = { position: "absolute" }; |
| 145 | elems.push(h("img", pieceSpecs)); |
| 146 | } |
| 147 | if (this.settings.hints && hintSquares[ci][cj]) { |
| 148 | elems.push( |
| 149 | h("img", { |
| 150 | "class": { |
| 151 | "mark-square": true |
| 152 | }, |
| 153 | attrs: { |
| 154 | src: "/images/mark.svg" |
| 155 | } |
| 156 | }) |
| 157 | ); |
| 158 | } |
| 159 | if (!!this.circles[squareId]) { |
| 160 | elems.push( |
| 161 | h("img", { |
| 162 | "class": { |
| 163 | "circle-square": true |
| 164 | }, |
| 165 | attrs: { |
| 166 | src: "/images/circle.svg" |
| 167 | } |
| 168 | }) |
| 169 | ); |
| 170 | } |
| 171 | const lightSquare = (ci + cj) % 2 == lightSquareMod; |
| 172 | return h( |
| 173 | "div", |
| 174 | { |
| 175 | "class": { |
| 176 | board: true, |
| 177 | ["board" + sizeY]: true, |
| 178 | "light-square": |
| 179 | !V.Notoodark && lightSquare && !V.Monochrome, |
| 180 | "dark-square": |
| 181 | !V.Notoodark && (!lightSquare || !!V.Monochrome), |
| 182 | "middle-square": V.Notoodark, |
| 183 | [this.settings.bcolor]: true, |
| 184 | "in-shadow": inShadow(ci, cj), |
| 185 | "highlight-light": inHighlight(ci, cj) && lightSquare, |
| 186 | "highlight-dark": |
| 187 | inHighlight(ci, cj) && (V.Monochrome || !lightSquare), |
| 188 | "incheck-light": |
| 189 | showCheck && lightSquare && incheckSq[ci][cj], |
| 190 | "incheck-dark": |
| 191 | showCheck && !lightSquare && incheckSq[ci][cj] |
| 192 | }, |
| 193 | attrs: { |
| 194 | id: getSquareId({ x: ci, y: cj }) |
| 195 | } |
| 196 | }, |
| 197 | elems |
| 198 | ); |
| 199 | }) |
| 200 | ); |
| 201 | }) |
| 202 | ); |
| 203 | if (!!this.vr.reserve) { |
| 204 | const playingColor = this.userColor || "w"; //default for an observer |
| 205 | const shiftIdx = playingColor == "w" ? 0 : 1; |
| 206 | // Some variants have more than sizeY reserve pieces (Clorange: 10) |
| 207 | const reserveSquareNb = Math.max(sizeY, V.RESERVE_PIECES.length); |
| 208 | let myReservePiecesArray = []; |
| 209 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { |
| 210 | const qty = this.vr.reserve[playingColor][V.RESERVE_PIECES[i]]; |
| 211 | myReservePiecesArray.push( |
| 212 | h( |
| 213 | "div", |
| 214 | { |
| 215 | "class": { board: true, ["board" + reserveSquareNb]: true }, |
| 216 | attrs: { id: getSquareId({ x: sizeX + shiftIdx, y: i }) }, |
| 217 | style: { opacity: qty > 0 ? 1 : 0.35 } |
| 218 | }, |
| 219 | [ |
| 220 | h("img", { |
| 221 | // NOTE: class "reserve" not used currently |
| 222 | "class": { piece: true, reserve: true }, |
| 223 | attrs: { |
| 224 | src: |
| 225 | "/images/pieces/" + |
| 226 | this.vr.getReservePpath(i, playingColor, orientation) + |
| 227 | ".svg" |
| 228 | } |
| 229 | }), |
| 230 | h( |
| 231 | "sup", |
| 232 | { |
| 233 | "class": { "reserve-count": true }, |
| 234 | style: { top: "calc(100% + 5px)" } |
| 235 | }, |
| 236 | [ qty ] |
| 237 | ) |
| 238 | ] |
| 239 | ) |
| 240 | ); |
| 241 | } |
| 242 | let oppReservePiecesArray = []; |
| 243 | const oppCol = V.GetOppCol(playingColor); |
| 244 | for (let i = 0; i < V.RESERVE_PIECES.length; i++) { |
| 245 | const qty = this.vr.reserve[oppCol][V.RESERVE_PIECES[i]]; |
| 246 | oppReservePiecesArray.push( |
| 247 | h( |
| 248 | "div", |
| 249 | { |
| 250 | "class": { board: true, ["board" + reserveSquareNb]: true }, |
| 251 | attrs: { id: getSquareId({ x: sizeX + (1 - shiftIdx), y: i }) }, |
| 252 | style: { opacity: qty > 0 ? 1 : 0.35 } |
| 253 | }, |
| 254 | [ |
| 255 | h("img", { |
| 256 | "class": { piece: true, reserve: true }, |
| 257 | attrs: { |
| 258 | src: |
| 259 | "/images/pieces/" + |
| 260 | this.vr.getReservePpath(i, oppCol, orientation) + |
| 261 | ".svg" |
| 262 | } |
| 263 | }), |
| 264 | h( |
| 265 | "sup", |
| 266 | { |
| 267 | "class": { "reserve-count": true }, |
| 268 | style: { top: "calc(100% + 5px)" } |
| 269 | }, |
| 270 | [ qty ] |
| 271 | ) |
| 272 | ] |
| 273 | ) |
| 274 | ); |
| 275 | } |
| 276 | const myReserveTop = ( |
| 277 | (playingColor == 'w' && orientation == 'b') || |
| 278 | (playingColor == 'b' && orientation == 'w') |
| 279 | ); |
| 280 | // Center reserves, assuming same number of pieces for each side: |
| 281 | const nbReservePieces = myReservePiecesArray.length; |
| 282 | const marginLeft = |
| 283 | ((100 - nbReservePieces * (100 / reserveSquareNb)) / 2) + "%"; |
| 284 | const reserveTop = |
| 285 | h( |
| 286 | "div", |
| 287 | { |
| 288 | "class": { |
| 289 | game: true, |
| 290 | "reserve-div": true |
| 291 | }, |
| 292 | style: { |
| 293 | "margin-left": marginLeft |
| 294 | } |
| 295 | }, |
| 296 | [ |
| 297 | h( |
| 298 | "div", |
| 299 | { |
| 300 | "class": { |
| 301 | row: true, |
| 302 | "reserve-row": true |
| 303 | } |
| 304 | }, |
| 305 | myReserveTop ? myReservePiecesArray : oppReservePiecesArray |
| 306 | ) |
| 307 | ] |
| 308 | ); |
| 309 | var reserveBottom = |
| 310 | h( |
| 311 | "div", |
| 312 | { |
| 313 | "class": { |
| 314 | game: true, |
| 315 | "reserve-div": true |
| 316 | }, |
| 317 | style: { |
| 318 | "margin-left": marginLeft |
| 319 | } |
| 320 | }, |
| 321 | [ |
| 322 | h( |
| 323 | "div", |
| 324 | { |
| 325 | "class": { |
| 326 | row: true, |
| 327 | "reserve-row": true |
| 328 | } |
| 329 | }, |
| 330 | myReserveTop ? oppReservePiecesArray : myReservePiecesArray |
| 331 | ) |
| 332 | ] |
| 333 | ); |
| 334 | elementArray.push(reserveTop); |
| 335 | } |
| 336 | elementArray.push(gameDiv); |
| 337 | if (!!this.vr.reserve) elementArray.push(reserveBottom); |
| 338 | const boardElt = document.getElementById("gamePosition"); |
| 339 | // boardElt might be undefine (at first drawing) |
| 340 | if (this.choices.length > 0 && !!boardElt) { |
| 341 | const squareWidth = boardElt.offsetWidth / sizeY; |
| 342 | const offset = [boardElt.offsetTop, boardElt.offsetLeft]; |
| 343 | const maxNbeltsPerRow = Math.min(this.choices.length, sizeY); |
| 344 | let topOffset = offset[0] + (sizeY / 2) * squareWidth - squareWidth / 2; |
| 345 | let choicesHeight = squareWidth; |
| 346 | if (this.choices.length >= sizeY) { |
| 347 | // A second row is required (Eightpieces variant) |
| 348 | topOffset -= squareWidth / 2; |
| 349 | choicesHeight *= 2; |
| 350 | } |
| 351 | const choices = h( |
| 352 | "div", |
| 353 | { |
| 354 | attrs: { id: "choices" }, |
| 355 | "class": { row: true }, |
| 356 | style: { |
| 357 | top: topOffset + "px", |
| 358 | left: |
| 359 | offset[1] + |
| 360 | (squareWidth * Math.max(sizeY - this.choices.length, 0)) / 2 + |
| 361 | "px", |
| 362 | width: (maxNbeltsPerRow * squareWidth) + "px", |
| 363 | height: choicesHeight + "px" |
| 364 | } |
| 365 | }, |
| 366 | [ h( |
| 367 | "div", |
| 368 | { |
| 369 | "class": { "full-width": true } |
| 370 | }, |
| 371 | this.choices.map(m => { |
| 372 | // A "choice" is a move |
| 373 | const applyMove = (e) => { |
| 374 | e.stopPropagation(); |
| 375 | // Force a delay between move is shown and clicked |
| 376 | // (otherwise a "double-click" bug might occur) |
| 377 | if (Date.now() - this.clickTime < 200) return; |
| 378 | this.choices = []; |
| 379 | this.play(m); |
| 380 | }; |
| 381 | const onClick = |
| 382 | this.mobileBrowser |
| 383 | ? { touchend: applyMove } |
| 384 | : { mouseup: applyMove }; |
| 385 | return h( |
| 386 | "div", |
| 387 | { |
| 388 | "class": { |
| 389 | board: true, |
| 390 | ["board" + sizeY]: true |
| 391 | }, |
| 392 | style: { |
| 393 | width: (100 / maxNbeltsPerRow) + "%", |
| 394 | "padding-bottom": (100 / maxNbeltsPerRow) + "%" |
| 395 | } |
| 396 | }, |
| 397 | [ |
| 398 | h("img", { |
| 399 | attrs: { |
| 400 | src: |
| 401 | "/images/pieces/" + |
| 402 | // orientation: extra arg useful for some variants: |
| 403 | this.vr.getPPpath(m, this.orientation) + |
| 404 | V.IMAGE_EXTENSION |
| 405 | }, |
| 406 | "class": { "choice-piece": true }, |
| 407 | on: onClick |
| 408 | }) |
| 409 | ] |
| 410 | ); |
| 411 | }) |
| 412 | ) ] |
| 413 | ); |
| 414 | elementArray.unshift(choices); |
| 415 | } |
| 416 | let onEvents = {}; |
| 417 | // NOTE: click = mousedown + mouseup |
| 418 | if (this.mobileBrowser) { |
| 419 | onEvents = { |
| 420 | on: { |
| 421 | touchstart: this.mousedown, |
| 422 | touchmove: this.mousemove, |
| 423 | touchend: this.mouseup |
| 424 | } |
| 425 | }; |
| 426 | } else { |
| 427 | onEvents = { |
| 428 | on: { |
| 429 | mousedown: this.mousedown, |
| 430 | mousemove: this.mousemove, |
| 431 | mouseup: this.mouseup, |
| 432 | contextmenu: this.blockContextMenu |
| 433 | } |
| 434 | }; |
| 435 | } |
| 436 | return ( |
| 437 | h( |
| 438 | "div", |
| 439 | Object.assign({ attrs: { id: "rootBoardElement" } }, onEvents), |
| 440 | elementArray |
| 441 | ) |
| 442 | ); |
| 443 | }, |
| 444 | updated: function() { |
| 445 | this.re_setDrawings(); |
| 446 | }, |
| 447 | methods: { |
| 448 | blockContextMenu: function(e) { |
| 449 | e.preventDefault(); |
| 450 | e.stopPropagation(); |
| 451 | return false; |
| 452 | }, |
| 453 | cancelResetArrows: function() { |
| 454 | this.startArrow = null; |
| 455 | this.arrows = []; |
| 456 | this.circles = {}; |
| 457 | const curCanvas = document.getElementById("arrowCanvas"); |
| 458 | if (!!curCanvas) curCanvas.parentNode.removeChild(curCanvas); |
| 459 | }, |
| 460 | coordsToXY: function(coords, top, left, squareWidth) { |
| 461 | return { |
| 462 | // [1] for x and [0] for y because conventions in rules are inversed. |
| 463 | x: ( |
| 464 | left + window.scrollX + |
| 465 | ( |
| 466 | squareWidth * |
| 467 | (this.orientation == 'w' ? coords[1] : (V.size.y - coords[1])) |
| 468 | ) |
| 469 | ), |
| 470 | y: ( |
| 471 | top + window.scrollY + |
| 472 | ( |
| 473 | squareWidth * |
| 474 | (this.orientation == 'w' ? coords[0] : (V.size.x - coords[0])) |
| 475 | ) |
| 476 | ) |
| 477 | }; |
| 478 | }, |
| 479 | computeEndArrow: function(start, end, top, left, squareWidth) { |
| 480 | const endCoords = this.coordsToXY(end, top, left, squareWidth); |
| 481 | const delta = [endCoords.x - start.x, endCoords.y - start.y]; |
| 482 | const dist = Math.sqrt(delta[0] * delta[0] + delta[1] * delta[1]); |
| 483 | // Simple heuristic for now, just remove 1/3 square. |
| 484 | // TODO: should depend on the orientation. |
| 485 | const fracSqWidth = squareWidth / 3; |
| 486 | return { |
| 487 | x: endCoords.x - delta[0] * fracSqWidth / dist, |
| 488 | y: endCoords.y - delta[1] * fracSqWidth / dist |
| 489 | }; |
| 490 | }, |
| 491 | drawCurrentArrow: function() { |
| 492 | const boardElt = document.getElementById("gamePosition"); |
| 493 | const squareWidth = boardElt.offsetWidth / V.size.y; |
| 494 | const bPos = boardElt.getBoundingClientRect(); |
| 495 | const aStart = |
| 496 | this.coordsToXY( |
| 497 | [this.startArrow[0] + 0.5, this.startArrow[1] + 0.5], |
| 498 | bPos.top, bPos.left, squareWidth); |
| 499 | const aEnd = |
| 500 | this.computeEndArrow( |
| 501 | aStart, [this.movingArrow[0] + 0.5, this.movingArrow[1] + 0.5], |
| 502 | bPos.top, bPos.left, squareWidth); |
| 503 | let currentArrow = document.getElementById("currentArrow"); |
| 504 | const d = |
| 505 | "M" + aStart.x + "," + aStart.y + " " + "L" + aEnd.x + "," + aEnd.y; |
| 506 | const arrowWidth = squareWidth / 4; |
| 507 | if (!!currentArrow) currentArrow.setAttribute("d", d); |
| 508 | else { |
| 509 | let domArrow = |
| 510 | document.createElementNS("http://www.w3.org/2000/svg", "path"); |
| 511 | domArrow.classList.add("svg-arrow"); |
| 512 | domArrow.id = "currentArrow"; |
| 513 | domArrow.setAttribute("d", d); |
| 514 | domArrow.style = "stroke-width:" + arrowWidth + "px"; |
| 515 | document.getElementById("arrowCanvas") |
| 516 | .insertAdjacentElement("beforeend", domArrow); |
| 517 | } |
| 518 | }, |
| 519 | addArrow: function(arrow) { |
| 520 | this.arrows.push(arrow); |
| 521 | // Also add to DOM: |
| 522 | const boardElt = document.getElementById("gamePosition"); |
| 523 | const squareWidth = boardElt.offsetWidth / V.size.y; |
| 524 | const bPos = boardElt.getBoundingClientRect(); |
| 525 | const newArrow = |
| 526 | this.getSvgArrow(arrow, bPos.top, bPos.left, squareWidth); |
| 527 | document.getElementById("arrowCanvas") |
| 528 | .insertAdjacentElement("beforeend", newArrow); |
| 529 | }, |
| 530 | getSvgArrow: function(arrow, top, left, squareWidth) { |
| 531 | const aStart = |
| 532 | this.coordsToXY( |
| 533 | [arrow.start[0] + 0.5, arrow.start[1] + 0.5], |
| 534 | top, left, squareWidth); |
| 535 | const aEnd = |
| 536 | this.computeEndArrow( |
| 537 | aStart, [arrow.end[0] + 0.5, arrow.end[1] + 0.5], |
| 538 | top, left, squareWidth); |
| 539 | const arrowWidth = squareWidth / 4; |
| 540 | let path = |
| 541 | document.createElementNS("http://www.w3.org/2000/svg", "path"); |
| 542 | path.classList.add("svg-arrow"); |
| 543 | path.setAttribute( |
| 544 | "d", |
| 545 | "M" + aStart.x + "," + aStart.y + " " + "L" + aEnd.x + "," + aEnd.y |
| 546 | ); |
| 547 | path.style = "stroke-width:" + arrowWidth + "px"; |
| 548 | return path; |
| 549 | }, |
| 550 | re_setDrawings: function() { |
| 551 | // Remove current canvas, if any |
| 552 | const curCanvas = document.getElementById("arrowCanvas"); |
| 553 | if (!!curCanvas) curCanvas.parentNode.removeChild(curCanvas); |
| 554 | // Add some drawing on board (for some variants + arrows and circles) |
| 555 | const boardElt = document.getElementById("gamePosition"); |
| 556 | const squareWidth = boardElt.offsetWidth / V.size.y; |
| 557 | const bPos = boardElt.getBoundingClientRect(); |
| 558 | let svgArrows = []; |
| 559 | this.arrows.forEach(a => { |
| 560 | svgArrows.push(this.getSvgArrow(a, bPos.top, bPos.left, squareWidth)); |
| 561 | }); |
| 562 | let vLines = []; |
| 563 | if (!!V.Lines) { |
| 564 | V.Lines.forEach(line => { |
| 565 | const lStart = |
| 566 | this.coordsToXY(line[0], bPos.top, bPos.left, squareWidth); |
| 567 | const lEnd = |
| 568 | this.coordsToXY(line[1], bPos.top, bPos.left, squareWidth); |
| 569 | let path = |
| 570 | document.createElementNS("http://www.w3.org/2000/svg", "path"); |
| 571 | if (line[0][0] == line[1][0] || line[0][1] == line[1][1]) |
| 572 | path.classList.add("svg-line"); |
| 573 | else |
| 574 | // "Diagonals" are drawn with a lighter color (TODO: generalize) |
| 575 | path.classList.add("svg-diag"); |
| 576 | path.setAttribute( |
| 577 | "d", |
| 578 | "M" + lStart.x + "," + lStart.y + " " + |
| 579 | "L" + lEnd.x + "," + lEnd.y |
| 580 | ); |
| 581 | vLines.push(path); |
| 582 | }); |
| 583 | } |
| 584 | let arrowCanvas = |
| 585 | document.createElementNS("http://www.w3.org/2000/svg", "svg"); |
| 586 | arrowCanvas.id = "arrowCanvas"; |
| 587 | arrowCanvas.setAttribute("stroke", "none"); |
| 588 | let defs = |
| 589 | document.createElementNS("http://www.w3.org/2000/svg", "defs"); |
| 590 | const arrowWidth = squareWidth / 4; |
| 591 | let marker = |
| 592 | document.createElementNS("http://www.w3.org/2000/svg", "marker"); |
| 593 | marker.id = "arrow"; |
| 594 | marker.setAttribute("markerWidth", (2 * arrowWidth) + "px"); |
| 595 | marker.setAttribute("markerHeight", (3 * arrowWidth) + "px"); |
| 596 | marker.setAttribute("markerUnits", "userSpaceOnUse"); |
| 597 | marker.setAttribute("refX", "0"); |
| 598 | marker.setAttribute("refY", (1.5 * arrowWidth) + "px"); |
| 599 | marker.setAttribute("orient", "auto"); |
| 600 | let head = |
| 601 | document.createElementNS("http://www.w3.org/2000/svg", "path"); |
| 602 | head.classList.add("arrow-head"); |
| 603 | head.setAttribute( |
| 604 | "d", |
| 605 | "M0,0 L0," + (3 * arrowWidth) + " L" + |
| 606 | (2 * arrowWidth) + "," + (1.5 * arrowWidth) + " z" |
| 607 | ); |
| 608 | marker.appendChild(head); |
| 609 | defs.appendChild(marker); |
| 610 | arrowCanvas.appendChild(defs); |
| 611 | svgArrows.concat(vLines).forEach(av => arrowCanvas.appendChild(av)); |
| 612 | document.getElementById("rootBoardElement").appendChild(arrowCanvas); |
| 613 | }, |
| 614 | mousedown: function(e) { |
| 615 | e.preventDefault(); |
| 616 | if (!this.mobileBrowser && e.which != 3) |
| 617 | // Cancel current drawing and circles, if any |
| 618 | this.cancelResetArrows(); |
| 619 | if (this.mobileBrowser || e.which == 1) { |
| 620 | // Mouse left button |
| 621 | if (!this.start) { |
| 622 | this.containerPos = |
| 623 | document.getElementById("boardContainer").getBoundingClientRect(); |
| 624 | // NOTE: classList[0] is enough: 'piece' is the first assigned class |
| 625 | const withPiece = (e.target.classList[0] == "piece"); |
| 626 | // Emit the click event which could be used by some variants |
| 627 | this.$emit( |
| 628 | "click-square", |
| 629 | getSquareFromId(withPiece ? e.target.parentNode.id : e.target.id) |
| 630 | ); |
| 631 | // Start square must contain a piece. |
| 632 | if (!withPiece) return; |
| 633 | let parent = e.target.parentNode; //surrounding square |
| 634 | // Show possible moves if current player allowed to play |
| 635 | const startSquare = getSquareFromId(parent.id); |
| 636 | this.possibleMoves = []; |
| 637 | const color = this.analyze ? this.vr.turn : this.userColor; |
| 638 | if (this.vr.canIplay(color, startSquare)) |
| 639 | this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare); |
| 640 | // For potential drag'n drop, remember start coordinates |
| 641 | // (to center the piece on mouse cursor) |
| 642 | const rect = parent.getBoundingClientRect(); |
| 643 | this.start = { |
| 644 | x: rect.x + rect.width / 2, |
| 645 | y: rect.y + rect.width / 2, |
| 646 | id: parent.id |
| 647 | }; |
| 648 | // Add the moving piece to the board, just after current image |
| 649 | this.selectedPiece = e.target.cloneNode(); |
| 650 | Object.assign( |
| 651 | this.selectedPiece.style, |
| 652 | { |
| 653 | position: "absolute", |
| 654 | top: 0, |
| 655 | display: "inline-block", |
| 656 | zIndex: 3000 |
| 657 | } |
| 658 | ); |
| 659 | parent.insertBefore(this.selectedPiece, e.target.nextSibling); |
| 660 | } else { |
| 661 | this.processMoveAttempt(e); |
| 662 | } |
| 663 | } else if (e.which == 3) { |
| 664 | // Mouse right button |
| 665 | this.containerPos = |
| 666 | document.getElementById("gamePosition").getBoundingClientRect(); |
| 667 | let elem = e.target; |
| 668 | // Next loop because of potential marks |
| 669 | while (elem.tagName == "IMG") elem = elem.parentNode; |
| 670 | this.startArrow = getSquareFromId(elem.id); |
| 671 | } |
| 672 | }, |
| 673 | mousemove: function(e) { |
| 674 | if (!this.selectedPiece && !this.startArrow) return; |
| 675 | // Cancel if off boardContainer |
| 676 | const [offsetX, offsetY] = |
| 677 | this.mobileBrowser |
| 678 | ? |
| 679 | [ |
| 680 | e.changedTouches[0].pageX, |
| 681 | // TODO: fixing attempt for smartphones, removing window.scrollY |
| 682 | e.changedTouches[0].pageY - window.scrollY |
| 683 | ] |
| 684 | : [e.clientX, e.clientY]; |
| 685 | if ( |
| 686 | offsetX < this.containerPos.left || |
| 687 | offsetX > this.containerPos.right || |
| 688 | offsetY < this.containerPos.top || |
| 689 | offsetY > this.containerPos.bottom |
| 690 | ) { |
| 691 | if (!!this.selectedPiece) { |
| 692 | this.selectedPiece.parentNode.removeChild(this.selectedPiece); |
| 693 | delete this.selectedPiece; |
| 694 | this.selectedPiece = null; |
| 695 | this.start = null; |
| 696 | this.possibleMoves = []; //in case of |
| 697 | this.click = ""; |
| 698 | let selected = document.querySelector(".ghost"); |
| 699 | if (!!selected) selected.classList.remove("ghost"); |
| 700 | } |
| 701 | else { |
| 702 | this.startArrow = null; |
| 703 | this.movingArrow = null; |
| 704 | const currentArrow = document.getElementById("currentArrow"); |
| 705 | if (!!currentArrow) |
| 706 | currentArrow.parentNode.removeChild(currentArrow); |
| 707 | } |
| 708 | return; |
| 709 | } |
| 710 | e.preventDefault(); |
| 711 | if (!!this.selectedPiece) { |
| 712 | // There is an active element: move it around |
| 713 | Object.assign( |
| 714 | this.selectedPiece.style, |
| 715 | { |
| 716 | left: offsetX - this.start.x + "px", |
| 717 | top: offsetY - this.start.y + "px" |
| 718 | } |
| 719 | ); |
| 720 | } |
| 721 | else { |
| 722 | let elem = e.target; |
| 723 | // Next loop because of potential marks |
| 724 | while (elem.tagName == "IMG") elem = elem.parentNode; |
| 725 | // To center the arrow in square: |
| 726 | const movingCoords = getSquareFromId(elem.id); |
| 727 | if ( |
| 728 | movingCoords[0] != this.startArrow[0] || |
| 729 | movingCoords[1] != this.startArrow[1] |
| 730 | ) { |
| 731 | this.movingArrow = movingCoords; |
| 732 | this.drawCurrentArrow(); |
| 733 | } |
| 734 | } |
| 735 | }, |
| 736 | mouseup: function(e) { |
| 737 | e.preventDefault(); |
| 738 | if (this.mobileBrowser || e.which == 1) { |
| 739 | if (!this.selectedPiece) return; |
| 740 | // Drag'n drop. Selected piece is no longer needed: |
| 741 | this.selectedPiece.parentNode.removeChild(this.selectedPiece); |
| 742 | delete this.selectedPiece; |
| 743 | this.selectedPiece = null; |
| 744 | this.processMoveAttempt(e); |
| 745 | } else if (e.which == 3) { |
| 746 | if (!this.startArrow) return; |
| 747 | // Mouse right button |
| 748 | this.movingArrow = null; |
| 749 | this.processArrowAttempt(e); |
| 750 | } |
| 751 | }, |
| 752 | // Called by BaseGame after partially undoing multi-moves: |
| 753 | resetCurrentAttempt: function() { |
| 754 | this.possibleMoves = []; |
| 755 | this.start = null; |
| 756 | this.click = ""; |
| 757 | this.selectedPiece = null; |
| 758 | }, |
| 759 | processMoveAttempt: function(e) { |
| 760 | // Obtain the move from start and end squares |
| 761 | const [offsetX, offsetY] = |
| 762 | this.mobileBrowser |
| 763 | ? |
| 764 | [ |
| 765 | e.changedTouches[0].pageX, |
| 766 | e.changedTouches[0].pageY - window.scrollY |
| 767 | ] |
| 768 | : [e.clientX, e.clientY]; |
| 769 | let landing = document.elementFromPoint(offsetX, offsetY); |
| 770 | // Next condition: classList.contains(piece) fails because of marks |
| 771 | while (landing.tagName == "IMG") landing = landing.parentNode; |
| 772 | if (this.start.id == landing.id) { |
| 773 | if (this.click == landing.id) { |
| 774 | // Second click on same square: cancel current move |
| 775 | this.possibleMoves = []; |
| 776 | this.start = null; |
| 777 | this.click = ""; |
| 778 | } else this.click = landing.id; |
| 779 | return; |
| 780 | } |
| 781 | this.start = null; |
| 782 | // OK: process move attempt, landing is a square node |
| 783 | let endSquare = getSquareFromId(landing.id); |
| 784 | let moves = this.findMatchingMoves(endSquare); |
| 785 | this.possibleMoves = []; |
| 786 | if (moves.length > 1) { |
| 787 | this.clickTime = Date.now(); |
| 788 | this.choices = moves; |
| 789 | } else if (moves.length == 1) this.play(moves[0]); |
| 790 | // else: forbidden move attempt |
| 791 | }, |
| 792 | processArrowAttempt: function(e) { |
| 793 | // Obtain the arrow from start and end squares |
| 794 | const [offsetX, offsetY] = [e.clientX, e.clientY]; |
| 795 | let landing = document.elementFromPoint(offsetX, offsetY); |
| 796 | // Next condition: classList.contains(piece) fails because of marks |
| 797 | while (landing.tagName == "IMG") landing = landing.parentNode; |
| 798 | const landingCoords = getSquareFromId(landing.id); |
| 799 | if ( |
| 800 | this.startArrow[0] == landingCoords[0] && |
| 801 | this.startArrow[1] == landingCoords[1] |
| 802 | ) { |
| 803 | // Draw (or erase) a circle |
| 804 | this.$set(this.circles, landing.id, !this.circles[landing.id]); |
| 805 | } |
| 806 | else { |
| 807 | // OK: add arrow, landing is a new square |
| 808 | const currentArrow = document.getElementById("currentArrow"); |
| 809 | currentArrow.parentNode.removeChild(currentArrow); |
| 810 | this.addArrow({ |
| 811 | start: this.startArrow, |
| 812 | end: landingCoords |
| 813 | }); |
| 814 | } |
| 815 | this.startArrow = null; |
| 816 | }, |
| 817 | findMatchingMoves: function(endSquare) { |
| 818 | // Run through moves list and return the matching set (if promotions...) |
| 819 | return ( |
| 820 | this.possibleMoves.filter(m => { |
| 821 | return (endSquare[0] == m.end.x && endSquare[1] == m.end.y); |
| 822 | }) |
| 823 | ); |
| 824 | }, |
| 825 | play: function(move) { |
| 826 | this.$emit("play-move", move); |
| 827 | } |
| 828 | } |
| 829 | }; |
| 830 | </script> |
| 831 | |
| 832 | <style lang="sass"> |
| 833 | // SVG dynamically added, so not scoped |
| 834 | #arrowCanvas |
| 835 | pointer-events: none |
| 836 | position: absolute |
| 837 | top: 0 |
| 838 | left: 0 |
| 839 | width: 100% |
| 840 | height: 100% |
| 841 | |
| 842 | .svg-arrow |
| 843 | opacity: 0.65 |
| 844 | stroke: #5f0e78 |
| 845 | fill: none |
| 846 | marker-end: url(#arrow) |
| 847 | |
| 848 | .svg-line |
| 849 | stroke: black |
| 850 | |
| 851 | .svg-diag |
| 852 | stroke: grey |
| 853 | |
| 854 | .arrow-head |
| 855 | fill: #5f0e78 |
| 856 | </style> |
| 857 | |
| 858 | <style lang="sass" scoped> |
| 859 | @import "@/styles/_board_squares_img.sass"; |
| 860 | |
| 861 | //.game.reserve-div |
| 862 | // TODO: would be cleaner to restrict width so that it doesn't overflow |
| 863 | // Commented out because pieces would disappear over the board otherwise: |
| 864 | //overflow: hidden |
| 865 | .reserve-count |
| 866 | width: 100% |
| 867 | text-align: center |
| 868 | display: inline-block |
| 869 | position: absolute |
| 870 | .reserve-row |
| 871 | margin-bottom: 15px |
| 872 | |
| 873 | .full-width |
| 874 | width: 100% |
| 875 | |
| 876 | .game |
| 877 | user-select: none |
| 878 | width: 100% |
| 879 | margin: 0 |
| 880 | .board |
| 881 | cursor: pointer |
| 882 | |
| 883 | #choices |
| 884 | user-select: none |
| 885 | margin: 0 |
| 886 | position: absolute |
| 887 | z-index: 300 |
| 888 | overflow-y: inherit |
| 889 | background-color: rgba(0,0,0,0) |
| 890 | img |
| 891 | cursor: pointer |
| 892 | background-color: #e6ee9c |
| 893 | &:hover |
| 894 | background-color: skyblue |
| 895 | &.choice-piece |
| 896 | width: 100% |
| 897 | height: auto |
| 898 | display: block |
| 899 | |
| 900 | img.ghost |
| 901 | // NOTE: no need to set z-index here, since opacity is low |
| 902 | position: absolute |
| 903 | opacity: 0.5 |
| 904 | top: 0 |
| 905 | |
| 906 | .incheck-light |
| 907 | background-color: rgba(204, 51, 0, 0.7) !important |
| 908 | .incheck-dark |
| 909 | background-color: rgba(204, 51, 0, 0.9) !important |
| 910 | |
| 911 | .light-square.lichess |
| 912 | background-color: #f0d9b5 |
| 913 | .dark-square.lichess |
| 914 | background-color: #b58863 |
| 915 | |
| 916 | .light-square.chesscom |
| 917 | background-color: #e5e5ca |
| 918 | .dark-square.chesscom |
| 919 | background-color: #6f8f57 |
| 920 | |
| 921 | .light-square.chesstempo |
| 922 | background-color: #dfdfdf |
| 923 | .dark-square.chesstempo |
| 924 | background-color: #7287b6 |
| 925 | |
| 926 | .middle-square.lichess |
| 927 | background-color: #D3B18C |
| 928 | |
| 929 | .middle-square.chesscom |
| 930 | background-color: #AABA91 |
| 931 | |
| 932 | .middle-square.chesstempo |
| 933 | background-color: #A9B3CB |
| 934 | |
| 935 | // TODO: no predefined highlight colors, but layers. How? |
| 936 | |
| 937 | .light-square.lichess.highlight-light |
| 938 | background-color: #cdd26a |
| 939 | .dark-square.lichess.highlight-dark |
| 940 | background-color: #aaa23a |
| 941 | |
| 942 | .light-square.chesscom.highlight-light |
| 943 | background-color: #f7f783 |
| 944 | .dark-square.chesscom.highlight-dark |
| 945 | background-color: #bacb44 |
| 946 | |
| 947 | .light-square.chesstempo.highlight-light |
| 948 | background-color: #9f9fff |
| 949 | .dark-square.chesstempo.highlight-dark |
| 950 | background-color: #557fff |
| 951 | </style> |