2121
2222import * as Blockly from 'blockly/core' ;
2323import * 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' ;
2526import { 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
3537const 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}
0 commit comments