@@ -25,27 +25,19 @@ import { MRC_STYLE_MECHANISMS } from '../themes/styles';
2525import { ExtendedPythonGenerator } from '../editor/extended_python_generator' ;
2626import { OUTPUT_NAME as MECHANISM_OUTPUT } from './mrc_mechanism' ;
2727import { OUTPUT_NAME as COMPONENT_OUTPUT } from './mrc_component' ;
28- import { createPlusField } from '../fields/field_plus' ;
29- import { createMinusField } from '../fields/field_minus' ;
30- import { Order } from 'blockly/python' ;
31-
3228
3329export const BLOCK_NAME = 'mrc_mechanism_component_holder' ;
3430
3531export const MECHANISM = 'mechanism' ;
3632export const COMPONENT = 'component' ;
3733
3834type MechanismComponentHolderExtraState = {
39- numMechanismInputs ?: number ,
40- numComponentInputs ?: number ,
35+ hideMechanims ?: boolean ;
4136}
4237
4338type MechanismComponentHolderBlock = Blockly . Block & MechanismComponentHolderMixin ;
4439interface MechanismComponentHolderMixin extends MechanismComponentHolderMixinType {
45- mrcNumMechanismInputs : number ,
46- mrcNumComponentInputs : number ,
47- lastMechanism : Blockly . Input ,
48- lastComponent : Blockly . Input
40+ mrcHideMechanisms : boolean ;
4941}
5042type MechanismComponentHolderMixinType = typeof MECHANISM_COMPONENT_HOLDER ;
5143
@@ -55,158 +47,41 @@ const MECHANISM_COMPONENT_HOLDER = {
5547 */
5648 init : function ( this : MechanismComponentHolderBlock ) : void {
5749 this . setInputsInline ( false ) ;
58- this . appendValueInput ( 'MECHANISM_1' )
59- . appendField ( 'Mechanisms' )
60- . appendField ( createPlusField ( MECHANISM ) , 'PLUS_MECHANISM' )
61- . setCheck ( MECHANISM_OUTPUT ) ;
50+ this . appendStatementInput ( 'MECHANISMS' ) . setCheck ( MECHANISM_OUTPUT ) . appendField ( 'Mechanisms' ) ;
51+ this . appendStatementInput ( 'COMPONENTS' ) . setCheck ( COMPONENT_OUTPUT ) . appendField ( 'Components' ) ;
6252
63- this . appendValueInput ( 'COMPONENT_1' )
64- . appendField ( 'Components' )
65- . appendField ( createPlusField ( COMPONENT ) , 'PLUS_COMPONENT' )
66- . setCheck ( COMPONENT_OUTPUT ) ;
6753 this . setOutput ( false ) ;
6854 this . setStyle ( MRC_STYLE_MECHANISMS ) ;
69- this . mrcNumComponentInputs = 1 ;
70- this . mrcNumMechanismInputs = 1 ;
71- } ,
72- plus : function ( this : MechanismComponentHolderBlock , type : string ) : void {
73- if ( type == MECHANISM ) {
74- this . addMechanismPart_ ( ) ;
75- } else if ( type == COMPONENT ) {
76- this . addComponentPart_ ( ) ;
77- }
78- } ,
79- addMechanismPart_ : function ( this : MechanismComponentHolderBlock ) : void {
80- this . mrcNumMechanismInputs += 1 ;
81- let newName = 'MECHANISM_' + this . mrcNumMechanismInputs ;
82- let lastLast = this . lastMechanism ;
83- this . lastMechanism = this . appendValueInput ( newName )
84- . setAlign ( Blockly . inputs . Align . RIGHT )
85- . appendField ( createMinusField ( MECHANISM ) , 'MINUS_MECHANISM' )
86- . setCheck ( MECHANISM_OUTPUT ) ;
87- this . moveInputBefore ( newName , 'COMPONENT_1' ) ;
88- if ( lastLast ) {
89- lastLast . removeField ( 'MINUS_MECHANISM' ) ;
90- }
91- } ,
92- addComponentPart_ : function ( this : MechanismComponentHolderBlock ) : void {
93- this . mrcNumComponentInputs += 1 ;
94- let newName = 'COMPONENT_' + this . mrcNumComponentInputs ;
95- let lastLast = this . lastComponent ;
96- this . lastComponent = this . appendValueInput ( newName )
97- . setAlign ( Blockly . inputs . Align . RIGHT )
98- . appendField ( createMinusField ( COMPONENT ) , 'MINUS_COMPONENT' )
99- . setCheck ( COMPONENT_OUTPUT ) ;
100- if ( lastLast ) {
101- lastLast . removeField ( 'MINUS_COMPONENT' ) ;
102- }
103- } ,
104- minus : function ( this : MechanismComponentHolderBlock , type : string ) : void {
105- if ( type == MECHANISM ) {
106- this . subtractMechanismPart_ ( ) ;
107- } else if ( type == COMPONENT ) {
108- this . subtractComponentPart_ ( ) ;
109- }
110- } ,
111- subtractMechanismPart_ : function ( this : MechanismComponentHolderBlock ) : void {
112- if ( this . mrcNumMechanismInputs > 1 ) {
113- let name = 'MECHANISM_' + this . mrcNumMechanismInputs ;
114- this . removeInput ( name ) ;
115- this . mrcNumMechanismInputs -= 1 ;
116- if ( this . mrcNumMechanismInputs > 1 ) {
117- name = 'MECHANISM_' + this . mrcNumMechanismInputs ;
118- let lastInput = this . getInput ( name ) ;
119- if ( lastInput ) {
120- this . lastMechanism = lastInput . appendField ( createMinusField ( MECHANISM ) , 'MINUS_MECHANISM' ) ;
121- }
122- }
123- }
124- } ,
125- subtractComponentPart_ : function ( this : MechanismComponentHolderBlock ) : void {
126- if ( this . mrcNumComponentInputs > 1 ) {
127- let name = 'COMPONENT_' + this . mrcNumComponentInputs ;
128- this . removeInput ( name ) ;
129- this . mrcNumComponentInputs -= 1 ;
130- if ( this . mrcNumComponentInputs > 1 ) {
131- name = 'COMPONENT_' + this . mrcNumComponentInputs ;
132- let lastInput = this . getInput ( name ) ;
133- if ( lastInput ) {
134- this . lastMechanism = lastInput . appendField ( createMinusField ( COMPONENT ) , 'MINUS_COMPONENT' ) ;
135- }
136- }
137- }
13855 } ,
13956 saveExtraState : function ( this : MechanismComponentHolderBlock ) : MechanismComponentHolderExtraState {
14057 const extraState : MechanismComponentHolderExtraState = {
14158 } ;
142- if ( this . mrcNumComponentInputs != 1 ) {
143- extraState . numComponentInputs = this . mrcNumComponentInputs ;
144- }
145- if ( this . mrcNumMechanismInputs != 1 ) {
146- extraState . numMechanismInputs = this . mrcNumMechanismInputs ;
59+ if ( this . mrcHideMechanisms == true ) {
60+ extraState . hideMechanims = this . mrcHideMechanisms ;
14761 }
14862 return extraState ;
14963 } ,
15064 /**
15165 * Applies the given state to this block.
15266 */
15367 loadExtraState : function ( this : MechanismComponentHolderBlock , extraState : MechanismComponentHolderExtraState ) : void {
154- this . mrcNumComponentInputs = ( extraState . numComponentInputs == undefined ) ? 1 : extraState . numComponentInputs ;
155- this . mrcNumMechanismInputs = ( extraState . numMechanismInputs == undefined ) ? 1 : extraState . numMechanismInputs ;
68+ this . mrcHideMechanisms = ( extraState . hideMechanims == undefined ) ? 1 : extraState . hideMechanims ;
15669 this . updateBlock_ ( ) ;
15770 } ,
15871 /**
15972 * Update the block to reflect the newly loaded extra state.
16073 */
16174 updateBlock_ : function ( this : MechanismComponentHolderBlock ) : void {
162- let number = 1 ;
163- while ( this . getInput ( 'MECHANISM_' + number ) ) {
164- this . removeInput ( 'MECHANISM_' + number ) ;
165- number += 1 ;
166- }
167- number = 1 ;
168- while ( this . getInput ( 'COMPONENT_' + number ) ) {
169- this . removeInput ( 'COMPONENT_' + number ) ;
170- number += 1 ;
171- }
172-
173- if ( this . mrcNumMechanismInputs != 0 ) {
174- this . appendValueInput ( 'MECHANISM_1' )
175- . appendField ( 'Mechanisms' )
176- . appendField ( createPlusField ( MECHANISM ) , 'PLUS_MECHANISM' )
177- . setCheck ( MECHANISM_OUTPUT ) ;
178- }
179- let remainingMechanisms = this . mrcNumMechanismInputs - 1 ;
180- number = 1 ;
181- while ( remainingMechanisms > 0 ) {
182- number += 1 ;
183- let newName = 'MECHANISM_' + number ;
184- this . lastMechanism = this . appendValueInput ( newName )
185- . setAlign ( Blockly . inputs . Align . RIGHT )
186- . setCheck ( MECHANISM_OUTPUT ) ;
187- if ( remainingMechanisms == 1 ) {
188- this . lastMechanism . appendField ( createMinusField ( MECHANISM ) , 'MINUS_MECHANISM' )
189- }
190- remainingMechanisms -- ;
191- }
192-
193- if ( this . mrcNumComponentInputs != 0 ) {
194- this . appendValueInput ( 'COMPONENT_1' )
195- . appendField ( 'Components' )
196- . appendField ( createPlusField ( COMPONENT ) , 'PLUS_COMPONENT' )
197- . setCheck ( COMPONENT_OUTPUT ) ;
198- }
199- let remainingComponents = this . mrcNumComponentInputs - 1 ;
200- number = 1 ;
201- while ( remainingComponents > 0 ) {
202- let newName = 'COMPONENT_' + number ;
203- this . lastComponent = this . appendValueInput ( newName )
204- . setAlign ( Blockly . inputs . Align . RIGHT )
205- . setCheck ( COMPONENT_OUTPUT ) ;
206- if ( remainingComponents == 1 ) {
207- this . lastComponent . appendField ( createMinusField ( COMPONENT ) , 'MINUS_COMPONENT' )
208- }
209- remainingComponents -- ;
75+ if ( this . mrcHideMechanisms ) {
76+ if ( this . getInput ( 'MECHANISMS' ) ) {
77+ this . removeInput ( 'MECHANISMS' )
78+ }
79+ }
80+ else {
81+ if ( ! this . getInput ( 'MECHANISMS' ) ) {
82+ this . appendStatementInput ( 'MECHANISMS' ) . setCheck ( MECHANISM_OUTPUT ) . appendField ( 'Mechanisms' ) ;
83+ this . moveInputBefore ( 'MECHANISMS' , 'COMPONENTS' )
84+ }
21085 }
21186 }
21287}
@@ -219,28 +94,18 @@ export const pythonFromBlock = function (
21994 block : MechanismComponentHolderBlock ,
22095 generator : ExtendedPythonGenerator ,
22196) {
222- let code = 'def define_hardware(self):\n' ;
97+ let code = 'def define_hardware(self):\n' + generator . INDENT + 'self.hardware = []\n' ;
22398
224- let body = '' ;
225- for ( let i = 1 ; i <= block . mrcNumMechanismInputs ; i ++ ) {
226- const name = 'MECHANISM_' + i ;
227- if ( block . getInput ( name ) ) {
228- let mechanismCode = generator . valueToCode ( block , name , Order . NONE ) || '' ;
229- if ( mechanismCode != '' ) {
230- body += generator . INDENT + mechanismCode + "\n" ;
231- }
232- }
99+ let mechanisms = '' ;
100+ let components = '' ;
101+
102+ if ( block . getInput ( 'MECHANISMS' ) ) {
103+ mechanisms = generator . statementToCode ( block , 'MECHANISMS' ) ;
233104 }
234- for ( let i = 1 ; i <= block . mrcNumComponentInputs ; i ++ ) {
235- const name = 'COMPONENT_' + i ;
236- if ( block . getInput ( name ) ) {
237- let componentCode = generator . valueToCode ( block , name , Order . NONE ) || '' ;
238- if ( componentCode != '' ) {
239- body += generator . INDENT + componentCode + "\n" ;
240- }
241- }
105+ if ( block . getInput ( 'COMPONENTS' ) ) {
106+ components = generator . statementToCode ( block , 'COMPONENTS' ) ;
242107 }
243-
108+ const body = mechanisms + components ;
244109 if ( body != '' ) {
245110 code += body ;
246111 } else {
0 commit comments