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
247 changes: 225 additions & 22 deletions src/blocks/mrc_call_python_function.ts

Large diffs are not rendered by default.

52 changes: 28 additions & 24 deletions src/blocks/mrc_component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,15 @@ import { Order } from 'blockly/python';

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 { getAllowedTypesForSetCheck, getClassData, getModuleData, getSubclassNames } from './utils/python';
import * as toolboxItems from '../toolbox/items';
import * as commonStorage from '../storage/common_storage';
import { createPortShadow } from './mrc_port';
import { createNumberShadowValue } from './utils/value';
import { ClassData, FunctionData } from './utils/python_json_types';
import { renameMethodCallers } from './mrc_call_python_function'


export const BLOCK_NAME = 'mrc_component';
Expand All @@ -48,14 +50,12 @@ type ComponentExtraState = {
importModule?: string,
// If staticFunctionName is not present, generate the constructor.
staticFunctionName?: string,
hideParams?: boolean,
params?: ConstructorArg[],
}

export type ComponentBlock = Blockly.Block & ComponentMixin;
interface ComponentMixin extends ComponentMixinType {
mrcArgs: ConstructorArg[],
hideParams: boolean,
mrcImportModule: string,
mrcStaticFunctionName: string,
}
Expand All @@ -67,8 +67,10 @@ const COMPONENT = {
*/
init: function (this: ComponentBlock): void {
this.setStyle(MRC_STYLE_COMPONENTS);
const nameField = new Blockly.FieldTextInput('')
nameField.setValidator(this.mrcNameFieldValidator.bind(this, nameField));
this.appendDummyInput()
.appendField(new Blockly.FieldTextInput(''), FIELD_NAME)
.appendField(nameField, FIELD_NAME)
.appendField(Blockly.Msg.OF_TYPE)
.appendField(createFieldNonEditableText(''), FIELD_TYPE);
this.setPreviousStatement(true, OUTPUT_NAME);
Expand Down Expand Up @@ -96,9 +98,6 @@ const COMPONENT = {
if (this.mrcStaticFunctionName) {
extraState.staticFunctionName = this.mrcStaticFunctionName;
}
if (this.hideParams) {
extraState.hideParams = this.hideParams;
}
return extraState;
},
/**
Expand All @@ -107,7 +106,6 @@ const COMPONENT = {
loadExtraState: function (this: ComponentBlock, extraState: ComponentExtraState): void {
this.mrcImportModule = extraState.importModule ? extraState.importModule : '';
this.mrcStaticFunctionName = extraState.staticFunctionName ? extraState.staticFunctionName : '';
this.hideParams = extraState.hideParams ? extraState.hideParams : false;
this.mrcArgs = [];

if (extraState.params) {
Expand All @@ -125,7 +123,8 @@ const COMPONENT = {
* Update the block to reflect the newly loaded extra state.
*/
updateBlock_: function (this: ComponentBlock): void {
if (this.hideParams == false) {
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
if (editor && editor.getCurrentModuleType() === commonStorage.MODULE_TYPE_ROBOT) {
// Add input sockets for the arguments.
for (let i = 0; i < this.mrcArgs.length; i++) {
const input = this.appendValueInput('ARG' + i)
Expand All @@ -137,6 +136,18 @@ const COMPONENT = {
}
}
},
mrcNameFieldValidator(this: ComponentBlock, nameField: Blockly.FieldTextInput, name: string): string {
// Strip leading and trailing whitespace.
name = name.trim();

const legalName = name;
const oldName = nameField.getValue();
if (oldName && oldName !== name && oldName !== legalName) {
// Rename any callers.
renameMethodCallers(this.workspace, this.id, legalName);
}
return legalName;
},
getComponent: function (this: ComponentBlock): commonStorage.Component | null {
const componentName = this.getFieldValue(FIELD_NAME);
const componentType = this.getFieldValue(FIELD_TYPE);
Expand All @@ -149,19 +160,14 @@ const COMPONENT = {
ports: ports,
};
},
getNewPort: function (this: ComponentBlock, i: number): string {
let extension = '';
if (i != 0) {
extension = '_' + (i + 1).toString();
}
return this.getFieldValue(FIELD_NAME) + extension + '_port';
getArgName: function (this: ComponentBlock, i: number): string {
return this.getFieldValue(FIELD_NAME) + '__' + this.mrcArgs[i].name;
},
getComponentPorts: function (this: ComponentBlock, ports: {[key: string]: string}): void {
getComponentPorts: function (this: ComponentBlock, ports: {[argName: string]: string}): void {
// Collect the ports for this component block.
for (let i = 0; i < this.mrcArgs.length; i++) {
const newPort = this.getNewPort(i);
// The key is the port, the value is the type.
ports[newPort] = this.mrcArgs[i].type;
const argName = this.getArgName(i);
ports[argName] = this.mrcArgs[i].type;
}
},
}
Expand All @@ -184,14 +190,13 @@ export const pythonFromBlock = function (
code += '(';

for (let i = 0; i < block.mrcArgs.length; i++) {
const fieldName = 'ARG' + i;
if (i != 0) {
code += ', '
code += ', ';
}
if (block.hideParams) {
code += block.mrcArgs[i].name + ' = ' + block.getNewPort(i);
if (generator.getModuleType() === commonStorage.MODULE_TYPE_ROBOT) {
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, 'ARG' + i, Order.NONE);
} else {
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, fieldName, Order.NONE);
code += block.mrcArgs[i].name + ' = ' + block.getArgName(i);
}
}
code += ')\n' + 'self.hardware.append(self.' + block.getFieldValue(FIELD_NAME) + ')\n';
Expand Down Expand Up @@ -230,7 +235,6 @@ function createComponentBlock(
importModule: classData.moduleName,
staticFunctionName: staticFunctionData.functionName,
params: [],
hideParams: (moduleType == commonStorage.MODULE_TYPE_MECHANISM),
};
const fields: {[key: string]: any} = {};
fields[FIELD_NAME] = componentName;
Expand Down
18 changes: 16 additions & 2 deletions src/blocks/mrc_mechanism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { getAllowedTypesForSetCheck } from './utils/python';
import * as toolboxItems from '../toolbox/items';
import * as commonStorage from '../storage/common_storage';
import * as value from './utils/value';
import { renameMethodCallers } from './mrc_call_python_function'

export const BLOCK_NAME = 'mrc_mechanism';
export const OUTPUT_NAME = 'mrc_mechansim';
Expand Down Expand Up @@ -62,13 +63,14 @@ const MECHANISM = {
*/
init: function (this: MechanismBlock): void {
this.setStyle(MRC_STYLE_MECHANISMS);
const nameField = new Blockly.FieldTextInput('')
nameField.setValidator(this.mrcNameFieldValidator.bind(this, nameField));
this.appendDummyInput()
.appendField(new Blockly.FieldTextInput('my_mech'), FIELD_NAME)
.appendField(nameField, FIELD_NAME)
.appendField(Blockly.Msg.OF_TYPE)
.appendField(createFieldNonEditableText(''), FIELD_TYPE);
this.setPreviousStatement(true, OUTPUT_NAME);
this.setNextStatement(true, OUTPUT_NAME);
//this.setOutput(true, OUTPUT_NAME);
},

/**
Expand Down Expand Up @@ -137,6 +139,18 @@ const MECHANISM = {
this.removeInput('ARG' + i);
}
},
mrcNameFieldValidator(this: MechanismBlock, nameField: Blockly.FieldTextInput, name: string): string {
// Strip leading and trailing whitespace.
name = name.trim();

const legalName = name;
const oldName = nameField.getValue();
if (oldName && oldName !== name && oldName !== legalName) {
// Rename any callers.
renameMethodCallers(this.workspace, this.id, legalName);
}
return legalName;
},
getMechanism: function (this: MechanismBlock): commonStorage.MechanismInRobot | null {
const mechanismName = this.getFieldValue(FIELD_NAME);
const mechanismType = this.mrcImportModule + '.' + this.getFieldValue(FIELD_TYPE);
Expand Down
17 changes: 15 additions & 2 deletions src/editor/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ export class Editor {
this.clearBlocklyWorkspace();

if (this.currentModule && this.currentProject) {
// Fetch the content for the current module and the robot.
// TODO: Also fetch the content for the mechanisms?
// Fetch the content for the current module, the robot, and the mechanisms.
const promises: { [modulePath: string]: Promise<string> } = {}; // value is promise of module content.
promises[this.modulePath] = this.storage.fetchModuleContentText(this.modulePath);
if (this.robotPath !== this.modulePath) {
Expand Down Expand Up @@ -377,6 +376,20 @@ export class Editor {
return this.currentProject ? this.currentProject.mechanisms : [];
}

/**
* Returns the Mechanism matching the given MechanismInRobot.
*/
public getMechanism(mechanismInRobot: commonStorage.MechanismInRobot): commonStorage.Mechanism | null {
if (this.currentProject) {
for (const mechanism of this.currentProject.mechanisms) {
if (mechanism.moduleName + '.' + mechanism.className === mechanismInRobot.className) {
return mechanism;
}
}
}
return null;
}

/**
* Returns the components defined in the given mechanism.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/storage/common_storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export type Module = {
projectName: string,
moduleName: string,
dateModifiedMillis: number,
className: string,
className: string, // Does not include the module name.
};

export type Robot = Module;
Expand Down Expand Up @@ -65,13 +65,13 @@ export type Method = {
export type MechanismInRobot = {
blockId: string, // ID of the mrc_mechanism block that adds the mechanism to the robot.
name: string,
className: string,
className: string, // Includes the module name, for example 'game_piece_shooter.GamePieceShooter'.
}

export type Component = {
blockId: string, // ID of the mrc_component block that adds the component to the robot or to a mechanism.
name: string,
className: string,
className: string, // Includes the module name, for example 'smart_motor.SmartMotor'.
ports: {[port: string]: string}, // The value is the type.
}

Expand Down
Loading