comparison js/core.js @ 2955:8fa0f2a59b6d

Implemented #243
author Nicholas Jillings <nicholas.jillings@mail.bcu.ac.uk>
date Tue, 31 Oct 2017 11:26:43 +0000
parents 7d1a34b5a4d2
children 31a7a34bad6b 824c8539fd02
comparison
equal deleted inserted replaced
2954:7b2229be3183 2955:8fa0f2a59b6d
1843 }); 1843 });
1844 this.audioObjects = []; 1844 this.audioObjects = [];
1845 this.timer = new timer(); 1845 this.timer = new timer();
1846 this.loopPlayback = audioHolderObject.loop; 1846 this.loopPlayback = audioHolderObject.loop;
1847 this.synchPlayback = audioHolderObject.synchronous; 1847 this.synchPlayback = audioHolderObject.synchronous;
1848 interfaceContext.keyboardInterface.resetKeyBindings();
1848 }; 1849 };
1849 1850
1850 this.checkAllPlayed = function () { 1851 this.checkAllPlayed = function () {
1851 var arr = []; 1852 var arr = [];
1852 for (var id = 0; id < this.audioObjects.length; id++) { 1853 for (var id = 0; id < this.audioObjects.length; id++) {
1983 } else if (this.state == -1) { 1984 } else if (this.state == -1) {
1984 // ERROR 1985 // ERROR
1985 this.interfaceDOM.error(); 1986 this.interfaceDOM.error();
1986 return; 1987 return;
1987 } 1988 }
1988 this.storeDOM.setAttribute('presentedId', interfaceObject.getPresentedId()); 1989 var presentedId = interfaceObject.getPresentedId();
1990 this.storeDOM.setAttribute('presentedId', presentedId);
1991
1992 // Key-bindings
1993 if (presentedId.length == 1) {
1994 interfaceContext.keyboardInterface.registerKeyBinding(presentedId, this);
1995 }
1989 }; 1996 };
1990 1997
1991 this.listenStart = function (setTime) { 1998 this.listenStart = function (setTime) {
1992 if (this.playing === false) { 1999 if (this.playing === false) {
1993 playCounter++; 2000 playCounter++;
2363 audioEngineContext.newTestPage(audioHolderObject, store); 2370 audioEngineContext.newTestPage(audioHolderObject, store);
2364 interfaceContext.commentBoxes.deleteCommentBoxes(); 2371 interfaceContext.commentBoxes.deleteCommentBoxes();
2365 interfaceContext.deleteCommentQuestions(); 2372 interfaceContext.deleteCommentQuestions();
2366 loadTest(audioHolderObject, store); 2373 loadTest(audioHolderObject, store);
2367 }; 2374 };
2375
2376 this.keyboardInterface = (function () {
2377 var keyboardInterfaceController = {
2378 keys: [],
2379 registerKeyBinding: function (key, audioObject) {
2380 if (typeof key != "string" || key.length != 1) {
2381 throw ("Key must be a singular character");
2382 }
2383 var included = this.keys.findIndex(function (k) {
2384 return k.key == key
2385 }) >= 0;
2386 if (included) {
2387 throw ("Key " + key + " already bounded!");
2388 }
2389 this.keys.push({
2390 key: key,
2391 audioObject: audioObject
2392 });
2393 return true;
2394 },
2395 deregisterKeyBinding: function (key) {
2396 var index = this.keys.findIndex(function (k) {
2397 return k.key == key;
2398 });
2399 if (index == -1) {
2400 throw ("Key " + key + " not bounded!");
2401 }
2402 this.keys.splice(index, 1);
2403 return true;
2404 },
2405 resetKeyBindings: function () {
2406 this.keys = [];
2407 },
2408 handleEvent: function (e) {
2409 function isPlaying() {
2410 return audioEngineContext.audioObjects.some(function (a) {
2411 return a.playing;
2412 });
2413 }
2414
2415 function keypress(key) {
2416 var index = this.keys.findIndex(function (k) {
2417 return k.key == key
2418 });
2419 if (index >= 0) {
2420 audioEngineContext.play(this.keys[index].audioObject.id);
2421 }
2422 }
2423 if (e.key === " ") {
2424 if (isPlaying()) {
2425 audioEngineContext.stop();
2426 }
2427 } else {
2428 keypress.call(this, e.key);
2429 }
2430 }
2431 };
2432 document.addEventListener("keydown", keyboardInterfaceController, false);
2433 return keyboardInterfaceController;
2434 })();
2368 2435
2369 // Bounded by interface!! 2436 // Bounded by interface!!
2370 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels 2437 // Interface object MUST have an exportXMLDOM method which returns the various DOM levels
2371 // For example, APE returns the slider position normalised in a <value> tag. 2438 // For example, APE returns the slider position normalised in a <value> tag.
2372 this.interfaceObjects = []; 2439 this.interfaceObjects = [];