@@ -34,31 +34,31 @@ export function getHardwareCategory(currentModule: commonStorage.Module): toolbo
3434 kind : 'category' ,
3535 name : Blockly . Msg [ 'MRC_CATEGORY_HARDWARE' ] ,
3636 contents : [
37- getRobotMechanismsBlocks ( currentModule ) ,
38- getComponentsBlocks ( false ) ,
37+ getRobotMechanismsCategory ( currentModule ) ,
38+ getComponentsCategory ( false ) ,
3939 ]
4040 } ;
4141 }
4242 if ( currentModule . moduleType === commonStorage . MODULE_TYPE_MECHANISM ) {
43- return getComponentsBlocks ( true ) ;
43+ return getComponentsCategory ( true ) ;
4444 }
4545 if ( currentModule . moduleType === commonStorage . MODULE_TYPE_OPMODE ) {
4646 return {
4747 kind : 'category' ,
4848 name : Blockly . Msg [ 'MRC_CATEGORY_ROBOT' ] ,
4949 contents : [
50- getRobotMechanismsBlocks ( currentModule ) ,
51- getRobotComponentsBlocks ( ) ,
52- getRobotMethodsBlocks ( ) ,
53- getRobotEventsBlocks ( ) ,
50+ getRobotMechanismsCategory ( currentModule ) ,
51+ getRobotComponentsCategory ( ) ,
52+ getRobotMethodsCategory ( ) ,
53+ getRobotEventsCategory ( ) ,
5454 ]
5555 } ;
5656 }
5757 throw new Error ( 'currentModule.moduleType has unexpected value: ' + currentModule . moduleType )
5858}
5959
60- function getRobotMechanismsBlocks ( currentModule : commonStorage . Module ) : toolboxItems . Category {
61- // getRobotMechanismsBlocks is called when the user is editing the robot or an opmode.
60+ function getRobotMechanismsCategory ( currentModule : commonStorage . Module ) : toolboxItems . Category {
61+ // getRobotMechanismsCategory is called when the user is editing the robot or an opmode.
6262 // If the user is editing the robot, it allows the user to add a mechanism to
6363 // the robot or use an existing mechanism.
6464 // If the user is editing an opmode, it allows the user to use a mechanism that
@@ -211,8 +211,8 @@ function getRobotMechanismsBlocks(currentModule: commonStorage.Module): toolboxI
211211 } ;
212212}
213213
214- function getRobotComponentsBlocks ( ) : toolboxItems . Category {
215- // getRobotComponentsBlocks is called when the user is editing an opmode.
214+ function getRobotComponentsCategory ( ) : toolboxItems . Category {
215+ // getRobotComponentsCategory is called when the user is editing an opmode.
216216 // It allows the user to use a component that was previously added to the Robot.
217217
218218 const contents : toolboxItems . ContentsType [ ] = [ ] ;
@@ -242,8 +242,8 @@ function getRobotComponentsBlocks(): toolboxItems.Category {
242242 } ;
243243}
244244
245- function getRobotMethodsBlocks ( ) : toolboxItems . Category {
246- // getRobotMethodsBlocks is called when the user is editing an opmode.
245+ function getRobotMethodsCategory ( ) : toolboxItems . Category {
246+ // getRobotMethodsCategory is called when the user is editing an opmode.
247247 // It allows the user to use methods there previously defined in the Robot.
248248
249249 const contents : toolboxItems . ContentsType [ ] = [ ] ;
@@ -266,8 +266,8 @@ function getRobotMethodsBlocks(): toolboxItems.Category {
266266 } ;
267267}
268268
269- function getComponentsBlocks ( hideParams : boolean ) : toolboxItems . Category {
270- // getComponentsBlocks is called when the user is editing the robot or a
269+ function getComponentsCategory ( hideParams : boolean ) : toolboxItems . Category {
270+ // getComponentsCategory is called when the user is editing the robot or a
271271 // mechanism. It allows the user to add a component or use an existing component.
272272
273273 const contents : toolboxItems . ContentsType [ ] = [ ] ;
@@ -303,26 +303,41 @@ function getComponentsBlocks(hideParams : boolean): toolboxItems.Category {
303303 } ;
304304}
305305
306- function getRobotEventsBlocks ( ) : toolboxItems . Category {
307- // getRobotEventsBlocks is called when the user is editing an opmode.
308- // It allows the user to create event handlers for events previously defined in the Robot.
306+ const CUSTOM_CATEGORY_ROBOT_EVENTS = 'ROBOT_EVENTS' ;
309307
310- const contents : toolboxItems . ContentsType [ ] = [ ] ;
308+ // The robot events category is shown when the user is editing an opmode.
309+ // It allows the user to create event handlers for events previously defined in the Robot.
310+ const getRobotEventsCategory = ( ) => ( {
311+ kind : 'category' ,
312+ name : Blockly . Msg [ 'MRC_CATEGORY_EVENTS' ] ,
313+ custom : CUSTOM_CATEGORY_ROBOT_EVENTS ,
314+ } ) ;
315+
316+ export class RobotEventsCategory {
317+ constructor ( blocklyWorkspace : Blockly . WorkspaceSvg ) {
318+ blocklyWorkspace . registerToolboxCategoryCallback ( CUSTOM_CATEGORY_ROBOT_EVENTS , this . robotEventsFlyout . bind ( this ) ) ;
319+ }
320+
321+ public robotEventsFlyout ( workspace : Blockly . WorkspaceSvg ) {
322+ const contents : toolboxItems . ContentsType [ ] = [ ] ;
323+
324+ // Get the list of events from the robot and add the blocks for handling events.
311325
312- // Get the list of events from the robot and add the blocks for calling the
313- // robot functions.
314- const workspace = Blockly . getMainWorkspace ( ) ;
315- if ( workspace ) {
316326 const editor = Editor . getEditorForBlocklyWorkspace ( workspace ) ;
317327 if ( editor ) {
318328 const eventsFromRobot = editor . getEventsFromRobot ( ) ;
319- addRobotEventHandlerBlocks ( eventsFromRobot , contents ) ;
329+ // Remove events if there is already a corresponding handler in the workspace.
330+ const eventHandlerNames = editor . getEventHandlerNamesFromWorkspace ( ) ;
331+ const eventsToShow = eventsFromRobot . filter ( event => {
332+ return ! eventHandlerNames . includes ( event . name ) ;
333+ } ) ;
334+ addRobotEventHandlerBlocks ( eventsToShow , contents ) ;
320335 }
321- }
322336
323- return {
324- kind : 'category' ,
325- name : Blockly . Msg [ 'MRC_CATEGORY_EVENTS' ] ,
326- contents,
327- } ;
337+ const toolboxInfo = {
338+ contents : contents ,
339+ } ;
340+
341+ return toolboxInfo ;
342+ }
328343}
0 commit comments