diff options
Diffstat (limited to 'objects.js')
| -rw-r--r-- | objects.js | 234 |
1 files changed, 219 insertions, 15 deletions
@@ -459,6 +459,23 @@ SpriteMorph.prototype.initBlocks = function () { category: 'sound', spec: 'stop all sounds' }, + doSetVolume: { + type: 'command', + category: 'sound', + spec: 'set volume to %n %', + defaults: [100] + }, + doChangeVolume: { + type: 'command', + category: 'sound', + spec: 'change volume by %n', + defaults: [-10] + }, + reportVolume: { + type: 'reporter', + category: 'sound', + spec: 'volume' + }, doRest: { type: 'command', category: 'sound', @@ -569,6 +586,16 @@ SpriteMorph.prototype.initBlocks = function () { category: 'pen', spec: 'stamp' }, + doStreamCamera: { + type: 'command', + category: 'pen', + spec: 'start streaming from the camera' + }, + doStopCamera: { + type: 'command', + category: 'pen', + spec: 'stop streaming from the camera' + }, // Control receiveGo: { @@ -775,6 +802,23 @@ SpriteMorph.prototype.initBlocks = function () { category: 'sensing', spec: 'color %clr is touching %clr ?' }, + reportCameraMotion: { + only: SpriteMorph, + type: 'predicate', + category: 'sensing', + spec: 'camera motion at my position?' + }, + reportCameraDirection: { + only: SpriteMorph, + type: 'reporter', + category: 'sensing', + spec: 'camera motion direction' + }, + reportStreamingCamera: { + type: 'predicate', + category: 'sensing', + spec: 'streaming from the camera?' + }, colorFiltered: { dev: true, type: 'reporter', @@ -836,6 +880,11 @@ SpriteMorph.prototype.initBlocks = function () { category: 'sensing', spec: 'key %key pressed?' }, + getKeysPressed: { + type: 'reporter', + category: 'sensing', + spec: 'keys pressed' + }, reportDistanceTo: { type: 'reporter', category: 'sensing', @@ -884,6 +933,16 @@ SpriteMorph.prototype.initBlocks = function () { category: 'sensing', spec: 'current %dates' }, + reportLanguage: { + type: 'reporter', + category: 'sensing', + spec: 'language' + }, + reportLocation: { + type: 'reporter', + category: 'sensing', + spec: 'location %locations' + }, // Operators reifyScript: { @@ -1309,6 +1368,8 @@ SpriteMorph.prototype.init = function (globals) { this.version = Date.now(); // for observer optimization this.isClone = false; // indicate a "temporary" Scratch-style clone this.cloneOriginName = ''; + this.volume = 100; + this.activeSounds = []; // sprite nesting properties this.parts = []; // not serialized, only anchor (name) @@ -1411,8 +1472,11 @@ SpriteMorph.prototype.appearIn = function (ide) { // SpriteMorph versioning SpriteMorph.prototype.setName = function (string) { - this.name = string || this.name; - this.version = Date.now(); + if (string != 'mouse-pointer' && string != 'pen trails' + && string != 'edge') { // used by system + this.name = string || this.name; + this.version = Date.now(); + } }; // SpriteMorph rendering @@ -1782,6 +1846,11 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('doPlaySoundUntilDone')); blocks.push(block('doStopAllSounds')); blocks.push('-'); + blocks.push(block('doSetVolume')); + blocks.push(block('doChangeVolume')); + blocks.push(watcherToggle('reportVolume')); + blocks.push(block('reportVolume')); + blocks.push('-'); blocks.push(block('doRest')); blocks.push('-'); blocks.push(block('doPlayNote')); @@ -1885,6 +1954,10 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('reportTouchingColor')); blocks.push(block('reportColorIsTouchingColor')); blocks.push('-'); + blocks.push(block('reportCameraMotion')); + blocks.push(block('reportCameraDirection')); + blocks.push(block('reportStreamingCamera')); + blocks.push('-'); blocks.push(block('doAsk')); blocks.push(watcherToggle('getLastAnswer')); blocks.push(block('getLastAnswer')); @@ -1896,6 +1969,7 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('reportMouseDown')); blocks.push('-'); blocks.push(block('reportKeyPressed')); + blocks.push(block('getKeysPressed')); blocks.push('-'); blocks.push(block('reportDistanceTo')); blocks.push('-'); @@ -1911,6 +1985,9 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('doSetFastTracking')); blocks.push('-'); blocks.push(block('reportDate')); + blocks.push('-'); + blocks.push(block('reportLanguage')); + blocks.push(block('reportLocation')); // for debugging: /////////////// @@ -2603,7 +2680,8 @@ SpriteMorph.prototype.reportCostumes = function () { // SpriteMorph sound management SpriteMorph.prototype.addSound = function (audio, name) { - this.sounds.add(new Sound(audio, name)); + var volume = this.volume; + this.sounds.add(new Sound(audio, name, volume)); }; SpriteMorph.prototype.playSound = function (name) { @@ -2614,7 +2692,18 @@ SpriteMorph.prototype.playSound = function (name) { ), active; if (sound) { + sound.volume = this.volume; active = sound.play(); + + if (stage.muted === true) { + active.volume = 0; + } + + this.activeSounds.push(active); + this.activeSounds = this.activeSounds.filter(function (aud) { + return !aud.ended && !aud.terminated; + }); + if (stage) { stage.activeSounds.push(active); stage.activeSounds = stage.activeSounds.filter(function (aud) { @@ -2625,6 +2714,34 @@ SpriteMorph.prototype.playSound = function (name) { } }; +SpriteMorph.prototype.doSetVolume = function (val) { + var myself = this; + myself.volume = Math.min(Math.max(0, val), 100); + + if (myself.parentThatIsA(StageMorph).muted === true) { + return; + } + + myself.activeSounds.forEach(function (snd) { + snd.volume = myself.volume / 100; // 'audio' objects + }); +}; + +SpriteMorph.prototype.doChangeVolume = function (val) { + this.doSetVolume(this.volume + val); +}; + +SpriteMorph.prototype.reportVolume = function () { + return this.volume; +} + +SpriteMorph.prototype.unmuteAllSounds = function () { + var stage = this.parentThatIsA(StageMorph); + stage.muted = false; + + this.doSetVolume(this.volume); +}; + SpriteMorph.prototype.reportSounds = function () { return this.sounds; }; @@ -3274,9 +3391,7 @@ Morph.prototype.setPosition = function (aPoint, justMe) { // override the inherited default to make sure my parts follow // unless it's justMe var delta = aPoint.subtract(this.topLeft()); - if ((delta.x !== 0) || (delta.y !== 0)) { - this.moveBy(delta, justMe); - } + this.moveBy(delta, justMe); }; SpriteMorph.prototype.forward = function (steps) { @@ -3509,7 +3624,19 @@ SpriteMorph.prototype.allHatBlocksForKey = function (key) { return this.scripts.children.filter(function (morph) { if (morph.selector) { if (morph.selector === 'receiveKey') { - return morph.inputs()[0].evaluate()[0] === key; + var selectedOption = morph.inputs()[0].evaluate()[0]; + + if (selectedOption === 'any key') { + return true; + } + if (selectedOption === 'number key' && + (key >= '0' && key <= '9')) { + return true; + } + if (selectedOption === key) { + return true; + } + return false; } } return false; @@ -3553,6 +3680,16 @@ SpriteMorph.prototype.getTempo = function () { return 0; }; +// SpriteMorph last key + +SpriteMorph.prototype.getKeysPressed = function () { + var stage = this.parentThatIsA(StageMorph); + if (stage) { + return stage.getKeysPressed(); + } + return ''; +}; + // SpriteMorph last message SpriteMorph.prototype.getLastMessage = function () { @@ -4280,6 +4417,8 @@ StageMorph.prototype.init = function (globals) { this.version = Date.now(); // for observers this.isFastTracked = false; this.cloneCount = 0; + this.volume = 100; + this.muted = false; this.timerStart = Date.now(); this.tempo = 60; // bpm @@ -4295,6 +4434,9 @@ StageMorph.prototype.init = function (globals) { this.paletteCache = {}; // not to be serialized (!) this.lastAnswer = ''; // last user input, do not persist this.activeSounds = []; // do not persist + this.streamingCamera = false; + this.lastCameraCanvas = null; + this.lastCameraMotion = new Point(0, 0); this.trailsCanvas = null; this.isThreadSafe = false; @@ -4563,6 +4705,16 @@ StageMorph.prototype.getTempo = function () { return +this.tempo; }; +// StageMorph keys + +StageMorph.prototype.getKeysPressed = function () { + var keys = []; + for (var key in this.keysPressed) { + keys.push(key); + } + return new List(keys); +}; + // StageMorph messages StageMorph.prototype.getLastMessage = function () { @@ -4616,7 +4768,7 @@ StageMorph.prototype.step = function () { world.keyboardReceiver = this; } if (world.currentKey === null) { - this.keyPressed = null; + this.keysPressed = {}; } // manage threads @@ -4804,7 +4956,7 @@ StageMorph.prototype.fireGreenFlagEvent = function () { StageMorph.prototype.fireStopAllEvent = function () { var ide = this.parentThatIsA(IDE_Morph); - this.threads.resumeAll(this.stage); + //this.threads.resumeAll(this.stage); // leads to a strange Note bug this.keysPressed = {}; this.threads.stopAll(); this.stopAllActiveSounds(); @@ -4958,6 +5110,11 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('doPlaySoundUntilDone')); blocks.push(block('doStopAllSounds')); blocks.push('-'); + blocks.push(block('doSetVolume')); + blocks.push(block('doChangeVolume')); + blocks.push(watcherToggle('reportVolume')); + blocks.push(block('reportVolume')); + blocks.push('-'); blocks.push(block('doRest')); blocks.push('-'); blocks.push(block('doPlayNote')); @@ -4984,6 +5141,9 @@ StageMorph.prototype.blockTemplates = function (category) { } else if (cat === 'pen') { blocks.push(block('clear')); + blocks.push('-'); + blocks.push(block('doStreamCamera')); + blocks.push(block('doStopCamera')); } else if (cat === 'control') { @@ -5040,6 +5200,8 @@ StageMorph.prototype.blockTemplates = function (category) { } else if (cat === 'sensing') { + blocks.push(block('reportStreamingCamera')); + blocks.push('-'); blocks.push(block('doAsk')); blocks.push(watcherToggle('getLastAnswer')); blocks.push(block('getLastAnswer')); @@ -5051,6 +5213,7 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('reportMouseDown')); blocks.push('-'); blocks.push(block('reportKeyPressed')); + blocks.push(block('getKeysPressed')); blocks.push('-'); blocks.push(block('doResetTimer')); blocks.push(watcherToggle('getTimer')); @@ -5064,6 +5227,9 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('doSetFastTracking')); blocks.push('-'); blocks.push(block('reportDate')); + blocks.push('-'); + blocks.push(block('reportLanguage')); + blocks.push(block('reportLocation')); // for debugging: /////////////// @@ -5493,6 +5659,15 @@ StageMorph.prototype.addSound StageMorph.prototype.playSound = SpriteMorph.prototype.playSound; +StageMorph.prototype.doSetVolume + = SpriteMorph.prototype.doSetVolume; + +StageMorph.prototype.doChangeVolume + = SpriteMorph.prototype.doChangeVolume; + +StageMorph.prototype.reportVolume + = SpriteMorph.prototype.reportVolume; + StageMorph.prototype.stopAllActiveSounds = function () { this.activeSounds.forEach(function (audio) { audio.pause(); @@ -5507,11 +5682,29 @@ StageMorph.prototype.pauseAllActiveSounds = function () { }; StageMorph.prototype.resumeAllActiveSounds = function () { + var newSounds = []; // remove Sounds that have been played so they do not resume + + this.activeSounds.forEach(function (audio) { + if (audio.ended === false) { + newSounds.push(audio); + audio.play(); + } + }); + + this.activeSounds = newSounds; +}; + +StageMorph.prototype.muteAllSounds = function () { + this.muted = true; + this.activeSounds.forEach(function (audio) { - audio.play(); + audio.volume = 0; }); }; +StageMorph.prototype.unmuteAllSounds + = SpriteMorph.prototype.unmuteAllSounds; + StageMorph.prototype.reportSounds = SpriteMorph.prototype.reportSounds; @@ -6265,9 +6458,10 @@ CostumeEditorMorph.prototype.mouseMove // Sound instance creation -function Sound(audio, name) { +function Sound(audio, name, volume) { this.audio = audio; // mandatory this.name = name || "Sound"; + this.volume = volume || 100; } Sound.prototype.play = function () { @@ -6275,6 +6469,7 @@ Sound.prototype.play = function () { // externally (i.e. by the stage) var aud = document.createElement('audio'); aud.src = this.audio.src; + aud.volume = Math.min(Math.max(0, this.volume), 100) / 100; aud.play(); return aud; }; @@ -6284,7 +6479,8 @@ Sound.prototype.copy = function () { cpy; snd.src = this.audio.src; - cpy = new Sound(snd, this.name ? copy(this.name) : null); + snd.volume = this.volume; + cpy = new Sound(snd, this.name ? copy(this.name) : null, this.volume ? copy(this.volume) : null); return cpy; }; @@ -6298,8 +6494,9 @@ Sound.prototype.toDataURL = function () { // Note instance creation -function Note(pitch) { +function Note(pitch, volume) { this.pitch = pitch === 0 ? 0 : pitch || 69; + this.volume = volume; this.setupContext(); this.oscillator = null; } @@ -6330,12 +6527,13 @@ Note.prototype.setupContext = function () { } Note.prototype.audioContext = new AudioContext(); Note.prototype.gainNode = Note.prototype.audioContext.createGain(); - Note.prototype.gainNode.gain.value = 0.25; // reduce volume by 1/4 }; // Note playing Note.prototype.play = function () { + this.gainNode.gain.value = 0.25 * this.volume / 100; // reduce volume by 1/4 + this.oscillator = this.audioContext.createOscillator(); if (!this.oscillator.start) { this.oscillator.start = this.oscillator.noteOn; @@ -6351,6 +6549,12 @@ Note.prototype.play = function () { this.oscillator.start(0); }; +Note.prototype.setVolume = function (volume) { + this.stop(); + this.volume = volume; + this.play(); +} + Note.prototype.stop = function () { if (this.oscillator) { this.oscillator.stop(0); @@ -6779,7 +6983,7 @@ WatcherMorph.prototype.object = function () { WatcherMorph.prototype.isGlobal = function (selector) { return contains( - ['getLastAnswer', 'getLastMessage', 'getTempo', 'getTimer', + ['getLastAnswer', 'getKeysPressed', 'getLastMessage', 'getTempo', 'getTimer', 'reportMouseX', 'reportMouseY', 'reportThreadCount'], selector ); |
