-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdraw.ts
161 lines (142 loc) · 4.38 KB
/
draw.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
import { State } from './state';
import { unselect, cancelMove, getKeyAtDomPos, getSnappedKeyAtDomPos } from './board';
import { eventPosition, isRightButton } from './util';
import * as cg from './types';
import { start as abaloneStart, processDraw as abaloneProcessDraw } from './variants/abalone/draw';
export interface DrawShape {
orig: cg.Key;
dest?: cg.Key;
brush: string;
modifiers?: DrawModifiers;
piece?: DrawShapePiece;
customSvg?: string;
}
export interface DrawShapePiece {
role: cg.Role;
playerIndex: cg.PlayerIndex;
scale?: number;
}
export interface DrawBrush {
key: string;
color: string;
opacity: number;
lineWidth: number;
}
export interface DrawBrushes {
[name: string]: DrawBrush;
}
export interface DrawModifiers {
lineWidth?: number;
}
export interface Drawable {
enabled: boolean; // can draw
visible: boolean; // can view
defaultSnapToValidMove: boolean;
eraseOnClick: boolean;
onChange?: (shapes: DrawShape[]) => void;
shapes: DrawShape[]; // user shapes
autoShapes: DrawShape[]; // computer shapes
current?: DrawCurrent;
brushes: DrawBrushes;
// drawable SVG pieces; used for crazyhouse drop
pieces: {
baseUrl: string;
};
prevSvgHash: string;
}
export interface DrawCurrent {
orig: cg.Key; // orig key of drawing
dest?: cg.Key; // shape dest, or undefined for circle
mouseSq?: cg.Key; // square being moused over
pos: cg.NumberPair; // relative current position
brush: string; // brush name for shape
snapToValidMove: boolean; // whether to snap to valid piece moves
}
const brushes = ['green', 'red', 'blue', 'yellow'];
export function start(state: State, e: cg.MouchEvent): void {
if (state.variant === 'abalone') {
return abaloneStart(state, e);
}
// support one finger touch only
if (e.touches && e.touches.length > 1) return;
e.stopPropagation();
e.preventDefault();
e.ctrlKey ? unselect(state) : cancelMove(state);
const pos = eventPosition(e)!,
orig = getKeyAtDomPos(pos, state.orientation, state.dom.bounds(), state.dimensions, state.variant);
if (!orig) return;
state.drawable.current = {
orig,
pos,
brush: eventBrush(e),
snapToValidMove: state.drawable.defaultSnapToValidMove,
};
processDraw(state);
}
export function processDraw(state: State): void {
if (state.variant === 'abalone') {
return abaloneProcessDraw(state);
}
requestAnimationFrame(() => {
const cur = state.drawable.current;
if (cur) {
const keyAtDomPos = getKeyAtDomPos(
cur.pos,
state.orientation,
state.dom.bounds(),
state.dimensions,
state.variant,
);
if (!keyAtDomPos) {
cur.snapToValidMove = false;
}
const mouseSq = cur.snapToValidMove
? getSnappedKeyAtDomPos(cur.orig, cur.pos, state.orientation, state.dom.bounds(), state.dimensions)
: keyAtDomPos;
if (mouseSq !== cur.mouseSq) {
cur.mouseSq = mouseSq;
cur.dest = mouseSq !== cur.orig ? mouseSq : undefined;
state.dom.redrawNow();
}
processDraw(state);
}
});
}
export function move(state: State, e: cg.MouchEvent): void {
if (state.drawable.current) state.drawable.current.pos = eventPosition(e)!;
}
export function end(state: State): void {
const cur = state.drawable.current;
if (cur) {
if (cur.mouseSq) addShape(state.drawable, cur);
cancel(state);
}
}
export function cancel(state: State): void {
if (state.drawable.current) {
state.drawable.current = undefined;
state.dom.redraw();
}
}
export function clear(state: State): void {
if (state.drawable.shapes.length) {
state.drawable.shapes = [];
state.dom.redraw();
onChange(state.drawable);
}
}
export function eventBrush(e: cg.MouchEvent): string {
const modA = (e.shiftKey || e.ctrlKey) && isRightButton(e);
const modB = e.altKey || e.metaKey || e.getModifierState?.('AltGraph');
return brushes[(modA ? 1 : 0) + (modB ? 2 : 0)];
}
function addShape(drawable: Drawable, cur: DrawCurrent): void {
const sameShape = (s: DrawShape) => s.orig === cur.orig && s.dest === cur.dest;
const similar = drawable.shapes.filter(sameShape)[0];
if (similar) drawable.shapes = drawable.shapes.filter(s => !sameShape(s));
if (!similar || similar.brush !== cur.brush) drawable.shapes.push(cur);
onChange(drawable);
}
function onChange(drawable: Drawable): void {
if (drawable.onChange) drawable.onChange(drawable.shapes);
}