2323import * as Blockly from 'blockly' ;
2424import { Order } from 'blockly/python' ;
2525
26+ import type { MessageInstance } from 'antd/es/message/interface' ;
2627import { Editor } from '../editor/editor' ;
2728import { ExtendedPythonGenerator } from '../editor/extended_python_generator' ;
2829import { createFieldFlydown } from '../fields/field_flydown' ;
@@ -34,6 +35,9 @@ import * as storageModuleContent from '../storage/module_content';
3435
3536export const BLOCK_NAME = 'mrc_event_handler' ;
3637
38+ const BUTTON_CALLBACK_KEY = 'EVENT_HANDLER_ALREADY_ON_WORKSPACE' ;
39+ const BUTTON_STYLE = 'eventHandlerButtonStyle' ;
40+
3741const FIELD_SENDER = 'SENDER' ;
3842const FIELD_EVENT_NAME = 'EVENT_NAME' ;
3943
@@ -48,6 +52,7 @@ export interface Parameter {
4852 type ?: string ;
4953}
5054
55+ const SENDER_VALUE_ROBOT = 'robot' ;
5156const WARNING_ID_EVENT_CHANGED = 'event changed' ;
5257
5358export type EventHandlerBlock = Blockly . Block & EventHandlerMixin & Blockly . BlockSvg ;
@@ -405,9 +410,22 @@ function generateRegisterEventHandler(
405410
406411export function addRobotEventHandlerBlocks (
407412 events : storageModuleContent . Event [ ] ,
413+ eventHandlerBlocks : EventHandlerBlock [ ] ,
408414 contents : toolboxItems . ContentsType [ ] ) {
415+ // Collect the ids of events for which there is already an event handler.
416+ const eventIds : string [ ] = [ ] ;
417+ eventHandlerBlocks . forEach ( eventHandlerBlock => {
418+ eventIds . push ( eventHandlerBlock . getEventId ( ) ) ;
419+ } ) ;
409420 events . forEach ( event => {
410- contents . push ( createRobotEventHandlerBlock ( event ) ) ;
421+ if ( eventIds . includes ( event . eventId ) ) {
422+ // If there is already an event handler for this event, put a button in the toolbox.
423+ const text = '\u00A0\u00A0when\u00A0\u00A0' + SENDER_VALUE_ROBOT + '\u00A0\u00A0' + event . name + '\u00A0\u00A0' ;
424+ const button = new toolboxItems . Button ( text , BUTTON_CALLBACK_KEY , BUTTON_STYLE ) ;
425+ contents . push ( button ) ;
426+ } else {
427+ contents . push ( createRobotEventHandlerBlock ( event ) ) ;
428+ }
411429 } ) ;
412430}
413431
@@ -425,7 +443,7 @@ function createRobotEventHandlerBlock(
425443 } ) ;
426444 } ) ;
427445 const fields : { [ key : string ] : any } = { } ;
428- fields [ FIELD_SENDER ] = 'robot' ;
446+ fields [ FIELD_SENDER ] = SENDER_VALUE_ROBOT ;
429447 fields [ FIELD_EVENT_NAME ] = event . name ;
430448 const inputs : { [ key : string ] : any } = { } ;
431449 return new toolboxItems . Block ( BLOCK_NAME , extraState , fields , Object . keys ( inputs ) . length ? inputs : null ) ;
@@ -434,9 +452,22 @@ function createRobotEventHandlerBlock(
434452export function addMechanismEventHandlerBlocks (
435453 mechanismInRobot : storageModuleContent . MechanismInRobot ,
436454 events : storageModuleContent . Event [ ] ,
455+ eventHandlerBlocks : EventHandlerBlock [ ] ,
437456 contents : toolboxItems . ContentsType [ ] ) {
457+ // Collect the ids of events for which there is already an event handler.
458+ const eventIds : string [ ] = [ ] ;
459+ eventHandlerBlocks . forEach ( eventHandlerBlock => {
460+ eventIds . push ( eventHandlerBlock . getEventId ( ) ) ;
461+ } ) ;
438462 events . forEach ( event => {
439- contents . push ( createMechanismEventHandlerBlock ( mechanismInRobot , event ) ) ;
463+ if ( eventIds . includes ( event . eventId ) ) {
464+ // If there is already an event handler for this event, put a button in the toolbox.
465+ const text = 'when ' + mechanismInRobot . name + ' ' + event . name ;
466+ const button = new toolboxItems . Button ( text , BUTTON_CALLBACK_KEY , BUTTON_STYLE ) ;
467+ contents . push ( button ) ;
468+ } else {
469+ contents . push ( createMechanismEventHandlerBlock ( mechanismInRobot , event ) ) ;
470+ }
440471 } ) ;
441472}
442473
@@ -502,3 +533,9 @@ export function renameMechanismName(workspace: Blockly.Workspace, mechanismId: s
502533 ( block as EventHandlerBlock ) . renameMechanismName ( mechanismId , newName ) ;
503534 } ) ;
504535}
536+
537+ export function registerToolboxButton ( workspace : Blockly . WorkspaceSvg , messageApi : MessageInstance ) {
538+ workspace . registerButtonCallback ( BUTTON_CALLBACK_KEY , function ( _button ) {
539+ messageApi . info ( Blockly . Msg . EVENT_HANDLER_ALREADY_ON_WORKSPACE ) ;
540+ } ) ;
541+ }
0 commit comments