Skip to content

Commit 457b477

Browse files
authored
Make it so you can make methods that return a value (#260)
* Make it so you can make methods that return a value * Change return to be at end of block
1 parent 912c007 commit 457b477

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

src/blocks/mrc_class_method_def.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import { MUTATOR_BLOCK_NAME, PARAM_CONTAINER_BLOCK_NAME, MethodMutatorArgBlock }
3838
export const BLOCK_NAME = 'mrc_class_method_def';
3939

4040
export const FIELD_METHOD_NAME = 'NAME';
41+
export const RETURN_VALUE = 'RETURN';
4142

4243
type Parameter = {
4344
name: string,
@@ -54,6 +55,7 @@ interface ClassMethodDefMixin extends ClassMethodDefMixinType {
5455
mrcParameters: Parameter[],
5556
mrcPythonMethodName: string,
5657
mrcFuncName: string | null,
58+
mrcUpdateReturnInput(): void,
5759
}
5860
type ClassMethodDefMixinType = typeof CLASS_METHOD_DEF;
5961

@@ -179,6 +181,7 @@ const CLASS_METHOD_DEF = {
179181
(this as Blockly.BlockSvg).setMutator(null);
180182
}
181183
this.mrcUpdateParams();
184+
this.mrcUpdateReturnInput();
182185
},
183186
compose: function (this: ClassMethodDefBlock, containerBlock: any) {
184187
// Parameter list.
@@ -250,6 +253,21 @@ const CLASS_METHOD_DEF = {
250253
}
251254
}
252255
},
256+
mrcUpdateReturnInput: function (this: ClassMethodDefBlock) {
257+
// Remove existing return input if it exists
258+
if (this.getInput(RETURN_VALUE)) {
259+
this.removeInput(RETURN_VALUE);
260+
}
261+
262+
// Add return input if return type is not 'None'
263+
if (this.mrcReturnType && this.mrcReturnType !== 'None') {
264+
this.appendValueInput(RETURN_VALUE)
265+
.setAlign(Blockly.inputs.Align.RIGHT)
266+
.appendField(Blockly.Msg.PROCEDURES_DEFRETURN_RETURN);
267+
// Move the return input to be after the statement input
268+
this.moveInputBefore('STACK', RETURN_VALUE);
269+
}
270+
},
253271
removeParameterFields: function (input: Blockly.Input) {
254272
const fieldsToRemove = input.fieldRow
255273
.filter(field => field.name?.startsWith('PARAM_'))
@@ -502,6 +520,23 @@ export function createCustomMethodBlock(): toolboxItems.Block {
502520
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, null);
503521
}
504522

523+
export function createCustomMethodBlockWithReturn(): toolboxItems.Block {
524+
const extraState: ClassMethodDefExtraState = {
525+
canChangeSignature: true,
526+
canBeCalledWithinClass: true,
527+
canBeCalledOutsideClass: true,
528+
returnType: 'Any',
529+
params: [],
530+
};
531+
const fields: {[key: string]: any} = {};
532+
fields[FIELD_METHOD_NAME] = 'my_method_with_return';
533+
const inputs: {[key: string]: any} = {};
534+
inputs[RETURN_VALUE] = {
535+
'type': 'input_value',
536+
};
537+
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, inputs);
538+
}
539+
505540
export function getBaseClassBlocks(
506541
baseClassName: string): toolboxItems.Block[] {
507542
const blocks: toolboxItems.Block[] = [];

src/toolbox/methods_category.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import * as storageModule from '../storage/module';
2626
import { MRC_CATEGORY_STYLE_METHODS } from '../themes/styles';
2727
import { CLASS_NAME_ROBOT_BASE, CLASS_NAME_OPMODE, CLASS_NAME_MECHANISM } from '../blocks/utils/python';
2828
import { addInstanceWithinBlocks } from '../blocks/mrc_call_python_function';
29-
import { createCustomMethodBlock, getBaseClassBlocks, FIELD_METHOD_NAME } from '../blocks/mrc_class_method_def';
29+
import { createCustomMethodBlock, getBaseClassBlocks, FIELD_METHOD_NAME, createCustomMethodBlockWithReturn } from '../blocks/mrc_class_method_def';
3030
import { Editor } from '../editor/editor';
3131

3232

@@ -113,6 +113,7 @@ class MethodsCategory {
113113
text: 'Custom Methods',
114114
},
115115
createCustomMethodBlock(),
116+
createCustomMethodBlockWithReturn()
116117
);
117118

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

0 commit comments

Comments
 (0)