This repository was archived by the owner on Feb 18, 2026. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathstate.ts
More file actions
155 lines (153 loc) · 5.03 KB
/
state.ts
File metadata and controls
155 lines (153 loc) · 5.03 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
import * as cg from './interfaces'
import { AnimCurrent } from './anim'
import { DragCurrent } from './drag'
export interface State {
pieces: cg.Pieces
pieceInHand: Piece & { force: boolean } | null
orientation: Color // board orientation. white | black
turnColor: Color // turn to play. white | black
check: Key | null // square currently in check "a2"
lastMove: KeyPair | null // squares part of the last move ["c3", "c4"]
selected: Key | null // square currently selected "a1"
coordinates: boolean // include coords attributes
symmetricCoordinates: boolean // symmetric coords for otb
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
fixed: boolean // board is viewOnly and pieces won't move
exploding: cg.Exploding | null
otb: boolean // is this an otb game?
otbMode: cg.OtbMode
highlight: {
lastMove: boolean // add last-move class to squares
check: boolean // add check class to squares
}
animation: {
enabled: boolean
duration: number
current: AnimCurrent | null
}
movable: {
free: boolean // all moves are valid - board editor
color: Color | 'both' | null // color that can move.
dests: DestsMap | null // valid moves. {"a2" ["a3" "a4"] "b1" ["a3" "c3"]}
showDests: boolean // whether to add the move-dest class on squares
dropped: KeyPair | null // last dropped [orig, dest], not to be animated
events: {
after?: (orig: Key, dest: Key, metadata: cg.MoveMetadata) => void // called after the move has been played
afterNewPiece?: (role: Role, key: Key, metadata: cg.MoveMetadata) => void // called after a new piece is dropped on the board
}
rookCastle?: boolean
}
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
current: KeyPair | null // keys of the current saved premove ["e2" "e4"]
dests: readonly Key[] | null // premove destinations for the current selection
events: {
set?: (orig: Key, dest: 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: cg.Drop | null // current saved predrop {role: 'knight'; key: 'e4'}
events: {
set?: (role: Role, key: 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
magnified: boolean // whether dragging piece is magnified
centerPiece: boolean // when magnified, center the piece under finger (otherwise shifted up)
preventDefault: boolean // whether to prevent default on move and end
showGhost: boolean // show ghost of piece being dragged
deleteOnDropOff: boolean // delete a piece when it is dropped off the board
current: DragCurrent | null
}
selectable: {
// disable to enforce dragging over click-click move
enabled: 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: Key, dest: Key, capturedPiece?: Piece) => void
dropNewPiece?: (piece: Piece, key: Key) => void
select?: (key: Key) => void
}
prev: cg.PrevData
}
export function makeDefaults(): State {
return {
pieces: new Map(),
pieceInHand: null,
orientation: 'white' as Color,
turnColor: 'white' as Color,
check: null,
lastMove: null,
selected: null,
coordinates: true,
symmetricCoordinates: false,
otb: false,
otbMode: 'facing' as cg.OtbMode,
autoCastle: true,
viewOnly: false,
fixed: false,
exploding: null,
highlight: {
lastMove: true,
check: true
},
animation: {
enabled: true,
duration: 200,
current: null
},
movable: {
free: true,
color: 'both' as Color | 'both',
dests: null,
dropped: null,
showDests: true,
rookCastle: true,
events: {}
},
premovable: {
enabled: true,
showDests: true,
castle: true,
dests: null,
current: null,
events: {}
},
predroppable: {
enabled: false,
current: null,
events: {}
},
draggable: {
enabled: true,
distance: 3,
magnified: true,
centerPiece: false,
preventDefault: true,
showGhost: true,
deleteOnDropOff: false,
current: null
},
selectable: {
enabled: true
},
events: {},
prev: {
orientation: null,
bounds: null,
turnColor: null,
otbMode: null
}
}
}