@@ -75,23 +75,10 @@ function setName(block: Blockly.BlockSvg){
7575
7676const MECHANISM_COMPONENT_HOLDER = {
7777 /**
78- * Block initialization.
79- */
78+ * Block initialization.
79+ */
8080 init : function ( this : MechanismComponentHolderBlock ) : void {
8181 this . setInputsInline ( false ) ;
82- this . appendStatementInput ( INPUT_MECHANISMS ) . setCheck ( MECHANISM_OUTPUT ) . appendField ( Blockly . Msg . MECHANISMS ) ;
83- this . appendStatementInput ( INPUT_COMPONENTS ) . setCheck ( COMPONENT_OUTPUT ) . appendField ( Blockly . Msg . COMPONENTS ) ;
84- const privateComponentsInput = this . appendStatementInput ( INPUT_PRIVATE_COMPONENTS ) . setCheck ( COMPONENT_OUTPUT ) . appendField ( Blockly . Msg . PRIVATE_COMPONENTS ) ;
85- // Set tooltip on the private components field
86- const privateComponentsField = privateComponentsInput . fieldRow [ 0 ] ;
87- if ( privateComponentsField ) {
88- privateComponentsField . setTooltip ( Blockly . Msg . PRIVATE_COMPONENTS_TOOLTIP ) ;
89- }
90- this . appendStatementInput ( INPUT_EVENTS ) . setCheck ( EVENT_OUTPUT ) . appendField ( Blockly . Msg . EVENTS ) ;
91-
92- // Update components tooltip based on private components visibility
93- this . updateComponentsTooltip_ ( ) ;
94-
9582 this . setOutput ( false ) ;
9683 this . setStyle ( MRC_STYLE_MECHANISMS ) ;
9784 ChangeFramework . registerCallback ( MRC_COMPONENT_NAME , [ Blockly . Events . BLOCK_MOVE , Blockly . Events . BLOCK_CHANGE ] , this . onBlockChanged ) ;
@@ -112,65 +99,43 @@ const MECHANISM_COMPONENT_HOLDER = {
11299 return extraState ;
113100 } ,
114101 /**
115- * Applies the given state to this block.
116- */
102+ * Applies the given state to this block.
103+ */
117104 loadExtraState : function ( this : MechanismComponentHolderBlock , extraState : MechanismComponentHolderExtraState ) : void {
118105 this . mrcHideMechanisms = ( extraState . hideMechanisms == undefined ) ? false : extraState . hideMechanisms ;
119106 this . mrcHidePrivateComponents = ( extraState . hidePrivateComponents == undefined ) ? false : extraState . hidePrivateComponents ;
120107 this . updateBlock_ ( ) ;
121108 } ,
122109 /**
123- * Update the components tooltip based on private components visibility .
110+ * Update the block to reflect the newly loaded extra state .
124111 */
125- updateComponentsTooltip_ : function ( this : MechanismComponentHolderBlock ) : void {
126- const componentsInput = this . getInput ( INPUT_COMPONENTS ) ;
127- if ( componentsInput && componentsInput . fieldRow [ 0 ] ) {
128- const componentsField = componentsInput . fieldRow [ 0 ] ;
129- // Only show tooltip if private components are also visible (not hidden)
130- if ( ! this . mrcHidePrivateComponents ) {
131- componentsField . setTooltip ( Blockly . Msg . COMPONENTS_TOOLTIP ) ;
132- } else {
133- componentsField . setTooltip ( '' ) ;
134- }
135- }
136- } ,
137- /**
138- * Update the block to reflect the newly loaded extra state.
139- */
140112 updateBlock_ : function ( this : MechanismComponentHolderBlock ) : void {
141113 // Handle mechanisms input visibility
142- if ( this . mrcHideMechanisms ) {
143- if ( this . getInput ( INPUT_MECHANISMS ) ) {
144- this . removeInput ( INPUT_MECHANISMS )
145- }
146- }
147- else {
148- if ( this . getInput ( INPUT_MECHANISMS ) == null ) {
149- this . appendStatementInput ( INPUT_MECHANISMS ) . setCheck ( MECHANISM_OUTPUT ) . appendField ( Blockly . Msg . MECHANISMS ) ;
150- this . moveInputBefore ( INPUT_MECHANISMS , INPUT_COMPONENTS )
151- }
114+ if ( ! this . mrcHideMechanisms ) {
115+ this . appendStatementInput ( INPUT_MECHANISMS )
116+ . setCheck ( MECHANISM_OUTPUT )
117+ . appendField ( Blockly . Msg . MECHANISMS ) ;
152118 }
153119
120+ const componentsField = new Blockly . FieldLabel ( Blockly . Msg . COMPONENTS ) ;
121+ this . appendStatementInput ( INPUT_COMPONENTS )
122+ . setCheck ( COMPONENT_OUTPUT )
123+ . appendField ( componentsField ) ;
124+
154125 // Handle private components input visibility
155- if ( this . mrcHidePrivateComponents ) {
156- if ( this . getInput ( INPUT_PRIVATE_COMPONENTS ) ) {
157- this . removeInput ( INPUT_PRIVATE_COMPONENTS )
158- }
159- }
160- else {
161- if ( this . getInput ( INPUT_PRIVATE_COMPONENTS ) == null ) {
162- const privateComponentsInput = this . appendStatementInput ( INPUT_PRIVATE_COMPONENTS ) . setCheck ( COMPONENT_OUTPUT ) . appendField ( Blockly . Msg . PRIVATE_COMPONENTS ) ;
163- // Set tooltip on the field
164- const privateComponentsField = privateComponentsInput . fieldRow [ 0 ] ;
165- if ( privateComponentsField ) {
166- privateComponentsField . setTooltip ( Blockly . Msg . PRIVATE_COMPONENTS_TOOLTIP ) ;
167- }
168- this . moveInputBefore ( INPUT_PRIVATE_COMPONENTS , INPUT_EVENTS )
169- }
126+ if ( ! this . mrcHidePrivateComponents ) {
127+ const privateComponentsField = new Blockly . FieldLabel ( Blockly . Msg . PRIVATE_COMPONENTS ) ;
128+ this . appendStatementInput ( INPUT_PRIVATE_COMPONENTS )
129+ . setCheck ( COMPONENT_OUTPUT )
130+ . appendField ( privateComponentsField ) ;
131+ // Set tooltips on both componentsField and privateComponentsField.
132+ componentsField . setTooltip ( Blockly . Msg . COMPONENTS_TOOLTIP ) ;
133+ privateComponentsField . setTooltip ( Blockly . Msg . PRIVATE_COMPONENTS_TOOLTIP ) ;
170134 }
171-
172- // Update components tooltip based on private components visibility
173- this . updateComponentsTooltip_ ( ) ;
135+
136+ this . appendStatementInput ( INPUT_EVENTS )
137+ . setCheck ( EVENT_OUTPUT )
138+ . appendField ( Blockly . Msg . EVENTS ) ;
174139 } ,
175140 onBlockChanged : function ( block : Blockly . BlockSvg , blockEvent : Blockly . Events . BlockBase ) {
176141 if ( blockEvent . type == Blockly . Events . BLOCK_MOVE ) {
@@ -319,9 +284,9 @@ function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, genera
319284 const components = generator . statementToCode ( block , INPUT_COMPONENTS ) ;
320285 const privateComponents = generator . statementToCode ( block , INPUT_PRIVATE_COMPONENTS ) ;
321286
322- const body = components + privateComponents ;
323- if ( body ) {
324- code += body ;
287+ const allComponents = components + privateComponents ;
288+ if ( allComponents ) {
289+ code += allComponents ;
325290 generator . addClassMethodDefinition ( 'define_hardware' , code ) ;
326291 }
327292}
@@ -342,7 +307,7 @@ export const pythonFromBlock = function (
342307
343308// Misc
344309
345- /**n
310+ /**
346311 * Returns true if the given workspace has a mrc_mechanism_component_holder
347312 * block that contains at least one component.
348313 */
@@ -464,3 +429,17 @@ export function getEvents(
464429 events . push ( ...eventsFromHolder ) ;
465430 } ) ;
466431}
432+
433+ /**
434+ * Hide private components.
435+ * This function should only be called when upgrading old projects.
436+ */
437+ export function hidePrivateComponents ( workspace : Blockly . Workspace ) {
438+ // Make sure the workspace is headless.
439+ if ( workspace . rendered ) {
440+ throw new Error ( 'hidePrivateComponents should never be called with a rendered workspace.' ) ;
441+ }
442+ workspace . getBlocksByType ( BLOCK_NAME ) . forEach ( block => {
443+ ( block as MechanismComponentHolderBlock ) . mrcHidePrivateComponents = true ;
444+ } ) ;
445+ }
0 commit comments