Skip to content

Commit 6cc482b

Browse files
authored
Fill the Robot -> Components category on the toolbox when the user is editing an opmode. (#156)
Commented out fake code.
1 parent d869f5e commit 6cc482b

File tree

4 files changed

+166
-158
lines changed

4 files changed

+166
-158
lines changed

src/blocks/mrc_call_python_function.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -545,12 +545,17 @@ const CALL_PYTHON_FUNCTION = {
545545
break;
546546
}
547547
case FunctionKind.INSTANCE_COMPONENT: {
548-
let componentNames: string[] = [];
548+
const componentNames: string[] = [];
549549
// Get the list of component names whose type matches this.mrcComponentClassName so we can
550550
// create a dropdown that has the appropriate component names.
551551
const editor = Editor.getEditorForBlocklyWorkspace(this.workspace);
552552
if (editor) {
553-
componentNames = editor.getComponentNames(this.mrcComponentClassName);
553+
const components = editor.getComponentsFromRobot();
554+
components.forEach(component => {
555+
if (component.className === this.mrcComponentClassName) {
556+
componentNames.push(component.name);
557+
}
558+
});
554559
}
555560
const componentName = this.getComponentName();
556561
if (!componentNames.includes(componentName)) {

src/editor/editor.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export class Editor {
139139
const promises: { [key: string]: Promise<string> } = {}; // key is module path, value is promise of module content.
140140
promises[this.modulePath] = this.storage.fetchModuleContent(this.modulePath);
141141
if (this.robotPath !== this.modulePath) {
142-
// Also fetch the robot module content. It contains components, etc, that can be used.
142+
// Also fetch the robot module content. It contains components, etc, that can be used in OpModes.
143143
promises[this.robotPath] = this.storage.fetchModuleContent(this.robotPath)
144144
}
145145

@@ -224,12 +224,12 @@ export class Editor {
224224
const exportedBlocks = JSON.stringify(this.generatorContext.getExportedBlocks());
225225
const blocksContent = JSON.stringify(
226226
Blockly.serialization.workspaces.save(this.blocklyWorkspace));
227-
const componentsContent = JSON.stringify(this.getComponents());
227+
const componentsContent = JSON.stringify(this.getComponentsFromWorkspace());
228228
return commonStorage.makeModuleContent(
229229
this.currentModule, pythonCode, blocksContent, exportedBlocks, componentsContent);
230230
}
231231

232-
private getComponents(): commonStorage.Component[] {
232+
public getComponentsFromWorkspace(): commonStorage.Component[] {
233233
const components: commonStorage.Component[] = [];
234234
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT ||
235235
this.currentModule?.moduleType === commonStorage.MODULE_TYPE_MECHANISM) {
@@ -257,28 +257,18 @@ export class Editor {
257257
}
258258

259259
/**
260-
* Returns the names of components defined in the robot that have the given component class name.
260+
* Returns the components defined in the robot.
261261
*/
262-
// TODO: what about components defined in a mechanism?
263-
public getComponentNames(componentClassName: string): string[] {
262+
public getComponentsFromRobot(): commonStorage.Component[] {
264263
let components: commonStorage.Component[];
265264

266265
if (this.currentModule?.moduleType === commonStorage.MODULE_TYPE_ROBOT) {
267-
components = this.getComponents();
268-
} else {
269-
if (!this.robotContent) {
270-
throw new Error('getComponentNames: this.robotContent is null.');
271-
}
272-
components = commonStorage.extractComponents(this.robotContent);
266+
return this.getComponentsFromWorkspace();
273267
}
274-
275-
const componentNames: string[] = [];
276-
components.forEach((component) => {
277-
if (component.className === componentClassName) {
278-
componentNames.push(component.name);
279-
}
280-
});
281-
return componentNames;
268+
if (!this.robotContent) {
269+
throw new Error('getComponentsFromRobot: this.robotContent is null.');
270+
}
271+
return commonStorage.extractComponents(this.robotContent);
282272
}
283273

284274
public static getEditorForBlocklyWorkspace(workspace: Blockly.Workspace): Editor | null {

src/toolbox/blocks_mechanisms.ts

Lines changed: 91 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -20,102 +20,105 @@
2020
*/
2121
import * as ToolboxItems from './items';
2222

23-
/**
24-
* TODO: This is all fake right now, it will be generated dynamically
25-
* based on reading things from mechanisms.
26-
*/
27-
2823
export function getAllPossibleMechanisms(): ToolboxItems.ContentsType[] {
29-
return [
24+
const contents: ToolboxItems.ContentsType[] = []
25+
26+
// TODO: Get the blocks for adding mechanisms.
3027

31-
{
32-
kind: 'block',
33-
type: 'mrc_mechanism',
28+
/* // Uncomment this fake code for testing purposes only.
29+
contents.push(
30+
{
31+
kind: 'block',
32+
type: 'mrc_mechanism',
33+
fields: {
34+
NAME: 'claw',
35+
TYPE: 'Claw'
36+
},
37+
extraState: {
38+
importModule: 'claw',
39+
params: [
40+
{ name: 'gripper_port', type: 'int' },
41+
{ name: 'piece_sensor_port', type: 'int' },
42+
]
43+
},
44+
inputs: {
45+
ARG0: {
46+
shadow: {
47+
type: 'mrc_port',
48+
fields: {
49+
TYPE: 'SmartMotor',
50+
PORT_NUM: 1
51+
},
52+
},
53+
},
54+
ARG1: {
55+
shadow: {
56+
type: 'mrc_port',
57+
fields: {
58+
TYPE: 'SmartIO',
59+
PORT_NUM: 1
60+
},
61+
},
62+
},
63+
}
64+
},
65+
{
66+
kind: 'block',
67+
type: 'mrc_mechanism',
68+
fields: {
69+
NAME: 'drive',
70+
TYPE: 'DriveMecanum'
71+
},
72+
extraState: {
73+
importModule: 'drive_mecanum',
74+
params: [
75+
{ name: 'front_left_drive_port', type: 'int' },
76+
{ name: 'front_right_drive_port', type: 'int' },
77+
{ name: 'back_left_drive_port', type: 'int' },
78+
{ name: 'back_right_drive_port', type: 'int' },
79+
]
80+
},
81+
inputs: {
82+
ARG0: {
83+
shadow: {
84+
type: 'mrc_port',
3485
fields: {
35-
NAME: 'claw',
36-
TYPE: 'Claw'
86+
TYPE: 'SmartMotor',
87+
PORT_NUM: 1
3788
},
38-
extraState: {
39-
importModule: 'claw',
40-
params: [{ name: 'gripper_port', type: 'int' },
41-
{ name: 'piece_sensor_port', type: 'int' },
42-
]
89+
},
90+
},
91+
ARG1: {
92+
shadow: {
93+
type: 'mrc_port',
94+
fields: {
95+
TYPE: 'SmartMotor',
96+
PORT_NUM: 2
4397
},
44-
inputs: {
45-
ARG0: {
46-
shadow: {
47-
type: 'mrc_port',
48-
fields: {
49-
TYPE: 'SmartMotor',
50-
PORT_NUM: 1
51-
},
52-
},
53-
},
54-
ARG1: {
55-
shadow: {
56-
type: 'mrc_port',
57-
fields: {
58-
TYPE: 'SmartIO',
59-
PORT_NUM: 1
60-
},
61-
},
62-
},
63-
}
98+
},
6499
},
65-
{
66-
kind: 'block',
67-
type: 'mrc_mechanism',
100+
ARG2: {
101+
shadow: {
102+
type: 'mrc_port',
68103
fields: {
69-
NAME: 'drive',
70-
TYPE: 'DriveMecanum'
104+
TYPE: 'SmartMotor',
105+
PORT_NUM: 3
71106
},
72-
extraState: {
73-
importModule: 'DriveMecanum',
74-
params: [{ name: 'front_left_drive_port', type: 'int' },
75-
{ name: 'front_right_drive_port', type: 'int' },
76-
{ name: 'back_left_drive_port', type: 'int' },
77-
{ name: 'back_right_drive_port', type: 'int' },
78-
]
107+
},
108+
},
109+
ARG3: {
110+
shadow: {
111+
type: 'mrc_port',
112+
fields: {
113+
TYPE: 'SmartMotor',
114+
PORT_NUM: 4
79115
},
80-
inputs: {
81-
ARG0: {
82-
shadow: {
83-
type: 'mrc_port',
84-
fields: {
85-
TYPE: 'SmartMotor',
86-
PORT_NUM: 1
87-
},
88-
},
89-
},
90-
ARG1: {
91-
shadow: {
92-
type: 'mrc_port',
93-
fields: {
94-
TYPE: 'SmartMotor',
95-
PORT_NUM: 2
96-
},
97-
},
98-
},
99-
ARG2: {
100-
shadow: {
101-
type: 'mrc_port',
102-
fields: {
103-
TYPE: 'SmartMotor',
104-
PORT_NUM: 3
105-
},
106-
},
107-
},
108-
ARG3: {
109-
shadow: {
110-
type: 'mrc_port',
111-
fields: {
112-
TYPE: 'SmartMotor',
113-
PORT_NUM: 4
114-
},
115-
},
116-
},
117-
}
116+
},
118117
},
119-
];
120-
}
118+
}
119+
}
120+
);
121+
*/
121122

123+
return contents;
124+
}

0 commit comments

Comments
 (0)