mirror of
https://github.com/scratchfoundation/scratch-vm.git
synced 2024-12-23 14:32:59 -05:00
Merge branch 'patch-24' of https://github.com/TheBrokenRail/scratch-vm into feature/194
This commit is contained in:
commit
04d9620631
4 changed files with 149 additions and 9 deletions
|
@ -52,9 +52,9 @@
|
|||
<button id="projectLoadButton">Load</button>
|
||||
<button id="createEmptyProject">New Project</button><br />
|
||||
<p>
|
||||
<input type="button" value="Export to XML" onclick="toXml()">
|
||||
<input type="button" value="Export to JSON" onclick="toJson()">
|
||||
|
||||
<input type="button" value="Import from XML" onclick="fromXml()" id="import">
|
||||
<input type="button" value="Import from JSON" onclick="fromJson()" id="import">
|
||||
<br /><br />
|
||||
<textarea id="importExport"></textarea>
|
||||
</p>
|
||||
|
@ -70,18 +70,17 @@
|
|||
<!-- Playground -->
|
||||
<script src="./playground.js"></script>
|
||||
<script>
|
||||
function toXml() {
|
||||
function toJson() {
|
||||
var output = document.getElementById('importExport');
|
||||
var xml = Blockly.Xml.workspaceToDom(workspace);
|
||||
output.value = Blockly.Xml.domToPrettyText(xml);
|
||||
var json = window.vm.toPrettyJSON(workspace);
|
||||
output.value = json;
|
||||
output.focus();
|
||||
output.select();
|
||||
}
|
||||
|
||||
function fromXml() {
|
||||
function fromJson() {
|
||||
var input = document.getElementById('importExport');
|
||||
var xml = Blockly.Xml.textToDom(input.value);
|
||||
Blockly.Xml.domToWorkspace(workspace, xml);
|
||||
window.vm.fromJSON(input.value);
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
|
|
96
src/index.js
96
src/index.js
|
@ -1,6 +1,7 @@
|
|||
var EventEmitter = require('events');
|
||||
var util = require('util');
|
||||
|
||||
var Blocks = require('./engine/blocks');
|
||||
var Sprite = require('./sprites/sprite');
|
||||
var Runtime = require('./engine/runtime');
|
||||
var sb2import = require('./import/sb2import');
|
||||
|
||||
|
@ -154,6 +155,99 @@ VirtualMachine.prototype.loadProject = function (json) {
|
|||
this.runtime.setEditingTarget(this.editingTarget);
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.toPrettyJSON = function (testing) {
|
||||
this.runtime.targets.testing = testing;
|
||||
var json = JSON.stringify(this.toJSON(), null, 4);
|
||||
this.runtime.targets.testing = false;
|
||||
if (testing == true) {
|
||||
console.log('Exported To JSON');
|
||||
}
|
||||
return json;
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.toJSON = function () {
|
||||
if (this.runtime.targets.hasOwnProperty('testing') == false) {
|
||||
this.runtime.targets.testing = false;
|
||||
}
|
||||
var obj = new Object();
|
||||
obj.sprites = this.runtime.targets;
|
||||
obj.meta = new Object();
|
||||
obj.meta.name = 'WIP';
|
||||
obj.meta.useragent = navigator.userAgent;
|
||||
return obj;
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.fromJSON = function (json, testing) {
|
||||
this.clear();
|
||||
if (testing == true) {
|
||||
console.log('Importing JSON');
|
||||
}
|
||||
var obj = JSON.parse(json);
|
||||
var i = 0;
|
||||
for (; i < obj.sprites.length; i++) {
|
||||
var blocks = new Blocks();
|
||||
var z = null;
|
||||
for (z in obj.sprites[i].sprite.blocks) {
|
||||
blocks[z] = obj.sprites[i].sprite.blocks[z];
|
||||
if (testing == true) {
|
||||
console.log('Importing ' + z + ' to Blocks');
|
||||
}
|
||||
}
|
||||
var sprite = new Sprite(blocks, this.runtime);
|
||||
var y = null;
|
||||
for (y in obj.sprites[i].sprite) {
|
||||
if (y === 'blocks') {
|
||||
continue;
|
||||
}
|
||||
sprite[y] = obj.sprites[i].sprite[y];
|
||||
if (testing == true) {
|
||||
console.log('Importing ' + y + ' to Sprite');
|
||||
}
|
||||
}
|
||||
var target = sprite.createClone();
|
||||
this.runtime.targets.push(target);
|
||||
var x = null;
|
||||
for (x in obj.sprites[i]) {
|
||||
if (x === 'sprite') {
|
||||
continue;
|
||||
}
|
||||
target[x] = obj.sprites[i][x];
|
||||
console.log('Importing ' + x + ' to Rendered Target');
|
||||
}
|
||||
target.updateAllDrawablePropertie();
|
||||
}
|
||||
this.editingTarget = this.runtime.targets[0];
|
||||
// Update the VM user's knowledge of targets and blocks on the workspace.
|
||||
this.emitTargetsUpdate();
|
||||
this.emitWorkspaceUpdate();
|
||||
this.runtime.setEditingTarget(this.editingTarget);
|
||||
if (testing == true) {
|
||||
console.log('Imported from JSON');
|
||||
}
|
||||
};
|
||||
|
||||
VirtualMachine.prototype.testJSON = function () {
|
||||
console.log('Exporting to JSON...');
|
||||
var json = this.toPrettyJSON(true);
|
||||
console.log('Importing from JSON...');
|
||||
this.fromJSON(json, true);
|
||||
console.log('Exporting to JSON...');
|
||||
var json2 = this.toPrettyJSON(true);
|
||||
if (json == json2) {
|
||||
console.log('JSON Test: Successful');
|
||||
} else {
|
||||
console.log('JSON Test: Failed: JSON Not Equivalent');
|
||||
console.log('JSON 1:');
|
||||
console.log(json);
|
||||
console.log('JSON 2:');
|
||||
console.log(json2);
|
||||
}
|
||||
this.editingTarget = this.runtime.targets[0];
|
||||
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.
|
||||
|
|
|
@ -64,6 +64,30 @@ RenderedTarget.prototype.initDrawable = function () {
|
|||
}
|
||||
};
|
||||
|
||||
RenderedTarget.prototype.toJSON = function () {
|
||||
var result = new Object();
|
||||
var notSaved = ['renderer', 'effects', 'sprite', 'drawableID', 'runtime', 'id', 'blocks'];
|
||||
var x = null;
|
||||
for (x in this) {
|
||||
if (typeof this[x] === 'function') {
|
||||
continue;
|
||||
if (this.runtime.targets.testing == true) {
|
||||
console.log('Ignoring ' + x);
|
||||
}
|
||||
}
|
||||
if (notSaved.indexOf(x) === -1) {
|
||||
if (this.runtime.targets.testing == true) {
|
||||
console.log('Exporting ' + x);
|
||||
}
|
||||
result[x] = this[x];
|
||||
} else if (this.runtime.targets.testing == true) {
|
||||
console.log('Ignoring ' + x);
|
||||
}
|
||||
}
|
||||
result.sprite = this.sprite.export();
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether this represents an "original" non-clone rendered-target for a sprite,
|
||||
* i.e., created by the editor and not clone blocks.
|
||||
|
|
|
@ -54,4 +54,27 @@ Sprite.prototype.createClone = function () {
|
|||
return newClone;
|
||||
};
|
||||
|
||||
Sprite.prototype.export = function () {
|
||||
var result = new Object();
|
||||
var notSaved = ['clones', 'runtime'];
|
||||
var x = null;
|
||||
for (x in this) {
|
||||
if (typeof this[x] === 'function') {
|
||||
continue;
|
||||
if (this.runtime.targets.testing == true) {
|
||||
console.log('Ignoring ' + x);
|
||||
}
|
||||
}
|
||||
if (notSaved.indexOf(x) === -1) {
|
||||
result[x] = this[x];
|
||||
if (this.runtime.targets.testing == true) {
|
||||
console.log('Exporting ' + x);
|
||||
}
|
||||
} else if (this.runtime.targets.testing == true) {
|
||||
console.log('Ignoring ' + x);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
module.exports = Sprite;
|
||||
|
|
Loading…
Reference in a new issue