diff options
Diffstat (limited to 'objects.js')
| -rw-r--r-- | objects.js | 144 |
1 files changed, 99 insertions, 45 deletions
@@ -125,7 +125,7 @@ PrototypeHatBlockMorph*/ // Global stuff //////////////////////////////////////////////////////// -modules.objects = '2014-July-30'; +modules.objects = '2014-December-04'; var SpriteMorph; var StageMorph; @@ -1168,6 +1168,13 @@ SpriteMorph.prototype.initBlocks = function () { category: 'lists', spec: 'map %repRing over %l' }, + doForEach: { + dev: true, + type: 'command', + category: 'lists', + spec: 'for %upvar in %l %cs', + defaults: [localize('each item')] + }, // Code mapping - experimental doMapCodeOrHeader: { // experimental @@ -1693,6 +1700,20 @@ SpriteMorph.prototype.blockTemplates = function (category) { return menu; } + function addVar(pair) { + if (pair) { + if (myself.variables.silentFind(pair[0])) { + myself.inform('that name is already in use'); + } else { + myself.addVariable(pair[0], pair[1]); + myself.toggleVariableWatcher(pair[0], pair[1]); + myself.blocksCache[cat] = null; + myself.paletteCache[cat] = null; + myself.parentThatIsA(IDE_Morph).refreshPalette(); + } + } + } + if (cat === 'motion') { blocks.push(block('forward')); @@ -1991,15 +2012,7 @@ SpriteMorph.prototype.blockTemplates = function (category) { function () { new VariableDialogMorph( null, - function (pair) { - if (pair && !myself.variables.silentFind(pair[0])) { - myself.addVariable(pair[0], pair[1]); - myself.toggleVariableWatcher(pair[0], pair[1]); - myself.blocksCache[cat] = null; - myself.paletteCache[cat] = null; - myself.parentThatIsA(IDE_Morph).refreshPalette(); - } - }, + addVar, myself ).prompt( 'Variable name', @@ -2081,6 +2094,8 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(txt); blocks.push('-'); blocks.push(block('reportMap')); + blocks.push('-'); + blocks.push(block('doForEach')); } ///////////////////////////////// @@ -2188,8 +2203,8 @@ SpriteMorph.prototype.freshPalette = function (category) { var defs = SpriteMorph.prototype.blocks, hiddens = StageMorph.prototype.hiddenPrimitives; return Object.keys(hiddens).some(function (any) { - return defs[any].category === category || - contains((more[category] || []), any); + return !isNil(defs[any]) && (defs[any].category === category + || contains((more[category] || []), any)); }); } @@ -2228,7 +2243,7 @@ SpriteMorph.prototype.freshPalette = function (category) { var hiddens = StageMorph.prototype.hiddenPrimitives, defs = SpriteMorph.prototype.blocks; Object.keys(hiddens).forEach(function (sel) { - if (defs[sel].category === category) { + if (defs[sel] && (defs[sel].category === category)) { delete StageMorph.prototype.hiddenPrimitives[sel]; } }); @@ -3296,8 +3311,11 @@ SpriteMorph.prototype.setCenter = function (aPoint, justMe) { SpriteMorph.prototype.nestingBounds = function () { // same as fullBounds(), except that it uses "parts" instead of children - var result; - result = this.bounds; + // and special cases the costume-less "arrow" shape's bounding box + var result = this.bounds; + if (!this.costume && this.penBounds) { + result = this.penBounds.translateBy(this.position()); + } this.parts.forEach(function (part) { if (part.isVisible) { result = result.merge(part.nestingBounds()); @@ -3521,6 +3539,7 @@ SpriteMorph.prototype.allMessageNames = function () { }; SpriteMorph.prototype.allHatBlocksFor = function (message) { + if (typeof message === 'number') {message = message.toString(); } return this.scripts.children.filter(function (morph) { var event; if (morph.selector) { @@ -4447,17 +4466,33 @@ StageMorph.prototype.drawOn = function (aCanvas, aRect) { hs = h / this.scale; context.save(); context.scale(this.scale, this.scale); - context.drawImage( - this.penTrails(), - src.left() / this.scale, - src.top() / this.scale, - ws, - hs, - area.left() / this.scale, - area.top() / this.scale, - ws, - hs - ); + try { + context.drawImage( + this.penTrails(), + src.left() / this.scale, + src.top() / this.scale, + ws, + hs, + area.left() / this.scale, + area.top() / this.scale, + ws, + hs + ); + } catch (err) { // sometimes triggered only by Firefox + // console.log(err); + context.restore(); + context.drawImage( + this.penTrails(), + 0, + 0, + this.dimensions.x, + this.dimensions.y, + this.left(), + this.top(), + this.dimensions.x * this.scale, + this.dimensions.y * this.scale + ); + } context.restore(); } }; @@ -4740,6 +4775,7 @@ StageMorph.prototype.fireKeyEvent = function (key) { var evt = key.toLowerCase(), hats = [], procs = [], + ide = this.parentThatIsA(IDE_Morph), myself = this; this.keysPressed[evt] = true; @@ -4747,19 +4783,24 @@ StageMorph.prototype.fireKeyEvent = function (key) { return this.fireGreenFlagEvent(); } if (evt === 'ctrl f') { - return this.parentThatIsA(IDE_Morph).currentSprite.searchBlocks(); + if (!ide.isAppMode) {ide.currentSprite.searchBlocks(); } + return; } if (evt === 'ctrl n') { - return this.parentThatIsA(IDE_Morph).createNewProject(); + if (!ide.isAppMode) {ide.createNewProject(); } + return; } if (evt === 'ctrl o') { - return this.parentThatIsA(IDE_Morph).openProjectsBrowser(); + if (!ide.isAppMode) {ide.openProjectsBrowser(); } + return; } if (evt === 'ctrl s') { - return this.parentThatIsA(IDE_Morph).save(); + if (!ide.isAppMode) {ide.save(); } + return; } if (evt === 'ctrl shift s') { - return this.parentThatIsA(IDE_Morph).saveProjectsBrowser(); + if (!ide.isAppMode) {return ide.saveProjectsBrowser(); } + return; } if (evt === 'esc') { return this.fireStopAllEvent(); @@ -4901,6 +4942,20 @@ StageMorph.prototype.blockTemplates = function (category) { ); } + function addVar(pair) { + if (pair) { + if (myself.variables.silentFind(pair[0])) { + myself.inform('that name is already in use'); + } else { + myself.addVariable(pair[0], pair[1]); + myself.toggleVariableWatcher(pair[0], pair[1]); + myself.blocksCache[cat] = null; + myself.paletteCache[cat] = null; + myself.parentThatIsA(IDE_Morph).refreshPalette(); + } + } + } + if (cat === 'motion') { txt = new TextMorph(localize( @@ -5147,15 +5202,7 @@ StageMorph.prototype.blockTemplates = function (category) { function () { new VariableDialogMorph( null, - function (pair) { - if (pair && !myself.variables.silentFind(pair[0])) { - myself.addVariable(pair[0], pair[1]); - myself.toggleVariableWatcher(pair[0], pair[1]); - myself.blocksCache[cat] = null; - myself.paletteCache[cat] = null; - myself.parentThatIsA(IDE_Morph).refreshPalette(); - } - }, + addVar, myself ).prompt( 'Variable name', @@ -5231,6 +5278,8 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(txt); blocks.push('-'); blocks.push(block('reportMap')); + blocks.push('-'); + blocks.push(block('doForEach')); } ///////////////////////////////// @@ -5369,7 +5418,7 @@ StageMorph.prototype.thumbnail = function (extentPoint, excludedSprite) { this.dimensions.y * this.scale ); this.children.forEach(function (morph) { - if (morph !== excludedSprite) { + if (morph.isVisible && (morph !== excludedSprite)) { fb = morph.fullBounds(); fimg = morph.fullImage(); if (fimg.width && fimg.height) { @@ -6516,10 +6565,14 @@ CellMorph.prototype.drawNew = function () { } this.contentsMorph.setColor(new Color(255, 255, 255)); } else if (typeof this.contents === 'boolean') { - this.contentsMorph = SpriteMorph.prototype.booleanMorph.call( + img = SpriteMorph.prototype.booleanMorph.call( null, this.contents - ); + ).fullImage(); + this.contentsMorph = new Morph(); + this.contentsMorph.silentSetWidth(img.width); + this.contentsMorph.silentSetHeight(img.height); + this.contentsMorph.image = img; } else if (this.contents instanceof HTMLCanvasElement) { this.contentsMorph = new Morph(); this.contentsMorph.silentSetWidth(this.contents.width); @@ -6844,7 +6897,8 @@ WatcherMorph.prototype.update = function () { if (this.target && this.getter) { this.updateLabel(); if (this.target instanceof VariableFrame) { - newValue = this.target.vars[this.getter]; + newValue = this.target.vars[this.getter] ? + this.target.vars[this.getter].value : undefined; } else { newValue = this.target[this.getter](); } @@ -6929,7 +6983,7 @@ WatcherMorph.prototype.fixLayout = function () { this.sliderMorph.button.pressColor.b += 100; this.sliderMorph.setHeight(fontSize); this.sliderMorph.action = function (num) { - myself.target.vars[myself.getter] = Math.round(num); + myself.target.vars[myself.getter].value = Math.round(num); }; this.add(this.sliderMorph); } |
