Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/import/sb2import.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}

Expand All @@ -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')) {
Expand Down Expand Up @@ -128,6 +131,7 @@ function parseScratchObject (object, runtime, topLevel) {
parseScratchObject(object.children[m], runtime, false);
}
}
return target;
}

/**
Expand Down
40 changes: 38 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down