From 16449ae73dcb62ea0041dfd3ba1564f1476d4d1a Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Tue, 29 Nov 2016 17:50:31 -0500 Subject: [PATCH] Add Loading And Exporting --- src/index.js | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 4ed724f39..f7473a723 100644 --- a/src/index.js +++ b/src/index.js @@ -1,6 +1,8 @@ var EventEmitter = require('events'); var util = require('util'); - +var Blocks = require('../engine/blocks'); +var RenderedTarget = require('../sprites/rendered-target'); +var Sprite = require('../sprites/sprite'); var Runtime = require('./engine/runtime'); var sb2import = require('./import/sb2import'); @@ -154,6 +156,41 @@ VirtualMachine.prototype.loadProject = function (json) { this.runtime.setEditingTarget(this.editingTarget); }; +VirtualMachine.prototype.exportToJson = function () { + this.clear(); + var obj = new Object(); + var i = 0; + obj.sprites = []; + for (; i < this.runtime.targets; i++) { + obj.sprites.push(this.runtime.targets[i].export()); + } + obj.meta.name = "WIP"; + obj.meta.useragent = navigator.userAgent; + return JSON.stringify(obj); +} + +VirtualMachine.prototype.importFromJson = function (json) { + var obj = JSON.parse(json); + var i = 0; + for (; i < obj.sprites.length; i++) { + var blocks = new Blocks(); + var sprite = new Sprite(blocks, this.runtime); + for (y in obj.sprites[i].sprite) { + sprite[y] = obj.sprites[i].sprite[y]; + } + var target = sprite.createClone(); + this.runtime.targets.push(target); + for (x in obj.sprites[i]) { + target[x] = obj.sprites[y]; + } + } + 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.