Skip to content

Commit

Permalink
Add per-block extensions and colors (#177)
Browse files Browse the repository at this point in the history
Co-authored-by: Muffin <[email protected]>
  • Loading branch information
LilyMakesThings and GarboMuffin authored Dec 31, 2023
1 parent b27142a commit 7f9c8f7
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1303,9 +1303,10 @@ class Runtime extends EventEmitter {
type: extendedOpcode,
inputsInline: true,
category: categoryInfo.name,
colour: categoryInfo.color1,
colourSecondary: categoryInfo.color2,
colourTertiary: categoryInfo.color3
extensions: blockInfo.extensions ?? [],
colour: blockInfo.color1 ?? categoryInfo.color1,
colourSecondary: blockInfo.color2 ?? categoryInfo.color2,
colourTertiary: blockInfo.color3 ?? categoryInfo.color3
};
const context = {
// TODO: store this somewhere so that we can map args appropriately after translation.
Expand All @@ -1325,7 +1326,9 @@ class Runtime extends EventEmitter {
const iconURI = blockInfo.blockIconURI || categoryInfo.blockIconURI;

if (iconURI) {
blockJSON.extensions = ['scratch_extension'];
if (!blockJSON.extensions.includes('scratch_extension')) {
blockJSON.extensions.push('scratch_extension');
}
blockJSON.message0 = '%1 %2';
const iconJSON = {
type: 'field_image',
Expand Down
130 changes: 130 additions & 0 deletions test/unit/tw_block_colors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
const {test} = require('tap');
const VirtualMachine = require('../../src/virtual-machine');
const BlockType = require('../../src/extension-support/block-type');

test('with explicit category colors', t => {
const vm = new VirtualMachine();
vm.extensionManager._registerInternalExtension({
getInfo: () => ({
id: 'testextension',
name: 'test',
color1: '#ff0000',
color2: '#00ff00',
color3: '#0000ff',
blocks: [
{
blockType: BlockType.COMMAND,
opcode: 'defaults'
},
{
blockType: BlockType.COMMAND,
opcode: 'color1',
color1: '#ffff00'
},
{
blockType: BlockType.COMMAND,
opcode: 'color2',
color2: '#ff00ff'
},
{
blockType: BlockType.COMMAND,
opcode: 'color3',
color3: '#00ffff'
},
{
blockType: BlockType.COMMAND,
opcode: 'colorAll',
color1: '#7fff00',
color2: '#ff007f',
color3: '#007fff'
}
]
})
});

const blocks = vm.runtime.getBlocksJSON();

t.equal(blocks[0].colour, '#ff0000');
t.equal(blocks[0].colourSecondary, '#00ff00');
t.equal(blocks[0].colourTertiary, '#0000ff');

t.equal(blocks[1].colour, '#ffff00');
t.equal(blocks[1].colourSecondary, '#00ff00');
t.equal(blocks[1].colourTertiary, '#0000ff');

t.equal(blocks[2].colour, '#ff0000');
t.equal(blocks[2].colourSecondary, '#ff00ff');
t.equal(blocks[2].colourTertiary, '#0000ff');

t.equal(blocks[3].colour, '#ff0000');
t.equal(blocks[3].colourSecondary, '#00ff00');
t.equal(blocks[3].colourTertiary, '#00ffff');

t.equal(blocks[4].colour, '#7fff00');
t.equal(blocks[4].colourSecondary, '#ff007f');
t.equal(blocks[4].colourTertiary, '#007fff');

t.end();
});

test('with the default colors', t => {
const vm = new VirtualMachine();
vm.extensionManager._registerInternalExtension({
getInfo: () => ({
id: 'testextension',
name: 'test',
blocks: [
{
blockType: BlockType.COMMAND,
opcode: 'defaults'
},
{
blockType: BlockType.COMMAND,
opcode: 'color1',
color1: '#ffff00'
},
{
blockType: BlockType.COMMAND,
opcode: 'color2',
color2: '#ff00ff'
},
{
blockType: BlockType.COMMAND,
opcode: 'color3',
color3: '#00ffff'
},
{
blockType: BlockType.COMMAND,
opcode: 'colorAll',
color1: '#7fff00',
color2: '#ff007f',
color3: '#007fff'
}
]
})
});

const blocks = vm.runtime.getBlocksJSON();

t.equal(blocks[0].colour, '#0FBD8C');
t.equal(blocks[0].colourSecondary, '#0DA57A');
t.equal(blocks[0].colourTertiary, '#0B8E69');

t.equal(blocks[1].colour, '#ffff00');
t.equal(blocks[1].colourSecondary, '#0DA57A');
t.equal(blocks[1].colourTertiary, '#0B8E69');

t.equal(blocks[2].colour, '#0FBD8C');
t.equal(blocks[2].colourSecondary, '#ff00ff');
t.equal(blocks[2].colourTertiary, '#0B8E69');

t.equal(blocks[3].colour, '#0FBD8C');
t.equal(blocks[3].colourSecondary, '#0DA57A');
t.equal(blocks[3].colourTertiary, '#00ffff');

t.equal(blocks[4].colour, '#7fff00');
t.equal(blocks[4].colourSecondary, '#ff007f');
t.equal(blocks[4].colourTertiary, '#007fff');

t.end();
});
57 changes: 57 additions & 0 deletions test/unit/tw_block_extensions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const {test} = require('tap');
const VirtualMachine = require('../../src/virtual-machine');
const BlockType = require('../../src/extension-support/block-type');

test('extensions', t => {
const vm = new VirtualMachine();
vm.extensionManager._registerInternalExtension({
getInfo: () => ({
id: 'testextension',
name: 'test',
blocks: [
{
blockType: BlockType.COMMAND,
opcode: 'nothing'
},
{
blockType: BlockType.COMMAND,
opcode: 'empty',
extensions: []
},
{
blockType: BlockType.COMMAND,
opcode: 'iconNothing',
blockIconURI: 'data:whatever'
},
{
blockType: BlockType.COMMAND,
opcode: 'iconEmpty',
blockIconURI: 'data:whatever',
extensions: []
},
{
blockType: BlockType.COMMAND,
opcode: 'iconSomething',
blockIconURI: 'data:whatever',
extensions: ['colours_sensing']
},
{
blockType: BlockType.COMMAND,
opcode: 'iconDuplicate',
blockIconURI: 'data:whatever',
extensions: ['colours_sensing', 'scratch_extension']
}
]
})
});

const blocks = vm.runtime.getBlocksJSON();
t.same(blocks[0].extensions, []);
t.same(blocks[1].extensions, []);
t.same(blocks[2].extensions, ['scratch_extension']);
t.same(blocks[3].extensions, ['scratch_extension']);
t.same(blocks[4].extensions, ['colours_sensing', 'scratch_extension']);
t.same(blocks[5].extensions, ['colours_sensing', 'scratch_extension']);

t.end();
});

0 comments on commit 7f9c8f7

Please sign in to comment.