Skip to content

Commit 6ebb3b5

Browse files
committed
Modified event_handlers_category.ts to work for robot events and mechanism events.
Use the block id of the mrc_event blocks to identify which event handlers are already on the workspace.
1 parent d556fe5 commit 6ebb3b5

File tree

4 files changed

+156
-35
lines changed

4 files changed

+156
-35
lines changed

src/blocks/mrc_event_handler.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,9 @@ const EVENT_HANDLER = {
278278
this.setWarningText(null, WARNING_ID_EVENT_CHANGED);
279279
}
280280
},
281+
getEventBlockId: function(this: EventHandlerBlock): string {
282+
return this.mrcOtherBlockId;
283+
},
281284
};
282285

283286
export function setup(): void {
@@ -423,10 +426,27 @@ export function getHasAnyEnabledEventHandlers(workspace: Blockly.Workspace): boo
423426
}).length > 0;
424427
}
425428

426-
export function getEventHandlerNames(workspace: Blockly.Workspace, names: string[]): void {
427-
// Here we collect the event names of the event handlers in the given
428-
// workspace, regardless of whether the event handler is enabled.
429+
export function getRobotEventHandlerBlocks(
430+
workspace: Blockly.Workspace,
431+
blocks: EventHandlerBlock[]): void {
432+
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
433+
const eventHandlerBlock = block as EventHandlerBlock;
434+
if (eventHandlerBlock.mrcSenderType == SenderType.ROBOT) {
435+
blocks.push(eventHandlerBlock);
436+
}
437+
});
438+
}
439+
440+
export function getMechanismEventHandlerBlocks(
441+
workspace: Blockly.Workspace,
442+
mechanismBlockId: string,
443+
blocks: EventHandlerBlock[]): void {
429444
workspace.getBlocksByType(BLOCK_NAME).forEach(block => {
430-
names.push(block.getFieldValue(FIELD_EVENT_NAME));
445+
const eventHandlerBlock = block as EventHandlerBlock;
446+
if (eventHandlerBlock.mrcSenderType == SenderType.MECHANISM) {
447+
if (eventHandlerBlock.mrcMechanismBlockId === mechanismBlockId) {
448+
blocks.push(eventHandlerBlock);
449+
}
450+
}
431451
});
432452
}

src/editor/editor.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ import * as mechanismComponentHolder from '../blocks/mrc_mechanism_component_hol
3434
//import { testAllBlocksInToolbox } from '../toolbox/toolbox_tests';
3535
import { MethodsCategory } from '../toolbox/methods_category';
3636
import { EventsCategory } from '../toolbox/event_category';
37-
import { RobotEventHandlersCategory } from '../toolbox/event_handlers_category';
37+
import {
38+
registerRobotEventHandlersCategory,
39+
registerMechanismEventHandlersCategory
40+
} from '../toolbox/event_handlers_category';
3841
import { getToolboxJSON } from '../toolbox/toolbox';
3942

4043
const EMPTY_TOOLBOX: Blockly.utils.toolbox.ToolboxDefinition = {
@@ -67,7 +70,7 @@ export class Editor {
6770
// Create the custom toolbox categories so they register their flyout callbacks.
6871
new MethodsCategory(blocklyWorkspace);
6972
new EventsCategory(blocklyWorkspace);
70-
new RobotEventHandlersCategory(blocklyWorkspace);
73+
registerRobotEventHandlersCategory(blocklyWorkspace);
7174
}
7275

7376
private onChangeWhileLoading(event: Blockly.Events.Abstract) {
@@ -174,6 +177,10 @@ export class Editor {
174177
? this.moduleContentText
175178
: modulePathToContentText[mechanism.modulePath]);
176179
}
180+
// Register the custom toolbox categories for the mechanisms in the robot.
181+
this.robotContent.getMechanisms().forEach(mechanismInRobot => {
182+
registerMechanismEventHandlersCategory(this.blocklyWorkspace, mechanismInRobot);
183+
});
177184
this.loadBlocksIntoBlocklyWorkspace();
178185
}
179186
}
@@ -305,10 +312,18 @@ export class Editor {
305312
return events;
306313
}
307314

308-
public getEventHandlerNamesFromWorkspace(): string[] {
309-
const names: string[] = [];
310-
eventHandler.getEventHandlerNames(this.blocklyWorkspace, names);
311-
return names;
315+
public getRobotEventHandlersAlreadyInWorkspace(): eventHandler.EventHandlerBlock[] {
316+
const eventHandlerBlocks: eventHandler.EventHandlerBlock[] = [];
317+
eventHandler.getRobotEventHandlerBlocks(this.blocklyWorkspace, eventHandlerBlocks);
318+
return eventHandlerBlocks;
319+
}
320+
321+
public getMechanismEventHandlersAlreadyInWorkspace(
322+
mechanismInRobot: storageModuleContent.MechanismInRobot): eventHandler.EventHandlerBlock[] {
323+
const eventHandlerBlocks: eventHandler.EventHandlerBlock[] = [];
324+
eventHandler.getMechanismEventHandlerBlocks(
325+
this.blocklyWorkspace, mechanismInRobot.blockId, eventHandlerBlocks);
326+
return eventHandlerBlocks;
312327
}
313328

314329
public async saveBlocks() {

src/toolbox/event_handlers_category.ts

Lines changed: 94 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,87 @@
2121

2222
import * as Blockly from 'blockly/core';
2323
import * as toolboxItems from './items';
24-
import { addRobotEventHandlerBlocks } from '../blocks/mrc_event_handler';
24+
import * as storageModuleContent from '../storage/module_content';
25+
import { addMechanismEventHandlerBlocks, addRobotEventHandlerBlocks } from '../blocks/mrc_event_handler';
2526
import { Editor } from '../editor/editor';
2627

27-
// The robot event handlers category is shown when the user is editing an opmode.
28-
// It allows the user to create event handlers for events previously defined in the Robot.
28+
// The event handlers category is shown when the user is editing an opmode.
29+
// It allows the user to create event handlers for events previously defined in the Robot or in a
30+
// mechanism.
2931

3032
// The event handlers category is a custom category because it must be updated dynamically.
3133
// As the user places event handler blocks on the blockly workspace, we update the category so it
3234
// doesn't contain blocks for the event handlers that have already been added to the blockly
3335
// workspace.
3436

3537
const CUSTOM_CATEGORY_EVENT_HANDLERS_ROBOT = 'EVENT_HANDLERS_ROBOT';
38+
const CUSTOM_CATEGORY_EVENT_HANDLERS_MECHANISM_PREFIX = 'EVENT_HANDLERS_MECHANISM_';
3639

37-
export const getRobotEventHandlersCategory = () => ({
38-
kind: 'category',
39-
name: Blockly.Msg['MRC_CATEGORY_EVENTS'],
40-
custom: CUSTOM_CATEGORY_EVENT_HANDLERS_ROBOT,
41-
});
40+
function getCustomValue(mechanismInRobot: storageModuleContent.MechanismInRobot | null): string {
41+
return (mechanismInRobot === null)
42+
? CUSTOM_CATEGORY_EVENT_HANDLERS_ROBOT
43+
: CUSTOM_CATEGORY_EVENT_HANDLERS_MECHANISM_PREFIX + mechanismInRobot.name;
44+
}
45+
46+
export function registerRobotEventHandlersCategory(blocklyWorkspace: Blockly.WorkspaceSvg): void {
47+
new EventHandlersCategory(blocklyWorkspace, null);
48+
}
49+
50+
export function getRobotEventHandlersCategory(): toolboxItems.Category {
51+
return {
52+
kind: 'category',
53+
name: Blockly.Msg['MRC_CATEGORY_EVENTS'],
54+
custom: getCustomValue(null),
55+
};
56+
}
57+
58+
export function registerMechanismEventHandlersCategory(
59+
blocklyWorkspace: Blockly.WorkspaceSvg, mechanismInRobot: storageModuleContent.MechanismInRobot): void {
60+
new EventHandlersCategory(blocklyWorkspace, mechanismInRobot);
61+
}
4262

43-
export class RobotEventHandlersCategory {
44-
constructor(blocklyWorkspace: Blockly.WorkspaceSvg) {
45-
blocklyWorkspace.registerToolboxCategoryCallback(
46-
CUSTOM_CATEGORY_EVENT_HANDLERS_ROBOT, this.robotEventHandlersFlyout.bind(this));
63+
export function getMechanismEventHandlersCategory(
64+
mechanismInRobot: storageModuleContent.MechanismInRobot): toolboxItems.Category {
65+
return {
66+
kind: 'category',
67+
name: Blockly.Msg['MRC_CATEGORY_EVENTS'],
68+
custom: getCustomValue(mechanismInRobot),
69+
};
70+
}
71+
72+
class EventHandlersCategory {
73+
mechanismInRobot: storageModuleContent.MechanismInRobot | null;
74+
75+
constructor(
76+
blocklyWorkspace: Blockly.WorkspaceSvg,
77+
mechanismInRobot: storageModuleContent.MechanismInRobot | null) {
78+
this.mechanismInRobot = mechanismInRobot;
79+
if (mechanismInRobot === null) {
80+
blocklyWorkspace.registerToolboxCategoryCallback(
81+
getCustomValue(mechanismInRobot),
82+
this.robotEventHandlersFlyout.bind(this));
83+
} else {
84+
blocklyWorkspace.registerToolboxCategoryCallback(
85+
getCustomValue(mechanismInRobot),
86+
this.mechanismEventHandlersFlyout.bind(this));
87+
}
4788
}
4889

4990
public robotEventHandlersFlyout(workspace: Blockly.WorkspaceSvg) {
5091
const contents: toolboxItems.ContentsType[] = [];
5192

52-
// Get the list of events from the robot and add the blocks for handling events.
53-
5493
const editor = Editor.getEditorForBlocklyWorkspace(workspace);
5594
if (editor) {
95+
// Get the list of events from the robot.
5696
const eventsFromRobot = editor.getEventsFromRobot();
5797
// Remove events if there is already a corresponding handler in the workspace.
58-
const eventHandlerNames = editor.getEventHandlerNamesFromWorkspace();
98+
const eventHandlerBlocks = editor.getRobotEventHandlersAlreadyInWorkspace();
99+
const eventBlockIds: string[] = [];
100+
eventHandlerBlocks.forEach(eventHandlerBlock => {
101+
eventBlockIds.push(eventHandlerBlock.getEventBlockId());
102+
});
59103
const eventsToShow = eventsFromRobot.filter(event => {
60-
return !eventHandlerNames.includes(event.name);
104+
return !eventBlockIds.includes(event.blockId);
61105
});
62106
addRobotEventHandlerBlocks(eventsToShow, contents);
63107
}
@@ -68,4 +112,38 @@ export class RobotEventHandlersCategory {
68112

69113
return toolboxInfo;
70114
}
115+
116+
public mechanismEventHandlersFlyout(workspace: Blockly.WorkspaceSvg) {
117+
const contents: toolboxItems.ContentsType[] = [];
118+
119+
const editor = Editor.getEditorForBlocklyWorkspace(workspace);
120+
if (editor && this.mechanismInRobot) {
121+
// Get the list of events from the mechanism.
122+
const mechanism = editor.getMechanism(this.mechanismInRobot);
123+
if (mechanism) {
124+
const eventsFromMechanism = editor.getEventsFromMechanism(mechanism);
125+
// Remove events if there is already a corresponding handler in the workspace.
126+
const eventHandlerBlocks = editor.getMechanismEventHandlersAlreadyInWorkspace(
127+
this.mechanismInRobot);
128+
const eventBlockIds: string[] = [];
129+
eventHandlerBlocks.forEach(eventHandlerBlock => {
130+
eventBlockIds.push(eventHandlerBlock.getEventBlockId());
131+
});
132+
const eventsToShow = eventsFromMechanism.filter(event => {
133+
return !eventBlockIds.includes(event.blockId);
134+
});
135+
addMechanismEventHandlerBlocks(this.mechanismInRobot, eventsToShow, contents);
136+
if (contents.length === 0) {
137+
const label : toolboxItems.Label = new toolboxItems.Label(Blockly.Msg['NO_MECHANISM_CONTENTS']);
138+
contents.push(label);
139+
}
140+
}
141+
}
142+
143+
const toolboxInfo = {
144+
contents: contents,
145+
};
146+
147+
return toolboxInfo;
148+
}
71149
}

src/toolbox/hardware_category.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import * as Blockly from 'blockly/core';
2323
import * as storageModule from '../storage/module';
2424
import * as toolboxItems from './items';
25-
import { getRobotEventHandlersCategory } from './event_handlers_category';
25+
import { getRobotEventHandlersCategory, getMechanismEventHandlersCategory } from './event_handlers_category';
2626
import { createMechanismBlock } from '../blocks/mrc_mechanism';
2727
import { getAllPossibleComponents } from '../blocks/mrc_component';
2828
import {
@@ -92,23 +92,31 @@ function getRobotMechanismsCategory(currentModule: storageModule.Module): toolbo
9292

9393
if (editor) {
9494
editor.getMechanismsFromRobot().forEach(mechanismInRobot => {
95-
const mechanismBlocks: toolboxItems.Item[] = [];
96-
97-
// Add the blocks for this mechanism's methods.
95+
// Add the blocks for this mechanism's methods and events.
9896
const mechanism = editor.getMechanism(mechanismInRobot);
9997
if (mechanism) {
100-
const methodsFromMechanism = editor.getMethodsFromMechanism(mechanism);
101-
addInstanceMechanismBlocks(mechanismInRobot, methodsFromMechanism, mechanismBlocks);
98+
const mechanismCategories: toolboxItems.Category[] = [];
10299

103-
if(mechanismBlocks.length === 0){
100+
// Get the list of methods from the mechanism and add the blocks for calling the methods.
101+
const mechanismMethodBlocks: toolboxItems.Item[] = [];
102+
const methodsFromMechanism = editor.getMethodsFromMechanism(mechanism);
103+
addInstanceMechanismBlocks(mechanismInRobot, methodsFromMechanism, mechanismMethodBlocks);
104+
if (mechanismMethodBlocks.length === 0) {
104105
const label : toolboxItems.Label = new toolboxItems.Label(Blockly.Msg['NO_MECHANISM_CONTENTS']);
105-
mechanismBlocks.push( label );
106+
mechanismMethodBlocks.push(label);
106107
}
108+
mechanismCategories.push({
109+
kind: 'category',
110+
name: Blockly.Msg['MRC_CATEGORY_METHODS'],
111+
contents: mechanismMethodBlocks,
112+
});
113+
114+
mechanismCategories.push(getMechanismEventHandlersCategory(mechanismInRobot));
107115

108116
contents.push({
109117
kind: 'category',
110118
name: mechanismInRobot.name,
111-
contents: mechanismBlocks,
119+
contents: mechanismCategories,
112120
});
113121
}
114122
});

0 commit comments

Comments
 (0)