Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
35 changes: 35 additions & 0 deletions src/blocks/mrc_class_method_def.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -54,6 +55,7 @@ interface ClassMethodDefMixin extends ClassMethodDefMixinType {
mrcParameters: Parameter[],
mrcPythonMethodName: string,
mrcFuncName: string | null,
mrcUpdateReturnInput(): void,
}
type ClassMethodDefMixinType = typeof CLASS_METHOD_DEF;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 before the statement input
this.moveInputBefore(RETURN_VALUE, 'STACK');
}
},
removeParameterFields: function (input: Blockly.Input) {
const fieldsToRemove = input.fieldRow
.filter(field => field.name?.startsWith('PARAM_'))
Expand Down Expand Up @@ -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[] = [];
Expand Down
3 changes: 2 additions & 1 deletion src/toolbox/methods_category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';


Expand Down Expand Up @@ -113,6 +113,7 @@ class MethodsCategory {
text: 'Custom Methods',
},
createCustomMethodBlock(),
createCustomMethodBlockWithReturn()
);

// Get blocks for calling methods defined in the current workspace.
Expand Down