react-native-chess-kit 0.1.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/LICENSE +21 -0
- package/README.md +168 -0
- package/lib/commonjs/board-background.js +49 -0
- package/lib/commonjs/board-background.js.map +1 -0
- package/lib/commonjs/board-coordinates.js +78 -0
- package/lib/commonjs/board-coordinates.js.map +1 -0
- package/lib/commonjs/board-drag-ghost.js +110 -0
- package/lib/commonjs/board-drag-ghost.js.map +1 -0
- package/lib/commonjs/board-legal-dots.js +67 -0
- package/lib/commonjs/board-legal-dots.js.map +1 -0
- package/lib/commonjs/board-piece.js +74 -0
- package/lib/commonjs/board-piece.js.map +1 -0
- package/lib/commonjs/board-pieces.js +47 -0
- package/lib/commonjs/board-pieces.js.map +1 -0
- package/lib/commonjs/board.js +188 -0
- package/lib/commonjs/board.js.map +1 -0
- package/lib/commonjs/index.js +26 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/commonjs/package.json +1 -0
- package/lib/commonjs/types.js +6 -0
- package/lib/commonjs/types.js.map +1 -0
- package/lib/commonjs/use-board-gesture.js +158 -0
- package/lib/commonjs/use-board-gesture.js.map +1 -0
- package/lib/commonjs/use-board-pieces.js +195 -0
- package/lib/commonjs/use-board-pieces.js.map +1 -0
- package/lib/commonjs/use-board-state.js +78 -0
- package/lib/commonjs/use-board-state.js.map +1 -0
- package/lib/module/board-background.js +44 -0
- package/lib/module/board-background.js.map +1 -0
- package/lib/module/board-coordinates.js +73 -0
- package/lib/module/board-coordinates.js.map +1 -0
- package/lib/module/board-drag-ghost.js +104 -0
- package/lib/module/board-drag-ghost.js.map +1 -0
- package/lib/module/board-legal-dots.js +62 -0
- package/lib/module/board-legal-dots.js.map +1 -0
- package/lib/module/board-piece.js +69 -0
- package/lib/module/board-piece.js.map +1 -0
- package/lib/module/board-pieces.js +42 -0
- package/lib/module/board-pieces.js.map +1 -0
- package/lib/module/board.js +184 -0
- package/lib/module/board.js.map +1 -0
- package/lib/module/index.js +21 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/module/types.js +4 -0
- package/lib/module/types.js.map +1 -0
- package/lib/module/use-board-gesture.js +154 -0
- package/lib/module/use-board-gesture.js.map +1 -0
- package/lib/module/use-board-pieces.js +189 -0
- package/lib/module/use-board-pieces.js.map +1 -0
- package/lib/module/use-board-state.js +74 -0
- package/lib/module/use-board-state.js.map +1 -0
- package/lib/typescript/board-background.d.ts +15 -0
- package/lib/typescript/board-background.d.ts.map +1 -0
- package/lib/typescript/board-coordinates.d.ts +20 -0
- package/lib/typescript/board-coordinates.d.ts.map +1 -0
- package/lib/typescript/board-drag-ghost.d.ts +21 -0
- package/lib/typescript/board-drag-ghost.d.ts.map +1 -0
- package/lib/typescript/board-legal-dots.d.ts +16 -0
- package/lib/typescript/board-legal-dots.d.ts.map +1 -0
- package/lib/typescript/board-piece.d.ts +36 -0
- package/lib/typescript/board-piece.d.ts.map +1 -0
- package/lib/typescript/board-pieces.d.ts +22 -0
- package/lib/typescript/board-pieces.d.ts.map +1 -0
- package/lib/typescript/board.d.ts +17 -0
- package/lib/typescript/board.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +4 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/types.d.ts +88 -0
- package/lib/typescript/types.d.ts.map +1 -0
- package/lib/typescript/use-board-gesture.d.ts +46 -0
- package/lib/typescript/use-board-gesture.d.ts.map +1 -0
- package/lib/typescript/use-board-pieces.d.ts +23 -0
- package/lib/typescript/use-board-pieces.d.ts.map +1 -0
- package/lib/typescript/use-board-state.d.ts +35 -0
- package/lib/typescript/use-board-state.d.ts.map +1 -0
- package/package.json +73 -0
- package/src/board-background.tsx +46 -0
- package/src/board-coordinates.tsx +98 -0
- package/src/board-drag-ghost.tsx +132 -0
- package/src/board-legal-dots.tsx +73 -0
- package/src/board-piece.tsx +104 -0
- package/src/board-pieces.tsx +56 -0
- package/src/board.tsx +203 -0
- package/src/index.ts +39 -0
- package/src/types.ts +114 -0
- package/src/use-board-gesture.ts +201 -0
- package/src/use-board-pieces.ts +158 -0
- package/src/use-board-state.ts +104 -0
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Board = void 0;
|
|
7
|
+
var _react = _interopRequireWildcard(require("react"));
|
|
8
|
+
var _reactNative = require("react-native");
|
|
9
|
+
var _reactNativeGestureHandler = require("react-native-gesture-handler");
|
|
10
|
+
var _useBoardPieces = require("./use-board-pieces");
|
|
11
|
+
var _useBoardState = require("./use-board-state");
|
|
12
|
+
var _useBoardGesture = require("./use-board-gesture");
|
|
13
|
+
var _boardBackground = require("./board-background");
|
|
14
|
+
var _boardCoordinates = require("./board-coordinates");
|
|
15
|
+
var _boardLegalDots = require("./board-legal-dots");
|
|
16
|
+
var _boardPieces = require("./board-pieces");
|
|
17
|
+
var _boardDragGhost = require("./board-drag-ghost");
|
|
18
|
+
var _jsxRuntime = require("react/jsx-runtime");
|
|
19
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
20
|
+
/**
|
|
21
|
+
* High-performance custom chess board built on Reanimated + Gesture Handler.
|
|
22
|
+
*
|
|
23
|
+
* Architecture:
|
|
24
|
+
* - 1 gesture handler (vs 32 in typical implementations)
|
|
25
|
+
* - ~40 components mounted (vs ~281)
|
|
26
|
+
* - ~75 native views (vs ~470)
|
|
27
|
+
* - 0 React Context providers
|
|
28
|
+
* - 0 re-renders during drag (pure worklet — only 2 shared value writes per frame)
|
|
29
|
+
*
|
|
30
|
+
* Follows chess.com/lichess pattern: single gesture receiver on the board,
|
|
31
|
+
* coordinate math to determine touched piece, shared values for drag state.
|
|
32
|
+
*/
|
|
33
|
+
const Board = exports.Board = /*#__PURE__*/(0, _react.forwardRef)(function Board({
|
|
34
|
+
fen,
|
|
35
|
+
orientation,
|
|
36
|
+
boardSize,
|
|
37
|
+
gestureEnabled,
|
|
38
|
+
player,
|
|
39
|
+
onMove,
|
|
40
|
+
colors,
|
|
41
|
+
moveDuration,
|
|
42
|
+
withLetters,
|
|
43
|
+
withNumbers,
|
|
44
|
+
renderPiece,
|
|
45
|
+
showLegalMoves,
|
|
46
|
+
moveMethod
|
|
47
|
+
}, ref) {
|
|
48
|
+
const squareSize = boardSize / 8;
|
|
49
|
+
|
|
50
|
+
// --- Piece data from FEN ---
|
|
51
|
+
const pieces = (0, _useBoardPieces.useBoardPieces)(fen);
|
|
52
|
+
|
|
53
|
+
// --- Chess.js for legal move validation ---
|
|
54
|
+
const boardState = (0, _useBoardState.useBoardState)(fen);
|
|
55
|
+
|
|
56
|
+
// Sync internal chess.js when parent changes FEN (puzzle reset, opponent move, etc.)
|
|
57
|
+
// Must be in useEffect — side effects during render violate React's rules
|
|
58
|
+
// and can fire multiple times in concurrent mode.
|
|
59
|
+
(0, _react.useEffect)(() => {
|
|
60
|
+
boardState.loadFen(fen);
|
|
61
|
+
}, [fen, boardState]);
|
|
62
|
+
|
|
63
|
+
// --- Selection state (triggers legal dots display) ---
|
|
64
|
+
const [selectedSquare, setSelectedSquare] = (0, _react.useState)(null);
|
|
65
|
+
const [legalMoves, setLegalMoves] = (0, _react.useState)([]);
|
|
66
|
+
|
|
67
|
+
// Default piece renderer (no-op if parent provides renderPiece)
|
|
68
|
+
const defaultRenderPiece = (0, _react.useCallback)((code, size) => /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNative.View, {
|
|
69
|
+
style: {
|
|
70
|
+
width: size,
|
|
71
|
+
height: size,
|
|
72
|
+
backgroundColor: 'rgba(0,0,0,0.3)'
|
|
73
|
+
}
|
|
74
|
+
}), []);
|
|
75
|
+
const pieceRenderer = renderPiece ?? defaultRenderPiece;
|
|
76
|
+
|
|
77
|
+
// --- Gesture callbacks ---
|
|
78
|
+
const handlePieceSelected = (0, _react.useCallback)(square => {
|
|
79
|
+
setSelectedSquare(square);
|
|
80
|
+
if (showLegalMoves) {
|
|
81
|
+
setLegalMoves(boardState.getLegalMoves(square));
|
|
82
|
+
}
|
|
83
|
+
}, [showLegalMoves, boardState]);
|
|
84
|
+
const handleSelectionCleared = (0, _react.useCallback)(() => {
|
|
85
|
+
setSelectedSquare(null);
|
|
86
|
+
setLegalMoves([]);
|
|
87
|
+
}, []);
|
|
88
|
+
const handlePieceMoved = (0, _react.useCallback)((from, to) => {
|
|
89
|
+
// Clear selection and legal dots
|
|
90
|
+
setSelectedSquare(null);
|
|
91
|
+
setLegalMoves([]);
|
|
92
|
+
|
|
93
|
+
// Notify parent — parent decides whether to accept/reject
|
|
94
|
+
onMove?.({
|
|
95
|
+
from,
|
|
96
|
+
to
|
|
97
|
+
});
|
|
98
|
+
}, [onMove]);
|
|
99
|
+
|
|
100
|
+
// --- Single centralized gesture ---
|
|
101
|
+
const {
|
|
102
|
+
gesture,
|
|
103
|
+
gestureState
|
|
104
|
+
} = (0, _useBoardGesture.useBoardGesture)({
|
|
105
|
+
squareSize,
|
|
106
|
+
orientation,
|
|
107
|
+
gestureEnabled,
|
|
108
|
+
player,
|
|
109
|
+
moveMethod,
|
|
110
|
+
pieces,
|
|
111
|
+
callbacks: {
|
|
112
|
+
onPieceSelected: handlePieceSelected,
|
|
113
|
+
onPieceMoved: handlePieceMoved,
|
|
114
|
+
onSelectionCleared: handleSelectionCleared
|
|
115
|
+
},
|
|
116
|
+
selectedSquare,
|
|
117
|
+
legalMoves
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// --- Imperative ref for parent (move, highlight, reset, undo) ---
|
|
121
|
+
(0, _react.useImperativeHandle)(ref, () => ({
|
|
122
|
+
move: move => {
|
|
123
|
+
// Pre-apply to internal chess.js so subsequent getLegalMoves calls
|
|
124
|
+
// reflect the new position. The parent will also update the FEN prop,
|
|
125
|
+
// which triggers useBoardPieces -> piece position animates via shared values.
|
|
126
|
+
boardState.applyMove(move.from, move.to);
|
|
127
|
+
},
|
|
128
|
+
highlight: (_square, _color) => {
|
|
129
|
+
// Highlights are handled by overlay layers in the consuming app,
|
|
130
|
+
// not internally — this is a no-op stub for API compatibility.
|
|
131
|
+
// Use the Board's overlay API or render your own highlight layer.
|
|
132
|
+
},
|
|
133
|
+
clearHighlights: () => {
|
|
134
|
+
// Same as highlight — handled by overlay layer
|
|
135
|
+
},
|
|
136
|
+
resetBoard: newFen => {
|
|
137
|
+
boardState.loadFen(newFen);
|
|
138
|
+
setSelectedSquare(null);
|
|
139
|
+
setLegalMoves([]);
|
|
140
|
+
},
|
|
141
|
+
undo: () => {
|
|
142
|
+
boardState.undoMove();
|
|
143
|
+
setSelectedSquare(null);
|
|
144
|
+
setLegalMoves([]);
|
|
145
|
+
}
|
|
146
|
+
}));
|
|
147
|
+
return /*#__PURE__*/(0, _jsxRuntime.jsx)(_reactNativeGestureHandler.GestureDetector, {
|
|
148
|
+
gesture: gesture,
|
|
149
|
+
children: /*#__PURE__*/(0, _jsxRuntime.jsxs)(_reactNative.View, {
|
|
150
|
+
style: {
|
|
151
|
+
width: boardSize,
|
|
152
|
+
height: boardSize
|
|
153
|
+
},
|
|
154
|
+
children: [/*#__PURE__*/(0, _jsxRuntime.jsx)(_boardBackground.BoardBackground, {
|
|
155
|
+
boardSize: boardSize,
|
|
156
|
+
lightColor: colors.light,
|
|
157
|
+
darkColor: colors.dark
|
|
158
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_boardCoordinates.BoardCoordinates, {
|
|
159
|
+
boardSize: boardSize,
|
|
160
|
+
orientation: orientation,
|
|
161
|
+
lightColor: colors.light,
|
|
162
|
+
darkColor: colors.dark,
|
|
163
|
+
withLetters: withLetters,
|
|
164
|
+
withNumbers: withNumbers
|
|
165
|
+
}), showLegalMoves && /*#__PURE__*/(0, _jsxRuntime.jsx)(_boardLegalDots.BoardLegalDots, {
|
|
166
|
+
legalMoves: legalMoves,
|
|
167
|
+
squareSize: squareSize,
|
|
168
|
+
orientation: orientation
|
|
169
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_boardPieces.BoardPiecesLayer, {
|
|
170
|
+
pieces: pieces,
|
|
171
|
+
squareSize: squareSize,
|
|
172
|
+
orientation: orientation,
|
|
173
|
+
moveDuration: moveDuration,
|
|
174
|
+
renderPiece: pieceRenderer,
|
|
175
|
+
activeSquare: gestureState.activeSquare,
|
|
176
|
+
isDragging: gestureState.isDragging
|
|
177
|
+
}), /*#__PURE__*/(0, _jsxRuntime.jsx)(_boardDragGhost.BoardDragGhost, {
|
|
178
|
+
squareSize: squareSize,
|
|
179
|
+
isDragging: gestureState.isDragging,
|
|
180
|
+
dragX: gestureState.dragX,
|
|
181
|
+
dragY: gestureState.dragY,
|
|
182
|
+
dragPieceCode: gestureState.dragPieceCode,
|
|
183
|
+
renderPiece: pieceRenderer
|
|
184
|
+
})]
|
|
185
|
+
})
|
|
186
|
+
});
|
|
187
|
+
});
|
|
188
|
+
//# sourceMappingURL=board.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","_interopRequireWildcard","require","_reactNative","_reactNativeGestureHandler","_useBoardPieces","_useBoardState","_useBoardGesture","_boardBackground","_boardCoordinates","_boardLegalDots","_boardPieces","_boardDragGhost","_jsxRuntime","e","t","WeakMap","r","n","__esModule","o","i","f","__proto__","default","has","get","set","hasOwnProperty","call","Object","defineProperty","getOwnPropertyDescriptor","Board","exports","forwardRef","fen","orientation","boardSize","gestureEnabled","player","onMove","colors","moveDuration","withLetters","withNumbers","renderPiece","showLegalMoves","moveMethod","ref","squareSize","pieces","useBoardPieces","boardState","useBoardState","useEffect","loadFen","selectedSquare","setSelectedSquare","useState","legalMoves","setLegalMoves","defaultRenderPiece","useCallback","code","size","jsx","View","style","width","height","backgroundColor","pieceRenderer","handlePieceSelected","square","getLegalMoves","handleSelectionCleared","handlePieceMoved","from","to","gesture","gestureState","useBoardGesture","callbacks","onPieceSelected","onPieceMoved","onSelectionCleared","useImperativeHandle","move","applyMove","highlight","_square","_color","clearHighlights","resetBoard","newFen","undo","undoMove","GestureDetector","children","jsxs","BoardBackground","lightColor","light","darkColor","dark","BoardCoordinates","BoardLegalDots","BoardPiecesLayer","activeSquare","isDragging","BoardDragGhost","dragX","dragY","dragPieceCode"],"sourceRoot":"..\\..\\src","sources":["board.tsx"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,uBAAA,CAAAC,OAAA;AACA,IAAAC,YAAA,GAAAD,OAAA;AACA,IAAAE,0BAAA,GAAAF,OAAA;AAGA,IAAAG,eAAA,GAAAH,OAAA;AACA,IAAAI,cAAA,GAAAJ,OAAA;AACA,IAAAK,gBAAA,GAAAL,OAAA;AACA,IAAAM,gBAAA,GAAAN,OAAA;AACA,IAAAO,iBAAA,GAAAP,OAAA;AACA,IAAAQ,eAAA,GAAAR,OAAA;AACA,IAAAS,YAAA,GAAAT,OAAA;AACA,IAAAU,eAAA,GAAAV,OAAA;AAAoD,IAAAW,WAAA,GAAAX,OAAA;AAAA,SAAAD,wBAAAa,CAAA,EAAAC,CAAA,6BAAAC,OAAA,MAAAC,CAAA,OAAAD,OAAA,IAAAE,CAAA,OAAAF,OAAA,YAAAf,uBAAA,YAAAA,CAAAa,CAAA,EAAAC,CAAA,SAAAA,CAAA,IAAAD,CAAA,IAAAA,CAAA,CAAAK,UAAA,SAAAL,CAAA,MAAAM,CAAA,EAAAC,CAAA,EAAAC,CAAA,KAAAC,SAAA,QAAAC,OAAA,EAAAV,CAAA,iBAAA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A,CACE;EAAC,CACQ,CAAC;AAEtB,CAAC,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "Board", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _board.Board;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "squareToXY", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _useBoardPieces.squareToXY;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "xyToSquare", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _useBoardPieces.xyToSquare;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
var _board = require("./board");
|
|
25
|
+
var _useBoardPieces = require("./use-board-pieces");
|
|
26
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_board","require","_useBoardPieces"],"sourceRoot":"..\\..\\src","sources":["index.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAOA,IAAAA,MAAA,GAAAC,OAAA;AA+BA,IAAAC,eAAA,GAAAD,OAAA","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"..\\..\\src","sources":["types.ts"],"mappings":"","ignoreList":[]}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useBoardGesture = useBoardGesture;
|
|
7
|
+
var _react = require("react");
|
|
8
|
+
var _reactNativeGestureHandler = require("react-native-gesture-handler");
|
|
9
|
+
var _reactNativeReanimated = require("react-native-reanimated");
|
|
10
|
+
var _useBoardPieces = require("./use-board-pieces");
|
|
11
|
+
/**
|
|
12
|
+
* Single centralized gesture handler for the entire board.
|
|
13
|
+
*
|
|
14
|
+
* Instead of 32 separate Gesture.Pan() handlers (one per piece), we use ONE
|
|
15
|
+
* handler on the board container. Touch -> coordinate math -> which piece.
|
|
16
|
+
*
|
|
17
|
+
* Supports three modes:
|
|
18
|
+
* - 'drag': drag piece to target square
|
|
19
|
+
* - 'click': tap source piece, then tap target square
|
|
20
|
+
* - 'both': drag or click (default)
|
|
21
|
+
*
|
|
22
|
+
* All drag position tracking uses shared values — zero JS bridge calls,
|
|
23
|
+
* zero re-renders during drag. Only the final move triggers JS via runOnJS.
|
|
24
|
+
*
|
|
25
|
+
* The gesture object is STABLE (only recreated when squareSize, orientation,
|
|
26
|
+
* gestureEnabled, player, or moveMethod change). Frequently-changing data
|
|
27
|
+
* (pieces, selectedSquare, legalMoves) is read from refs via runOnJS bridge
|
|
28
|
+
* functions, avoiding costly gesture teardown/rebuild on every move.
|
|
29
|
+
*/
|
|
30
|
+
function useBoardGesture({
|
|
31
|
+
squareSize,
|
|
32
|
+
orientation,
|
|
33
|
+
gestureEnabled,
|
|
34
|
+
player,
|
|
35
|
+
moveMethod,
|
|
36
|
+
pieces,
|
|
37
|
+
callbacks,
|
|
38
|
+
selectedSquare,
|
|
39
|
+
legalMoves
|
|
40
|
+
}) {
|
|
41
|
+
// Shared values for drag tracking — updated on UI thread only
|
|
42
|
+
const activeSquare = (0, _reactNativeReanimated.useSharedValue)(null);
|
|
43
|
+
const dragX = (0, _reactNativeReanimated.useSharedValue)(0);
|
|
44
|
+
const dragY = (0, _reactNativeReanimated.useSharedValue)(0);
|
|
45
|
+
const isDragging = (0, _reactNativeReanimated.useSharedValue)(false);
|
|
46
|
+
const dragPieceCode = (0, _reactNativeReanimated.useSharedValue)(null);
|
|
47
|
+
const gestureState = {
|
|
48
|
+
activeSquare,
|
|
49
|
+
dragX,
|
|
50
|
+
dragY,
|
|
51
|
+
isDragging,
|
|
52
|
+
dragPieceCode
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// --- Refs for frequently-changing data (read from JS thread via runOnJS) ---
|
|
56
|
+
// These update every move but do NOT cause gesture object recreation.
|
|
57
|
+
const piecesRef = (0, _react.useRef)(pieces);
|
|
58
|
+
piecesRef.current = pieces;
|
|
59
|
+
const selectedSquareRef = (0, _react.useRef)(selectedSquare);
|
|
60
|
+
selectedSquareRef.current = selectedSquare;
|
|
61
|
+
const legalMovesRef = (0, _react.useRef)(legalMoves);
|
|
62
|
+
legalMovesRef.current = legalMoves;
|
|
63
|
+
const callbacksRef = (0, _react.useRef)(callbacks);
|
|
64
|
+
callbacksRef.current = callbacks;
|
|
65
|
+
|
|
66
|
+
// --- JS-thread bridge functions called from worklets via runOnJS ---
|
|
67
|
+
// These read current values from refs, so they always have fresh data.
|
|
68
|
+
|
|
69
|
+
const handleBegin = (0, _react.useCallback)((touchX, touchY) => {
|
|
70
|
+
const square = (0, _useBoardPieces.xyToSquare)(touchX, touchY, squareSize, orientation);
|
|
71
|
+
const currentPieces = piecesRef.current;
|
|
72
|
+
const currentSelected = selectedSquareRef.current;
|
|
73
|
+
const currentLegalMoves = legalMovesRef.current;
|
|
74
|
+
const cbs = callbacksRef.current;
|
|
75
|
+
const canClick = moveMethod !== 'drag';
|
|
76
|
+
|
|
77
|
+
// Build lookup for the current touch
|
|
78
|
+
const piece = currentPieces.find(p => p.square === square);
|
|
79
|
+
const isPlayerPiece = piece ? player === 'both' || (piece.color === 'w' ? 'white' : 'black') === player : false;
|
|
80
|
+
|
|
81
|
+
// Click-to-move: second tap on a legal target square
|
|
82
|
+
const legalSquares = new Set(currentLegalMoves.map(m => m.square));
|
|
83
|
+
if (canClick && currentSelected && legalSquares.has(square)) {
|
|
84
|
+
cbs.onPieceMoved(currentSelected, square);
|
|
85
|
+
activeSquare.value = null;
|
|
86
|
+
isDragging.value = false;
|
|
87
|
+
dragPieceCode.value = null;
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (isPlayerPiece && piece) {
|
|
91
|
+
// Tapped/started dragging a player piece
|
|
92
|
+
activeSquare.value = square;
|
|
93
|
+
dragX.value = touchX;
|
|
94
|
+
dragY.value = touchY;
|
|
95
|
+
dragPieceCode.value = piece.code;
|
|
96
|
+
cbs.onPieceSelected(square);
|
|
97
|
+
} else {
|
|
98
|
+
// Tapped empty square or opponent piece — clear selection
|
|
99
|
+
activeSquare.value = null;
|
|
100
|
+
dragPieceCode.value = null;
|
|
101
|
+
if (currentSelected) {
|
|
102
|
+
cbs.onSelectionCleared();
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}, [squareSize, orientation, player, moveMethod, activeSquare, dragX, dragY, isDragging, dragPieceCode]);
|
|
106
|
+
const handleEnd = (0, _react.useCallback)((touchX, touchY) => {
|
|
107
|
+
const fromSquare = activeSquare.value;
|
|
108
|
+
if (!fromSquare) return;
|
|
109
|
+
const toSquare = (0, _useBoardPieces.xyToSquare)(touchX, touchY, squareSize, orientation);
|
|
110
|
+
isDragging.value = false;
|
|
111
|
+
if (fromSquare !== toSquare) {
|
|
112
|
+
callbacksRef.current.onPieceMoved(fromSquare, toSquare);
|
|
113
|
+
}
|
|
114
|
+
activeSquare.value = null;
|
|
115
|
+
dragPieceCode.value = null;
|
|
116
|
+
}, [squareSize, orientation, activeSquare, isDragging, dragPieceCode]);
|
|
117
|
+
|
|
118
|
+
// --- Build the gesture (STABLE — only changes on layout/config changes) ---
|
|
119
|
+
const canDrag = moveMethod !== 'click';
|
|
120
|
+
const gesture = (0, _react.useMemo)(() => {
|
|
121
|
+
return _reactNativeGestureHandler.Gesture.Pan().enabled(gestureEnabled).minDistance(0) // Also detect taps (zero-distance pans)
|
|
122
|
+
.onBegin(e => {
|
|
123
|
+
'worklet';
|
|
124
|
+
|
|
125
|
+
// Bridge to JS for piece lookup + selection logic
|
|
126
|
+
(0, _reactNativeReanimated.runOnJS)(handleBegin)(e.x, e.y);
|
|
127
|
+
}).onStart(() => {
|
|
128
|
+
'worklet';
|
|
129
|
+
|
|
130
|
+
if (!canDrag || !activeSquare.value) return;
|
|
131
|
+
isDragging.value = true;
|
|
132
|
+
}).onUpdate(e => {
|
|
133
|
+
'worklet';
|
|
134
|
+
|
|
135
|
+
if (!canDrag || !isDragging.value) return;
|
|
136
|
+
// Only 2 shared value writes — no JS bridge, no re-renders
|
|
137
|
+
dragX.value = e.x;
|
|
138
|
+
dragY.value = e.y;
|
|
139
|
+
}).onEnd(e => {
|
|
140
|
+
'worklet';
|
|
141
|
+
|
|
142
|
+
if (!isDragging.value || !activeSquare.value) return;
|
|
143
|
+
(0, _reactNativeReanimated.runOnJS)(handleEnd)(e.x, e.y);
|
|
144
|
+
}).onFinalize(() => {
|
|
145
|
+
'worklet';
|
|
146
|
+
|
|
147
|
+
// Safety reset if gesture was interrupted
|
|
148
|
+
isDragging.value = false;
|
|
149
|
+
});
|
|
150
|
+
}, [gestureEnabled, canDrag, handleBegin, handleEnd,
|
|
151
|
+
// Shared values are stable refs — listed for exhaustive-deps but don't cause recreations
|
|
152
|
+
activeSquare, dragX, dragY, isDragging]);
|
|
153
|
+
return {
|
|
154
|
+
gesture,
|
|
155
|
+
gestureState
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
//# sourceMappingURL=use-board-gesture.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","require","_reactNativeGestureHandler","_reactNativeReanimated","_useBoardPieces","useBoardGesture","squareSize","orientation","gestureEnabled","player","moveMethod","pieces","callbacks","selectedSquare","legalMoves","activeSquare","useSharedValue","dragX","dragY","isDragging","dragPieceCode","gestureState","piecesRef","useRef","current","selectedSquareRef","legalMovesRef","callbacksRef","handleBegin","useCallback","touchX","touchY","square","xyToSquare","currentPieces","currentSelected","currentLegalMoves","cbs","canClick","piece","find","p","isPlayerPiece","color","legalSquares","Set","map","m","has","onPieceMoved","value","code","onPieceSelected","onSelectionCleared","handleEnd","fromSquare","toSquare","canDrag","gesture","useMemo","Gesture","Pan","enabled","minDistance","onBegin","e","runOnJS","x","y","onStart","onUpdate","onEnd","onFinalize"],"sourceRoot":"..\\..\\src","sources":["use-board-gesture.ts"],"mappings":";;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AACA,IAAAC,0BAAA,GAAAD,OAAA;AACA,IAAAE,sBAAA,GAAAF,OAAA;AAMA,IAAAG,eAAA,GAAAH,OAAA;AA2BA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASI,eAAeA,CAAC;EAC9BC,UAAU;EACVC,WAAW;EACXC,cAAc;EACdC,MAAM;EACNC,UAAU;EACVC,MAAM;EACNC,SAAS;EACTC,cAAc;EACdC;AACqB,CAAC,EAAyB;EAC/C;EACA,MAAMC,YAAY,GAAG,IAAAC,qCAAc,EAAgB,IAAI,CAAC;EACxD,MAAMC,KAAK,GAAG,IAAAD,qCAAc,EAAC,CAAC,CAAC;EAC/B,MAAME,KAAK,GAAG,IAAAF,qCAAc,EAAC,CAAC,CAAC;EAC/B,MAAMG,UAAU,GAAG,IAAAH,qCAAc,EAAC,KAAK,CAAC;EACxC,MAAMI,aAAa,GAAG,IAAAJ,qCAAc,EAAgB,IAAI,CAAC;EAEzD,MAAMK,YAA0B,GAAG;IACjCN,YAAY;IACZE,KAAK;IACLC,KAAK;IACLC,UAAU;IACVC;EACF,CAAC;;EAED;EACA;EACA,MAAME,SAAS,GAAG,IAAAC,aAAM,EAACZ,MAAM,CAAC;EAChCW,SAAS,CAACE,OAAO,GAAGb,MAAM;EAE1B,MAAMc,iBAAiB,GAAG,IAAAF,aAAM,EAACV,cAAc,CAAC;EAChDY,iBAAiB,CAACD,OAAO,GAAGX,cAAc;EAE1C,MAAMa,aAAa,GA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,EACLC,UAAU,CACX,CAAC;EAEF,OAAO;IAAEuC,OAAO;IAAErC;EAAa,CAAC;AAClC","ignoreList":[]}
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.squareToXY = squareToXY;
|
|
7
|
+
exports.useBoardPieces = useBoardPieces;
|
|
8
|
+
exports.xyToSquare = xyToSquare;
|
|
9
|
+
var _react = require("react");
|
|
10
|
+
const FEN_PIECE_MAP = {
|
|
11
|
+
p: {
|
|
12
|
+
code: 'bp',
|
|
13
|
+
color: 'b'
|
|
14
|
+
},
|
|
15
|
+
r: {
|
|
16
|
+
code: 'br',
|
|
17
|
+
color: 'b'
|
|
18
|
+
},
|
|
19
|
+
n: {
|
|
20
|
+
code: 'bn',
|
|
21
|
+
color: 'b'
|
|
22
|
+
},
|
|
23
|
+
b: {
|
|
24
|
+
code: 'bb',
|
|
25
|
+
color: 'b'
|
|
26
|
+
},
|
|
27
|
+
q: {
|
|
28
|
+
code: 'bq',
|
|
29
|
+
color: 'b'
|
|
30
|
+
},
|
|
31
|
+
k: {
|
|
32
|
+
code: 'bk',
|
|
33
|
+
color: 'b'
|
|
34
|
+
},
|
|
35
|
+
P: {
|
|
36
|
+
code: 'wp',
|
|
37
|
+
color: 'w'
|
|
38
|
+
},
|
|
39
|
+
R: {
|
|
40
|
+
code: 'wr',
|
|
41
|
+
color: 'w'
|
|
42
|
+
},
|
|
43
|
+
N: {
|
|
44
|
+
code: 'wn',
|
|
45
|
+
color: 'w'
|
|
46
|
+
},
|
|
47
|
+
B: {
|
|
48
|
+
code: 'wb',
|
|
49
|
+
color: 'w'
|
|
50
|
+
},
|
|
51
|
+
Q: {
|
|
52
|
+
code: 'wq',
|
|
53
|
+
color: 'w'
|
|
54
|
+
},
|
|
55
|
+
K: {
|
|
56
|
+
code: 'wk',
|
|
57
|
+
color: 'w'
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const FILES = 'abcdefgh';
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Parse the piece-placement part of a FEN string into an array of pieces.
|
|
64
|
+
* Pure function — no React dependencies, suitable for worklets if needed.
|
|
65
|
+
*/
|
|
66
|
+
function parseFenPieces(fen) {
|
|
67
|
+
const placement = fen.split(' ')[0];
|
|
68
|
+
const ranks = placement.split('/');
|
|
69
|
+
const pieces = [];
|
|
70
|
+
for (let rankIdx = 0; rankIdx < ranks.length; rankIdx++) {
|
|
71
|
+
const rank = ranks[rankIdx];
|
|
72
|
+
let fileIdx = 0;
|
|
73
|
+
for (const char of rank) {
|
|
74
|
+
if (char >= '1' && char <= '8') {
|
|
75
|
+
fileIdx += parseInt(char, 10);
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
const mapping = FEN_PIECE_MAP[char];
|
|
79
|
+
if (mapping) {
|
|
80
|
+
// FEN ranks are from rank 8 (index 0) down to rank 1 (index 7)
|
|
81
|
+
const square = `${FILES[fileIdx]}${8 - rankIdx}`;
|
|
82
|
+
pieces.push({
|
|
83
|
+
code: mapping.code,
|
|
84
|
+
square,
|
|
85
|
+
color: mapping.color
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
fileIdx++;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return pieces;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Convert square notation to pixel coordinates (top-left corner).
|
|
96
|
+
* Orientation-aware: flips the board when playing as black.
|
|
97
|
+
*/
|
|
98
|
+
function squareToXY(square, squareSize, orientation) {
|
|
99
|
+
'worklet';
|
|
100
|
+
|
|
101
|
+
const fileIdx = square.charCodeAt(0) - 97; // 'a'=0 .. 'h'=7
|
|
102
|
+
const rankIdx = parseInt(square[1], 10) - 1; // '1'=0 .. '8'=7
|
|
103
|
+
|
|
104
|
+
const col = orientation === 'white' ? fileIdx : 7 - fileIdx;
|
|
105
|
+
const row = orientation === 'white' ? 7 - rankIdx : rankIdx;
|
|
106
|
+
return {
|
|
107
|
+
x: col * squareSize,
|
|
108
|
+
y: row * squareSize
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Convert pixel coordinates to a square notation string.
|
|
114
|
+
* Clamps to board bounds. Orientation-aware.
|
|
115
|
+
*/
|
|
116
|
+
function xyToSquare(x, y, squareSize, orientation) {
|
|
117
|
+
'worklet';
|
|
118
|
+
|
|
119
|
+
const col = Math.max(0, Math.min(7, Math.floor(x / squareSize)));
|
|
120
|
+
const row = Math.max(0, Math.min(7, Math.floor(y / squareSize)));
|
|
121
|
+
const fileIdx = orientation === 'white' ? col : 7 - col;
|
|
122
|
+
const rankIdx = orientation === 'white' ? 7 - row : row;
|
|
123
|
+
|
|
124
|
+
// String.fromCharCode not available in worklets — use lookup
|
|
125
|
+
const files = 'abcdefgh';
|
|
126
|
+
return `${files[fileIdx]}${rankIdx + 1}`;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Manages the piece list derived from FEN, with stable IDs for React keys.
|
|
131
|
+
*
|
|
132
|
+
* Stable IDs prevent unmount/remount cycles when pieces change position.
|
|
133
|
+
* A piece keeps its ID as long as it exists on the board — only capture
|
|
134
|
+
* (removal) or promotion (code change) creates a new ID.
|
|
135
|
+
*/
|
|
136
|
+
function useBoardPieces(fen) {
|
|
137
|
+
// Track piece-code counters across renders for stable ID assignment
|
|
138
|
+
const idCounterRef = (0, _react.useRef)({});
|
|
139
|
+
const prevPiecesRef = (0, _react.useRef)([]);
|
|
140
|
+
return (0, _react.useMemo)(() => {
|
|
141
|
+
const parsed = parseFenPieces(fen);
|
|
142
|
+
const prev = prevPiecesRef.current;
|
|
143
|
+
const prevBySquare = new Map(prev.map(p => [p.square, p]));
|
|
144
|
+
|
|
145
|
+
// Try to reuse IDs from previous render:
|
|
146
|
+
// 1. Same code on same square -> keep ID (piece didn't move)
|
|
147
|
+
// 2. Same code moved to a new square -> find unmatched previous piece of same code
|
|
148
|
+
const usedPrevIds = new Set();
|
|
149
|
+
const result = [];
|
|
150
|
+
|
|
151
|
+
// First pass: exact square matches (piece stayed or appeared on same square)
|
|
152
|
+
const unmatched = [];
|
|
153
|
+
for (const p of parsed) {
|
|
154
|
+
const existing = prevBySquare.get(p.square);
|
|
155
|
+
if (existing && existing.code === p.code && !usedPrevIds.has(existing.id)) {
|
|
156
|
+
usedPrevIds.add(existing.id);
|
|
157
|
+
result.push({
|
|
158
|
+
...p,
|
|
159
|
+
id: existing.id
|
|
160
|
+
});
|
|
161
|
+
} else {
|
|
162
|
+
unmatched.push(p);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Second pass: match unmatched pieces by code to previous pieces that moved
|
|
167
|
+
for (const p of unmatched) {
|
|
168
|
+
let matchedId = null;
|
|
169
|
+
for (const prevPiece of prev) {
|
|
170
|
+
if (prevPiece.code === p.code && !usedPrevIds.has(prevPiece.id)) {
|
|
171
|
+
matchedId = prevPiece.id;
|
|
172
|
+
usedPrevIds.add(prevPiece.id);
|
|
173
|
+
break;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
if (matchedId) {
|
|
177
|
+
result.push({
|
|
178
|
+
...p,
|
|
179
|
+
id: matchedId
|
|
180
|
+
});
|
|
181
|
+
} else {
|
|
182
|
+
// New piece (promotion, or first render) — assign fresh ID
|
|
183
|
+
const counter = idCounterRef.current;
|
|
184
|
+
counter[p.code] = (counter[p.code] ?? 0) + 1;
|
|
185
|
+
result.push({
|
|
186
|
+
...p,
|
|
187
|
+
id: `${p.code}-${counter[p.code]}`
|
|
188
|
+
});
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
prevPiecesRef.current = result;
|
|
192
|
+
return result;
|
|
193
|
+
}, [fen]);
|
|
194
|
+
}
|
|
195
|
+
//# sourceMappingURL=use-board-pieces.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_react","require","FEN_PIECE_MAP","p","code","color","r","n","b","q","k","P","R","N","B","Q","K","FILES","parseFenPieces","fen","placement","split","ranks","pieces","rankIdx","length","rank","fileIdx","char","parseInt","mapping","square","push","squareToXY","squareSize","orientation","charCodeAt","col","row","x","y","xyToSquare","Math","max","min","floor","files","useBoardPieces","idCounterRef","useRef","prevPiecesRef","useMemo","parsed","prev","current","prevBySquare","Map","map","usedPrevIds","Set","result","unmatched","existing","get","has","id","add","matchedId","prevPiece","counter"],"sourceRoot":"..\\..\\src","sources":["use-board-pieces.ts"],"mappings":";;;;;;;;AAAA,IAAAA,MAAA,GAAAC,OAAA;AAIA,MAAMC,aAAiE,GAAG;EACxEC,CAAC,EAAE;IAAEC,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;EAC7BC,CAAC,EAAE;IAAEF,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;EAC7BE,CAAC,EAAE;IAAEH,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;EAC7BG,CAAC,EAAE;IAAEJ,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;EAC7BI,CAAC,EAAE;IAAEL,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;EAC7BK,CAAC,EAAE;IAAEN,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;EAC7BM,CAAC,EAAE;IAAEP,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;EAC7BO,CAAC,EAAE;IAAER,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;EAC7BQ,CAAC,EAAE;IAAET,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;EAC7BS,CAAC,EAAE;IAAEV,IAAI,EAAE,IAAI;IAAEC,KAAK,EAAE;EAAI,CAAC;E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