cm-chessboard 7.4.4 → 7.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/examples/validate-moves.html +9 -3
- package/package.json +1 -1
- package/src/Chessboard.js +3 -2
- package/src/view/ChessboardView.js +25 -10
- package/src/view/VisualMoveInput.js +64 -54
package/README.md
CHANGED
|
@@ -91,7 +91,7 @@ this.props = {
|
|
|
91
91
|
borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
|
|
92
92
|
aspectRatio: 1, // height/width of the board
|
|
93
93
|
pieces: {
|
|
94
|
-
type: PIECES_FILE_TYPE.svgSprite, // pieces are in
|
|
94
|
+
type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
|
|
95
95
|
file: "standard.svg", // the filename of the sprite in `assets/pieces/`
|
|
96
96
|
tileSize: 40 // the tile size in the sprite
|
|
97
97
|
},
|
|
@@ -39,6 +39,8 @@
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
function inputHandler(event) {
|
|
42
|
+
console.log("inputHandler", event)
|
|
43
|
+
console.log("fen", event.chessboard.getPosition())
|
|
42
44
|
event.chessboard.removeMarkers(MARKER_TYPE.dot)
|
|
43
45
|
event.chessboard.removeMarkers(MARKER_TYPE.bevel)
|
|
44
46
|
if (event.type === INPUT_EVENT_TYPE.moveInputStarted) {
|
|
@@ -58,7 +60,6 @@
|
|
|
58
60
|
const move = {from: event.squareFrom, to: event.squareTo, promotion: event.promotion}
|
|
59
61
|
const result = chess.move(move)
|
|
60
62
|
if (result) {
|
|
61
|
-
event.chessboard.disableMoveInput()
|
|
62
63
|
this.chessboard.state.moveInputProcess.then(() => { // wait for the move input process has finished
|
|
63
64
|
this.chessboard.setPosition(chess.fen(), true).then(() => { // update position, maybe castled and wait for animation has finished
|
|
64
65
|
makeEngineMove(event.chessboard)
|
|
@@ -76,8 +77,9 @@
|
|
|
76
77
|
event.chessboard.setPosition(chess.fen(), true)
|
|
77
78
|
makeEngineMove(event.chessboard)
|
|
78
79
|
} else {
|
|
79
|
-
|
|
80
|
+
// promotion canceled
|
|
80
81
|
event.chessboard.enableMoveInput(inputHandler, COLOR.white)
|
|
82
|
+
event.chessboard.setPosition(chess.fen(), true)
|
|
81
83
|
}
|
|
82
84
|
})
|
|
83
85
|
return true
|
|
@@ -85,13 +87,17 @@
|
|
|
85
87
|
}
|
|
86
88
|
}
|
|
87
89
|
return result
|
|
90
|
+
} else if (event.type === INPUT_EVENT_TYPE.moveInputFinished) {
|
|
91
|
+
if(event.legalMove) {
|
|
92
|
+
event.chessboard.disableMoveInput()
|
|
93
|
+
}
|
|
88
94
|
}
|
|
89
95
|
}
|
|
90
96
|
|
|
91
97
|
const board = new Chessboard(document.getElementById("board"), {
|
|
92
98
|
position: chess.fen(),
|
|
93
99
|
assetsUrl: "../assets/",
|
|
94
|
-
style: {borderType: BORDER_TYPE.none, pieces: {file: "staunty.svg"}},
|
|
100
|
+
style: {borderType: BORDER_TYPE.none, pieces: {file: "staunty.svg"}, animationDuration: 300},
|
|
95
101
|
orientation: COLOR.white,
|
|
96
102
|
extensions: [
|
|
97
103
|
{class: Markers, props: {autoMarkers: MARKER_TYPE.square}},
|
package/package.json
CHANGED
package/src/Chessboard.js
CHANGED
|
@@ -19,7 +19,8 @@ export const INPUT_EVENT_TYPE = {
|
|
|
19
19
|
moveInputStarted: "moveInputStarted",
|
|
20
20
|
movingOverSquare: "movingOverSquare", // while dragging or hover after click
|
|
21
21
|
validateMoveInput: "validateMoveInput",
|
|
22
|
-
moveInputCanceled: "moveInputCanceled"
|
|
22
|
+
moveInputCanceled: "moveInputCanceled",
|
|
23
|
+
moveInputFinished: "moveInputFinished"
|
|
23
24
|
}
|
|
24
25
|
export const SQUARE_SELECT_TYPE = {
|
|
25
26
|
primary: "primary",
|
|
@@ -63,7 +64,7 @@ export class Chessboard {
|
|
|
63
64
|
borderType: BORDER_TYPE.none, // "thin" thin border, "frame" wide border with coordinates in it, "none" no border
|
|
64
65
|
aspectRatio: 1, // height/width of the board
|
|
65
66
|
pieces: {
|
|
66
|
-
type: PIECES_FILE_TYPE.svgSprite, // pieces are in
|
|
67
|
+
type: PIECES_FILE_TYPE.svgSprite, // pieces are in an SVG sprite, no other type supported for now
|
|
67
68
|
file: "standard.svg", // the filename of the sprite in `assets/pieces/`
|
|
68
69
|
tileSize: 40 // the tile size in the sprite
|
|
69
70
|
},
|
|
@@ -13,11 +13,7 @@ import {Svg} from "../lib/Svg.js"
|
|
|
13
13
|
export class ChessboardView {
|
|
14
14
|
constructor(chessboard) {
|
|
15
15
|
this.chessboard = chessboard
|
|
16
|
-
this.
|
|
17
|
-
this.moveInputStartedCallback.bind(this),
|
|
18
|
-
this.movingOverSquareCallback.bind(this),
|
|
19
|
-
this.validateMoveInputCallback.bind(this),
|
|
20
|
-
this.moveInputCanceledCallback.bind(this))
|
|
16
|
+
this.visualMoveInput = new VisualMoveInput(this)
|
|
21
17
|
if (chessboard.props.assetsCache) {
|
|
22
18
|
this.cacheSpriteToDiv("cm-chessboard-sprite", this.getSpriteUrl())
|
|
23
19
|
}
|
|
@@ -47,11 +43,11 @@ export class ChessboardView {
|
|
|
47
43
|
}
|
|
48
44
|
|
|
49
45
|
pointerDownHandler(e) {
|
|
50
|
-
this.
|
|
46
|
+
this.visualMoveInput.onPointerDown(e)
|
|
51
47
|
}
|
|
52
48
|
|
|
53
49
|
destroy() {
|
|
54
|
-
this.
|
|
50
|
+
this.visualMoveInput.destroy()
|
|
55
51
|
if (this.resizeObserver) {
|
|
56
52
|
this.resizeObserver.unobserve(this.chessboard.context)
|
|
57
53
|
}
|
|
@@ -223,10 +219,12 @@ export class ChessboardView {
|
|
|
223
219
|
|
|
224
220
|
redrawPieces(squares = this.chessboard.state.position.squares) {
|
|
225
221
|
const childNodes = Array.from(this.piecesGroup.childNodes)
|
|
222
|
+
const isDragging = this.visualMoveInput.isDragging()
|
|
226
223
|
for (let i = 0; i < 64; i++) {
|
|
227
224
|
const pieceName = squares[i]
|
|
228
225
|
if (pieceName) {
|
|
229
|
-
|
|
226
|
+
const square = Position.indexToSquare(i)
|
|
227
|
+
this.drawPieceOnSquare(square, pieceName, isDragging && square === this.visualMoveInput.fromSquare)
|
|
230
228
|
}
|
|
231
229
|
}
|
|
232
230
|
for (const childNode of childNodes) {
|
|
@@ -250,10 +248,13 @@ export class ChessboardView {
|
|
|
250
248
|
return pieceGroup
|
|
251
249
|
}
|
|
252
250
|
|
|
253
|
-
drawPieceOnSquare(square, pieceName) {
|
|
251
|
+
drawPieceOnSquare(square, pieceName, hidden = false) {
|
|
254
252
|
const pieceGroup = Svg.addElement(this.piecesGroup, "g", {})
|
|
255
253
|
pieceGroup.setAttribute("data-piece", pieceName)
|
|
256
254
|
pieceGroup.setAttribute("data-square", square)
|
|
255
|
+
if(hidden) {
|
|
256
|
+
pieceGroup.setAttribute("visibility","hidden")
|
|
257
|
+
}
|
|
257
258
|
const point = this.squareToPoint(square)
|
|
258
259
|
const transform = (this.svg.createSVGTransform())
|
|
259
260
|
transform.setTranslate(point.x, point.y)
|
|
@@ -302,7 +303,7 @@ export class ChessboardView {
|
|
|
302
303
|
// enable and disable move input //
|
|
303
304
|
|
|
304
305
|
enableMoveInput(eventHandler, color = undefined) {
|
|
305
|
-
if(this.moveInputCallback) {
|
|
306
|
+
if (this.moveInputCallback) {
|
|
306
307
|
throw Error("moveInput already enabled")
|
|
307
308
|
}
|
|
308
309
|
if (color === COLOR.white) {
|
|
@@ -390,6 +391,20 @@ export class ChessboardView {
|
|
|
390
391
|
}
|
|
391
392
|
}
|
|
392
393
|
|
|
394
|
+
moveInputFinishedCallback(squareFrom, squareTo, legalMove) {
|
|
395
|
+
const data = {
|
|
396
|
+
chessboard: this.chessboard,
|
|
397
|
+
type: INPUT_EVENT_TYPE.moveInputFinished,
|
|
398
|
+
squareFrom: squareFrom,
|
|
399
|
+
squareTo: squareTo,
|
|
400
|
+
legalMove: legalMove
|
|
401
|
+
}
|
|
402
|
+
this.chessboard.state.invokeExtensionPoints(EXTENSION_POINT.moveInput, data)
|
|
403
|
+
if (this.moveInputCallback) {
|
|
404
|
+
this.moveInputCallback(data)
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
393
408
|
// Helpers //
|
|
394
409
|
|
|
395
410
|
visualizeInputState() {
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
import {Svg} from "../lib/Svg.js"
|
|
8
8
|
import {Utils} from "../lib/Utils.js"
|
|
9
9
|
|
|
10
|
-
const
|
|
10
|
+
const MOVE_INPUT_STATE = {
|
|
11
11
|
waitForInputStart: 0,
|
|
12
12
|
pieceClickedThreshold: 1,
|
|
13
13
|
clickTo: 2,
|
|
@@ -30,29 +30,35 @@ const DRAG_THRESHOLD = 4
|
|
|
30
30
|
|
|
31
31
|
export class VisualMoveInput {
|
|
32
32
|
|
|
33
|
-
constructor(view
|
|
33
|
+
constructor(view) {
|
|
34
34
|
this.view = view
|
|
35
35
|
this.chessboard = view.chessboard
|
|
36
|
+
this.moveInputState = null
|
|
37
|
+
this.fromSquare = null
|
|
38
|
+
this.toSquare = null
|
|
36
39
|
this.moveInputStartedCallback = (square) => {
|
|
37
|
-
const result = moveInputStartedCallback(square)
|
|
40
|
+
const result = view.moveInputStartedCallback(square)
|
|
38
41
|
if (result) {
|
|
39
42
|
this.chessboard.state.moveInputProcess = Utils.createTask()
|
|
43
|
+
this.chessboard.state.moveInputProcess.then((result) => {
|
|
44
|
+
view.moveInputFinishedCallback(this.fromSquare, this.toSquare, result)
|
|
45
|
+
})
|
|
40
46
|
}
|
|
41
47
|
return result
|
|
42
48
|
}
|
|
43
49
|
this.movingOverSquareCallback = (fromSquare, toSquare) => {
|
|
44
|
-
movingOverSquareCallback(fromSquare, toSquare)
|
|
50
|
+
view.movingOverSquareCallback(fromSquare, toSquare)
|
|
45
51
|
}
|
|
46
52
|
this.validateMoveInputCallback = (fromSquare, toSquare) => {
|
|
47
|
-
const result = validateMoveInputCallback(fromSquare, toSquare)
|
|
53
|
+
const result = view.validateMoveInputCallback(fromSquare, toSquare)
|
|
48
54
|
this.chessboard.state.moveInputProcess.resolve(result)
|
|
49
55
|
return result
|
|
50
56
|
}
|
|
51
57
|
this.moveInputCanceledCallback = (fromSquare, toSquare, reason) => {
|
|
52
|
-
moveInputCanceledCallback(fromSquare, toSquare, reason)
|
|
58
|
+
view.moveInputCanceledCallback(fromSquare, toSquare, reason)
|
|
53
59
|
this.chessboard.state.moveInputProcess.resolve()
|
|
54
60
|
}
|
|
55
|
-
this.setMoveInputState(
|
|
61
|
+
this.setMoveInputState(MOVE_INPUT_STATE.waitForInputStart)
|
|
56
62
|
}
|
|
57
63
|
|
|
58
64
|
setMoveInputState(newState, params = undefined) {
|
|
@@ -64,11 +70,11 @@ export class VisualMoveInput {
|
|
|
64
70
|
|
|
65
71
|
switch (newState) {
|
|
66
72
|
|
|
67
|
-
case
|
|
73
|
+
case MOVE_INPUT_STATE.waitForInputStart:
|
|
68
74
|
break
|
|
69
75
|
|
|
70
|
-
case
|
|
71
|
-
if (
|
|
76
|
+
case MOVE_INPUT_STATE.pieceClickedThreshold:
|
|
77
|
+
if (MOVE_INPUT_STATE.waitForInputStart !== prevState && MOVE_INPUT_STATE.clickTo !== prevState) {
|
|
72
78
|
throw new Error("moveInputState")
|
|
73
79
|
}
|
|
74
80
|
if (this.pointerMoveListener) {
|
|
@@ -110,25 +116,25 @@ export class VisualMoveInput {
|
|
|
110
116
|
}
|
|
111
117
|
break
|
|
112
118
|
|
|
113
|
-
case
|
|
119
|
+
case MOVE_INPUT_STATE.clickTo:
|
|
114
120
|
if (this.draggablePiece) {
|
|
115
121
|
Svg.removeElement(this.draggablePiece)
|
|
116
122
|
this.draggablePiece = undefined
|
|
117
123
|
}
|
|
118
|
-
if (prevState ===
|
|
124
|
+
if (prevState === MOVE_INPUT_STATE.dragTo) {
|
|
119
125
|
this.view.setPieceVisibility(params.square, true)
|
|
120
126
|
}
|
|
121
127
|
break
|
|
122
128
|
|
|
123
|
-
case
|
|
124
|
-
if (
|
|
129
|
+
case MOVE_INPUT_STATE.secondClickThreshold:
|
|
130
|
+
if (MOVE_INPUT_STATE.clickTo !== prevState) {
|
|
125
131
|
throw new Error("moveInputState")
|
|
126
132
|
}
|
|
127
133
|
this.startPoint = params.point
|
|
128
134
|
break
|
|
129
135
|
|
|
130
|
-
case
|
|
131
|
-
if (
|
|
136
|
+
case MOVE_INPUT_STATE.dragTo:
|
|
137
|
+
if (MOVE_INPUT_STATE.pieceClickedThreshold !== prevState) {
|
|
132
138
|
throw new Error("moveInputState")
|
|
133
139
|
}
|
|
134
140
|
if (this.view.chessboard.state.inputEnabled) {
|
|
@@ -137,8 +143,8 @@ export class VisualMoveInput {
|
|
|
137
143
|
}
|
|
138
144
|
break
|
|
139
145
|
|
|
140
|
-
case
|
|
141
|
-
if (
|
|
146
|
+
case MOVE_INPUT_STATE.clickDragTo:
|
|
147
|
+
if (MOVE_INPUT_STATE.secondClickThreshold !== prevState) {
|
|
142
148
|
throw new Error("moveInputState")
|
|
143
149
|
}
|
|
144
150
|
if (this.view.chessboard.state.inputEnabled) {
|
|
@@ -147,29 +153,29 @@ export class VisualMoveInput {
|
|
|
147
153
|
}
|
|
148
154
|
break
|
|
149
155
|
|
|
150
|
-
case
|
|
151
|
-
if ([
|
|
156
|
+
case MOVE_INPUT_STATE.moveDone:
|
|
157
|
+
if ([MOVE_INPUT_STATE.dragTo, MOVE_INPUT_STATE.clickTo, MOVE_INPUT_STATE.clickDragTo].indexOf(prevState) === -1) {
|
|
152
158
|
throw new Error("moveInputState")
|
|
153
159
|
}
|
|
154
160
|
this.toSquare = params.square
|
|
155
161
|
if (this.toSquare && this.validateMoveInputCallback(this.fromSquare, this.toSquare)) {
|
|
156
|
-
if (prevState ===
|
|
162
|
+
if (prevState === MOVE_INPUT_STATE.clickTo) {
|
|
157
163
|
this.chessboard.movePiece(this.fromSquare, this.toSquare, true).then(() => {
|
|
158
|
-
this.setMoveInputState(
|
|
164
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
159
165
|
})
|
|
160
166
|
} else {
|
|
161
167
|
this.chessboard.movePiece(this.fromSquare, this.toSquare, false).then(() => {
|
|
162
168
|
this.view.setPieceVisibility(this.toSquare, true)
|
|
163
|
-
this.setMoveInputState(
|
|
169
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
164
170
|
})
|
|
165
171
|
}
|
|
166
172
|
} else {
|
|
167
173
|
this.view.setPieceVisibility(this.fromSquare, true)
|
|
168
|
-
this.setMoveInputState(
|
|
174
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
169
175
|
}
|
|
170
176
|
break
|
|
171
177
|
|
|
172
|
-
case
|
|
178
|
+
case MOVE_INPUT_STATE.reset:
|
|
173
179
|
if (this.fromSquare && !this.toSquare && this.movedPiece) {
|
|
174
180
|
this.chessboard.state.position.setPiece(this.fromSquare, this.movedPiece)
|
|
175
181
|
}
|
|
@@ -192,7 +198,7 @@ export class VisualMoveInput {
|
|
|
192
198
|
removeEventListener("contextmenu", this.contextMenuListener)
|
|
193
199
|
this.contextMenuListener = undefined
|
|
194
200
|
}
|
|
195
|
-
this.setMoveInputState(
|
|
201
|
+
this.setMoveInputState(MOVE_INPUT_STATE.waitForInputStart)
|
|
196
202
|
break
|
|
197
203
|
|
|
198
204
|
default:
|
|
@@ -203,7 +209,7 @@ export class VisualMoveInput {
|
|
|
203
209
|
createDraggablePiece(pieceName) {
|
|
204
210
|
// maybe I should use the existing piece from the board and don't create a new one
|
|
205
211
|
if (this.draggablePiece) {
|
|
206
|
-
throw Error("
|
|
212
|
+
throw Error("draggablePiece already exists")
|
|
207
213
|
}
|
|
208
214
|
this.draggablePiece = Svg.createSvg(document.body)
|
|
209
215
|
this.draggablePiece.classList.add("cm-chessboard-draggable-piece")
|
|
@@ -241,7 +247,7 @@ export class VisualMoveInput {
|
|
|
241
247
|
e.preventDefault()
|
|
242
248
|
}
|
|
243
249
|
}
|
|
244
|
-
if (this.moveInputState !==
|
|
250
|
+
if (this.moveInputState !== MOVE_INPUT_STATE.waitForInputStart ||
|
|
245
251
|
this.chessboard.state.inputWhiteEnabled && color === "w" ||
|
|
246
252
|
this.chessboard.state.inputBlackEnabled && color === "b") {
|
|
247
253
|
let point
|
|
@@ -250,16 +256,16 @@ export class VisualMoveInput {
|
|
|
250
256
|
} else if (e.type === "touchstart") {
|
|
251
257
|
point = {x: e.touches[0].clientX, y: e.touches[0].clientY}
|
|
252
258
|
}
|
|
253
|
-
if (this.moveInputState ===
|
|
254
|
-
this.setMoveInputState(
|
|
259
|
+
if (this.moveInputState === MOVE_INPUT_STATE.waitForInputStart && pieceName && this.moveInputStartedCallback(square)) {
|
|
260
|
+
this.setMoveInputState(MOVE_INPUT_STATE.pieceClickedThreshold, {
|
|
255
261
|
square: square,
|
|
256
262
|
piece: pieceName,
|
|
257
263
|
point: point,
|
|
258
264
|
type: e.type
|
|
259
265
|
})
|
|
260
|
-
} else if (this.moveInputState ===
|
|
266
|
+
} else if (this.moveInputState === MOVE_INPUT_STATE.clickTo) {
|
|
261
267
|
if (square === this.fromSquare) {
|
|
262
|
-
this.setMoveInputState(
|
|
268
|
+
this.setMoveInputState(MOVE_INPUT_STATE.secondClickThreshold, {
|
|
263
269
|
square: square,
|
|
264
270
|
piece: pieceName,
|
|
265
271
|
point: point,
|
|
@@ -273,17 +279,17 @@ export class VisualMoveInput {
|
|
|
273
279
|
if (color && startPieceColor === pieceColor) {
|
|
274
280
|
this.moveInputCanceledCallback(this.fromSquare, square, MOVE_CANCELED_REASON.clickedAnotherPiece)
|
|
275
281
|
if (this.moveInputStartedCallback(square)) {
|
|
276
|
-
this.setMoveInputState(
|
|
282
|
+
this.setMoveInputState(MOVE_INPUT_STATE.pieceClickedThreshold, {
|
|
277
283
|
square: square,
|
|
278
284
|
piece: pieceName,
|
|
279
285
|
point: point,
|
|
280
286
|
type: e.type
|
|
281
287
|
})
|
|
282
288
|
} else {
|
|
283
|
-
this.setMoveInputState(
|
|
289
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
284
290
|
}
|
|
285
291
|
} else {
|
|
286
|
-
this.setMoveInputState(
|
|
292
|
+
this.setMoveInputState(MOVE_INPUT_STATE.moveDone, {square: square})
|
|
287
293
|
}
|
|
288
294
|
}
|
|
289
295
|
}
|
|
@@ -307,18 +313,18 @@ export class VisualMoveInput {
|
|
|
307
313
|
pageY = e.touches[0].pageY
|
|
308
314
|
target = document.elementFromPoint(clientX, clientY)
|
|
309
315
|
}
|
|
310
|
-
if (this.moveInputState ===
|
|
316
|
+
if (this.moveInputState === MOVE_INPUT_STATE.pieceClickedThreshold || this.moveInputState === MOVE_INPUT_STATE.secondClickThreshold) {
|
|
311
317
|
if (Math.abs(this.startPoint.x - clientX) > DRAG_THRESHOLD || Math.abs(this.startPoint.y - clientY) > DRAG_THRESHOLD) {
|
|
312
|
-
if (this.moveInputState ===
|
|
313
|
-
this.setMoveInputState(
|
|
318
|
+
if (this.moveInputState === MOVE_INPUT_STATE.secondClickThreshold) {
|
|
319
|
+
this.setMoveInputState(MOVE_INPUT_STATE.clickDragTo, {square: this.fromSquare, piece: this.movedPiece})
|
|
314
320
|
} else {
|
|
315
|
-
this.setMoveInputState(
|
|
321
|
+
this.setMoveInputState(MOVE_INPUT_STATE.dragTo, {square: this.fromSquare, piece: this.movedPiece})
|
|
316
322
|
}
|
|
317
323
|
if (this.view.chessboard.state.inputEnabled) {
|
|
318
324
|
this.moveDraggablePiece(pageX, pageY)
|
|
319
325
|
}
|
|
320
326
|
}
|
|
321
|
-
} else if (this.moveInputState ===
|
|
327
|
+
} else if (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo || this.moveInputState === MOVE_INPUT_STATE.clickTo) {
|
|
322
328
|
if (target && target.getAttribute && target.parentElement === this.view.boardGroup) {
|
|
323
329
|
const square = target.getAttribute("data-square")
|
|
324
330
|
if (square !== this.fromSquare && square !== this.toSquare) {
|
|
@@ -334,7 +340,7 @@ export class VisualMoveInput {
|
|
|
334
340
|
this.movingOverSquareCallback(this.fromSquare, null)
|
|
335
341
|
}
|
|
336
342
|
}
|
|
337
|
-
if (this.view.chessboard.state.inputEnabled && (this.moveInputState ===
|
|
343
|
+
if (this.view.chessboard.state.inputEnabled && (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo)) {
|
|
338
344
|
this.moveDraggablePiece(pageX, pageY)
|
|
339
345
|
}
|
|
340
346
|
}
|
|
@@ -351,46 +357,50 @@ export class VisualMoveInput {
|
|
|
351
357
|
const square = target.getAttribute("data-square")
|
|
352
358
|
|
|
353
359
|
if (square) {
|
|
354
|
-
if (this.moveInputState ===
|
|
360
|
+
if (this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo) {
|
|
355
361
|
if (this.fromSquare === square) {
|
|
356
|
-
if (this.moveInputState ===
|
|
362
|
+
if (this.moveInputState === MOVE_INPUT_STATE.clickDragTo) {
|
|
357
363
|
this.chessboard.state.position.setPiece(this.fromSquare, this.movedPiece)
|
|
358
364
|
this.view.setPieceVisibility(this.fromSquare)
|
|
359
365
|
this.moveInputCanceledCallback(square, null, MOVE_CANCELED_REASON.draggedBack)
|
|
360
|
-
this.setMoveInputState(
|
|
366
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
361
367
|
} else {
|
|
362
|
-
this.setMoveInputState(
|
|
368
|
+
this.setMoveInputState(MOVE_INPUT_STATE.clickTo, {square: square})
|
|
363
369
|
}
|
|
364
370
|
} else {
|
|
365
|
-
this.setMoveInputState(
|
|
371
|
+
this.setMoveInputState(MOVE_INPUT_STATE.moveDone, {square: square})
|
|
366
372
|
}
|
|
367
|
-
} else if (this.moveInputState ===
|
|
368
|
-
this.setMoveInputState(
|
|
369
|
-
} else if (this.moveInputState ===
|
|
370
|
-
this.setMoveInputState(
|
|
373
|
+
} else if (this.moveInputState === MOVE_INPUT_STATE.pieceClickedThreshold) {
|
|
374
|
+
this.setMoveInputState(MOVE_INPUT_STATE.clickTo, {square: square})
|
|
375
|
+
} else if (this.moveInputState === MOVE_INPUT_STATE.secondClickThreshold) {
|
|
376
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
371
377
|
this.moveInputCanceledCallback(square, null, MOVE_CANCELED_REASON.secondClick)
|
|
372
378
|
}
|
|
373
379
|
} else {
|
|
374
380
|
this.view.redrawPieces()
|
|
375
381
|
const moveStartSquare = this.fromSquare
|
|
376
|
-
this.setMoveInputState(
|
|
382
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
377
383
|
this.moveInputCanceledCallback(moveStartSquare, null, MOVE_CANCELED_REASON.movedOutOfBoard)
|
|
378
384
|
}
|
|
379
385
|
} else {
|
|
380
386
|
this.view.redrawPieces()
|
|
381
|
-
this.setMoveInputState(
|
|
387
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
382
388
|
}
|
|
383
389
|
}
|
|
384
390
|
|
|
385
391
|
onContextMenu(e) { // while moving
|
|
386
392
|
e.preventDefault()
|
|
387
393
|
this.view.redrawPieces()
|
|
388
|
-
this.setMoveInputState(
|
|
394
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
389
395
|
this.moveInputCanceledCallback(this.fromSquare, null, MOVE_CANCELED_REASON.secondaryClick)
|
|
390
396
|
}
|
|
391
397
|
|
|
398
|
+
isDragging() {
|
|
399
|
+
return this.moveInputState === MOVE_INPUT_STATE.dragTo || this.moveInputState === MOVE_INPUT_STATE.clickDragTo
|
|
400
|
+
}
|
|
401
|
+
|
|
392
402
|
destroy() {
|
|
393
|
-
this.setMoveInputState(
|
|
403
|
+
this.setMoveInputState(MOVE_INPUT_STATE.reset)
|
|
394
404
|
}
|
|
395
405
|
|
|
396
406
|
}
|