Skip to content

Commit 38f6fa6

Browse files
committed
Make components in mechanism "real"
1 parent 4a3b433 commit 38f6fa6

File tree

2 files changed

+62
-23
lines changed

2 files changed

+62
-23
lines changed

src/toolbox/blocks_components.ts

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,35 @@
1616
*/
1717

1818
/**
19-
* @author [email protected] (Alan Smith)
19+
* @fileoverview Component blocks for the toolbox.
2020
*/
21+
2122
import * as ToolboxItems from './items';
2223
import * as ColorSensor from './hardware_components/color_sensor';
2324
import * as SmartMotor from './hardware_components/smart_motor';
24-
import * as TouchSensor from './hardware_components/touch_sensor';
2525
import * as Servo from './hardware_components/servo';
26+
import * as TouchSensor from './hardware_components/touch_sensor';
27+
28+
const ALL_COMPONENTS: Record<string, (componentName: string) => ToolboxItems.ContentsType[]> = {
29+
[ColorSensor.TYPE_NAME]: ColorSensor.getBlocks,
30+
[SmartMotor.TYPE_NAME]: SmartMotor.getBlocks,
31+
[TouchSensor.TYPE_NAME]: TouchSensor.getBlocks,
32+
[Servo.TYPE_NAME]: Servo.getBlocks,
33+
};
34+
35+
export function getAllPossibleComponents(hideParams: boolean): ToolboxItems.ContentsType[] {
36+
return [
37+
SmartMotor.getDefinitionBlock(hideParams),
38+
TouchSensor.getDefinitionBlock(hideParams),
39+
ColorSensor.getDefinitionBlock(hideParams),
40+
Servo.getDefinitionBlock(hideParams),
41+
];
42+
}
2643

27-
export function getAllPossibleComponents(hideParams : boolean): ToolboxItems.ContentsType[] {
28-
return [
29-
SmartMotor.getDefinitionBlock(hideParams),
30-
TouchSensor.getDefinitionBlock(hideParams),
31-
ColorSensor.getDefinitionBlock(hideParams),
32-
Servo.getDefinitionBlock(hideParams),
33-
];
44+
export function getBlocks(componentType: string, componentName: string): ToolboxItems.ContentsType[] {
45+
const getBlocksFunction = ALL_COMPONENTS[componentType];
46+
if (getBlocksFunction) {
47+
return getBlocksFunction(componentName);
48+
}
49+
return [];
3450
}

src/toolbox/hardware_category.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ import * as Blockly from 'blockly/core';
2929
import * as toolboxItems from './items';
3030
import * as commonStorage from '../storage/common_storage';
3131
import { getAllPossibleMechanisms } from './blocks_mechanisms';
32-
import { getAllPossibleComponents } from './blocks_components';
33-
import * as SmartMotor from './hardware_components/smart_motor';
34-
import * as TouchSensor from './hardware_components/touch_sensor';
32+
import { getAllPossibleComponents, getBlocks } from './blocks_components';
33+
import * as MechanismComponentHolder from '../blocks/mrc_mechanism_component_holder';
3534

3635
export function getHardwareCategory(currentModule: commonStorage.Module) {
3736
if (currentModule.moduleType === commonStorage.MODULE_TYPE_OPMODE) {
@@ -243,22 +242,46 @@ function getRobotMethodsBlocks(currentModule: commonStorage.Module) {
243242

244243
function getComponentsBlocks(currentModule: commonStorage.Module) {
245244
const contents = [];
245+
246+
// Add the "+ Component" category
246247
contents.push({
247248
kind: 'category',
248249
name: '+ Component',
249250
contents: getAllPossibleComponents(true)
250251
});
251-
contents.push({
252-
kind: 'category',
253-
name: 'my_motor',
254-
contents: SmartMotor.getBlocks('my_motor')
255-
},
256-
{
257-
kind: 'category',
258-
name: 'my_touch_sensor',
259-
contents: TouchSensor.getBlocks('my_touch_sensor')
260-
},
261-
);
252+
253+
// Get components from the current workspace
254+
const workspace = Blockly.getMainWorkspace();
255+
if (workspace) {
256+
const holderBlocks = workspace.getBlocksByType(MechanismComponentHolder.BLOCK_NAME);
257+
258+
holderBlocks.forEach(holderBlock => {
259+
// Get component blocks from the COMPONENTS input
260+
const componentsInput = holderBlock.getInput('COMPONENTS');
261+
if (componentsInput && componentsInput.connection) {
262+
let componentBlock = componentsInput.connection.targetBlock();
263+
264+
// Walk through all connected component blocks
265+
while (componentBlock) {
266+
if (componentBlock.type === 'mrc_component') {
267+
const componentName = componentBlock.getFieldValue('NAME');
268+
const componentType = componentBlock.getFieldValue('TYPE');
269+
270+
if (componentName && componentType) {
271+
// Get the blocks for this specific component
272+
contents.push({
273+
kind: 'category',
274+
name: componentName,
275+
contents: getBlocks(componentType, componentName),
276+
});
277+
}
278+
}
279+
// Move to the next block in the chain
280+
componentBlock = componentBlock.getNextBlock();
281+
}
282+
}
283+
});
284+
}
262285
return {
263286
kind: 'category',
264287
name: 'Components',

0 commit comments

Comments
 (0)