summaryrefslogtreecommitdiff
path: root/snip.js
blob: 5ea1bc45ac99f7af29a9dfd793a9280ea5cefc61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
document.customBlocks = document.customBlocks || [];
document.customBlockList = document.customBlockList || [];

///////////////////////////////////////////////////
// -> insert custom block definitions here       //
document.customBlocks.push({test: {type: 'command', category: 'looks', spec: 'test %s'}});
document.customBlockList.push({category: 'looks', block: 'test'});
SpriteMorph.prototype.test = function (text) {
    alert("It works! " + text);
};
// end of insert, you can ignore the stuff below //
///////////////////////////////////////////////////

// helper functions
function objects_merge(a, b) {
    var res = {};
    for (var aattrname in a) {res[aattrname] = a[aattrname];}
    for (var battrname in b) {res[battrname] = b[battrname];}
    return res;
}

function block(selector) {
    var newBlock = SpriteMorph.prototype.blockForSelector(selector, true);
    newBlock.isTemplate = true;
    return newBlock;
}

if(!SpriteMorph.prototype.wasBlockTemplates) {
    SpriteMorph.prototype.wasBlockTemplates = SpriteMorph.prototype.blockTemplates;
    // re-define template function to inject custom blocks
    SpriteMorph.prototype.blockTemplates = function (category) {
        blocks = this.wasBlockTemplates(category);
        document.customBlockList.forEach(function (cblock) {
            if(cblock.category == category) {
                blocks.push(block(cblock.block));
            }
        });
        return blocks;
    };
}

if(!SpriteMorph.prototype.wasInitBlocks) {
    SpriteMorph.prototype.wasInitBlocks = SpriteMorph.prototype.initBlocks;
    // re-define block initialization function
    SpriteMorph.prototype.initBlocks = function () {
        this.wasInitBlocks();
        document.customBlocks.forEach(function (extBlock) {
            SpriteMorph.prototype.blocks = objects_merge(SpriteMorph.prototype.blocks, extBlock);
        });
    };
}

// refresh the palettes
world.children.forEach(function (child) {
    child.flushPaletteCache(); // TODO cache clearing does not work like expected
    child.refreshPalette();
    child.sprites.asArray().forEach(function (sprite) {
        sprite.initBlocks();
    });
});