Skip to content

Commit b0119dc

Browse files
committed
Made so you can only drag parameters into a method with that named parameter
1 parent 41b6b30 commit b0119dc

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

src/blocks/mrc_get_parameter.ts

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,21 @@ import { Order } from 'blockly/python';
2525
import { MRC_STYLE_VARIABLES } from '../themes/styles'
2626
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
2727
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
28+
import { BLOCK_NAME as MRC_CLASS_METHOD_DEF, ClassMethodDefBlock} from './mrc_class_method_def';
29+
import * as ChangeFramework from './utils/change_framework'
30+
import { truncate } from 'node:fs';
2831

2932
export const BLOCK_NAME = 'mrc_get_parameter';
33+
export const OUTPUT_NAME = 'mrc_get_parameter_output';
3034

3135

32-
type GetParameterBlock = Blockly.Block & GetParameterMixin;
36+
type GetParameterBlock = Blockly.Block & Blockly.BlockSvg & GetParameterMixin;
3337
interface GetParameterMixin extends GetParameterMixinType {
3438
}
3539
type GetParameterMixinType = typeof GET_PARAMETER_BLOCK;
3640

3741
const GET_PARAMETER_BLOCK = {
38-
parameterType : '', // Later this will be set to the type of the parameter, e.g. 'string', 'number', etc.
42+
parameterType: '', // Later this will be set to the type of the parameter, e.g. 'string', 'number', etc.
3943
/**
4044
* Block initialization.
4145
*/
@@ -45,13 +49,37 @@ const GET_PARAMETER_BLOCK = {
4549
.appendField('parameter')
4650
.appendField(createFieldNonEditableText('parameter'), 'PARAMETER_NAME');
4751

48-
this.setOutput(true, this.parameterType);
52+
this.setOutput(true, [OUTPUT_NAME, this.parameterType]);
53+
ChangeFramework.registerCallback(BLOCK_NAME, [Blockly.Events.BLOCK_MOVE], this.onBlockChanged);
4954
},
5055
setNameAndType: function (this: GetParameterBlock, name: string, type: string): void {
5156
this.setFieldValue(name, 'PARAMETER_NAME');
5257
this.parameterType = type;
53-
this.setOutput(true, type);
54-
}
58+
this.setOutput(true, [OUTPUT_NAME, type]);
59+
},
60+
61+
onBlockChanged(block: Blockly.BlockSvg, blockEvent: Blockly.Events.BlockBase): void {
62+
let blockBlock = block as Blockly.Block;
63+
64+
if (blockEvent.type === Blockly.Events.BLOCK_MOVE) {
65+
let parent = ChangeFramework.getParentOfType(block, MRC_CLASS_METHOD_DEF);
66+
67+
if (parent) {
68+
// It is a class method definition, so we see if this variable is in it.
69+
let classMethodDefBlock = parent as ClassMethodDefBlock;
70+
for(const parameter of classMethodDefBlock.mrcParameters) {
71+
if (parameter.name === blockBlock.getFieldValue('PARAMETER_NAME')) {
72+
// If it is, we allow it to stay.
73+
blockBlock.setWarningText(null);
74+
return;
75+
}
76+
}
77+
}
78+
// If we end up here it shouldn't be allowed
79+
block.unplug(true);
80+
blockBlock.setWarningText("Parameters can only go in their method's block.")
81+
}
82+
},
5583
}
5684

5785
export const setup = function () {
@@ -62,8 +90,8 @@ export const pythonFromBlock = function (
6290
block: GetParameterBlock,
6391
generator: ExtendedPythonGenerator,
6492
) {
65-
//TODO (Alan) : Specify the type here as well
66-
let code = block.getFieldValue('PARAMETER_NAME');
93+
//TODO (Alan) : Specify the type here as well
94+
let code = block.getFieldValue('PARAMETER_NAME');
6795

68-
return [code, Order.ATOMIC];
96+
return [code, Order.ATOMIC];
6997
}

0 commit comments

Comments
 (0)