-
-
Notifications
You must be signed in to change notification settings - Fork 312
Expand file tree
/
Copy pathdrag.ts
More file actions
229 lines (212 loc) · 7.71 KB
/
drag.ts
File metadata and controls
229 lines (212 loc) · 7.71 KB
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
import { State } from './state.js';
import * as board from './board.js';
import * as util from './util.js';
import { clear as drawClear } from './draw.js';
import * as cg from './types.js';
import { anim } from './anim.js';
export interface DragCurrent {
orig: cg.Key; // orig key of dragging piece
piece: cg.Piece;
origPos: cg.NumberPair; // first event position
pos: cg.NumberPair; // latest event position
started: boolean; // whether the drag has started; as per the distance setting
element: cg.PieceNode | (() => cg.PieceNode | undefined);
newPiece?: boolean; // it it a new piece from outside the board
force?: boolean; // can the new piece replace an existing one (editor)
previouslySelected?: cg.Key;
originTarget: EventTarget | null;
keyHasChanged: boolean; // whether the drag has left the orig key
}
export function start(s: State, e: cg.MouchEvent): void {
if (!(s.trustAllEvents || e.isTrusted)) return; // only trust when trustAllEvents is enabled
if (e.buttons !== undefined && e.buttons > 1) return; // only touch or left click
if (e.touches && e.touches.length > 1) return; // support one finger touch only
const bounds = s.dom.bounds(),
position = util.eventPosition(e)!,
orig = board.getKeyAtDomPos(position, board.whitePov(s), bounds);
if (!orig) return;
const piece = s.pieces.get(orig);
const previouslySelected = s.selected;
if (!previouslySelected && s.drawable.enabled) {
if (s.drawable.eraseOnMouchDown) drawClear(s);
else if (piece?.color !== s.turnColor) s.pixelCoordsOfMouchDownToMaybeClearShapes = position;
}
// Prevent touch scroll and create no corresponding mouse event, if there
// is an intent to interact with the board.
if (
e.cancelable !== false &&
(!e.touches || s.blockTouchScroll || piece || previouslySelected || pieceCloseTo(s, position))
)
e.preventDefault();
else if (e.touches) return; // Handle only corresponding mouse event https://github.com/lichess-org/chessground/pull/268
const hadPremove = !!s.premovable.current;
const hadPredrop = !!s.predroppable.current;
s.stats.ctrlKey = e.ctrlKey;
if (s.selected && board.canMove(s, s.selected, orig)) {
anim(state => board.selectSquare(state, orig), s);
} else {
board.selectSquare(s, orig);
}
const stillSelected = s.selected === orig;
const element = pieceElementByKey(s, orig);
if (piece && element && stillSelected && board.isDraggable(s, orig)) {
s.draggable.current = {
orig,
piece,
origPos: position,
pos: position,
started: s.draggable.autoDistance && s.stats.dragged,
element,
previouslySelected,
originTarget: e.target,
keyHasChanged: false,
};
element.cgDragging = true;
element.classList.add('dragging');
// place ghost
const ghost = s.dom.elements.ghost;
if (ghost) {
ghost.className = `ghost ${piece.color} ${piece.role}`;
util.translate(ghost, util.posToTranslate(bounds)(util.key2pos(orig), board.whitePov(s)));
util.setVisible(ghost, true);
}
processDrag(s);
} else {
if (hadPremove) board.unsetPremove(s);
if (hadPredrop) board.unsetPredrop(s);
}
s.dom.redraw();
}
function pieceCloseTo(s: State, pos: cg.NumberPair): boolean {
const asWhite = board.whitePov(s),
bounds = s.dom.bounds(),
radiusSq = Math.pow((s.touchIgnoreRadius * bounds.width) / 16, 2) * 2;
for (const key of s.pieces.keys()) {
const center = util.computeSquareCenter(key, asWhite, bounds);
if (util.distanceSq(center, pos) <= radiusSq) return true;
}
return false;
}
export function dragNewPiece(s: State, piece: cg.Piece, e: cg.MouchEvent, force?: boolean): void {
const key: cg.Key = 'a0';
s.pieces.set(key, piece);
s.dom.redraw();
const position = util.eventPosition(e)!;
s.draggable.current = {
orig: key,
piece,
origPos: position,
pos: position,
started: true,
element: () => pieceElementByKey(s, key),
originTarget: e.target,
newPiece: true,
force: !!force,
keyHasChanged: false,
};
processDrag(s);
}
function processDrag(s: State): void {
requestAnimationFrame(() => {
const cur = s.draggable.current;
if (!cur) return;
// cancel animations while dragging
if (s.animation.current?.plan.anims.has(cur.orig)) s.animation.current = undefined;
// if moving piece is gone, cancel
const origPiece = s.pieces.get(cur.orig);
if (!origPiece || !util.samePiece(origPiece, cur.piece)) cancel(s);
else {
if (!cur.started && util.distanceSq(cur.pos, cur.origPos) >= Math.pow(s.draggable.distance, 2))
cur.started = true;
if (cur.started) {
// support lazy elements
if (typeof cur.element === 'function') {
const found = cur.element();
if (!found) return;
found.cgDragging = true;
found.classList.add('dragging');
cur.element = found;
}
const bounds = s.dom.bounds();
util.translate(cur.element, [
cur.pos[0] - bounds.left - bounds.width / 16,
cur.pos[1] - bounds.top - bounds.height / 16,
]);
cur.keyHasChanged ||= cur.orig !== board.getKeyAtDomPos(cur.pos, board.whitePov(s), bounds);
}
}
processDrag(s);
});
}
export function move(s: State, e: cg.MouchEvent): void {
// support one finger touch only
if (s.draggable.current && (!e.touches || e.touches.length < 2)) {
s.draggable.current.pos = util.eventPosition(e)!;
}
}
export function end(s: State, e: cg.MouchEvent): void {
const position = util.eventPosition(e);
if (
position &&
s.pixelCoordsOfMouchDownToMaybeClearShapes &&
util.distanceSq(position, s.pixelCoordsOfMouchDownToMaybeClearShapes) < 225
)
drawClear(s);
s.pixelCoordsOfMouchDownToMaybeClearShapes = undefined;
const cur = s.draggable.current;
if (!cur) return;
// create no corresponding mouse event
if (e.type === 'touchend' && e.cancelable !== false) e.preventDefault();
// comparing with the origin target is an easy way to test that the end event
// has the same touch origin
if (e.type === 'touchend' && cur.originTarget !== e.target && !cur.newPiece) {
s.draggable.current = undefined;
return;
}
board.unsetPremove(s);
board.unsetPredrop(s);
// touchend has no position; so use the last touchmove position instead
const eventPos = util.eventPosition(e) || cur.pos;
const dest = board.getKeyAtDomPos(eventPos, board.whitePov(s), s.dom.bounds());
if (dest && cur.started && cur.orig !== dest) {
if (cur.newPiece) board.dropNewPiece(s, cur.orig, dest, cur.force);
else {
s.stats.ctrlKey = e.ctrlKey;
if (board.userMove(s, cur.orig, dest)) s.stats.dragged = true;
}
} else if (cur.newPiece) {
s.pieces.delete(cur.orig);
} else if (s.draggable.deleteOnDropOff && !dest) {
s.pieces.delete(cur.orig);
board.callUserFunction(s.events.change);
}
if ((cur.orig === cur.previouslySelected || cur.keyHasChanged) && (cur.orig === dest || !dest))
board.unselect(s);
else if (!s.selectable.enabled) board.unselect(s);
removeDragElements(s);
s.draggable.current = undefined;
s.dom.redraw();
}
export function cancel(s: State): void {
const cur = s.draggable.current;
if (cur) {
if (cur.newPiece) s.pieces.delete(cur.orig);
s.draggable.current = undefined;
board.unselect(s);
removeDragElements(s);
s.dom.redraw();
}
}
function removeDragElements(s: State): void {
const e = s.dom.elements;
if (e.ghost) util.setVisible(e.ghost, false);
}
function pieceElementByKey(s: State, key: cg.Key): cg.PieceNode | undefined {
let el = s.dom.elements.board.firstChild;
while (el) {
if ((el as cg.KeyedNode).cgKey === key && (el as cg.KeyedNode).tagName === 'PIECE')
return el as cg.PieceNode;
el = el.nextSibling;
}
return;
}