diff options
| author | Jens Mönig <jens@moenig.org> | 2015-03-21 11:12:43 +0100 |
|---|---|---|
| committer | Jens Mönig <jens@moenig.org> | 2015-03-21 11:12:43 +0100 |
| commit | 4acf1896bd9fa57d3dbe3f4721b6790c6f3b0d56 (patch) | |
| tree | 8d44be9ed6bb5458d4354d59200783669dbc5cb2 /store.js | |
| parent | fdd2ecf7d91d8026d3ca42c4bd52774877c72645 (diff) | |
| download | snap-4acf1896bd9fa57d3dbe3f4721b6790c6f3b0d56.tar.gz snap-4acf1896bd9fa57d3dbe3f4721b6790c6f3b0d56.zip | |
Prototypal inheritance of sprite-local variables
(experimental) slotwise inheritance à la Henry Lieberman for
sprite-local variables.
see
http://web.media.mit.edu/~lieber/Lieberary/OOP/Delegation/Delegation.htm
l
Let a sprite inherit another sprite’s local variables by making it the
“parent” in the sprite-icon’s context menu (the button icon in the
sprite corral underneath the stage). The child not only inherits the
variable slot but also - dynamically - the parent variable’s value.
Changing the parent’s variable value also changes it for every child.
If a child uses SET or CHANGE on an inherited variable it automatically
“shadows” it with its own value, thereby stopping dynamic participation
in the parent slot’s value (in effect dis-inheriting that slot).
Deleting a shadowed variable slot once again reinstates its inheritance
status.
inherited variables are shown as “ghosted” both in the child’s
variables palette and in such stage watchers. “Shadowing” them
un-ghosts both the variable blob template in the palette and the
watcher onstage (if any). Deleting a shadowed variable once again
ghosts the watcher and the palette block template.
Delete a (shadowed) variable either via the “Delete a variable” button
in the IDE or using the new “Delete” block in the variables category
Diffstat (limited to 'store.js')
| -rw-r--r-- | store.js | 45 |
1 files changed, 40 insertions, 5 deletions
@@ -61,7 +61,7 @@ SyntaxElementMorph, Variable*/ // Global stuff //////////////////////////////////////////////////////// -modules.store = '2015-March-15'; +modules.store = '2015-March-21'; // XML_Serializer /////////////////////////////////////////////////////// @@ -459,9 +459,17 @@ SnapSerializer.prototype.rawLoadProjectModel = function (xmlNode) { myself.loadValue(model); }); - // restore nesting associations + // restore inheritance and nesting associations myself.project.stage.children.forEach(function (sprite) { - var anchor; + var exemplar, anchor; + if (sprite.inheritanceInfo) { // only sprites can inherit + exemplar = myself.project.sprites[ + sprite.inheritanceInfo.exemplar + ]; + if (exemplar) { + sprite.setExemplar(exemplar); + } + } if (sprite.nestingInfo) { // only sprites may have nesting info anchor = myself.project.sprites[sprite.nestingInfo.anchor]; if (anchor) { @@ -471,6 +479,7 @@ SnapSerializer.prototype.rawLoadProjectModel = function (xmlNode) { } }); myself.project.stage.children.forEach(function (sprite) { + delete sprite.inheritanceInfo; if (sprite.nestingInfo) { // only sprites may have nesting info sprite.nestingScale = +(sprite.nestingInfo.scale || sprite.scale); delete sprite.nestingInfo; @@ -639,9 +648,17 @@ SnapSerializer.prototype.loadSprites = function (xmlString, ide) { myself.loadObject(sprite, model); }); - // restore nesting associations + // restore inheritance and nesting associations project.stage.children.forEach(function (sprite) { - var anchor; + var exemplar, anchor; + if (sprite.inheritanceInfo) { // only sprites can inherit + exemplar = project.sprites[ + sprite.inheritanceInfo.exemplar + ]; + if (exemplar) { + sprite.setExemplar(exemplar); + } + } if (sprite.nestingInfo) { // only sprites may have nesting info anchor = project.sprites[sprite.nestingInfo.anchor]; if (anchor) { @@ -651,6 +668,7 @@ SnapSerializer.prototype.loadSprites = function (xmlString, ide) { } }); project.stage.children.forEach(function (sprite) { + delete sprite.inheritanceInfo; if (sprite.nestingInfo) { // only sprites may have nesting info sprite.nestingScale = +(sprite.nestingInfo.scale || sprite.scale); delete sprite.nestingInfo; @@ -690,6 +708,7 @@ SnapSerializer.prototype.loadMediaModel = function (xmlNode) { SnapSerializer.prototype.loadObject = function (object, model) { // private var blocks = model.require('blocks'); + this.loadInheritanceInfo(object, model); this.loadNestingInfo(object, model); this.loadCostumes(object, model); this.loadSounds(object, model); @@ -699,6 +718,14 @@ SnapSerializer.prototype.loadObject = function (object, model) { this.loadScripts(object.scripts, model.require('scripts')); }; +SnapSerializer.prototype.loadInheritanceInfo = function (object, model) { + // private + var info = model.childNamed('inherit'); + if (info) { + object.inheritanceInfo = info.attributes; + } +}; + SnapSerializer.prototype.loadNestingInfo = function (object, model) { // private var info = model.childNamed('nest'); @@ -1467,6 +1494,7 @@ SpriteMorph.prototype.toXML = function (serializer) { ' draggable="@"' + '%' + ' costume="@" color="@,@,@" pen="@" ~>' + + '%' + // inheritance info '%' + // nesting info '<costumes>%</costumes>' + '<sounds>%</sounds>' + @@ -1489,6 +1517,13 @@ SpriteMorph.prototype.toXML = function (serializer) { this.color.b, this.penPoint, + // inheritance info + this.exemplar + ? '<inherit exemplar="' + + this.exemplar.name + + '"/>' + : '', + // nesting info this.anchor ? '<nest anchor="' + |
