diff --git a/src/import/sb2import.js b/src/import/sb2import.js index b97144d200d..927fb650d61 100644 --- a/src/import/sb2import.js +++ b/src/import/sb2import.js @@ -19,12 +19,14 @@ var List = require('../engine/list'); * and process the top-level object (the stage object). * @param {!string} json SB2-format JSON to load. * @param {!Runtime} runtime Runtime object to load all structures into. + * @param {Boolean=} opt_forceSprite If set, treat as sprite (Sprite2). + * @return {?Target} Top-level target created (stage or sprite). */ -function sb2import (json, runtime) { - parseScratchObject( +function sb2import (json, runtime, opt_forceSprite) { + return parseScratchObject( JSON.parse(json), runtime, - true + !opt_forceSprite ); } @@ -33,6 +35,7 @@ function sb2import (json, runtime) { * @param {!Object} object From-JSON "Scratch object:" sprite, stage, watcher. * @param {!Runtime} runtime Runtime object to load all structures into. * @param {boolean} topLevel Whether this is the top-level object (stage). + * @return {?Target} Target created (stage or sprite). */ function parseScratchObject (object, runtime, topLevel) { if (!object.hasOwnProperty('objName')) { @@ -128,6 +131,7 @@ function parseScratchObject (object, runtime, topLevel) { parseScratchObject(object.children[m], runtime, false); } } + return target; } /** diff --git a/src/index.js b/src/index.js index 014dc2b23e3..7fa2237623a 100644 --- a/src/index.js +++ b/src/index.js @@ -175,14 +175,50 @@ VirtualMachine.prototype.loadProject = function (json) { this.clear(); // @todo: Handle other formats, e.g., Scratch 1.4, Scratch 3.0. sb2import(json, this.runtime); - // Select the first target for editing, e.g., the stage. - this.editingTarget = this.runtime.targets[0]; + // Select the first target for editing, e.g., the first sprite. + this.editingTarget = this.runtime.targets[1]; + // Update the VM user's knowledge of targets and blocks on the workspace. + this.emitTargetsUpdate(); + this.emitWorkspaceUpdate(); + this.runtime.setEditingTarget(this.editingTarget); +}; + +/** + * Add a single sprite from the "Sprite2" (i.e., SB2 sprite) format. + * @param {?string} json JSON string representing the sprite. + */ +VirtualMachine.prototype.addSprite2 = function (json) { + // Select new sprite. + this.editingTarget = sb2import(json, this.runtime, true); // Update the VM user's knowledge of targets and blocks on the workspace. this.emitTargetsUpdate(); this.emitWorkspaceUpdate(); this.runtime.setEditingTarget(this.editingTarget); }; +/** + * Add a costume to the current editing target. + * @param {!Object} costumeObject Object representing the costume. + */ +VirtualMachine.prototype.addCostume = function (costumeObject) { + this.editingTarget.sprite.costumes.push(costumeObject); + // Switch to the costume. + this.editingTarget.setCostume( + this.editingTarget.sprite.costumes.length - 1 + ); +}; + +/** + * Add a backdrop to the stage. + * @param {!Object} backdropObject Object representing the backdrop. + */ +VirtualMachine.prototype.addBackdrop = function (backdropObject) { + var stage = this.runtime.getTargetForStage(); + stage.sprite.costumes.push(backdropObject); + // Switch to the backdrop. + stage.setCostume(stage.sprite.costumes.length - 1); +}; + /** * Temporary way to make an empty project, in case the desired project * cannot be loaded from the online server.