From 14822fb957f04c279dab25d5c9623f634cbbaf23 Mon Sep 17 00:00:00 2001 From: jmoenig Date: Fri, 18 Oct 2019 11:48:11 +0200 Subject: [PATCH] added "new costume" primitive reporter to "looks" category --- HISTORY.md | 4 +++- snap.html | 4 ++-- src/blocks.js | 13 ++++++++++++- src/objects.js | 7 +++++++ src/threads.js | 41 +++++++++++++++++++++++++++-------------- 5 files changed, 51 insertions(+), 18 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index b1c0c6bf..82c63ef2 100755 --- a/HISTORY.md +++ b/HISTORY.md @@ -2,7 +2,8 @@ ## in development: * **New Features:** - * added selectors for sprites' and the stage's bounding box (left, right, top, bottom) to MY dropdown + * new primitive in "looks": NEW COSTUME from a list of pixels and dimensions, allowing CURRENT + * added selectors for sprites' and the stage's bounding box (LEFT, RIGHT, TOP, BOTTOM) to MY dropdown * **Notable Changes:** * running STOP ALL now also toggles (pauses and resumes) all generic WHEN hat blocks (just like pressing the stop button) * **Notable Fixes:** @@ -14,6 +15,7 @@ ### 2019-10-18 * objects, blocks, threads: added dimension getters for the stage * German translation update (left, right, top, bottom selectors in MY) +* blocks, objects, threads: added "new costume" primitive reporter to "looks" category ### 2019-10-17 * objects, blocks, threads: added selectors for sprites' bounding box (left, right, top, bottom) to MY dropdown diff --git a/snap.html b/snap.html index 5b4553f9..28658d10 100755 --- a/snap.html +++ b/snap.html @@ -6,8 +6,8 @@ - - + + diff --git a/src/blocks.js b/src/blocks.js index 70585c95..955b7906 100644 --- a/src/blocks.js +++ b/src/blocks.js @@ -148,7 +148,7 @@ CustomCommandBlockMorph, SymbolMorph, ToggleButtonMorph, DialMorph*/ // Global stuff //////////////////////////////////////////////////////// -modules.blocks = '2019-October-17'; +modules.blocks = '2019-October-18'; var SyntaxElementMorph; var BlockMorph; @@ -1139,6 +1139,16 @@ SyntaxElementMorph.prototype.labelPart = function (spec) { ); part.setContents(1); break; + case '%dim': + part = new InputSlotMorph( + null, + true, + { + current : ['current'] + } + ); + // part.setContents( ['current']); + break; case '%rel': part = new InputSlotMorph( null, // text @@ -2372,6 +2382,7 @@ SyntaxElementMorph.prototype.endLayout = function () { %inst - white roundish type-in slot with drop-down for instruments %ida - white roundish type-in slot with drop-down for list indices %idx - white roundish type-in slot for indices incl. "any" + %dim - white roundish type-in slot for dimensinos incl. "current" %obj - specially drawn slot for object reporters %rel - chameleon colored rectangular drop-down for relation options %spr - chameleon colored rectangular drop-down for object-names diff --git a/src/objects.js b/src/objects.js index 7be2accf..f6936d01 100644 --- a/src/objects.js +++ b/src/objects.js @@ -312,6 +312,11 @@ SpriteMorph.prototype.initBlocks = function () { spec: '%img of costume %cst', defaults: [['width']] }, + reportNewCostume: { + type: 'reporter', + category: 'looks', + spec: 'new costume %l width: %dim height: %dim' + }, reportNewCostumeStretched: { type: 'reporter', category: 'looks', @@ -2210,6 +2215,7 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push(block('doThink')); blocks.push('-'); blocks.push(block('reportGetImageAttribute')); + blocks.push(block('reportNewCostume')); blocks.push(block('reportNewCostumeStretched')); blocks.push('-'); blocks.push(block('changeEffect')); @@ -8268,6 +8274,7 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('getCostumeIdx')); blocks.push('-'); blocks.push(block('reportGetImageAttribute')); + blocks.push(block('reportNewCostume')); blocks.push(block('reportNewCostumeStretched')); blocks.push('-'); blocks.push(block('changeEffect')); diff --git a/src/threads.js b/src/threads.js index d9763051..cdcb9022 100644 --- a/src/threads.js +++ b/src/threads.js @@ -61,7 +61,7 @@ StageMorph, SpriteMorph, StagePrompterMorph, Note, modules, isString, copy, isNil, WatcherMorph, List, ListWatcherMorph, alert, console, TableMorph, Color, TableFrameMorph, ColorSlotMorph, isSnapObject, Map, newCanvas, Symbol*/ -modules.threads = '2019-October-17'; +modules.threads = '2019-October-18'; var ThreadManager; var Process; @@ -4915,11 +4915,6 @@ Process.prototype.reportGetImageAttribute = function (choice, name) { Process.prototype.reportNewCostumeStretched = function (name, xP, yP) { var cst; - if (!isFinite(+xP * +yP) || isNaN(+xP * +yP)) { - throw new Error( - 'expecting a finite number\nbut getting Infinity or NaN' - ); - } if (name instanceof List) { return this.reportNewCostume(name, xP, yP); } @@ -4927,6 +4922,11 @@ Process.prototype.reportNewCostumeStretched = function (name, xP, yP) { if (!cst) { return new Costume(); } + if (!isFinite(+xP * +yP) || isNaN(+xP * +yP)) { + throw new Error( + 'expecting a finite number\nbut getting Infinity or NaN' + ); + } return cst.stretched( Math.round(cst.width() * +xP / 100), Math.round(cst.height() * +yP / 100) @@ -4951,16 +4951,29 @@ Process.prototype.costumeNamed = function (name) { }; Process.prototype.reportNewCostume = function (pixels, width, height, name) { - // private + var rcvr = this.blockReceiver(), + stage = rcvr.parentThatIsA(StageMorph), + canvas, ctx, src, dta, i, k, px; + + this.assertType(pixels, 'list'); + if (this.inputOption(width) === 'current') { + width = rcvr.costume ? rcvr.costume.width() : stage.dimensions.x; + } + if (this.inputOption(height) === 'current') { + height = rcvr.costume ? rcvr.costume.height() : stage.dimensions.y; + } width = Math.abs(Math.floor(+width)); height = Math.abs(Math.floor(+height)); + if (!isFinite(width * height) || isNaN(width * height)) { + throw new Error( + 'expecting a finite number\nbut getting Infinity or NaN' + ); + } - var canvas = newCanvas(new Point(width, height), true), - ctx = canvas.getContext('2d'), - src = pixels.asArray(), - dta = ctx.createImageData(width, height), - i, k, px; - + canvas = newCanvas(new Point(width, height), true); + ctx = canvas.getContext('2d'); + src = pixels.asArray(); + dta = ctx.createImageData(width, height); for (i = 0; i < src.length; i += 1) { px = src[i].asArray(); for (k = 0; k < 4; k += 1) { @@ -4970,7 +4983,7 @@ Process.prototype.reportNewCostume = function (pixels, width, height, name) { ctx.putImageData(dta, 0, 0); return new Costume( canvas, - name || this.blockReceiver().newCostumeName(localize('snap')) + name || rcvr.newCostumeName(localize('snap')) ); };