Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 7 additions & 5 deletions src/blocks/mrc_mechanism.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Parameter = {
};

type MechanismExtraState = {
mechanismModuleId?: string,
importModule?: string,
parameters?: Parameter[],
}
Expand All @@ -54,6 +55,7 @@ const WARNING_ID_MECHANISM_CHANGED = 'mechanism changed';

export type MechanismBlock = Blockly.Block & MechanismMixin & Blockly.BlockSvg;
interface MechanismMixin extends MechanismMixinType {
mrcMechanismModuleId: string
mrcImportModule: string,
mrcParameters: Parameter[],
}
Expand All @@ -80,6 +82,7 @@ const MECHANISM = {
*/
saveExtraState: function (this: MechanismBlock): MechanismExtraState {
const extraState: MechanismExtraState = {
mechanismModuleId: this.mrcMechanismModuleId,
};
extraState.parameters = [];
this.mrcParameters.forEach((arg) => {
Expand All @@ -97,6 +100,7 @@ const MECHANISM = {
* Applies the given state to this block.
*/
loadExtraState: function (this: MechanismBlock, extraState: MechanismExtraState): void {
this.mrcMechanismModuleId = extraState.mechanismModuleId ? extraState.mechanismModuleId : '';
this.mrcImportModule = extraState.importModule ? extraState.importModule : '';
this.mrcParameters = [];
if (extraState.parameters) {
Expand Down Expand Up @@ -157,6 +161,7 @@ const MECHANISM = {
const mechanismName = this.getFieldValue(FIELD_NAME);
const mechanismType = this.mrcImportModule + '.' + this.getFieldValue(FIELD_TYPE);
return {
moduleId: this.mrcMechanismModuleId,
blockId: this.id,
name: mechanismName,
className: mechanismType,
Expand All @@ -169,14 +174,10 @@ const MECHANISM = {
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
if (editor) {
// Find the mechanism.
// TODO(lizlooney): The user can rename the mechanism. We need to store a UUID in
// each module JSON file so we can track mechanisms, etc, even if the name changes.
// Then here, we'd look for the mechanism with the marching UUID, and we'd update the
// FIELD_TYPE value if the mechanism's class name had changed.
let foundMechanism: storageModule.Mechanism | null = null;
const components: storageModuleContent.Component[] = []
for (const mechanism of editor.getMechanisms()) {
if (mechanism.className === this.getFieldValue(FIELD_TYPE)) {
if (mechanism.moduleId === this.mrcMechanismModuleId) {
foundMechanism = mechanism;
components.push(...editor.getComponentsFromMechanism(mechanism));
break;
Expand Down Expand Up @@ -251,6 +252,7 @@ export function createMechanismBlock(
const snakeCaseName = storageNames.pascalCaseToSnakeCase(mechanism.className);
const mechanismName = 'my_' + snakeCaseName;
const extraState: MechanismExtraState = {
mechanismModuleId: mechanism.moduleId,
importModule: snakeCaseName,
parameters: [],
};
Expand Down
2 changes: 1 addition & 1 deletion src/storage/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ export const MODULE_TYPE_MECHANISM = 'mechanism';
export const MODULE_TYPE_OPMODE = 'opmode';

export type Module = {
// TODO(lizlooney): Add a uuid so we can keep track of mechanisms in the robot even if the user renames the mechamism
modulePath: string,
moduleType: string,
moduleId: string,
projectName: string, // For example, WackyWheelerRobot
className: string, // For example, GamePieceShooter.
dateModifiedMillis: number,
Expand Down
15 changes: 15 additions & 0 deletions src/storage/module_content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* @author [email protected] (Liz Looney)
*/

import * as Blockly from 'blockly/core';
import * as storageModule from './module';
import * as storageNames from './names';
import startingOpModeBlocks from '../modules/opmode_start.json';
Expand All @@ -39,6 +40,7 @@ export type Method = {
};

export type MechanismInRobot = {
moduleId: string // ID of the mechanism module.
blockId: string, // ID of the mrc_mechanism block that adds the mechanism to the robot.
name: string,
className: string, // Includes the module name, for example 'game_piece_shooter.GamePieceShooter'.
Expand Down Expand Up @@ -79,6 +81,7 @@ export function newRobotContent(projectName: string): string {
const module: storageModule.Robot = {
modulePath: storageNames.makeRobotPath(projectName),
moduleType: storageModule.MODULE_TYPE_ROBOT,
moduleId: Blockly.utils.idGenerator.genUid(),
projectName: projectName,
className: storageNames.CLASS_NAME_ROBOT,
dateModifiedMillis: 0,
Expand All @@ -94,6 +97,7 @@ export function newMechanismContent(projectName: string, mechanismClassName: str
const module: storageModule.Mechanism = {
modulePath: storageNames.makeModulePath(projectName, mechanismClassName),
moduleType: storageModule.MODULE_TYPE_MECHANISM,
moduleId: Blockly.utils.idGenerator.genUid(),
projectName: projectName,
className: mechanismClassName,
dateModifiedMillis: 0,
Expand All @@ -109,6 +113,7 @@ export function newOpModeContent(projectName: string, opModeClassName: string):
const module: storageModule.OpMode = {
modulePath: storageNames.makeModulePath(projectName, opModeClassName),
moduleType: storageModule.MODULE_TYPE_OPMODE,
moduleId: Blockly.utils.idGenerator.genUid(),
projectName: projectName,
className: opModeClassName,
dateModifiedMillis: 0,
Expand All @@ -129,6 +134,7 @@ export function makeModuleContentText(
methods: Method[]): string {
const moduleContent = new ModuleContent(
module.moduleType,
module.moduleId,
blocks,
mechanisms,
components,
Expand All @@ -139,8 +145,12 @@ export function makeModuleContentText(

export function parseModuleContentText(moduleContentText: string): ModuleContent {
const parsedContent = JSON.parse(moduleContentText);
if (!parsedContent.moduleId) {
parsedContent.moduleId = Blockly.utils.idGenerator.genUid();
}
return new ModuleContent(
parsedContent.moduleType,
parsedContent.moduleId,
parsedContent.blocks,
parsedContent.mechanisms,
parsedContent.components,
Expand All @@ -151,6 +161,7 @@ export function parseModuleContentText(moduleContentText: string): ModuleContent
export class ModuleContent {
constructor(
private moduleType: string,
private moduleId: string,
private blocks : { [key: string]: any },
private mechanisms: MechanismInRobot[],
private components: Component[],
Expand All @@ -166,6 +177,10 @@ export class ModuleContent {
return this.moduleType;
}

getModuleId(): string {
return this.moduleId;
}

getBlocks(): { [key: string]: any } {
return this.blocks;
}
Expand Down
1 change: 1 addition & 0 deletions src/storage/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export async function listProjects(storage: commonStorage.Storage): Promise<Proj
const module: storageModule.Module = {
modulePath: modulePath,
moduleType: moduleType,
moduleId: moduleContent.getModuleId(),
projectName: storageNames.getProjectName(modulePath),
className: storageNames.getClassName(modulePath),
dateModifiedMillis: dateModifiedMillis,
Expand Down