Skip to content

Commit b08d01f

Browse files
committed
In mrc_steps.ts:
Initialize mrcStepNames to []. Removed "if (state && state.stepNames) {" from loadExtraState. Add braces to if statements. Changed python code that checks whether _initialize_steps has been set. Added createStepsBlocks function to create the steps block for the toolbox. In methods_category.ts: Modified MethodsCategory.methodsFlyout to call createStepsBlocks to create the mrc_steps block for the toolbox.
1 parent 2d53e0b commit b08d01f

File tree

2 files changed

+25
-13
lines changed

2 files changed

+25
-13
lines changed

src/blocks/mrc_steps.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2727
import { createStepFieldFlydown } from '../fields/field_flydown';
2828
import { BLOCK_NAME as MRC_JUMP_TO_STEP } from './mrc_jump_to_step';
2929
import * as stepContainer from './mrc_step_container'
30+
import * as value from './utils/value';
31+
import * as toolboxItems from '../toolbox/items';
3032

3133
export const BLOCK_NAME = 'mrc_steps';
3234

@@ -49,7 +51,7 @@ const STEPS = {
4951
* Block initialization.
5052
*/
5153
init: function (this: StepsBlock): void {
52-
this.mrcStepNames = ["0"];
54+
this.mrcStepNames = [];
5355
this.appendDummyInput()
5456
.appendField(Blockly.Msg.STEPS);
5557
this.setInputsInline(false);
@@ -63,10 +65,8 @@ const STEPS = {
6365
};
6466
},
6567
loadExtraState: function (this: StepsBlock, state: StepsExtraState): void {
66-
if (state && state.stepNames) {
67-
this.mrcStepNames = state.stepNames;
68-
this.updateShape_();
69-
}
68+
this.mrcStepNames = state.stepNames;
69+
this.updateShape_();
7070
},
7171
compose: function (this: StepsBlock, containerBlock: Blockly.Block) {
7272
if (containerBlock.type !== stepContainer.STEP_CONTAINER_BLOCK_NAME) {
@@ -171,8 +171,12 @@ const STEPS = {
171171
const stepConnection = this.getInput('STEP_' + currentIndex)?.connection?.targetConnection;
172172

173173
// Temporarily disconnect
174-
if (conditionConnection) conditionConnection.disconnect();
175-
if (stepConnection) stepConnection.disconnect();
174+
if (conditionConnection) {
175+
conditionConnection.disconnect();
176+
}
177+
if (stepConnection) {
178+
stepConnection.disconnect();
179+
}
176180

177181
// Remove old inputs
178182
this.removeInput('CONDITION_' + currentIndex, false);
@@ -256,7 +260,7 @@ export const pythonFromBlock = function (
256260
generator: ExtendedPythonGenerator,
257261
) {
258262
let code = 'def steps(self):\n';
259-
code += generator.INDENT + 'if not self._initialized_steps:\n';
263+
code += generator.INDENT + 'if not hasattr(self, "_initialized_steps"):\n';
260264
code += generator.INDENT.repeat(2) + 'self._current_step = "' + block.mrcStepNames[0] + '"\n';
261265
code += generator.INDENT.repeat(2) + 'self._initialized_steps = True\n\n';
262266
code += generator.INDENT + 'if self._current_step == None:\n';
@@ -282,4 +286,14 @@ export const pythonFromBlock = function (
282286
generator.addClassMethodDefinition('steps', code);
283287

284288
return ''
285-
}
289+
}
290+
291+
export function createStepsBlock(): toolboxItems.Block {
292+
const extraState: StepsExtraState = {
293+
stepNames: ['0'],
294+
};
295+
const fields: {[key: string]: any} = {};
296+
const inputs: {[key: string]: any} = {};
297+
inputs['CONDITION_' + 0] = value.createBooleanShadowValue(true);
298+
return new toolboxItems.Block(BLOCK_NAME, extraState, fields, inputs);
299+
}

src/toolbox/methods_category.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ 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';
2929
import { createCustomMethodBlock, getBaseClassBlocks, FIELD_METHOD_NAME, createCustomMethodBlockWithReturn } from '../blocks/mrc_class_method_def';
30+
import { createStepsBlock } from '../blocks/mrc_steps';
3031
import { Editor } from '../editor/editor';
3132

3233

@@ -101,10 +102,7 @@ class MethodsCategory {
101102
case storageModule.ModuleType.OPMODE:
102103
const hasSteps = editor.isStepsInWorkspace();
103104
if (!hasSteps) {
104-
contents.push({
105-
kind: 'block',
106-
type: 'mrc_steps',
107-
});
105+
contents.push(createStepsBlock());
108106
}
109107
// Add the methods for an OpMode.
110108
this.addClassBlocksForCurrentModule(

0 commit comments

Comments
 (0)