-
Notifications
You must be signed in to change notification settings - Fork 14
/
eventHandler.ts
299 lines (275 loc) · 9.01 KB
/
eventHandler.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
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
// [Smoosic](https://github.com/AaronDavidNewman/Smoosic)
// Copyright (c) Aaron David Newman 2021.
import { KeyEvent } from '../smo/data/common';
import { SuiExceptionHandler } from '../ui/exceptions';
import { Qwerty } from '../ui/qwerty';
import { SuiModifierDialogFactory } from '../ui/dialogs/factory';
import { SuiPiano } from '../render/sui/piano'
import { SuiHelp } from '../ui/help';
import { CompleteNotifier, ModalComponent } from '../ui/common';
import { SuiTracker } from '../render/sui/tracker';
import { defaultEditorKeys } from '../ui/keyBindings/default/editorKeys';
import { defaultTrackerKeys } from '../ui/keyBindings/default/trackerKeys';
import { SuiScoreViewOperations } from '../render/sui/scoreViewOperations';
import { BrowserEventSource, EventHandler } from '../ui/eventSource';
import { SuiKeyCommands } from './keyCommands';
import { KeyBinding, ModalEventHandler } from './common';
import { ModifierTab } from '../smo/xform/selections';
import { SvgHelpers } from '../render/sui/svgHelpers';
import { SuiMenuManager } from '../ui/menus/manager';
import { SmoConfiguration } from './configuration';
declare var $: any;
/**
* Handle keyboard/mouse events, and pass them to the renderer and other UI elements.
*/
export interface EventHandlerParams {
view: SuiScoreViewOperations,
eventSource: BrowserEventSource,
tracker: SuiTracker,
keyCommands: SuiKeyCommands,
menus: SuiMenuManager,
completeNotifier: CompleteNotifier,
keyBindings: KeyBinding[],
config: SmoConfiguration
}
/**
* this is the default keyboard/mouse handler for smoosic in application mode.
* It diverts key events to tracker or key commmands as appropriate, and mouse events to
* tracker. Modal elements take this control away temporarily.
*
* It also handles some global events such as window resize and scroll of the music region.
* @category SuiApplication
*/
export class SuiEventHandler implements ModalEventHandler {
static reentry: boolean = false;
static keyboardUi: Qwerty;
static showQwerty() {
SuiEventHandler.keyboardUi = Qwerty;
Qwerty.displayKb();
}
view: SuiScoreViewOperations;
eventSource: BrowserEventSource;
tracker: SuiTracker;
keyBind: KeyBinding[];
completeNotifier: CompleteNotifier;
keyCommands: SuiKeyCommands;
resizing: boolean = false;
undoStatus: number = 0;
trackScrolling: boolean = false;
config: SmoConfiguration;
keyHandlerObj: any = null;
menus: SuiMenuManager;
piano: SuiPiano | null = null;
exhandler: SuiExceptionHandler;
constructor(params: EventHandlerParams) {
(globalThis as any).SuiEventHandlerInstance = this;
this.view = params.view;
this.config = params.config;
this.menus = params.menus;
this.completeNotifier = params.completeNotifier;
this.eventSource = params.eventSource;
this.tracker = params.tracker; // needed for key event handling
this.keyBind = params.keyBindings;
this.keyCommands = params.keyCommands;
this.keyCommands.view = this.view;
this.resizing = false;
this.undoStatus = 0;
this.trackScrolling = false;
this.keyHandlerObj = null;
// create global exception instance
this.exhandler = new SuiExceptionHandler(this);
this.bindEvents();
this.bindResize();
this.createPiano();
}
static get scrollable() {
return '.musicRelief';
}
handleScrollEvent() {
const self = this;
if (self.trackScrolling) {
return;
}
self.trackScrolling = true;
setTimeout(function () {
try {
// wait until redraw is done to track scroll events.
self.trackScrolling = false;
// Thisi s a WIP...
self.view.tracker.scroller.handleScroll($(SuiEventHandler.scrollable)[0].scrollLeft, $(SuiEventHandler.scrollable)[0].scrollTop);
} catch (e) {
SuiExceptionHandler.instance.exceptionHandler(e);
}
}, 500);
}
createPiano() {
this.piano = new SuiPiano(this.view);
}
resizeEvent() {
var self = this;
if (this.resizing) {
return;
}
if (!this.piano) {
return;
}
if ($('body').hasClass('printing')) {
return;
}
this.resizing = true;
setTimeout(function () {
console.log('resizing');
self.resizing = false;
self.piano!.handleResize();
self.view.refreshViewport();
}, 1);
}
createModifierDialog(modifierSelection: ModifierTab) {
var parameters = {
modifier: modifierSelection.modifier,
view: this.view, eventSource: this.eventSource,
completeNotifier: this.completeNotifier, keyCommands: this.keyCommands,
ctor: '', // filled in by the factory
tracker: this.tracker,
startPromise: null,
id: 'modifier-dialog',
undoBuffer: this.view.undoBuffer,
config: this.config
}
return SuiModifierDialogFactory.createModifierDialog(modifierSelection.modifier, parameters);
}
// If the user has selected a modifier via the mouse/touch, bring up mod dialog
// for that modifier
trackerModifierSelect(ev: KeyEvent) {
var modSelection = this.view.tracker.getSelectedModifier();
if (modSelection) {
var dialog = this.createModifierDialog(modSelection);
if (dialog) {
// this.view.tracker.selectSuggestion(ev);
return;
// this.unbindKeyboardForModal(dialog);
} else {
this.view.tracker.advanceModifierSelection(this.view.score, ev);
}
} else {
this.view.tracker.selectSuggestion(this.view.score, ev);
}
return;
}
// ### bindResize
// This handles both resizing of the music area (scrolling) and resizing of the window.
// The latter results in a redraw, the former just resets the client/logical map of elements
// in the tracker.
bindResize() {
const self = this;
const el = $(SuiEventHandler.scrollable)[0];
// unit test programs don't have resize html
if (!el) {
return;
}
window.addEventListener('resize', function () {
self.resizeEvent();
});
let scrollCallback = () => {
self.handleScrollEvent();
};
el.onscroll = scrollCallback;
}
// ### renderElement
// return render element that is the DOM parent of the svg
get renderElement() {
return this.view.renderer.renderElement;
}
// ## editorKeyBindingDefaults
// ## Description:
// execute a simple command on the editor, based on a keystroke.
static get editorKeyBindingDefaults() {
return defaultEditorKeys.keys;
}
// ## trackerKeyBindingDefaults
// ### Description:
// Key bindings for the tracker. The tracker is the 'cursor' in the music
// that lets you select and edit notes.
static get trackerKeyBindingDefaults() {
return defaultTrackerKeys.keys;
}
helpControls() {
var self = this;
var rebind = function () {
self.bindEvents();
}
}
menuHelp() {
SuiHelp.displayHelp();
}
evKey(evdata: any) {
if ($('body').hasClass('translation-mode')) {
return;
}
console.log("KeyboardEvent: key='" + evdata.key + "' | code='" +
evdata.code + "'"
+ " shift='" + evdata.shiftKey + "' control='" + evdata.ctrlKey + "'" + " alt='" + evdata.altKey + "'");
evdata.preventDefault();
if (SuiEventHandler.keyboardUi) {
Qwerty.handleKeyEvent(evdata);
}
const dataCopy = SuiTracker.serializeEvent(evdata);
this.view.renderer.updatePromise().then(() => {
if (dataCopy.key == '?') {
SuiHelp.displayHelp();
}
if (dataCopy.key == '/') {
// set up menu DOM.
this.menus.slashMenuMode(this.completeNotifier);
}
if (dataCopy.key == 'Enter') {
this.trackerModifierSelect(dataCopy);
}
var binding: KeyBinding | undefined = this.keyBind.find((ev: KeyBinding) =>
ev.event === 'keydown' && ev.key === dataCopy.key &&
ev.ctrlKey === dataCopy.ctrlKey &&
ev.altKey === dataCopy.altKey && dataCopy.shiftKey === ev.shiftKey);
if (binding) {
try {
if (binding.module === 'tracker') {
(this.tracker as any)[binding.action](this.view.score, dataCopy);
} else {
(this.keyCommands as any)[binding.action](dataCopy);
}
} catch (e) {
if (typeof (e) === 'string') {
console.error(e);
}
this.exhandler.exceptionHandler(e);
}
}
});
}
mouseMove(ev: any) {
this.view.tracker.intersectingArtifact(SvgHelpers.smoBox({
x: ev.clientX,
y: ev.clientY
}));
}
mouseClick(ev: any) {
const dataCopy = SuiTracker.serializeEvent(ev);
this.view.renderer.updatePromise().then(() => {
this.view.tracker.selectSuggestion(this.view.score, dataCopy);
var modifier = this.view.tracker.getSelectedModifier();
if (modifier) {
this.createModifierDialog(modifier);
}
});
}
bindEvents() {
const self = this;
const tracker = this.view.tracker;
$('body').off('forceScrollEvent').on('forceScrollEvent', function () {
self.handleScrollEvent();
});
$('body').off('forceResizeEvent').on('forceResizeEvent', function () {
self.resizeEvent();
});
this.helpControls();
}
}