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

Behavior editor #22307

Merged
merged 12 commits into from
May 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"devDependencies": {
"@cdo/apps": "file:src",
"@code-dot-org/artist": "github:code-dot-org/artist#9c3ad003522aeab9d652fb1d8703a95d7ebde393",
"@code-dot-org/blockly": "2.0.1",
"@code-dot-org/blockly": "2.2.1",
"@code-dot-org/bramble": "0.1.26",
"@code-dot-org/craft": "github:code-dot-org/craft#46cddf8482ef7fad2cd1c4e9ce813c7fde4e8adf",
"@code-dot-org/johnny-five": "0.11.1-cdo.2",
Expand Down
6 changes: 5 additions & 1 deletion apps/src/gamelab/GameLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ GameLab.prototype.init = function (config) {

if (this.studioApp_.isUsingBlockly()) {
// Custom blockly config options for game lab jr
config.valueTypeTabShapeMap = { Sprite: 'angle' };
config.valueTypeTabShapeMap = {
Sprite: 'angle',
Behavior: 'rounded',
Location: 'square',
};

this.studioApp_.displayAlert('#belowVisualization', {type: 'warning', sideMargin: 0},
<div>
Expand Down
134 changes: 132 additions & 2 deletions apps/src/gamelab/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,12 @@ export default {
const createJsWrapperBlock = createJsWrapperBlockCreator(
blockly,
'gamelab',
[SPRITE_TYPE],
[
// Strict Types
blockly.BlockValueType.SPRITE,
blockly.BlockValueType.BEHAVIOR,
blockly.BlockValueType.LOCATION,
],
SPRITE_TYPE,
customInputTypes,
);
Expand Down Expand Up @@ -481,6 +486,19 @@ export default {
// Legacy style block definitions :(
const generator = blockly.Generator.get('JavaScript');

const behaviorEditor = new Blockly.FunctionEditor(
{
FUNCTION_HEADER: 'Behavior',
FUNCTION_NAME_LABEL: 'Name your behavior:',
FUNCTION_DESCRIPTION_LABEL: 'What is your behavior supposed to do?',
},
'behavior_definition',
{
[Blockly.BlockValueType.SPRITE]: 'sprite_parameter_get',
},
true /* disableParamEditing */,
);

Blockly.Blocks.sprite_variables_get = {
// Variable getter.
init: function () {
Expand Down Expand Up @@ -517,6 +535,30 @@ export default {
Blockly.Variables.registerGetter(Blockly.BlockValueType.SPRITE,
'sprite_variables_get');

Blockly.Blocks.sprite_parameter_get = {
init() {
var fieldLabel = new Blockly.FieldLabel(Blockly.Msg.VARIABLES_GET_ITEM);
// Must be marked EDITABLE so that cloned blocks share the same var name
fieldLabel.EDITABLE = true;
this.setHelpUrl(Blockly.Msg.VARIABLES_GET_HELPURL);
this.setHSV(7, 0.80, 0.95);
this.appendDummyInput()
.appendTitle(Blockly.Msg.VARIABLES_GET_TITLE)
.appendTitle(fieldLabel , 'VAR')
.appendTitle(Blockly.Msg.VARIABLES_GET_TAIL);
this.setStrictOutput(true, Blockly.BlockValueType.SPRITE);
this.setTooltip(Blockly.Msg.VARIABLES_GET_TOOLTIP);
},
renameVar(oldName, newName) {
if (behaviorEditor.isOpen()) {
behaviorEditor.renameParameter(oldName, newName);
behaviorEditor.refreshParamsEverywhere();
}
},
removeVar: Blockly.Blocks.variables_get.removeVar,
};
generator.sprite_parameter_get = generator.variables_get;

Blockly.Blocks.sprite_variables_set = {
// Variable setter.
init: function () {
Expand Down Expand Up @@ -551,14 +593,102 @@ export default {
generator.sprite_variables_set = generator.variables_set;
Blockly.Variables.registerSetter(Blockly.BlockValueType.SPRITE,
'sprite_variables_set');

Blockly.Blocks.gamelab_behavior_get = {
init() {
var fieldLabel = new Blockly.FieldLabel(Blockly.Msg.VARIABLES_GET_ITEM);
// Must be marked EDITABLE so that cloned blocks share the same var name
fieldLabel.EDITABLE = true;
this.setHelpUrl(Blockly.Msg.VARIABLES_GET_HELPURL);
this.setHSV(136, 0.84, 0.80);
const mainTitle = this.appendDummyInput()
.appendTitle(fieldLabel, 'VAR')
.appendTitle(Blockly.Msg.VARIABLES_GET_TAIL);

if (Blockly.useModalFunctionEditor) {
var editLabel = new Blockly.FieldIcon(Blockly.Msg.FUNCTION_EDIT);
Blockly.bindEvent_(editLabel.fieldGroup_, 'mousedown', this, this.openEditor);
mainTitle.appendTitle(editLabel);
}

this.setStrictOutput(true, Blockly.BlockValueType.BEHAVIOR);
this.setTooltip(Blockly.Msg.VARIABLES_GET_TOOLTIP);
},

openEditor(e) {
e.stopPropagation();
behaviorEditor.openEditorForFunction(this, this.getTitleValue('VAR'));
},

getVars() {
return Blockly.Variables.getVars.bind(this)(Blockly.BlockValueType.BEHAVIOR);
},

renameVar(oldName, newName) {
if (Blockly.Names.equals(oldName, this.getTitleValue('VAR'))) {
this.setTitleValue(newName, 'VAR');
}
},

renameProcedure(oldName, newName) {
if (Blockly.Names.equals(oldName, this.getTitleValue('VAR'))) {
this.setTitleValue(newName, 'VAR');
}
},
};

generator.gamelab_behavior_get = function () {
return [
Blockly.JavaScript.variableDB_.getName(
this.getTitleValue('VAR'),
Blockly.Procedures.NAME_TYPE),
Blockly.JavaScript.ORDER_ATOMIC
];
};

Blockly.Blocks.behavior_definition = Blockly.Block.createProcedureDefinitionBlock({
initPostScript(block) {
block.setHSV(136, 0.84, 0.80);
block.parameterNames_ = ['this sprite'];
block.parameterTypes_ = [Blockly.BlockValueType.SPRITE];
},
overrides: {
getVars(category) {
return {
Behavior: [this.getTitleValue('NAME')],
};
},
callType_: 'gamelab_behavior_get',
}
});

generator.behavior_definition = generator.procedures_defnoreturn;

Blockly.Procedures.DEFINITION_BLOCK_TYPES.push('behavior_definition');
Blockly.Variables.registerGetter(Blockly.BlockValueType.BEHAVIOR,
'gamelab_behavior_get');
Blockly.Flyout.configure(Blockly.BlockValueType.BEHAVIOR, {
initialize(flyout, cursor) {
if (behaviorEditor && !behaviorEditor.isOpen()) {
flyout.addButtonToFlyout_(cursor, 'Create a Behavior',
behaviorEditor.openWithNewFunction.bind(behaviorEditor));
}
},
addDefaultVar: false,
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't love how much manual work is required here to get this to work, particularly given how much of it is just copying stuff from procedures_defnoreturn. Can some of this be pushed into Blockly and generalized? (possibly in a separate PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a helper in code-dot-org/blockly#107 and used it in 1a2037d, cutting out quite a lot of the extra junk here.

},

installCustomBlocks(blockly, blockInstallOptions, customBlocks, level, hideCustomBlocks) {
const SPRITE_TYPE = blockly.BlockValueType.SPRITE;
const blockNames = customBlocks.map(createJsWrapperBlockCreator(
blockly,
'gamelab',
[SPRITE_TYPE],
[
// Strict Types
blockly.BlockValueType.SPRITE,
blockly.BlockValueType.BEHAVIOR,
blockly.BlockValueType.LOCATION,
],
SPRITE_TYPE,
customInputTypes,
));
Expand Down
6 changes: 3 additions & 3 deletions apps/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
version "0.0.1"
resolved "https://codeload.github.com/code-dot-org/artist/tar.gz/9c3ad003522aeab9d652fb1d8703a95d7ebde393"

"@code-dot-org/blockly@2.0.1":
version "2.0.1"
resolved "https://registry.yarnpkg.com/@code-dot-org/blockly/-/blockly-2.0.1.tgz#1c9f8c2282842727041324d764186ddf4eb26112"
"@code-dot-org/blockly@2.2.1":
version "2.2.1"
resolved "https://registry.yarnpkg.com/@code-dot-org/blockly/-/blockly-2.2.1.tgz#ab5c086613686ed3d3b571bdbde6a405fe80e8e8"

"@code-dot-org/bramble@0.1.26":
version "0.1.26"
Expand Down
1 change: 1 addition & 0 deletions dashboard/app/models/levels/gamelab_jr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ def common_blocks(type)
<block type="text_join_simple" inputcount="2" />
<block type="text" />
</category>
<category name="Behaviors" custom="Behavior" />
XML
end

Expand Down