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