-
Notifications
You must be signed in to change notification settings - Fork 628
Expand file tree
/
Copy pathkeyboardShortcuts.ts
More file actions
419 lines (351 loc) · 12.7 KB
/
keyboardShortcuts.ts
File metadata and controls
419 lines (351 loc) · 12.7 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
import { Store } from 'redux';
import { ImageEditorTool, ImageEditorStore, TilemapState, AnimationState, CursorSize } from './store/imageReducer';
import { dispatchChangeZoom, dispatchUndoImageEdit, dispatchRedoImageEdit, dispatchChangeImageTool, dispatchSwapBackgroundForeground, dispatchChangeSelectedColor, dispatchImageEdit, dispatchChangeCursorSize, dispatchChangeCurrentFrame, dispatchSetFrames} from './actions/dispatch';
import { mainStore } from './store/imageStore';
import { EditState, flipEdit, getEditState, outlineEdit, replaceColorEdit, rotateEdit } from './toolDefinitions';
let store = mainStore;
let lockRefs: number[] = [];
export function addKeyListener() {
lockRefs = [];
document.addEventListener("keydown", handleKeyDown);
document.addEventListener("keydown", handleUndoRedo, true);
document.addEventListener("keydown", overrideBlocklyShortcuts, true);
}
export function removeKeyListener() {
document.removeEventListener("keydown", handleKeyDown);
document.removeEventListener("keydown", handleUndoRedo, true);
document.removeEventListener("keydown", overrideBlocklyShortcuts, true);
}
// Disables shortcuts and returns a ref. Enable by passing the ref to release shortcut lock
export function obtainShortcutLock(): number {
let ref = 0;
while (!ref) ref = Math.random() * Number.MAX_SAFE_INTEGER
lockRefs.push(ref);
return ref;
}
// Enables shortcuts using the ref obtained from obtainShortcutLock
export function releaseShortcutLock(ref: number) {
const index = lockRefs.indexOf(ref)
if (index !== -1) {
lockRefs.splice(index, 1);
}
}
export function areShortcutsEnabled() {
return !lockRefs.length;
}
export function setStore(newStore?: Store<ImageEditorStore>) {
store = newStore || mainStore;
}
function handleUndoRedo(event: KeyboardEvent) {
const controlOrMeta = event.ctrlKey || event.metaKey; // ctrl on windows, meta on mac
if (event.key === "Undo" || (controlOrMeta && event.key === "z" && !event.shiftKey)) {
undo();
event.preventDefault();
event.stopPropagation();
} else if (event.key === "Redo" || (controlOrMeta && event.key === "y") || (controlOrMeta && event.key === "Z" && event.shiftKey)) {
redo();
event.preventDefault();
event.stopPropagation();
}
}
function overrideBlocklyShortcuts(event: KeyboardEvent) {
if (event.key === "Backspace" || event.key === "Delete") {
handleKeyDown(event);
event.stopPropagation();
}
}
function handleKeyDown(event: KeyboardEvent) {
if (!areShortcutsEnabled()) return;
if (event.shiftKey && /^(?:Digit[1-9])|(?:Key[A-F])$/.test(event.code)) {
if (event.code.indexOf("Digit") == 0) outline(parseInt(event.code.substring(5)))
else outline(parseInt(event.code.substring(3), 16));
return;
}
// Mostly copied from the photoshop shortcuts
switch (event.key) {
case "e":
setTool(ImageEditorTool.Erase);
break;
case "q":
setTool(ImageEditorTool.Pan);
break;
case "b":
case "p":
setTool(ImageEditorTool.Paint);
break;
case "g":
setTool(ImageEditorTool.Fill);
break;
case "m":
setTool(ImageEditorTool.Marquee);
break;
case "u":
setTool(ImageEditorTool.Rect);
break;
case "l":
setTool(ImageEditorTool.Line);
break;
case "c":
setTool(ImageEditorTool.Circle);
break;
case "-":
case "_":
zoom(-1);
break;
case "=":
case "+":
zoom(1);
break;
case "x":
swapForegroundBackground();
break;
case "h":
flip(false);
break;
case "v":
flip(true);
break;
case "H":
flipAllFrames(false);
break;
case "V":
flipAllFrames(true);
break;
case "[":
rotate(false);
break;
case "]":
rotate(true);
break;
case "{":
rotateAllFrames(false);
break;
case "}":
rotateAllFrames(true);
break;
case ">":
changeCursorSize(true);
break;
case "<":
changeCursorSize(false);
break;
case ".":
advanceFrame(true);
break;
case ",":
advanceFrame(false);
break;
case "r":
doColorReplace();
break;
case "R":
doColorReplaceAllFrames();
break;
case "ArrowLeft":
moveMarqueeSelection(-1, 0, event.shiftKey);
break;
case "ArrowRight":
moveMarqueeSelection(1, 0, event.shiftKey);
break;
case "ArrowUp":
moveMarqueeSelection(0, -1, event.shiftKey);
break;
case "ArrowDown":
moveMarqueeSelection(0, 1, event.shiftKey);
break;
case "Backspace":
case "Delete":
deleteSelection(event.shiftKey);
break;
}
const editorState = store.getState().editor;
if (!editorState.isTilemap && /^Digit\d$/.test(event.code)) {
const keyAsNum = +event.code.slice(-1);
const color = keyAsNum + (event.shiftKey ? 9 : 0);
// TODO: if we need to generalize for different numbers of colors,
// will need to fix the magic 16 here
if (color >= 0 && color < 16)
setColor(color);
}
}
function currentEditState(): [EditState, "tilemap" | "animation" | "image"] {
const state = store.getState();
if (state.editor.isTilemap) {
const tilemapState = state.store.present as TilemapState;
return [getEditState(tilemapState.tilemap, true, state.editor.drawingMode), "tilemap"]
}
else {
const animationState = state.store.present as AnimationState;
return [getEditState(animationState.frames[animationState.currentFrame], false, state.editor.drawingMode), animationState.frames.length > 1 ? "animation" : "image" ]
}
}
function undo() {
dispatchAction(dispatchUndoImageEdit());
}
function redo() {
dispatchAction(dispatchRedoImageEdit());
}
function setTool(tool: ImageEditorTool) {
dispatchAction(dispatchChangeImageTool(tool));
}
function setColor(selectedColor: number) {
dispatchAction(dispatchChangeSelectedColor(selectedColor))
}
function zoom(delta: number) {
dispatchAction(dispatchChangeZoom(delta));
}
function swapForegroundBackground() {
dispatchAction(dispatchSwapBackgroundForeground());
}
function dispatchAction(action: any) {
store.dispatch(action);
}
function changeCursorSize(larger: boolean) {
let nextSize: CursorSize;
const currentSize = store.getState().editor.cursorSize;
switch (currentSize) {
case CursorSize.One:
nextSize = larger ? CursorSize.Three : CursorSize.One;
break;
case CursorSize.Three:
nextSize = larger ? CursorSize.Five : CursorSize.One;
break;
case CursorSize.Five:
nextSize = larger ? CursorSize.Five : CursorSize.Three;
}
if (currentSize !== nextSize) {
dispatchAction(dispatchChangeCursorSize(nextSize));
}
}
export function flip(vertical: boolean) {
const [ editState, type ] = currentEditState();
const flipped = flipEdit(editState, vertical, type === "tilemap");
dispatchAction(dispatchImageEdit(flipped.toImageState()));
}
export function flipAllFrames(vertical: boolean) {
editAllFrames(() => flip(vertical), editState => flipEdit(editState, vertical, false));
}
export function rotate(clockwise: boolean) {
const [ editState, type ] = currentEditState();
const rotated = rotateEdit(editState, clockwise, type === "tilemap", type === "animation");
dispatchAction(dispatchImageEdit(rotated.toImageState()));
}
export function rotateAllFrames(clockwise: boolean) {
editAllFrames(() => rotate(clockwise), editState => rotateEdit(editState, clockwise, false, true));
}
export function outline(color: number) {
const [ editState, type ] = currentEditState();
if (type === "tilemap") return;
const outlined = outlineEdit(editState, color);
dispatchAction(dispatchImageEdit(outlined.toImageState()));
}
export function replaceColor(fromColor: number, toColor: number) {
const [ editState, type ] = currentEditState();
const replaced = replaceColorEdit(editState, fromColor, toColor);
dispatchAction(dispatchImageEdit(replaced.toImageState()));
}
function doColorReplace() {
const state = store.getState();
const fromColor = state.editor.backgroundColor;
const toColor = state.editor.selectedColor;
const [ editState ] = currentEditState();
const replaced = replaceColorEdit(editState, fromColor, toColor);
dispatchAction(dispatchImageEdit(replaced.toImageState()));
}
function doColorReplaceAllFrames() {
const state = store.getState();
const fromColor = state.editor.backgroundColor;
const toColor = state.editor.selectedColor;
editAllFrames(doColorReplace, editState => replaceColorEdit(editState, fromColor, toColor))
}
export function advanceFrame(forwards: boolean) {
const state = store.getState();
if (state.editor.isTilemap) return;
const present = state.store.present as AnimationState;
if (present.frames.length <= 1) return;
let nextFrame: number;
if (forwards) {
nextFrame = (present.currentFrame + 1) % present.frames.length;
}
else {
nextFrame = (present.currentFrame + present.frames.length - 1) % present.frames.length;
}
dispatchAction(dispatchChangeCurrentFrame(nextFrame));
}
function editAllFrames(singleFrameShortcut: () => void, doEdit: (editState: EditState) => EditState) {
const state = store.getState();
if (state.editor.isTilemap) {
singleFrameShortcut();
return;
}
const present = state.store.present as AnimationState;
if (present.frames.length === 1) {
singleFrameShortcut();
return;
}
const current = present.frames[present.currentFrame];
// if the current frame has a marquee selection, apply that selection
// to all frames
const hasFloatingLayer = !!current.floating;
const layerWidth = current.floating?.bitmap.width;
const layerHeight = current.floating?.bitmap.height;
const layerX = current.layerOffsetX;
const layerY = current.layerOffsetY;
const newFrames: pxt.sprite.ImageState[] = [];
for (const frame of present.frames) {
const editState = getEditState(frame, false);
if (hasFloatingLayer) {
if (editState.floating?.image) {
// check the existing floating layer to see if it matches before
// merging down. otherwise non-square rotations might lose
// information if they cause the floating layer to go off the canvas
if (editState.layerOffsetX !== layerX ||
editState.layerOffsetY !== layerY ||
editState.floating.image.width !== layerWidth ||
editState.floating.image.height !== layerHeight
) {
editState.mergeFloatingLayer();
editState.copyToLayer(layerX, layerY, layerWidth, layerHeight, true);
}
}
else {
editState.copyToLayer(layerX, layerY, layerWidth, layerHeight, true);
}
}
else {
editState.mergeFloatingLayer();
}
const edited = doEdit(editState);
newFrames.push(edited.toImageState());
}
dispatchAction(dispatchSetFrames(newFrames, present.currentFrame));
}
function moveMarqueeSelection(dx: number, dy: number, allFrames = false) {
const [ editState ] = currentEditState();
if (!editState.floating?.image) return;
const moveState = (editState: EditState) => {
editState.layerOffsetX += dx;
editState.layerOffsetY += dy;
return editState;
};
if (!allFrames) {
dispatchAction(dispatchImageEdit(moveState(editState).toImageState()));
}
else {
editAllFrames(() => moveMarqueeSelection(dx, dy), moveState);
}
}
function deleteSelection(allFrames = false) {
const [ editState ] = currentEditState();
if (!editState.floating?.image) return;
const deleteFloatingLayer = (editState: EditState) => {
editState.floating = null;
return editState;
};
if (!allFrames) {
dispatchAction(dispatchImageEdit(deleteFloatingLayer(editState).toImageState()));
}
else {
editAllFrames(() => deleteSelection(), deleteFloatingLayer);
}
}