Commit | Line | Data |
---|---|---|
24340cae | 1 | <script> |
e2732923 BA |
2 | import { getSquareId, getSquareFromId } from "@/utils/squareId"; |
3 | import { ArrayFun } from "@/utils/array"; | |
dfeb96ea | 4 | import { store } from "@/store"; |
cf2343ce BA |
5 | export default { |
6 | name: 'my-board', | |
7 | // Last move cannot be guessed from here, and is required to highlight squares | |
8 | // vr: object to check moves, print board... | |
93d1d7a7 | 9 | // userColor is left undefined for an external observer |
f51c37cf | 10 | props: ["vr","lastMove","analyze","incheck","orientation","userColor","vname"], |
cf2343ce BA |
11 | data: function () { |
12 | return { | |
cf2343ce BA |
13 | possibleMoves: [], //filled after each valid click/dragstart |
14 | choices: [], //promotion pieces, or checkered captures... (as moves) | |
15 | selectedPiece: null, //moving piece (or clicked piece) | |
cf2343ce | 16 | start: {}, //pixels coordinates + id of starting square (click or drag) |
dfeb96ea | 17 | settings: store.state.settings, |
cf2343ce BA |
18 | }; |
19 | }, | |
20 | render(h) { | |
21 | if (!this.vr) | |
7b3cf1b7 BA |
22 | { |
23 | // Return empty div of class 'game' to avoid error when setting size | |
24 | return h("div", | |
25 | { | |
26 | "class": { | |
27 | "game": true, | |
28 | }, | |
29 | }); | |
30 | } | |
cf2343ce BA |
31 | const [sizeX,sizeY] = [V.size.x,V.size.y]; |
32 | // Precompute hints squares to facilitate rendering | |
e2732923 | 33 | let hintSquares = ArrayFun.init(sizeX, sizeY, false); |
cf2343ce BA |
34 | this.possibleMoves.forEach(m => { hintSquares[m.end.x][m.end.y] = true; }); |
35 | // Also precompute in-check squares | |
e2732923 | 36 | let incheckSq = ArrayFun.init(sizeX, sizeY, false); |
cf2343ce | 37 | this.incheck.forEach(sq => { incheckSq[sq[0]][sq[1]] = true; }); |
06e79b07 | 38 | |
7b3cf1b7 BA |
39 | let boardElt = document.querySelector(".game"); |
40 | const squareWidth = (!!boardElt | |
41 | ? boardElt.offsetWidth / sizeY | |
06e79b07 | 42 | : 40); //arbitrary value (not relevant) |
168a5e4c BA |
43 | const offset = (!!boardElt |
44 | ? [boardElt.offsetTop, boardElt.offsetLeft] | |
45 | : [0, 0]); | |
cf2343ce BA |
46 | // Create board element (+ reserves if needed by variant or mode) |
47 | const lm = this.lastMove; | |
dfeb96ea | 48 | const showLight = this.settings.highlight && this.vname != "Dark"; |
cf2343ce BA |
49 | const gameDiv = h( |
50 | 'div', | |
51 | { | |
52 | 'class': { | |
53 | 'game': true, | |
54 | 'clearer': true, | |
55 | }, | |
56 | }, | |
57 | [...Array(sizeX).keys()].map(i => { | |
58 | let ci = (this.orientation=='w' ? i : sizeX-i-1); | |
59 | return h( | |
60 | 'div', | |
61 | { | |
62 | 'class': { | |
63 | 'row': true, | |
64 | }, | |
65 | style: { 'opacity': this.choices.length>0?"0.5":"1" }, | |
66 | }, | |
67 | [...Array(sizeY).keys()].map(j => { | |
68 | let cj = (this.orientation=='w' ? j : sizeY-j-1); | |
69 | let elems = []; | |
e2732923 | 70 | if (this.vr.board[ci][cj] != V.EMPTY && (this.vname!="Dark" |
93d1d7a7 BA |
71 | || this.analyze || (!!this.userColor |
72 | && this.vr.enlightened[this.userColor][ci][cj]))) | |
cf2343ce BA |
73 | { |
74 | elems.push( | |
75 | h( | |
76 | 'img', | |
77 | { | |
78 | 'class': { | |
79 | 'piece': true, | |
80 | 'ghost': !!this.selectedPiece | |
81 | && this.selectedPiece.parentNode.id == "sq-"+ci+"-"+cj, | |
82 | }, | |
83 | attrs: { | |
50aed5a1 BA |
84 | src: "/images/pieces/" + |
85 | V.getPpath(this.vr.board[ci][cj]) + ".svg", | |
cf2343ce BA |
86 | }, |
87 | } | |
88 | ) | |
89 | ); | |
90 | } | |
dfeb96ea | 91 | if (this.settings.hints && hintSquares[ci][cj]) |
cf2343ce BA |
92 | { |
93 | elems.push( | |
94 | h( | |
95 | 'img', | |
96 | { | |
97 | 'class': { | |
98 | 'mark-square': true, | |
99 | }, | |
100 | attrs: { | |
101 | src: "/images/mark.svg", | |
102 | }, | |
103 | } | |
104 | ) | |
105 | ); | |
106 | } | |
107 | return h( | |
108 | 'div', | |
109 | { | |
110 | 'class': { | |
111 | 'board': true, | |
112 | ['board'+sizeY]: true, | |
113 | 'light-square': (i+j)%2==0, | |
114 | 'dark-square': (i+j)%2==1, | |
dfeb96ea | 115 | [this.settings.bcolor]: true, |
93d1d7a7 BA |
116 | 'in-shadow': this.vname=="Dark" && !this.analyze |
117 | && (!this.userColor | |
118 | || !this.vr.enlightened[this.userColor][ci][cj]), | |
cf2343ce BA |
119 | 'highlight': showLight && !!lm && lm.end.x == ci && lm.end.y == cj, |
120 | 'incheck': showLight && incheckSq[ci][cj], | |
121 | }, | |
122 | attrs: { | |
123 | id: getSquareId({x:ci,y:cj}), | |
124 | }, | |
125 | }, | |
126 | elems | |
127 | ); | |
128 | }) | |
129 | ); | |
24340cae | 130 | }) |
cf2343ce | 131 | ); |
d4036efe | 132 | const playingColor = this.userColor || "w"; //default for an observer |
945a6165 BA |
133 | let elementArray = [gameDiv]; |
134 | if (this.choices.length > 0) | |
135 | { | |
136 | const choices = h( | |
137 | 'div', | |
138 | { | |
139 | attrs: { "id": "choices" }, | |
140 | 'class': { 'row': true }, | |
141 | style: { | |
142 | "top": (offset[0] + (sizeY/2)*squareWidth-squareWidth/2) + "px", | |
143 | "left": (offset[1] + squareWidth*(sizeY - this.choices.length)/2) + "px", | |
144 | "width": (this.choices.length * squareWidth) + "px", | |
145 | "height": squareWidth + "px", | |
146 | }, | |
147 | }, | |
148 | this.choices.map(m => { //a "choice" is a move | |
149 | return h('div', | |
150 | { | |
151 | 'class': { | |
152 | 'board': true, | |
153 | ['board'+sizeY]: true, | |
154 | }, | |
155 | style: { | |
156 | 'width': (100/this.choices.length) + "%", | |
157 | 'padding-bottom': (100/this.choices.length) + "%", | |
158 | }, | |
159 | }, | |
160 | [h('img', | |
161 | { | |
162 | attrs: { "src": '/images/pieces/' + | |
163 | V.getPpath(m.appear[0].c+m.appear[0].p) + '.svg' }, | |
164 | 'class': { 'choice-piece': true }, | |
165 | on: { | |
166 | "click": e => { this.play(m); this.choices=[]; }, | |
167 | }, | |
168 | }) | |
169 | ] | |
170 | ); | |
171 | }) | |
172 | ); | |
173 | elementArray.unshift(choices); | |
174 | } | |
cf2343ce BA |
175 | if (!!this.vr.reserve) |
176 | { | |
93d1d7a7 | 177 | const shiftIdx = (playingColor=="w" ? 0 : 1); |
cf2343ce BA |
178 | let myReservePiecesArray = []; |
179 | for (let i=0; i<V.RESERVE_PIECES.length; i++) | |
180 | { | |
181 | myReservePiecesArray.push(h('div', | |
182 | { | |
183 | 'class': {'board':true, ['board'+sizeY]:true}, | |
184 | attrs: { id: getSquareId({x:sizeX+shiftIdx,y:i}) } | |
185 | }, | |
186 | [ | |
187 | h('img', | |
188 | { | |
189 | 'class': {"piece":true, "reserve":true}, | |
190 | attrs: { | |
191 | "src": "/images/pieces/" + | |
93d1d7a7 | 192 | this.vr.getReservePpath(playingColor,i) + ".svg", |
cf2343ce BA |
193 | } |
194 | }), | |
195 | h('sup', | |
196 | {"class": { "reserve-count": true } }, | |
93d1d7a7 | 197 | [ this.vr.reserve[playingColor][V.RESERVE_PIECES[i]] ] |
cf2343ce BA |
198 | ) |
199 | ])); | |
200 | } | |
201 | let oppReservePiecesArray = []; | |
93d1d7a7 | 202 | const oppCol = V.GetOppCol(playingColor); |
cf2343ce BA |
203 | for (let i=0; i<V.RESERVE_PIECES.length; i++) |
204 | { | |
205 | oppReservePiecesArray.push(h('div', | |
206 | { | |
207 | 'class': {'board':true, ['board'+sizeY]:true}, | |
208 | attrs: { id: getSquareId({x:sizeX+(1-shiftIdx),y:i}) } | |
209 | }, | |
210 | [ | |
211 | h('img', | |
212 | { | |
213 | 'class': {"piece":true, "reserve":true}, | |
214 | attrs: { | |
215 | "src": "/images/pieces/" + | |
216 | this.vr.getReservePpath(oppCol,i) + ".svg", | |
217 | } | |
218 | }), | |
219 | h('sup', | |
220 | {"class": { "reserve-count": true } }, | |
221 | [ this.vr.reserve[oppCol][V.RESERVE_PIECES[i]] ] | |
222 | ) | |
223 | ])); | |
224 | } | |
225 | let reserves = h('div', | |
226 | { | |
227 | 'class':{ | |
228 | 'game': true, | |
229 | "reserve-div": true, | |
230 | }, | |
231 | }, | |
232 | [ | |
233 | h('div', | |
234 | { | |
235 | 'class': { | |
236 | 'row': true, | |
237 | "reserve-row-1": true, | |
238 | }, | |
239 | }, | |
240 | myReservePiecesArray | |
241 | ), | |
242 | h('div', | |
243 | { 'class': { 'row': true }}, | |
244 | oppReservePiecesArray | |
245 | ) | |
246 | ] | |
247 | ); | |
248 | elementArray.push(reserves); | |
249 | } | |
4b26ecb8 BA |
250 | let onEvents = {}; |
251 | // NOTE: click = mousedown + mouseup | |
252 | if ('ontouchstart' in window) | |
253 | { | |
254 | onEvents = { | |
255 | on: { | |
256 | touchstart: this.mousedown, | |
257 | touchmove: this.mousemove, | |
258 | touchend: this.mouseup, | |
259 | }, | |
260 | }; | |
261 | } | |
262 | else | |
263 | { | |
264 | onEvents = { | |
cf2343ce | 265 | on: { |
65495c17 BA |
266 | mousedown: this.mousedown, |
267 | mousemove: this.mousemove, | |
268 | mouseup: this.mouseup, | |
cf2343ce | 269 | }, |
4b26ecb8 BA |
270 | }; |
271 | } | |
272 | return h( | |
273 | 'div', | |
274 | onEvents, | |
cf2343ce BA |
275 | elementArray |
276 | ); | |
277 | }, | |
278 | methods: { | |
279 | mousedown: function(e) { | |
280 | e = e || window.event; | |
281 | let ingame = false; | |
282 | let elem = e.target; | |
283 | while (!ingame && elem !== null) | |
284 | { | |
285 | if (elem.classList.contains("game")) | |
286 | { | |
287 | ingame = true; | |
288 | break; | |
289 | } | |
290 | elem = elem.parentElement; | |
291 | } | |
292 | if (!ingame) //let default behavior (click on button...) | |
293 | return; | |
294 | e.preventDefault(); //disable native drag & drop | |
295 | if (!this.selectedPiece && e.target.classList.contains("piece")) | |
296 | { | |
83c6c2c9 | 297 | let parent = e.target.parentNode; |
cf2343ce | 298 | // Next few lines to center the piece on mouse cursor |
83c6c2c9 | 299 | let rect = parent.getBoundingClientRect(); |
cf2343ce BA |
300 | this.start = { |
301 | x: rect.x + rect.width/2, | |
302 | y: rect.y + rect.width/2, | |
83c6c2c9 | 303 | id: parent.id |
cf2343ce BA |
304 | }; |
305 | this.selectedPiece = e.target.cloneNode(); | |
bd76b456 BA |
306 | let spStyle = this.selectedPiece.style |
307 | spStyle.position = "absolute"; | |
308 | spStyle.top = 0; | |
309 | spStyle.display = "inline-block"; | |
310 | spStyle.zIndex = 3000; | |
83c6c2c9 | 311 | const startSquare = getSquareFromId(parent.id); |
cf2343ce | 312 | this.possibleMoves = []; |
93d1d7a7 | 313 | const color = (this.analyze ? this.vr.turn : this.userColor); |
cf2343ce BA |
314 | if (this.vr.canIplay(color,startSquare)) |
315 | this.possibleMoves = this.vr.getPossibleMovesFrom(startSquare); | |
316 | // Next line add moving piece just after current image | |
317 | // (required for Crazyhouse reserve) | |
83c6c2c9 | 318 | parent.insertBefore(this.selectedPiece, e.target.nextSibling); |
cf2343ce BA |
319 | } |
320 | }, | |
321 | mousemove: function(e) { | |
322 | if (!this.selectedPiece) | |
323 | return; | |
324 | e = e || window.event; | |
325 | // If there is an active element, move it around | |
326 | if (!!this.selectedPiece) | |
327 | { | |
328 | const [offsetX,offsetY] = !!e.clientX | |
329 | ? [e.clientX,e.clientY] //desktop browser | |
330 | : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; //smartphone | |
331 | this.selectedPiece.style.left = (offsetX-this.start.x) + "px"; | |
332 | this.selectedPiece.style.top = (offsetY-this.start.y) + "px"; | |
333 | } | |
334 | }, | |
335 | mouseup: function(e) { | |
336 | if (!this.selectedPiece) | |
337 | return; | |
338 | e = e || window.event; | |
cf2343ce BA |
339 | this.selectedPiece.style.zIndex = -3000; //HACK to find square from final coords |
340 | const [offsetX,offsetY] = !!e.clientX | |
341 | ? [e.clientX,e.clientY] | |
342 | : [e.changedTouches[0].pageX, e.changedTouches[0].pageY]; | |
343 | let landing = document.elementFromPoint(offsetX, offsetY); | |
344 | this.selectedPiece.style.zIndex = 3000; | |
345 | // Next condition: classList.contains(piece) fails because of marks | |
346 | while (landing.tagName == "IMG") | |
347 | landing = landing.parentNode; | |
348 | if (this.start.id == landing.id) | |
349 | { | |
350 | // A click: selectedPiece and possibleMoves are already filled | |
351 | return; | |
352 | } | |
bd76b456 | 353 | // OK: process move attempt, landing is a square node |
cf2343ce BA |
354 | let endSquare = getSquareFromId(landing.id); |
355 | let moves = this.findMatchingMoves(endSquare); | |
356 | this.possibleMoves = []; | |
357 | if (moves.length > 1) | |
358 | this.choices = moves; | |
359 | else if (moves.length==1) | |
360 | this.play(moves[0]); | |
361 | // Else: impossible move | |
362 | this.selectedPiece.parentNode.removeChild(this.selectedPiece); | |
363 | delete this.selectedPiece; | |
364 | this.selectedPiece = null; | |
365 | }, | |
366 | findMatchingMoves: function(endSquare) { | |
367 | // Run through moves list and return the matching set (if promotions...) | |
368 | let moves = []; | |
369 | this.possibleMoves.forEach(function(m) { | |
370 | if (endSquare[0] == m.end.x && endSquare[1] == m.end.y) | |
371 | moves.push(m); | |
372 | }); | |
373 | return moves; | |
374 | }, | |
375 | play: function(move) { | |
376 | this.$emit('play-move', move); | |
377 | }, | |
378 | }, | |
379 | }; | |
380 | </script> | |
4473050c | 381 | |
41c80bb6 | 382 | <style lang="sass" scoped> |
50aed5a1 BA |
383 | .game.reserve-div |
384 | margin-bottom: 18px | |
385 | ||
386 | .reserve-count | |
387 | padding-left: 40% | |
388 | ||
389 | .reserve-row-1 | |
390 | margin-bottom: 15px | |
391 | ||
41cb9b94 BA |
392 | // NOTE: no variants with reserve of size != 8 |
393 | ||
50aed5a1 | 394 | .game |
cf94b843 BA |
395 | width: 100% |
396 | margin: 0 | |
50aed5a1 BA |
397 | .board |
398 | cursor: pointer | |
50aed5a1 BA |
399 | |
400 | #choices | |
168a5e4c BA |
401 | margin: 0 |
402 | position: absolute | |
50aed5a1 BA |
403 | z-index: 300 |
404 | overflow-y: inherit | |
405 | background-color: rgba(0,0,0,0) | |
406 | img | |
407 | cursor: pointer | |
408 | background-color: #e6ee9c | |
409 | &:hover | |
410 | background-color: skyblue | |
411 | &.choice-piece | |
412 | width: 100% | |
413 | height: auto | |
414 | display: block | |
415 | ||
50aed5a1 BA |
416 | img.ghost |
417 | position: absolute | |
418 | opacity: 0.4 | |
419 | top: 0 | |
420 | ||
421 | .highlight | |
422 | background-color: #00cc66 !important | |
423 | ||
50aed5a1 BA |
424 | .incheck |
425 | background-color: #cc3300 !important | |
426 | ||
427 | .light-square.lichess | |
428 | background-color: #f0d9b5; | |
429 | .dark-square.lichess | |
430 | background-color: #b58863; | |
431 | ||
432 | .light-square.chesscom | |
433 | background-color: #e5e5ca; | |
434 | .dark-square.chesscom | |
435 | background-color: #6f8f57; | |
436 | ||
437 | .light-square.chesstempo | |
438 | background-color: #fdfdfd; | |
439 | .dark-square.chesstempo | |
440 | background-color: #88a0a8; | |
4473050c | 441 | </style> |