-
-
Notifications
You must be signed in to change notification settings - Fork 261
/
state.ts
199 lines (196 loc) · 7.11 KB
/
state.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
import * as fen from './fen.js';
import { AnimCurrent } from './anim.js';
import { DragCurrent } from './drag.js';
import { Drawable } from './draw.js';
import { timer } from './util.js';
import * as cg from './types.js';
export interface HeadlessState {
pieces: cg.Pieces;
orientation: cg.Color; // board orientation. white | black
turnColor: cg.Color; // turn to play. white | black
check?: cg.Key; // square currently in check "a2"
lastMove?: cg.Key[]; // squares part of the last move ["c3"; "c4"]
selected?: cg.Key; // square currently selected "a1"
coordinates: boolean; // include coords attributes
coordinatesOnSquares: boolean; // include coords attributes on every square
ranksPosition: cg.RanksPosition; // position ranks on either side. left | right
autoCastle: boolean; // immediately complete the castle by moving the rook after king move
viewOnly: boolean; // don't bind events: the user will never be able to move pieces around
disableContextMenu: boolean; // because who needs a context menu on a chessboard
addPieceZIndex: boolean; // adds z-index values to pieces (for 3D)
addDimensionsCssVarsTo?: HTMLElement; // add ---cg-width and ---cg-height CSS vars containing the board's dimensions to this element
blockTouchScroll: boolean; // block scrolling via touch dragging on the board, e.g. for coordinate training
pieceKey: boolean; // add a data-key attribute to piece elements
trustAllEvents?: boolean; // disable checking for human only input (e.isTrusted)
highlight: {
lastMove: boolean; // add last-move class to squares
check: boolean; // add check class to squares
custom?: cg.SquareClasses; // add custom classes to custom squares
};
animation: {
enabled: boolean;
duration: number;
current?: AnimCurrent;
};
movable: {
free: boolean; // all moves are valid - board editor
color?: cg.Color | 'both'; // color that can move. white | black | both
dests?: cg.Dests; // valid moves. {"a2" ["a3" "a4"] "b1" ["a3" "c3"]}
showDests: boolean; // whether to add the move-dest class on squares
events: {
after?: (orig: cg.Key, dest: cg.Key, metadata: cg.MoveMetadata) => void; // called after the move has been played
afterNewPiece?: (role: cg.Role, key: cg.Key, metadata: cg.MoveMetadata) => void; // called after a new piece is dropped on the board
};
rookCastle: boolean; // castle by moving the king to the rook
};
premovable: {
enabled: boolean; // allow premoves for color that can not move
showDests: boolean; // whether to add the premove-dest class on squares
castle: boolean; // whether to allow king castle premoves
dests?: cg.Key[]; // premove destinations for the current selection
customDests?: cg.Dests; // use custom valid premoves. {"a2" ["a3" "a4"] "b1" ["a3" "c3"]}
current?: cg.KeyPair; // keys of the current saved premove ["e2" "e4"]
events: {
set?: (orig: cg.Key, dest: cg.Key, metadata?: cg.SetPremoveMetadata) => void; // called after the premove has been set
unset?: () => void; // called after the premove has been unset
};
};
predroppable: {
enabled: boolean; // allow predrops for color that can not move
current?: {
// current saved predrop {role: 'knight'; key: 'e4'}
role: cg.Role;
key: cg.Key;
};
events: {
set?: (role: cg.Role, key: cg.Key) => void; // called after the predrop has been set
unset?: () => void; // called after the predrop has been unset
};
};
draggable: {
enabled: boolean; // allow moves & premoves to use drag'n drop
distance: number; // minimum distance to initiate a drag; in pixels
autoDistance: boolean; // lets chessground set distance to zero when user drags pieces
showGhost: boolean; // show ghost of piece being dragged
deleteOnDropOff: boolean; // delete a piece when it is dropped off the board
current?: DragCurrent;
};
dropmode: {
active: boolean;
piece?: cg.Piece;
};
selectable: {
// disable to enforce dragging over click-click move
enabled: boolean;
};
stats: {
// was last piece dragged or clicked?
// needs default to false for touch
dragged: boolean;
ctrlKey?: boolean;
};
events: {
change?: () => void; // called after the situation changes on the board
// called after a piece has been moved.
// capturedPiece is undefined or like {color: 'white'; 'role': 'queen'}
move?: (orig: cg.Key, dest: cg.Key, capturedPiece?: cg.Piece) => void;
dropNewPiece?: (piece: cg.Piece, key: cg.Key) => void;
select?: (key: cg.Key) => void; // called when a square is selected
insert?: (elements: cg.Elements) => void; // when the board DOM has been (re)inserted
};
drawable: Drawable;
exploding?: cg.Exploding;
hold: cg.Timer;
}
export interface State extends HeadlessState {
dom: cg.Dom;
}
export function defaults(): HeadlessState {
return {
pieces: fen.read(fen.initial),
orientation: 'white',
turnColor: 'white',
coordinates: true,
coordinatesOnSquares: false,
ranksPosition: 'right',
autoCastle: true,
viewOnly: false,
disableContextMenu: false,
addPieceZIndex: false,
blockTouchScroll: false,
pieceKey: false,
trustAllEvents: false,
highlight: {
lastMove: true,
check: true,
},
animation: {
enabled: true,
duration: 200,
},
movable: {
free: true,
color: 'both',
showDests: true,
events: {},
rookCastle: true,
},
premovable: {
enabled: true,
showDests: true,
castle: true,
events: {},
},
predroppable: {
enabled: false,
events: {},
},
draggable: {
enabled: true,
distance: 3,
autoDistance: true,
showGhost: true,
deleteOnDropOff: false,
},
dropmode: {
active: false,
},
selectable: {
enabled: true,
},
stats: {
// on touchscreen, default to "tap-tap" moves
// instead of drag
dragged: !('ontouchstart' in window),
},
events: {},
drawable: {
enabled: true, // can draw
visible: true, // can view
defaultSnapToValidMove: true,
eraseOnClick: true,
shapes: [],
autoShapes: [],
brushes: {
green: { key: 'g', color: '#15781B', opacity: 1, lineWidth: 10 },
red: { key: 'r', color: '#882020', opacity: 1, lineWidth: 10 },
blue: { key: 'b', color: '#003088', opacity: 1, lineWidth: 10 },
yellow: { key: 'y', color: '#e68f00', opacity: 1, lineWidth: 10 },
paleBlue: { key: 'pb', color: '#003088', opacity: 0.4, lineWidth: 15 },
paleGreen: { key: 'pg', color: '#15781B', opacity: 0.4, lineWidth: 15 },
paleRed: { key: 'pr', color: '#882020', opacity: 0.4, lineWidth: 15 },
paleGrey: {
key: 'pgr',
color: '#4a4a4a',
opacity: 0.35,
lineWidth: 15,
},
purple: { key: 'purple', color: '#68217a', opacity: 0.65, lineWidth: 10 },
pink: { key: 'pink', color: '#ee2080', opacity: 0.5, lineWidth: 10 },
white: { key: 'white', color: 'white', opacity: 1, lineWidth: 10 },
},
prevSvgHash: '',
},
hold: timer(),
};
}