Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
69 changes: 31 additions & 38 deletions src/blocks/mrc_call_python_function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,66 +549,59 @@ const CALL_PYTHON_FUNCTION = {
}
this.updateBlock_();
},
getComponents: function(this: CallPythonFunctionBlock): storageModuleContent.Component[] {
getComponents: function(this: CallPythonFunctionBlock, editor: Editor): storageModuleContent.Component[] {
// Get the list of components whose type matches this.mrcComponentClassName.
const components: storageModuleContent.Component[] = [];
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
if (editor) {
let componentsToConsider: storageModuleContent.Component[] = [];
if (this.mrcMechanismId) {
// Only consider components that belong to the mechanism.
// this.mrcMechanismId is the mechanismId from the MechanismInRobot.
// We need to get the MechanismInRobot with that id, then get the mechanism, and then get
// the public components defined in that mechanism.
for (const mechanismInRobot of editor.getMechanismsFromRobot()) {
if (mechanismInRobot.mechanismId === this.mrcMechanismId) {
for (const mechanism of editor.getMechanisms()) {
if (mechanism.moduleId === mechanismInRobot.moduleId) {
componentsToConsider = editor.getComponentsFromMechanism(mechanism);
break;
}
let componentsToConsider: storageModuleContent.Component[] = [];
if (this.mrcMechanismId) {
// Only consider components that belong to the mechanism.
// this.mrcMechanismId is the mechanismId from the MechanismInRobot.
// We need to get the MechanismInRobot with that id, then get the mechanism, and then get
// the public components defined in that mechanism.
for (const mechanismInRobot of editor.getMechanismsFromRobot()) {
if (mechanismInRobot.mechanismId === this.mrcMechanismId) {
for (const mechanism of editor.getMechanisms()) {
if (mechanism.moduleId === mechanismInRobot.moduleId) {
componentsToConsider = editor.getComponentsFromMechanism(mechanism);
break;
}
break;
}
break;
}
} else if (editor.getModuleType() === storageModule.ModuleType.MECHANISM) {
// Only consider components (regular and private) in the current workspace.
componentsToConsider = editor.getAllComponentsFromWorkspace();
} else {
// Only consider components in the robot.
componentsToConsider = editor.getComponentsFromRobot();
}
componentsToConsider.forEach(component => {
if (component.className === this.mrcComponentClassName) {
components.push(component);
}
});
} else if (editor.getModuleType() === storageModule.ModuleType.MECHANISM) {
// Only consider components (regular and private) in the current workspace.
componentsToConsider = editor.getAllComponentsFromWorkspace();
} else {
// Only consider components in the robot.
componentsToConsider = editor.getComponentsFromRobot();
}
componentsToConsider.forEach(component => {
if (component.className === this.mrcComponentClassName) {
components.push(component);
}
});
return components;
},

/**
* mrcOnModuleCurrent is called for each CallPythonFunctionBlock when the module becomes the current module.
*/
mrcOnModuleCurrent: function(this: CallPythonFunctionBlock): void {
this.checkFunction();
mrcOnModuleCurrent: function(this: CallPythonFunctionBlock, editor: Editor): void {
this.checkFunction(editor);
},
/**
* mrcOnLoad is called for each CallPythonFunctionBlock when the blocks are loaded in the blockly
* workspace.
*/
mrcOnLoad: function(this: CallPythonFunctionBlock): void {
this.checkFunction();
mrcOnLoad: function(this: CallPythonFunctionBlock, editor: Editor): void {
this.checkFunction(editor);
},
/**
* checkFunction checks the block, updates it, and/or adds a warning balloon if necessary.
* It is called from mrcOnModuleCurrent and mrcOnLoad above.
*/
checkFunction: function(this: CallPythonFunctionBlock): void {
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
if (!editor) {
return;
}
checkFunction: function(this: CallPythonFunctionBlock, editor: Editor): void {
const warnings: string[] = [];

// If this block is calling a component method, check whether the component
Expand All @@ -621,7 +614,7 @@ const CALL_PYTHON_FUNCTION = {
if (this.mrcFunctionKind === FunctionKind.INSTANCE_COMPONENT) {
const componentNames: string[] = [];
this.mrcMapComponentNameToId = {}
this.getComponents().forEach(component => {
this.getComponents(editor).forEach(component => {
componentNames.push(component.name);
this.mrcMapComponentNameToId[component.name] = component.componentId;
});
Expand Down
7 changes: 4 additions & 3 deletions src/blocks/mrc_component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { MRC_STYLE_COMPONENTS } from '../themes/styles'
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
import { Editor } from '../editor/editor';
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
import { getModuleTypeForWorkspace } from './utils/workspaces';
import { getAllowedTypesForSetCheck, getClassData, getSubclassNames } from './utils/python';
import * as toolboxItems from '../toolbox/items';
import * as storageModule from '../storage/module';
Expand Down Expand Up @@ -142,8 +143,8 @@ const COMPONENT = {
* Update the block to reflect the newly loaded extra state.
*/
updateBlock_: function (this: ComponentBlock): void {
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
if (editor && editor.getModuleType() === storageModule.ModuleType.ROBOT) {
const moduleType = getModuleTypeForWorkspace(this.workspace);
if (moduleType === storageModule.ModuleType.ROBOT) {
// Add input sockets for the arguments.
for (let i = 0; i < this.mrcArgs.length; i++) {
const input = this.appendValueInput('ARG' + i)
Expand Down Expand Up @@ -192,7 +193,7 @@ const COMPONENT = {
/**
* mrcOnLoad is called for each ComponentBlock when the blocks are loaded in the blockly workspace.
*/
mrcOnLoad: function(this: ComponentBlock): void {
mrcOnLoad: function(this: ComponentBlock, _editor: Editor): void {
this.checkBlockIsInHolder();
},
/**
Expand Down
3 changes: 2 additions & 1 deletion src/blocks/mrc_event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import * as Blockly from 'blockly';
import { MRC_STYLE_EVENTS } from '../themes/styles'
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
import { Parameter } from './mrc_class_method_def';
import { Editor } from '../editor/editor';
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
import * as paramContainer from './mrc_param_container'
import {
Expand Down Expand Up @@ -196,7 +197,7 @@ const EVENT = {
/**
* mrcOnLoad is called for each EventBlock when the blocks are loaded in the blockly workspace.
*/
mrcOnLoad: function(this: EventBlock): void {
mrcOnLoad: function(this: EventBlock, _editor: Editor): void {
this.checkBlockIsInHolder();
},
/**
Expand Down
167 changes: 82 additions & 85 deletions src/blocks/mrc_event_handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,114 +170,111 @@ const EVENT_HANDLER = {
/**
* mrcOnModuleCurrent is called for each EventHandlerBlock when the module becomes the current module.
*/
mrcOnModuleCurrent: function(this: EventHandlerBlock): void {
this.checkEvent();
mrcOnModuleCurrent: function(this: EventHandlerBlock, editor: Editor): void {
this.checkEvent(editor);
},
/**
* mrcOnLoad is called for each EventHandlerBlock when the blocks are loaded in the blockly
* workspace.
*/
mrcOnLoad: function(this: EventHandlerBlock): void {
this.checkEvent();
mrcOnLoad: function(this: EventHandlerBlock, editor: Editor): void {
this.checkEvent(editor);
},
/**
* checkEvent checks the block, updates it, and/or adds a warning balloon if necessary.
* It is called from mrcOnModuleCurrent and mrcOnLoad above.
*/
checkEvent: function(this: EventHandlerBlock): void {
checkEvent: function(this: EventHandlerBlock, editor: Editor): void {
const warnings: string[] = [];

const editor = Editor.getEditorForBlocklyWorkspace(this.workspace, true /* returnCurrentIfNotFound */);
if (editor) {
if (this.mrcSenderType === SenderType.ROBOT) {
// This block is an event handler for a robot event.
// Check whether the robot event still exists and whether it has been changed.
// If the robot event doesn't exist, put a visible warning on this block.
// If the robot event has changed, update the block if possible or put a
// visible warning on it.
let foundRobotEvent = false;
const robotEvents = editor.getEventsFromRobot();
for (const robotEvent of robotEvents) {
if (robotEvent.eventId === this.mrcEventId) {
foundRobotEvent = true;
if (this.getFieldValue(FIELD_EVENT_NAME) !== robotEvent.name) {
this.setFieldValue(robotEvent.name, FIELD_EVENT_NAME);
}
this.mrcParameters = [];
robotEvent.args.forEach(arg => {
this.mrcParameters.push({
name: arg.name,
type: arg.type,
});
if (this.mrcSenderType === SenderType.ROBOT) {
// This block is an event handler for a robot event.
// Check whether the robot event still exists and whether it has been changed.
// If the robot event doesn't exist, put a visible warning on this block.
// If the robot event has changed, update the block if possible or put a
// visible warning on it.
let foundRobotEvent = false;
const robotEvents = editor.getEventsFromRobot();
for (const robotEvent of robotEvents) {
if (robotEvent.eventId === this.mrcEventId) {
foundRobotEvent = true;
if (this.getFieldValue(FIELD_EVENT_NAME) !== robotEvent.name) {
this.setFieldValue(robotEvent.name, FIELD_EVENT_NAME);
}
this.mrcParameters = [];
robotEvent.args.forEach(arg => {
this.mrcParameters.push({
name: arg.name,
type: arg.type,
});
this.mrcUpdateParams();
});
this.mrcUpdateParams();

// Since we found the robot event, we can break out of the loop.
break;
}
}
if (!foundRobotEvent) {
warnings.push(Blockly.Msg.EVENT_HANDLER_ROBOT_EVENT_NOT_FOUND);
// Since we found the robot event, we can break out of the loop.
break;
}
}
if (!foundRobotEvent) {
warnings.push(Blockly.Msg.EVENT_HANDLER_ROBOT_EVENT_NOT_FOUND);
}
}

if (this.mrcSenderType === SenderType.MECHANISM) {
// This block is an event handler for a mechanism event.
// Check whether the mechanism still exists, whether it has been
// changed, whether the event still exists, and whether the event has
// been changed.
// If the mechanism doesn't exist, put a visible warning on this block.
// If the mechanism has changed, update the block if possible or put a
// visible warning on it.
// If the event doesn't exist, put a visible warning on this block.
// If the event has changed, update the block if possible or put a
// visible warning on it.
let foundMechanism = false;
const mechanismsInRobot = editor.getMechanismsFromRobot();
for (const mechanismInRobot of mechanismsInRobot) {
if (mechanismInRobot.mechanismId === this.mrcMechanismId) {
foundMechanism = true;

// If the mechanism name has changed, we can handle that.
if (this.getFieldValue(FIELD_SENDER) !== mechanismInRobot.name) {
this.setFieldValue(mechanismInRobot.name, FIELD_SENDER);
}
if (this.mrcSenderType === SenderType.MECHANISM) {
// This block is an event handler for a mechanism event.
// Check whether the mechanism still exists, whether it has been
// changed, whether the event still exists, and whether the event has
// been changed.
// If the mechanism doesn't exist, put a visible warning on this block.
// If the mechanism has changed, update the block if possible or put a
// visible warning on it.
// If the event doesn't exist, put a visible warning on this block.
// If the event has changed, update the block if possible or put a
// visible warning on it.
let foundMechanism = false;
const mechanismsInRobot = editor.getMechanismsFromRobot();
for (const mechanismInRobot of mechanismsInRobot) {
if (mechanismInRobot.mechanismId === this.mrcMechanismId) {
foundMechanism = true;

// If the mechanism name has changed, we can handle that.
if (this.getFieldValue(FIELD_SENDER) !== mechanismInRobot.name) {
this.setFieldValue(mechanismInRobot.name, FIELD_SENDER);
}

let foundMechanismEvent = false;
const mechanism = editor.getMechanism(mechanismInRobot);
const mechanismEvents: storageModuleContent.Event[] = mechanism
? editor.getEventsFromMechanism(mechanism) : [];
for (const mechanismEvent of mechanismEvents) {
if (mechanismEvent.eventId === this.mrcEventId) {
foundMechanismEvent = true;
if (this.getFieldValue(FIELD_EVENT_NAME) !== mechanismEvent.name) {
this.setFieldValue(mechanismEvent.name, FIELD_EVENT_NAME);
}

this.mrcParameters = [];
mechanismEvent.args.forEach(arg => {
this.mrcParameters.push({
name: arg.name,
type: arg.type,
});
let foundMechanismEvent = false;
const mechanism = editor.getMechanism(mechanismInRobot);
const mechanismEvents: storageModuleContent.Event[] = mechanism
? editor.getEventsFromMechanism(mechanism) : [];
for (const mechanismEvent of mechanismEvents) {
if (mechanismEvent.eventId === this.mrcEventId) {
foundMechanismEvent = true;
if (this.getFieldValue(FIELD_EVENT_NAME) !== mechanismEvent.name) {
this.setFieldValue(mechanismEvent.name, FIELD_EVENT_NAME);
}

this.mrcParameters = [];
mechanismEvent.args.forEach(arg => {
this.mrcParameters.push({
name: arg.name,
type: arg.type,
});
this.mrcUpdateParams();
});
this.mrcUpdateParams();

// Since we found the mechanism event, we can break out of the loop.
break;
}
// Since we found the mechanism event, we can break out of the loop.
break;
}
if (!foundMechanismEvent) {
warnings.push(Blockly.Msg.EVENT_HANDLER_MECHANISM_EVENT_NOT_FOUND);
}

// Since we found the mechanism, we can break out of the loop.
break;
}
if (!foundMechanismEvent) {
warnings.push(Blockly.Msg.EVENT_HANDLER_MECHANISM_EVENT_NOT_FOUND);
}

// Since we found the mechanism, we can break out of the loop.
break;
}
if (!foundMechanism) {
warnings.push(Blockly.Msg.EVENT_HANDLER_MECHANISM_NOT_FOUND);
}
}
if (!foundMechanism) {
warnings.push(Blockly.Msg.EVENT_HANDLER_MECHANISM_NOT_FOUND);
}
}

Expand Down
Loading