Skip to content
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
664 changes: 331 additions & 333 deletions src/blocks/mrc_call_python_function.ts

Large diffs are not rendered by default.

82 changes: 71 additions & 11 deletions src/blocks/mrc_class_method_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ import { Order } from 'blockly/python';
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
import * as commonStorage from '../storage/common_storage';
import { renameMethodCallers, mutateMethodCallers } from './mrc_call_python_function'
import * as toolboxItems from '../toolbox/items';
import { getClassData } from './utils/python';
import { FunctionData } from './utils/python_json_types';
import { findConnectedBlocksOfType } from './utils/find_connected_blocks';
import { BLOCK_NAME as MRC_GET_PARAMETER_BLOCK_NAME } from './mrc_get_parameter';
import { MUTATOR_BLOCK_NAME, PARAM_CONTAINER_BLOCK_NAME, MethodMutatorArgBlock } from './mrc_param_container'

export const BLOCK_NAME = 'mrc_class_method_def';

const FIELD_METHOD_NAME = 'NAME';

export type Parameter = {
name: string,
type?: string,
Expand Down Expand Up @@ -88,7 +93,7 @@ const CLASS_METHOD_DEF = {
*/
init: function (this: ClassMethodDefBlock): void {
this.appendDummyInput("TITLE")
.appendField('', 'NAME');
.appendField('', FIELD_METHOD_NAME);
this.setOutput(false);
this.setStyle(MRC_STYLE_FUNCTIONS);
this.appendStatementInput('STACK').appendField('');
Expand Down Expand Up @@ -143,27 +148,27 @@ const CLASS_METHOD_DEF = {
});
});
this.updateBlock_();
mutateMethodCallers(this.workspace, this.getFieldValue('NAME'), this.saveExtraState());
mutateMethodCallers(this.workspace, this.getFieldValue(FIELD_METHOD_NAME), this.saveExtraState());
},
/**
* Update the block to reflect the newly loaded extra state.
*/
updateBlock_: function (this: ClassMethodDefBlock): void {
const name = this.getFieldValue('NAME');
const name = this.getFieldValue(FIELD_METHOD_NAME);
const input = this.getInput('TITLE');
if (!input) {
return;
}
input.removeField('NAME');
input.removeField(FIELD_METHOD_NAME);

if (this.mrcCanChangeSignature) {
const nameField = new Blockly.FieldTextInput(name);
input.insertFieldAt(0, nameField, 'NAME');
input.insertFieldAt(0, nameField, FIELD_METHOD_NAME);
this.setMutator(new Blockly.icons.MutatorIcon([MUTATOR_BLOCK_NAME], this));
nameField.setValidator(this.mrcNameFieldValidator.bind(this, nameField));
}
else {
input.insertFieldAt(0, createFieldNonEditableText(name), 'NAME');
input.insertFieldAt(0, createFieldNonEditableText(name), FIELD_METHOD_NAME);
//Case because a current bug in blockly where it won't allow passing null to Blockly.Block.setMutator makes it necessary.
(this as Blockly.BlockSvg).setMutator(null);
}
Expand All @@ -189,7 +194,7 @@ const CLASS_METHOD_DEF = {
paramBlock.nextConnection && paramBlock.nextConnection.targetBlock();
}
this.mrcUpdateParams();
mutateMethodCallers(this.workspace, this.getFieldValue('NAME'), this.saveExtraState());
mutateMethodCallers(this.workspace, this.getFieldValue(FIELD_METHOD_NAME), this.saveExtraState());
},
decompose: function (this: ClassMethodDefBlock, workspace: Blockly.Workspace) {
// This is a special sub-block that only gets created in the mutator UI.
Expand Down Expand Up @@ -268,7 +273,7 @@ const CLASS_METHOD_DEF = {
return this.mrcCanChangeSignature;
},
getMethodName: function (this: ClassMethodDefBlock): string {
return this.getFieldValue('NAME');
return this.getFieldValue(FIELD_METHOD_NAME);
},
};

Expand Down Expand Up @@ -315,7 +320,7 @@ function isMethodNameUsed(
if (block === opt_exclude) {
continue;
}
if (nameLowerCase === block.getFieldValue('NAME').toLowerCase()) {
if (nameLowerCase === block.getFieldValue(FIELD_METHOD_NAME).toLowerCase()) {
return true;
}
const classMethodDefBlock = block as ClassMethodDefBlock;
Expand All @@ -335,7 +340,7 @@ export const pythonFromBlock = function (
block: ClassMethodDefBlock,
generator: ExtendedPythonGenerator,
) {
const blocklyName = block.mrcPythonMethodName ? block.mrcPythonMethodName : block.getFieldValue('NAME');
const blocklyName = block.mrcPythonMethodName ? block.mrcPythonMethodName : block.getFieldValue(FIELD_METHOD_NAME);

const funcName = generator.getProcedureName(blocklyName);

Expand Down Expand Up @@ -415,7 +420,7 @@ export const pythonFromBlock = function (
// Update the mrcMethod.
block.mrcMethod = {
blockId: block.id,
visibleName: block.getFieldValue('NAME'),
visibleName: block.getFieldValue(FIELD_METHOD_NAME),
pythonName: funcName,
returnType: block.mrcReturnType,
args: [{
Expand All @@ -433,3 +438,58 @@ export const pythonFromBlock = function (

return '';
}

// Functions used for creating blocks for the toolbox.

export function createCustomMethodBlock(): toolboxItems.Block {
const extraState: ClassMethodDefExtraState = {
canChangeSignature: true,
canBeCalledWithinClass: true,
canBeCalledOutsideClass: true,
returnType: 'None',
params: [],
};
const fields: {[key: string]: any} = {};
fields[FIELD_METHOD_NAME] = 'my_method';
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, null);
}

export function getBaseClassBlocks(
baseClassName: string): toolboxItems.Block[] {
const blocks: toolboxItems.Block[] = [];
const classData = getClassData(baseClassName);
if (classData) {
classData.instanceMethods.forEach(functionData => {
blocks.push(createClassMethodDefBlock(
functionData,
/* canChangeSignature */ false,
/* canBeCalledWithinClass */ false,
/* canBeCalledOutsideClass */ false,
));
});
}
return blocks;
}

function createClassMethodDefBlock(
functionData: FunctionData,
canChangeSignature: boolean,
canBeCalledWithinClass: boolean,
canBeCalledOutsideClass: boolean): toolboxItems.Block {
const extraState: ClassMethodDefExtraState = {
canChangeSignature,
canBeCalledWithinClass,
canBeCalledOutsideClass,
returnType: functionData.returnType,
params: [],
};
for (let i = 1; i < functionData.args.length; i++) {
extraState.params.push({
'name': functionData.args[i].name,
'type': functionData.args[i].type,
});
}
const fields: {[key: string]: any} = {};
fields[FIELD_METHOD_NAME] = functionData.functionName;
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, null);
}
24 changes: 19 additions & 5 deletions src/blocks/mrc_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
import { MUTATOR_BLOCK_NAME, PARAM_CONTAINER_BLOCK_NAME, MethodMutatorArgBlock } from './mrc_param_container'
import * as ChangeFramework from './utils/change_framework';
import { BLOCK_NAME as MRC_MECHANISM_COMPONENT_HOLDER } from './mrc_mechanism_component_holder';
import * as toolboxItems from '../toolbox/items';

export const BLOCK_NAME = 'mrc_event';
export const OUTPUT_NAME = 'mrc_event';

const FIELD_EVENT_NAME = 'NAME';

export type Parameter = {
name: string,
type?: string,
Expand All @@ -54,7 +57,7 @@ const EVENT = {
init: function (this: EventBlock): void {
this.setStyle(MRC_STYLE_EVENTS);
this.appendDummyInput("TITLE")
.appendField(new Blockly.FieldTextInput('my_event'), 'NAME');
.appendField(new Blockly.FieldTextInput('my_event'), FIELD_EVENT_NAME);
this.setPreviousStatement(true, OUTPUT_NAME);
this.setNextStatement(true, OUTPUT_NAME);
this.setMutator(new Blockly.icons.MutatorIcon([MUTATOR_BLOCK_NAME], this));
Expand Down Expand Up @@ -99,15 +102,15 @@ const EVENT = {
* Update the block to reflect the newly loaded extra state.
*/
updateBlock_: function (this: EventBlock): void {
const name = this.getFieldValue('NAME');
const name = this.getFieldValue(FIELD_EVENT_NAME);
const input = this.getInput('TITLE');
if (!input) {
return;
}
input.removeField('NAME');
input.removeField(FIELD_EVENT_NAME);

const nameField = new Blockly.FieldTextInput(name);
input.insertFieldAt(0, nameField, 'NAME');
input.insertFieldAt(0, nameField, FIELD_EVENT_NAME);
this.setMutator(new Blockly.icons.MutatorIcon([MUTATOR_BLOCK_NAME], this));
// nameField.setValidator(this.mrcNameFieldValidator.bind(this, nameField));

Expand All @@ -132,7 +135,7 @@ const EVENT = {
paramBlock.nextConnection && paramBlock.nextConnection.targetBlock();
}
this.mrcUpdateParams();
//mutateMethodCallers(this.workspace, this.getFieldValue('NAME'), this.saveExtraState());
//mutateMethodCallers(this.workspace, this.getFieldValue(FIELD_EVENT_NAME), this.saveExtraState());
},
decompose: function (this: EventBlock, workspace: Blockly.Workspace) {
// This is a special sub-block that only gets created in the mutator UI.
Expand Down Expand Up @@ -206,3 +209,14 @@ export const pythonFromBlock = function (
//TODO (Alan): What should this do here??
return '';
}

// Functions used for creating blocks for the toolbox.

export function createCustomEventBlock(): toolboxItems.Block {
const extraState: EventExtraState = {
params: [],
};
const fields: {[key: string]: any} = {};
fields[FIELD_EVENT_NAME] = 'my_event';
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, null);
}
46 changes: 22 additions & 24 deletions src/blocks/mrc_get_python_enum_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,6 @@ export function initializeEnum(
PythonEnumTooltips[enumClassName] = tooltips;
}

// Functions used for creating blocks for the toolbox.

export function addEnumBlocks(enums: EnumData[], contents: toolboxItems.ContentsType[]) {
for (const enumData of enums) {
for (const enumValue of enumData.enumValues) {
const block = createEnumBlock(enumValue, enumData);
contents.push(block);
}
}
}

function createEnumBlock(enumValue: string, enumData: EnumData): toolboxItems.Block {
const extraState: GetPythonEnumValueExtraState = {
enumType: enumData.enumClassName,
importModule: enumData.moduleName,
};
const fields: {[key: string]: any} = {};
fields[FIELD_ENUM_CLASS_NAME] = enumData.enumClassName;
fields[FIELD_ENUM_VALUE] = enumValue;
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, null);
}

//..............................................................................

type GetPythonEnumValueBlock = Blockly.Block & GetPythonEnumValueMixin;
interface GetPythonEnumValueMixin extends GetPythonEnumValueMixinType {
mrcEnumType: string,
Expand Down Expand Up @@ -175,3 +151,25 @@ export const pythonFromBlock = function(
const code = enumClassName + '.' + enumValue;
return [code, Order.MEMBER];
};

// Functions used for creating blocks for the toolbox.

export function addEnumBlocks(enums: EnumData[], contents: toolboxItems.ContentsType[]) {
for (const enumData of enums) {
for (const enumValue of enumData.enumValues) {
const block = createEnumBlock(enumValue, enumData);
contents.push(block);
}
}
}

function createEnumBlock(enumValue: string, enumData: EnumData): toolboxItems.Block {
const extraState: GetPythonEnumValueExtraState = {
enumType: enumData.enumClassName,
importModule: enumData.moduleName,
};
const fields: {[key: string]: any} = {};
fields[FIELD_ENUM_CLASS_NAME] = enumData.enumClassName;
fields[FIELD_ENUM_VALUE] = enumValue;
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, null);
}
Loading