diff --git a/src/blocks/mrc_class_method_def.ts b/src/blocks/mrc_class_method_def.ts index 9543106a..66826404 100644 --- a/src/blocks/mrc_class_method_def.ts +++ b/src/blocks/mrc_class_method_def.ts @@ -38,6 +38,7 @@ import { MUTATOR_BLOCK_NAME, PARAM_CONTAINER_BLOCK_NAME, MethodMutatorArgBlock } export const BLOCK_NAME = 'mrc_class_method_def'; export const FIELD_METHOD_NAME = 'NAME'; +export const RETURN_VALUE = 'RETURN'; type Parameter = { name: string, @@ -54,6 +55,7 @@ interface ClassMethodDefMixin extends ClassMethodDefMixinType { mrcParameters: Parameter[], mrcPythonMethodName: string, mrcFuncName: string | null, + mrcUpdateReturnInput(): void, } type ClassMethodDefMixinType = typeof CLASS_METHOD_DEF; @@ -179,6 +181,7 @@ const CLASS_METHOD_DEF = { (this as Blockly.BlockSvg).setMutator(null); } this.mrcUpdateParams(); + this.mrcUpdateReturnInput(); }, compose: function (this: ClassMethodDefBlock, containerBlock: any) { // Parameter list. @@ -250,6 +253,21 @@ const CLASS_METHOD_DEF = { } } }, + mrcUpdateReturnInput: function (this: ClassMethodDefBlock) { + // Remove existing return input if it exists + if (this.getInput(RETURN_VALUE)) { + this.removeInput(RETURN_VALUE); + } + + // Add return input if return type is not 'None' + if (this.mrcReturnType && this.mrcReturnType !== 'None') { + this.appendValueInput(RETURN_VALUE) + .setAlign(Blockly.inputs.Align.RIGHT) + .appendField(Blockly.Msg.PROCEDURES_DEFRETURN_RETURN); + // Move the return input to be after the statement input + this.moveInputBefore('STACK', RETURN_VALUE); + } + }, removeParameterFields: function (input: Blockly.Input) { const fieldsToRemove = input.fieldRow .filter(field => field.name?.startsWith('PARAM_')) @@ -502,6 +520,23 @@ export function createCustomMethodBlock(): toolboxItems.Block { return new toolboxItems.Block(BLOCK_NAME, extraState, fields, null); } +export function createCustomMethodBlockWithReturn(): toolboxItems.Block { + const extraState: ClassMethodDefExtraState = { + canChangeSignature: true, + canBeCalledWithinClass: true, + canBeCalledOutsideClass: true, + returnType: 'Any', + params: [], + }; + const fields: {[key: string]: any} = {}; + fields[FIELD_METHOD_NAME] = 'my_method_with_return'; + const inputs: {[key: string]: any} = {}; + inputs[RETURN_VALUE] = { + 'type': 'input_value', + }; + return new toolboxItems.Block(BLOCK_NAME, extraState, fields, inputs); +} + export function getBaseClassBlocks( baseClassName: string): toolboxItems.Block[] { const blocks: toolboxItems.Block[] = []; diff --git a/src/toolbox/methods_category.ts b/src/toolbox/methods_category.ts index c6505850..66d5ab18 100644 --- a/src/toolbox/methods_category.ts +++ b/src/toolbox/methods_category.ts @@ -26,7 +26,7 @@ import * as storageModule from '../storage/module'; import { MRC_CATEGORY_STYLE_METHODS } from '../themes/styles'; import { CLASS_NAME_ROBOT_BASE, CLASS_NAME_OPMODE, CLASS_NAME_MECHANISM } from '../blocks/utils/python'; import { addInstanceWithinBlocks } from '../blocks/mrc_call_python_function'; -import { createCustomMethodBlock, getBaseClassBlocks, FIELD_METHOD_NAME } from '../blocks/mrc_class_method_def'; +import { createCustomMethodBlock, getBaseClassBlocks, FIELD_METHOD_NAME, createCustomMethodBlockWithReturn } from '../blocks/mrc_class_method_def'; import { Editor } from '../editor/editor'; @@ -113,6 +113,7 @@ class MethodsCategory { text: 'Custom Methods', }, createCustomMethodBlock(), + createCustomMethodBlockWithReturn() ); // Get blocks for calling methods defined in the current workspace.