Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variables to dropdown menu, and more goodies #3102

76 changes: 75 additions & 1 deletion src/lib/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default function (vm) {
type: 'field_dropdown',
name: name,
options: function () {
return start.concat(menuOptionsFn());
return start.concat(menuOptionsFn(this));
}
}
],
Expand Down Expand Up @@ -110,6 +110,75 @@ export default function (vm) {
return [[myself, '_myself_']].concat(spriteMenu());
};

const variablePropertyMenu = function (thisValue) {

This comment was marked as abuse.

// Fill the output array with sprite properties by default. This will be changed to stage properties if the
// stage is selected.
const xPosition = ScratchBlocks.ScratchMsgs.translate('SENSING_OF_XPOSITION', 'x position');
const yPosition = ScratchBlocks.ScratchMsgs.translate('SENSING_OF_YPOSITION', 'y position');
const direction = ScratchBlocks.ScratchMsgs.translate('SENSING_OF_DIRECTION', 'direction');
const costumeNumber = ScratchBlocks.ScratchMsgs.translate('SENSING_OF_COSTUMENUMBER', 'costume #');
const costumeName = ScratchBlocks.ScratchMsgs.translate('SENSING_OF_COSTUMENAME', 'costume name');
const size = ScratchBlocks.ScratchMsgs.translate('SENSING_OF_SIZE', 'size');
const volume = ScratchBlocks.ScratchMsgs.translate('SENSING_OF_VOLUME', 'volume');
let output = [
[xPosition, 'x position'],
[yPosition, 'y position'],
[direction, 'direction'],
[costumeNumber, 'costume #'],
[costumeName, 'costume name'],
[size, 'size'],
[volume, 'volume']
];

This comment was marked as abuse.


const sourceBlock = thisValue.sourceBlock_; // This is the <shadow>.
const ofBlock = sourceBlock && sourceBlock.parentBlock_; // This is the "of" block.

This comment was marked as abuse.

if (ofBlock) {
let block;
let blocks;
if (vm.editingTarget) {
blocks = vm.editingTarget.blocks;
block = blocks.getBlock(ofBlock.id);
}
if (!block) {
// The block may be in the flyout.
blocks = vm.runtime.flyoutBlocks;
block = blocks.getBlock(ofBlock.id);
}
if (block) {
// Get the name of the sprite which is selected. This is based on the OBJECT dropdown's selected value,
// which may be different from the value returned by a block placed within that input, if present.
// TODO: Variables should probably not be fetched at all if the OBJECT dropdown contains a block.
towerofnix marked this conversation as resolved.
Show resolved Hide resolved
const objectInput = blocks.getInputs(block).OBJECT;
const objectInputBlock = blocks.getBlock(objectInput.block);
const valueField = blocks.getFields(objectInputBlock).OBJECT;
const objectName = valueField.value;

// Only return options for local variables of sprites, since the stage's local variables are really
// the project's global variables.
towerofnix marked this conversation as resolved.
Show resolved Hide resolved
if (objectName === '_stage_') {
// If the stage is the selected object (of the dropdown), only return properties relevant to it.
const backdropNumber = ScratchBlocks.ScratchMsgs.translate(
'SENSING_OF_BACKDROPNUMBER', 'backdrop #');
const backdropName = ScratchBlocks.ScratchMsgs.translate(
'SENSING_OF_BACKDROPNAME', 'backdrop name');
output = [
[backdropNumber, 'backdrop #'],
[backdropName, 'backdrop name'],
[volume, 'volume']
];
towerofnix marked this conversation as resolved.
Show resolved Hide resolved
} else {
const target = vm.runtime.getSpriteTargetByName(objectName);
if (target) {
// Pass true to skip the stage: we only want the sprite's own local variables.
const variableNames = target.getAllVariableNamesInScopeByType('', true);

This comment was marked as abuse.

output = output.concat(variableNames.map(name => [name, name]));
}
}
}
}
return output;
};

const soundColors = ScratchBlocks.Colours.sounds;

const looksColors = ScratchBlocks.Colours.looks;
Expand Down Expand Up @@ -180,6 +249,11 @@ export default function (vm) {
this.jsonInit(json);
};

ScratchBlocks.Blocks.sensing_of_property_menu.init = function () {
const json = jsonForMenuBlock('PROPERTY', variablePropertyMenu, sensingColors, []);
this.jsonInit(json);
};

ScratchBlocks.Blocks.sensing_distancetomenu.init = function () {
const mouse = ScratchBlocks.ScratchMsgs.translate('SENSING_DISTANCETO_POINTER', 'mouse-pointer');
const json = jsonForMenuBlock('DISTANCETOMENU', spriteMenu, sensingColors, [
Expand Down
3 changes: 3 additions & 0 deletions src/lib/make-toolbox-xml.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,9 @@ const sensing = function (isStage) {
<block type="sensing_resettimer"/>
${blockSeparator}
<block id="of" type="sensing_of">
<value name="PROPERTY">
<shadow id="sensing_of_property_menu" type="sensing_of_property_menu"/>
</value>
<value name="OBJECT">
<shadow id="sensing_of_object_menu" type="sensing_of_object_menu"/>
</value>
Expand Down