diff options
| author | Jens Mönig <jens@moenig.org> | 2014-07-17 10:47:49 +0200 |
|---|---|---|
| committer | Jens Mönig <jens@moenig.org> | 2014-07-17 10:47:49 +0200 |
| commit | c11e4c7c6d67a3882d0bbb36cb039d807fa49acd (patch) | |
| tree | aa2f99c894553cd70be153a226d8d11bc109e973 /objects.js | |
| parent | a34e140dab390c1749b7bdabe8fb3ecb0cab6013 (diff) | |
| parent | 3ca3d33642497b3b4e2a2d016f7a6824b681c5b1 (diff) | |
| download | snap-yow-c11e4c7c6d67a3882d0bbb36cb039d807fa49acd.tar.gz snap-yow-c11e4c7c6d67a3882d0bbb36cb039d807fa49acd.zip | |
Merge pull request #340 from cs10/extensions
Save stage/pentrails as costume (for animations)
Diffstat (limited to 'objects.js')
| -rw-r--r-- | objects.js | 70 |
1 files changed, 70 insertions, 0 deletions
@@ -415,6 +415,12 @@ SpriteMorph.prototype.initBlocks = function () { spec: 'go back %n layers', defaults: [1] }, + doScreenshot: { + type: 'command', + category: 'looks', + spec: 'save %imgsource as costume named %s', + defaults: [['pen trails'], localize('screenshot')] + }, // Looks - Debugging primitives for development mode reportCostumes: { @@ -1319,6 +1325,8 @@ SpriteMorph.prototype.init = function (globals) { this.changed(); this.drawNew(); this.changed(); + + this.screenshotNames = {}; // Keeps track of stage image names }; // SpriteMorph duplicating (fullCopy) @@ -1695,6 +1703,8 @@ SpriteMorph.prototype.blockTemplates = function (category) { blocks.push('-'); blocks.push(block('comeToFront')); blocks.push(block('goBack')); + blocks.push('-'); + blocks.push(block('doScreenshot')); // for debugging: /////////////// @@ -4076,6 +4086,58 @@ SpriteMorph.prototype.reactToDropOf = function (morph, hand) { morph.slideBackTo(hand.grabOrigin); }; +var re = /\((\d+)\)$/; // RegExp to match costume names +SpriteMorph.prototype.newCostumeName = function (name) { + var foundSameName = false, + foundIndex = false, + lastIndex = 0, + i = 1, + p = null, + costume = null; + + for (i = 1; i <= this.costumes.length(); i++) { + costume = this.costumes.at(i); + if (costume !== null) { + if (costume.name === name) { + foundSameName = true; + } + if (foundSameName) { + p = re.exec(costume.name); + if (p) { + lastIndex = Number(p[1]); + foundIndex = true; + } + } + } + } + if (foundSameName) { + if (foundIndex) { + lastIndex += 1; + return name + '(' + lastIndex + ')'; // New index with a +1 + } + return name + '(2)'; // No indexing has started so start it off with a (1) + } + return name; +}; + +SpriteMorph.prototype.doScreenshot = function (imgSource, data) { + var canvas, + stage = this.parentThatIsA(StageMorph), + costume; + data = this.newCostumeName(data); + if (imgSource[0] === undefined) { + return; + } + if (imgSource[0] === "pen trails") { + canvas = stage.trailsCanvas; + costume = new Costume(canvas, data).copy(); // Copy is required to prevent mutation + } else if (imgSource[0] === "stage image") { + canvas = stage.fullImageClassic(); + costume = new Costume(canvas, data); + } + this.addCostume(costume); +}; + // SpriteHighlightMorph ///////////////////////////////////////////////// // SpriteHighlightMorph inherits from Morph: @@ -4753,6 +4815,8 @@ StageMorph.prototype.blockTemplates = function (category) { blocks.push(block('setEffect')); blocks.push(block('clearEffects')); blocks.push('-'); + blocks.push(block('doScreenshot')); + blocks.push('-'); blocks.push(block('show')); blocks.push(block('hide')); @@ -5244,6 +5308,12 @@ StageMorph.prototype.deleteVariable = SpriteMorph.prototype.deleteVariable; // StageMorph block rendering +StageMorph.prototype.doScreenshot + = SpriteMorph.prototype.doScreenshot; + +StageMorph.prototype.newCostumeName + = SpriteMorph.prototype.newCostumeName; + StageMorph.prototype.blockForSelector = SpriteMorph.prototype.blockForSelector; |
