From 40b530fa7e134dac857d2519fc62daec783a09f3 Mon Sep 17 00:00:00 2001 From: TheBrokenRail Date: Mon, 12 Sep 2016 11:05:16 -0400 Subject: [PATCH] Offline Support [READY TO MERGE?] [ALONG WITH THE SCRATCH-RENDER PR] (#138) * Remove Fallback Project * Added A Newline and Removed Fallback Project * Made The SB2 Project Import Field Fit In More With All Other Textboxes * Better Description Of SB2 Import Field * Oops! * The XML Import Area Doses Not Need To Be Resizable! * That Didn't Work As Planned * Now it won't rezize * Added SB3New * Add Code For SB3New * Better 404 * Indentation * NodeJS complains about quotes * Moved And Renamed SB3New * Renamed Refrences * Prefix With ./ * Typo * Newline Fix * Scratch Cat By Default! * Single Quotes And Max Legth Fixed * Better Comment Above loadProject * Move newProject to index.js * Added Dependincies to index.js * Deleted newProject.js * Removed Unneeeded Semicolon * @param {?string} * Added SVG For Scxratch Cat * Now Uses Scratch Cat Offline (REQUIRES PULL REQUEST MADE TO SCRATCH-RENDER TO BE MERGED OR IT WILL NOT WORK) * Added Stage PNG * Added Stage Creation * Fix Sizing Issues * Import And Compatibility With Less Hacky Image Import (Scratch-Render) * Import And Compatibility With Less Hacky Image Import (Scratch-Render) * Fixed Line Length * Added Navigation Bar * Added CSS For Drop Down Menus * Nicer Scale * Nicer Dropdowns * Remobved Dropdown CSS (It Didn't Work) * Add Scratch Player Bar CSS * Add Scratch Player Bar HTML --- playground/index.html | 29 +++++++++++---------- playground/playground.css | 53 ++++++++++++++++++++++++++++++++++---- playground/playground.js | 10 ++++--- src/Stage.png | Bin 0 -> 1346 bytes src/import/sb2import.js | 3 ++- src/index.js | 46 ++++++++++++++++++++++++++++++++- src/scratch_cat.svg | 42 ++++++++++++++++++++++++++++++ 7 files changed, 158 insertions(+), 25 deletions(-) create mode 100644 src/Stage.png create mode 100644 src/scratch_cat.svg diff --git a/playground/index.html b/playground/index.html index e33b21034..74a26d422 100644 --- a/playground/index.html +++ b/playground/index.html @@ -9,19 +9,20 @@
-

Scratch VM Playground

- - - -

- Renderer
- VM Threads
- VM Block Representation
- Import/Export -

+
- Render
- + Home
+ + +
+
+
+
Thread explorer @@ -32,8 +33,8 @@

         
- Import/Export - + Import/Export
+ Project ID:

diff --git a/playground/playground.css b/playground/playground.css index 32dd55836..7f789b9bd 100644 --- a/playground/playground.css +++ b/playground/playground.css @@ -1,6 +1,9 @@ body { background: rgb(36,36,36); } +textarea { + resize: none; +} a { color: rgb(217,217,217); } @@ -16,13 +19,12 @@ a { position: absolute; left: 1%; right: 60%; - top: 0; + top: 1%; bottom: 0; width: 35%; } #blockexplorer, #threadexplorer, #importexport { position: absolute; - width: 100%; height: 75%; overflow: scroll; border: 1px solid #fff; @@ -30,17 +32,58 @@ a { color: rgb(217,217,217); font-family: monospace; font-size: 10pt; + width: 480px; + height: 360px; } - #tab-blockexplorer, #tab-threadexplorer, #tab-importexport { display: none; } - #importExport { - width: 400px; + width: 480px; height: 360px; background: rgb(36,36,36); color: rgb(217,217,217); font-family: monospace; font-size: 10pt; } +#projectId { + background: rgb(36,36,36); + color: rgb(217,217,217); + font-family: monospace; + font-size: 10pt; +} +ul { + display: block; + list-style-type: none; + margin: 0; + padding: 0; + overflow: hidden; + background-color: #333; +} +rendererTop { + display: block; + list-style-type: none; + margin: 0; + padding: 0; + overflow: hidden; + background-color: #696969; + width: 480px; +} +rendererButtons { + float: right; + padding: 12px; +} +li { + float: left; +} +li a { + display: block; + color: white; + text-align: center; + padding: 14px 16px; + text-decoration: none; +} +/* Change the link color to #111 (black) on hover */ +li a:hover { + background-color: #111; +} diff --git a/playground/playground.js b/playground/playground.js index 3592df1d3..30d10c123 100644 --- a/playground/playground.js +++ b/playground/playground.js @@ -1,11 +1,13 @@ var loadProject = function () { - var id = location.hash.substring(1) || 119615668; + var id = location.hash.substring(1); var url = 'https://projects.scratch.mit.edu/internalapi/project/' + id + '/get/'; var r = new XMLHttpRequest(); - r.addEventListener('load', function() { - window.vm.loadProject(this.responseText); - }); + r.onreadystatechange = function() { + if (this.readyState == 4) { + window.vm.loadProject(this.responseText); + } + }; r.open('GET', url); r.send(); }; diff --git a/src/Stage.png b/src/Stage.png new file mode 100644 index 0000000000000000000000000000000000000000..c60861c2bd315d6eaffdb9dfb632abb49e1102d4 GIT binary patch literal 1346 zcmeAS@N?(olHy`uVBq!ia0y~yV0-|?85~SN5r)wHmdKI;Vst00K&1Q~&?~ literal 0 HcmV?d00001 diff --git a/src/import/sb2import.js b/src/import/sb2import.js index b337f9326..494f528cd 100644 --- a/src/import/sb2import.js +++ b/src/import/sb2import.js @@ -51,7 +51,8 @@ function parseScratchObject (object, runtime, topLevel) { var costume = object.costumes[i]; // @todo: Make sure all the relevant metadata is being pulled out. sprite.costumes.push({ - skin: costume.baseLayerMD5, + skin: 'https://cdn.assets.scratch.mit.edu/internalapi/asset/' + + costume.baseLayerMD5 + '/get/', name: costume.costumeName, bitmapResolution: costume.bitmapResolution, rotationCenterX: costume.rotationCenterX, diff --git a/src/index.js b/src/index.js index e4dd8df5a..ceb52152f 100644 --- a/src/index.js +++ b/src/index.js @@ -3,6 +3,8 @@ var util = require('util'); var Runtime = require('./engine/runtime'); var sb2import = require('./import/sb2import'); +var Sprite = require('./sprites/sprite'); +var Blocks = require('./engine/blocks'); /** * Whether the environment is a WebWorker. @@ -104,9 +106,51 @@ VirtualMachine.prototype.postIOData = function (device, data) { /** * Load a project from a Scratch 2.0 JSON representation. - * @param {string} json JSON string representing the project. + * @param {?string} json JSON string representing the project. + * If JSON is NULL, fallback to creating blank project. */ VirtualMachine.prototype.loadProject = function (json) { + if (!json) { + // Creates 'Stage' + var blocks2 = new Blocks(); + var stage = new Sprite(blocks2); + stage.name = 'Stage'; + stage.costumes.push({ + skin: '/src/Stage.png', + name: 'backdrop1', + bitmapResolution: 1, + rotationCenterX: 240, + rotationCenterY: 180}); + var target2 = stage.createClone(); + this.runtime.targets.push(target2); + target2.x = 0; + target2.y = 0; + target2.direction = 90; + target2.size = 200; + target2.visible = true; + target2.isStage = true; + // Creates 'Sprite1' + var blocks1 = new Blocks(); + var sprite = new Sprite(blocks1); + sprite.name = 'Sprite1'; + sprite.costumes.push({ + skin: '/src/scratch_cat.svg', + name: 'costume1', + bitmapResolution: 1, + rotationCenterX: 47, + rotationCenterY: 55}); + var target1 = sprite.createClone(); + this.runtime.targets.push(target1); + target1.x = 0; + target1.y = 0; + target1.direction = 90; + target1.size = 100; + target1.visible = true; + this.editingTarget = this.runtime.targets[0]; + this.emitTargetsUpdate(); + this.emitWorkspaceUpdate(); + return; + } // @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. diff --git a/src/scratch_cat.svg b/src/scratch_cat.svg new file mode 100644 index 000000000..823455468 --- /dev/null +++ b/src/scratch_cat.svg @@ -0,0 +1,42 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +