YPR->Snap! support for OF block

thanks, Nathan!
pull/3/merge
jmoenig 2013-03-20 17:46:12 +01:00
rodzic 45fcc9d14d
commit 86779af8f0
1 zmienionych plików z 35 dodań i 10 usunięć

45
ypr.js
Wyświetl plik

@ -8,13 +8,11 @@ The above copyright notice and this permission notice shall be included in all c
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
last change: 2013-March-20 by Jens Moenig
*/ */
var sb = (function (sb) { var sb = (function (sb) {
'use strict'; 'use strict';
function extend(o, p) { function extend(o, p) {
var key; var key;
for (key in p) if (p.hasOwnProperty(key)) { for (key in p) if (p.hasOwnProperty(key)) {
@ -937,7 +935,7 @@ var sb = (function (sb) {
'distanceTo:': 'reportDistanceTo', 'distanceTo:': 'reportDistanceTo',
'timerReset': 'doResetTimer', 'timerReset': 'doResetTimer',
'timer': 'reportTimer', 'timer': 'reportTimer',
// 'getAttribute:of:': '', 'getAttribute:of:': 'reportAttributeOf',
// 'attribute:of:': '', // 'attribute:of:': '',
// 'soundLevel': '', // 'soundLevel': '',
// 'isLoud': '', // 'isLoud': '',
@ -1011,6 +1009,9 @@ var sb = (function (sb) {
Text.prototype.toXML = function (string) { Text.prototype.toXML = function (string) {
string.push(escapeXML(this.value)); string.push(escapeXML(this.value));
}; };
Text.prototype.toDOM = function () {
return document.createTextNode(this.value);
};
function Element(tagName) { function Element(tagName) {
this.tagName = tagName; this.tagName = tagName;
@ -1034,6 +1035,18 @@ var sb = (function (sb) {
string.push('/>'); string.push('/>');
} }
}; };
Element.prototype.toDOM = function () {
var el = document.createElement(this.tagName),
attributes = this.attributes, key,
children = this.children, i = 0, child;
for (key in attributes) if (attributes.hasOwnProperty(key)) {
el.setAttribute(key, attributes[key]);
}
while (child = children[i++]) {
el.appendChild(child.toDOM());
}
return el;
};
function n(tagName, attributes, children) { function n(tagName, attributes, children) {
var el = new Element(tagName), key; var el = new Element(tagName), key;
@ -1289,7 +1302,9 @@ var sb = (function (sb) {
node.children.push(n('script', {}, nsScript(arg))); node.children.push(n('script', {}, nsScript(arg)));
} }
} else { } else {
node.children.push(nValue(arg)); a = nValue(arg);
delete a.attributes.id;
node.children.push(a);
} }
}); });
return node; return node;
@ -1350,17 +1365,20 @@ var sb = (function (sb) {
}); });
} }
function nsSprites(sprites, vars, lists) { function nsSprites(sprites, vars, lists) {
return sprites.map(function (sprite) { return sprites.map(function (sprite, i) {
return id(n('sprite', { return id(n('sprite', {
name: sprite.objName, name: sprite.objName,
x: (sprite.bounds.origin.x + sprite.bounds.corner.x) / 2 - 240, x: (sprite.bounds.origin.x + sprite.bounds.corner.x) / 2 - 240,
y: 180 - (sprite.bounds.origin.y + sprite.bounds.corner.y) / 2, y: 180 - (sprite.bounds.origin.y + sprite.bounds.corner.y) / 2,
hidden: sprite.flags === 1,
heading: sprite.rotationDegrees + 90, heading: sprite.rotationDegrees + 90,
scale: sprite.scalePoint.x, scale: sprite.scalePoint.x,
rotation: rotationStyles[sprite.rotationStyle], rotation: rotationStyles[sprite.rotationStyle],
draggable: sprite.draggable, draggable: sprite.draggable,
costume: costumeIndex(sprite), costume: costumeIndex(sprite),
color: (sprite.color.r * 255 | 0) + ',' + (sprite.color.g * 255 | 0) + ',' + (sprite.color.b * 255 | 0) color: (sprite.color.r * 255 | 0) + ',' + (sprite.color.g * 255 | 0) + ',' + (sprite.color.b * 255 | 0),
pen: 'tip',
idx: i
}, [ }, [
n('variables', {}, nsVariables(sprite.vars, sprite.lists)), n('variables', {}, nsVariables(sprite.vars, sprite.lists)),
n('costumes', {}, nsCostumes(sprite.media)), n('costumes', {}, nsCostumes(sprite.media)),
@ -1379,21 +1397,25 @@ var sb = (function (sb) {
}); });
})); }));
} }
console.log(project);
preCustomBlocks('__global__', project.stage.customBlocks); preCustomBlocks('__global__', project.stage.customBlocks);
project.stage.sprites.forEach(function (sprite) { project.stage.sprites.forEach(function (sprite) {
preCustomBlocks(sprite.objName, sprite.customBlocks); preCustomBlocks(sprite.objName, sprite.customBlocks);
}); });
xml = n('project', { xml = n('project', {
name: projectName, name: projectName,
app: 'Snap! 4.0, http://snap.berkeley.edu; serialized by yprxml, http://dl.dropbox.com/u/10715865/Web/sb/yprxml.html', app: 'Snap! 4.0, http://snap.berkeley.edu; serialized by yprxml, http://dl.dropbox.com/u/10715865/Web/snap/app/yprxml.html',
version: '1' version: '1'
}, [ }, [
n('notes', {}, [t(project.info.get('comment') || '')]), n('notes', {}, [t(project.info.get('comment') || '')]),
n('thumbnail', {}, [t((a = project.info.get('thumbnail')) ? a.image.toDataURL() : '')]), n('thumbnail', {}, [t((a = project.info.get('thumbnail')) ? a.image.toDataURL() : '')]),
id(n('stage', { id(n('stage', {
name: 'Stage',
tempo: project.stage.tempoBPM,
costume: costumeIndex(project.stage), costume: costumeIndex(project.stage),
threadsafe: 'false' threadsafe: 'false',
scheduled: true
}, [ }, [
n('pentrails', {}, [t((a = project.info.get('penTrails')) ? a.image.toDataURL() : '')]), n('pentrails', {}, [t((a = project.info.get('penTrails')) ? a.image.toDataURL() : '')]),
n('costumes', {}, nsCostumes(project.stage.media)), n('costumes', {}, nsCostumes(project.stage.media)),
@ -1407,6 +1429,9 @@ var sb = (function (sb) {
n('blocks', {}, nsCustomBlocks(project.stage.customBlocks, true)) n('blocks', {}, nsCustomBlocks(project.stage.customBlocks, true))
]); ]);
xml.toXML(out = []); xml.toXML(out = []);
var d = document.createElement('textarea');
d.style.position = 'absolute';
document.body.appendChild(d).value = out.join('');
return out.join(''); return out.join('');
}; };