scratch-gui
Version:
GraphicaL User Interface for creating and running Scratch 3.0 projects
200 lines (171 loc) • 6.89 kB
JavaScript
import ScratchBlocks from 'scratch-blocks';
/**
* Connect scratch blocks with the vm
* @param {VirtualMachine} vm - The scratch vm
* @return {ScratchBlocks} ScratchBlocks connected with the vm
*/
export default function (vm) {
const jsonForMenuBlock = function (name, menuOptionsFn, colors, start) {
return {
message0: '%1',
args0: [
{
type: 'field_dropdown',
name: name,
options: function () {
return start.concat(menuOptionsFn());
}
}
],
inputsInline: true,
output: 'String',
colour: colors.secondary,
colourSecondary: colors.secondary,
colourTertiary: colors.tertiary,
outputShape: ScratchBlocks.OUTPUT_SHAPE_ROUND
};
};
const jsonForHatBlockMenu = function (hatName, name, menuOptionsFn, colors, start) {
return {
message0: hatName,
args0: [
{
type: 'field_dropdown',
name: name,
options: function () {
return start.concat(menuOptionsFn());
}
}
],
colour: colors.primary,
colourSecondary: colors.secondary,
colourTertiary: colors.tertiary,
extensions: ['shape_hat']
};
};
const soundsMenu = function () {
if (vm.editingTarget && vm.editingTarget.sprite.sounds.length > 0) {
return vm.editingTarget.sprite.sounds.map(sound => [sound.name, sound.name]);
}
return [['', '']];
};
const costumesMenu = function () {
if (vm.editingTarget && vm.editingTarget.getCostumes().length > 0) {
return vm.editingTarget.getCostumes().map(costume => [costume.name, costume.name]);
}
return [['', '']];
};
const backdropsMenu = function () {
if (vm.runtime.targets[0] && vm.runtime.targets[0].getCostumes().length > 0) {
return vm.runtime.targets[0].getCostumes().map(costume => [costume.name, costume.name])
.concat([['next backdrop', 'next backdrop'],
['previous backdrop', 'previous backdrop'],
['random backdrop', 'random backdrop']]);
}
return [['', '']];
};
const backdropNamesMenu = function () {
const stage = vm.runtime.getTargetForStage();
if (stage && stage.getCostumes().length > 0) {
return stage.getCostumes().map(costume => [costume.name, costume.name]);
}
return [['', '']];
};
const spriteMenu = function () {
const sprites = [];
for (const targetId in vm.runtime.targets) {
if (!vm.runtime.targets.hasOwnProperty(targetId)) continue;
if (vm.runtime.targets[targetId].isOriginal) {
if (!vm.runtime.targets[targetId].isStage) {
if (vm.runtime.targets[targetId] === vm.editingTarget) {
continue;
}
sprites.push([vm.runtime.targets[targetId].sprite.name, vm.runtime.targets[targetId].sprite.name]);
}
}
}
return sprites;
};
const cloneMenu = function () {
if (vm.editingTarget && vm.editingTarget.isStage) {
const menu = spriteMenu();
if (menu.length === 0) {
return [['', '']]; // Empty menu matches Scratch 2 behavior
}
return menu;
}
return [['myself', '_myself_']].concat(spriteMenu());
};
const soundColors = ScratchBlocks.Colours.sounds;
const looksColors = ScratchBlocks.Colours.looks;
const motionColors = ScratchBlocks.Colours.motion;
const sensingColors = ScratchBlocks.Colours.sensing;
const controlColors = ScratchBlocks.Colours.control;
const eventColors = ScratchBlocks.Colours.event;
ScratchBlocks.Blocks.sound_sounds_menu.init = function () {
const json = jsonForMenuBlock('SOUND_MENU', soundsMenu, soundColors, []);
this.jsonInit(json);
};
ScratchBlocks.Blocks.looks_costume.init = function () {
const json = jsonForMenuBlock('COSTUME', costumesMenu, looksColors, []);
this.jsonInit(json);
};
ScratchBlocks.Blocks.looks_backdrops.init = function () {
const json = jsonForMenuBlock('BACKDROP', backdropsMenu, looksColors, []);
this.jsonInit(json);
};
ScratchBlocks.Blocks.event_whenbackdropswitchesto.init = function () {
const json = jsonForHatBlockMenu(
ScratchBlocks.Msg.EVENT_WHENBACKDROPSWITCHESTO,
'BACKDROP', backdropNamesMenu, eventColors, []);
this.jsonInit(json);
};
ScratchBlocks.Blocks.motion_pointtowards_menu.init = function () {
const json = jsonForMenuBlock('TOWARDS', spriteMenu, motionColors, [
['mouse-pointer', '_mouse_']
]);
this.jsonInit(json);
};
ScratchBlocks.Blocks.motion_goto_menu.init = function () {
const json = jsonForMenuBlock('TO', spriteMenu, motionColors, [
['random position', '_random_'],
['mouse-pointer', '_mouse_']
]);
this.jsonInit(json);
};
ScratchBlocks.Blocks.motion_glideto_menu.init = function () {
const json = jsonForMenuBlock('TO', spriteMenu, motionColors, [
['random position', '_random_'],
['mouse-pointer', '_mouse_']
]);
this.jsonInit(json);
};
ScratchBlocks.Blocks.sensing_of_object_menu.init = function () {
const json = jsonForMenuBlock('OBJECT', spriteMenu, sensingColors, [
['Stage', '_stage_']
]);
this.jsonInit(json);
};
ScratchBlocks.Blocks.sensing_distancetomenu.init = function () {
const json = jsonForMenuBlock('DISTANCETOMENU', spriteMenu, sensingColors, [
['mouse-pointer', '_mouse_']
]);
this.jsonInit(json);
};
ScratchBlocks.Blocks.sensing_touchingobjectmenu.init = function () {
const json = jsonForMenuBlock('TOUCHINGOBJECTMENU', spriteMenu, sensingColors, [
['mouse-pointer', '_mouse_'],
['edge', '_edge_']
]);
this.jsonInit(json);
};
ScratchBlocks.Blocks.control_create_clone_of_menu.init = function () {
const json = jsonForMenuBlock('CLONE_OPTION', cloneMenu, controlColors, []);
this.jsonInit(json);
};
ScratchBlocks.VerticalFlyout.getCheckboxState = function (blockId) {
const monitoredBlock = vm.runtime.monitorBlocks._blocks[blockId];
return monitoredBlock ? monitoredBlock.isMonitored : false;
};
return ScratchBlocks;
}