-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender.ts
374 lines (348 loc) · 13.6 KB
/
render.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import { State } from './state';
import {
key2pos,
createEl,
posToTranslateAbs,
translateRel,
translateAbs,
calculatePlayerEmptyAreas,
} from './util';
import { p1Pov } from './board';
import { AnimCurrent, AnimVectors, AnimVector, AnimFadings } from './anim';
import { DragCurrent } from './drag';
import * as cg from './types';
import * as T from './transformations';
export type PieceName = string; // `$playerIndex $role`
export type SquareClasses = Map<cg.Key, string>;
// ported from https://github.com/veloce/lichobile/blob/master/src/js/chessground/view.js
// in case of bugs, blame @veloce
export function render(s: State): void {
const orientation = s.orientation,
asP1: boolean = p1Pov(s),
posToTranslate = s.dom.relative ? s.posToTranslateRelative : s.posToTranslateAbsolute(s.dom.bounds(), s.dimensions, s.variant),
translate = s.dom.relative ? translateRel : translateAbs,
boardEl: HTMLElement = s.dom.elements.board,
pieces: cg.Pieces = s.pieces,
pocketPieces: cg.Piece[] = s.pocketPieces,
curAnim: AnimCurrent | undefined = s.animation.current,
anims: AnimVectors = curAnim ? curAnim.plan.anims : new Map(),
fadings: AnimFadings = curAnim ? curAnim.plan.fadings : new Map(),
curDrag: DragCurrent | undefined = s.draggable.current,
squares: SquareClasses = computeSquareClasses(s),
samePieces: Set<cg.Key> = new Set(),
sameSquares: Set<cg.Key> = new Set(),
movedPieces: Map<PieceName, cg.PieceNode[]> = new Map(),
movedSquares: Map<string, cg.SquareNode[]> = new Map(); // by class name
let k: cg.Key,
el: cg.PieceNode | cg.SquareNode | undefined,
pieceAtKey: cg.Piece | undefined,
elPieceName: PieceName,
anim: AnimVector | undefined,
fading: cg.Piece | undefined,
pMvdset: cg.PieceNode[] | undefined,
pMvd: cg.PieceNode | undefined,
sMvdset: cg.SquareNode[] | undefined,
sMvd: cg.SquareNode | undefined;
// walk over all board dom elements, apply animations and flag moved pieces
el = boardEl.firstChild as cg.PieceNode | cg.SquareNode | undefined;
while (el) {
k = el.cgKey;
if (isPieceNode(el)) {
pieceAtKey = pieces.get(k);
anim = anims.get(k);
fading = fadings.get(k);
elPieceName = el.cgPiece;
// if piece not being dragged anymore, remove dragging style
if (el.cgDragging && (!curDrag || curDrag.orig !== k)) {
el.classList.remove('dragging');
translate(el, posToTranslate(key2pos(k), orientation, s.dimensions, s.variant));
el.cgDragging = false;
}
// remove fading class if it still remains
if (!fading && el.cgFading) {
el.cgFading = false;
el.classList.remove('fading');
}
// there is now a piece at this dom key
if (pieceAtKey) {
// continue animation if already animating and same piece
// (otherwise it could animate a captured piece)
if (
anim &&
el.cgAnimating &&
elPieceName === pieceNameOf(pieceAtKey, s.myPlayerIndex, s.orientation, s.variant, k)
) {
const pos = key2pos(k);
pos[0] += anim[2];
pos[1] += anim[3];
el.classList.add('anim');
translate(el, posToTranslate(pos, orientation, s.dimensions, s.variant));
} else if (el.cgAnimating) {
el.cgAnimating = false;
el.classList.remove('anim');
translate(el, posToTranslate(key2pos(k), orientation, s.dimensions, s.variant));
if (s.addPieceZIndex) el.style.zIndex = posZIndex(key2pos(k), orientation, asP1, s.dimensions);
}
// same piece: flag as same
if (
elPieceName === pieceNameOf(pieceAtKey, s.myPlayerIndex, s.orientation, s.variant, k) &&
(!fading || !el.cgFading)
) {
samePieces.add(k);
}
// different piece: flag as moved unless it is a fading piece
else {
if (fading && elPieceName === pieceNameOf(fading, s.myPlayerIndex, s.orientation, s.variant, k)) {
el.classList.add('fading');
el.cgFading = true;
} else {
appendValue(movedPieces, elPieceName, el);
}
}
}
// no piece: flag as moved
else {
appendValue(movedPieces, elPieceName, el);
}
} else if (isSquareNode(el)) {
const cn = el.className;
if (squares.get(k) === cn) sameSquares.add(k);
else if (movedSquares.has(cn)) appendValue(movedSquares, cn, el);
else movedSquares.set(cn, [el]);
}
el = el.nextSibling as cg.PieceNode | cg.SquareNode | undefined;
}
// walk over all squares in current set, apply dom changes to moved squares
// or append new squares
for (const [sk, className] of squares) {
if (!sameSquares.has(sk)) {
sMvdset = movedSquares.get(className);
sMvd = sMvdset && sMvdset.pop();
const translation = posToTranslate(key2pos(sk), orientation, s.dimensions, s.variant);
if (sMvd) {
sMvd.cgKey = sk;
translate(sMvd, translation);
} else {
const squareNode = createEl('square', className) as cg.SquareNode;
squareNode.cgKey = sk;
translate(squareNode, translation);
boardEl.insertBefore(squareNode, boardEl.firstChild);
}
}
}
// walk over all pieces in current set, apply dom changes to moved pieces
// or append new pieces
for (const [k, p] of pieces) {
anim = anims.get(k);
if (!samePieces.has(k)) {
pMvdset = movedPieces.get(pieceNameOf(p, s.myPlayerIndex, s.orientation, s.variant, k));
pMvd = pMvdset && pMvdset.pop();
// a same piece was moved
if (pMvd) {
// apply dom changes
pMvd.cgKey = k;
if (pMvd.cgFading) {
pMvd.classList.remove('fading');
pMvd.cgFading = false;
}
const pos = key2pos(k);
if (s.addPieceZIndex) pMvd.style.zIndex = posZIndex(pos, orientation, asP1, s.dimensions);
if (anim) {
pMvd.cgAnimating = true;
pMvd.classList.add('anim');
pos[0] += anim[2];
pos[1] += anim[3];
}
translate(pMvd, posToTranslate(pos, orientation, s.dimensions, s.variant));
}
// no piece in moved obj: insert the new piece
// assumes the new piece is not being dragged
else {
const pieceName = pieceNameOf(p, s.myPlayerIndex, s.orientation, s.variant, k),
pieceNode = createEl('piece', pieceName) as cg.PieceNode,
pos = key2pos(k);
pieceNode.cgPiece = pieceName;
pieceNode.cgKey = k;
if (anim) {
pieceNode.cgAnimating = true;
pos[0] += anim[2];
pos[1] += anim[3];
}
translate(pieceNode, posToTranslate(pos, orientation, s.dimensions, s.variant));
if (s.addPieceZIndex) pieceNode.style.zIndex = posZIndex(pos, orientation, asP1, s.dimensions);
boardEl.appendChild(pieceNode);
}
}
}
// walk over all pocketPieces and set nodes
for (const p of pocketPieces) {
const classSpecificKey = p.playerIndex === 'p1' ? 'a1' : 'a2';
const pieceName = pieceNameOf(p, s.myPlayerIndex, s.orientation, s.variant, classSpecificKey as cg.Key),
pieceNode = createEl('piece', 'pocket ' + pieceName) as cg.PieceNode;
pieceNode.cgPiece = pieceName;
pieceNode.cgKey = s.orientation === 'p1' ? 'a2' : 'a1'; // always have 0 transform (top left corner)
boardEl.appendChild(pieceNode);
}
// remove any element that remains in the moved sets
for (const nodes of movedPieces.values()) removeNodes(s, nodes);
for (const nodes of movedSquares.values()) removeNodes(s, nodes);
}
export function updateBounds(s: State): void {
if (s.dom.relative) return;
const orientation = s.orientation,
posToTranslate = posToTranslateAbs(s.dom.bounds(), s.dimensions, s.variant);
let el = s.dom.elements.board.firstChild as cg.PieceNode | cg.SquareNode | undefined;
while (el) {
if ((isPieceNode(el) && !el.cgAnimating) || isSquareNode(el)) {
translateAbs(el, posToTranslate(key2pos(el.cgKey), orientation));
}
el = el.nextSibling as cg.PieceNode | cg.SquareNode | undefined;
}
}
export function isPieceNode(el: cg.PieceNode | cg.SquareNode): el is cg.PieceNode {
return el.tagName === 'PIECE';
}
export function isSquareNode(el: cg.PieceNode | cg.SquareNode): el is cg.SquareNode {
return el.tagName === 'SQUARE';
}
export function removeNodes(s: State, nodes: HTMLElement[]): void {
for (const node of nodes) s.dom.elements.board.removeChild(node);
}
export function posZIndex(pos: cg.Pos, orientation: cg.Orientation, asP1: boolean, bd: cg.BoardDimensions): string {
pos = T.mapToP1[orientation](pos, bd);
let z = 2 + (pos[1] - 1) * bd.height + (bd.width - pos[0]);
if (asP1) z = 67 - z;
return z + '';
}
export function pieceNameOf(
piece: cg.Piece,
myPlayerIndex: cg.PlayerIndex,
orientation: cg.Orientation,
variant: cg.Variant,
k: cg.Key,
): string {
const promoted = piece.promoted ? 'promoted ' : '';
const side =
(piece.playerIndex === myPlayerIndex && orientation === myPlayerIndex) ||
(piece.playerIndex !== myPlayerIndex && orientation !== myPlayerIndex)
? 'ally'
: 'enemy';
const posClass =
variant === 'backgammon' || variant === 'hyper' || variant === 'nackgammon'
? backgammonPosClass(k, orientation)
: '';
return `${piece.playerIndex} ${promoted}${piece.role} ${side}${posClass}`;
}
function backgammonPosClass(k: cg.Key, orientation: cg.Orientation): string {
const pos = key2pos(k);
const leftOrRight = pos[0] <= 6 ? 'left' : 'right';
const topOrBottom = orientation === 'p1' ? (pos[1] <= 1 ? 'bottom' : 'top') : pos[1] <= 1 ? 'top' : 'bottom';
return ` ${topOrBottom} ${leftOrRight}`;
}
export function computeSquareClasses(s: State): SquareClasses {
const squares: SquareClasses = new Map();
if (s.lastMove && s.highlight.lastMove) {
let first = true;
for (const k of s.lastMove) {
if (k !== 'a0') {
if (first) {
addSquare(squares, k, 'last-move from' + variantSpecificHighlightClass(s.variant, k, s.orientation));
first = false;
} else {
addSquare(squares, k, 'last-move to' + variantSpecificHighlightClass(s.variant, k, s.orientation));
}
} else {
first = false;
}
}
}
if (s.check && s.highlight.check) addSquare(squares, s.check, 'check');
if (s.selectOnly) {
for (const key of s.selectedPieces.keys()) {
addSquare(squares, key, 'selected');
}
//area highlight for go games
if (s.variant && ['go9x9', 'go13x13', 'go19x19'].includes(s.variant)) {
const areas = calculatePlayerEmptyAreas(s.pieces, s.dimensions, s.selectedPieces);
for (const [s, p] of areas) {
addSquare(squares, s, 'area ' + p);
}
}
}
if (s.selected) {
addSquare(squares, s.selected, 'selected' + variantSpecificHighlightClass(s.variant, s.selected, s.orientation));
if (s.movable.showDests) {
const dests = s.movable.dests?.get(s.selected);
if (dests)
for (const k of dests) {
addSquare(
squares,
k,
'move-dest' + (s.pieces.has(k) ? ' oc' : '') + variantSpecificHighlightClass(s.variant, k, s.orientation),
);
}
const pDests = s.premovable.dests;
if (pDests)
for (const k of pDests) {
addSquare(
squares,
k,
'premove-dest' +
(s.pieces.has(k) ? ' oc' : '') +
variantSpecificHighlightClass(s.variant, k, s.orientation),
);
}
}
} else if (s.dropmode.active || s.draggable.current?.orig === 'a0') {
const piece = s.dropmode.active ? s.dropmode.piece : s.draggable.current?.piece;
if (piece) {
// TODO: there was a function called isPredroppable that was used in drag.ts or drop.ts or both.
// Maybe use the same here to decide what to render instead of potentially making it possible both
// kinds of highlighting to happen if something was not cleared up in the state.
// In other place (pocket.ts) this condition is used ot decide similar question: ctrl.myplayerIndex === ctrl.turnPlayerIndex
if (s.dropmode.showDropDests) {
const dests = s.dropmode.dropDests?.get(piece.role);
if (dests)
for (const k of dests) {
addSquare(squares, k, 'move-dest');
}
}
if (s.predroppable.showDropDests) {
const pDests = s.predroppable.dropDests;
if (pDests)
for (const k of pDests) {
addSquare(squares, k, 'premove-dest' + (s.pieces.has(k) ? ' oc' : ''));
}
}
}
}
const premove = s.premovable.current;
if (premove) for (const k of premove) addSquare(squares, k, 'current-premove');
else if (s.predroppable.current) addSquare(squares, s.predroppable.current.key, 'current-premove');
const o = s.exploding;
if (o) for (const k of o.keys) addSquare(squares, k, 'exploding' + o.stage);
return squares;
}
function addSquare(squares: SquareClasses, key: cg.Key, klass: string): void {
const classes = squares.get(key);
if (classes) squares.set(key, `${classes} ${klass}`);
else squares.set(key, klass);
}
export function appendValue<K, V>(map: Map<K, V[]>, key: K, value: V): void {
const arr = map.get(key);
if (arr) arr.push(value);
else map.set(key, [value]);
}
function variantSpecificHighlightClass(variant: cg.Variant, k: cg.Key, orientation: cg.Orientation): string {
switch (variant) {
case 'togyzkumalak':
case 'bestemshe':
return k[1] === '1' ? ' p1' : ' p2';
case 'nackgammon':
case 'hyper':
case 'backgammon':
return backgammonPosClass(k, orientation);
default:
return '';
}
}