Skip to content

Commit beed3d9

Browse files
committed
Move to have components and mechanisms like statements
1 parent a02535a commit beed3d9

File tree

6 files changed

+141
-190
lines changed

6 files changed

+141
-190
lines changed

src/blocks/mrc_component.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ const COMPONENT = {
5757
.appendField(new Blockly.FieldTextInput('my_mech'), 'NAME')
5858
.appendField('of type')
5959
.appendField(createFieldNonEditableText(''), 'TYPE');
60-
this.setPreviousStatement(false);
61-
this.setNextStatement(false);
62-
this.setOutput(true, OUTPUT_NAME);
60+
this.setPreviousStatement(true, OUTPUT_NAME);
61+
this.setNextStatement(true, OUTPUT_NAME);
62+
// this.setOutput(true, OUTPUT_NAME);
6363
},
6464

6565
/**
@@ -134,6 +134,6 @@ export const pythonFromBlock = function (
134134
}
135135
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, fieldName, Order.NONE);
136136
}
137-
code += ')';
138-
return [code, Order.ATOMIC];
137+
code += ')\n' + "self.hardware.append(self." + block.getFieldValue('NAME') + ")\n";
138+
return code;
139139
}

src/blocks/mrc_mechanism.ts

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ const MECHANISM = {
5757
.appendField(new Blockly.FieldTextInput('my_mech'), 'NAME')
5858
.appendField('of type')
5959
.appendField(createFieldNonEditableText(''), 'TYPE');
60-
this.setPreviousStatement(false);
61-
this.setNextStatement(false);
62-
this.setOutput(true, OUTPUT_NAME);
60+
this.setPreviousStatement(true, OUTPUT_NAME);
61+
this.setNextStatement(true, OUTPUT_NAME);
62+
//this.setOutput(true, OUTPUT_NAME);
6363
},
6464

6565
/**
@@ -74,7 +74,7 @@ const MECHANISM = {
7474
'name': arg.name,
7575
'type': arg.type,
7676
});
77-
});
77+
});
7878
if (this.mrcImportModule) {
7979
extraState.importModule = this.mrcImportModule;
8080
}
@@ -87,7 +87,7 @@ const MECHANISM = {
8787
this.mrcImportModule = extraState.importModule ? extraState.importModule : '';
8888
this.mrcArgs = [];
8989

90-
if(extraState.params){
90+
if (extraState.params) {
9191
extraState.params.forEach((arg) => {
9292
this.mrcArgs.push({
9393
'name': arg.name,
@@ -101,17 +101,17 @@ const MECHANISM = {
101101
/**
102102
* Update the block to reflect the newly loaded extra state.
103103
*/
104-
updateBlock_: function(this: MechanismBlock): void {
105-
// Add input sockets for the arguments.
106-
for (let i = 0; i < this.mrcArgs.length; i++) {
107-
const input = this.appendValueInput('ARG' + i)
108-
.setAlign(Blockly.inputs.Align.RIGHT)
109-
.appendField(this.mrcArgs[i].name);
110-
if (this.mrcArgs[i].type) {
111-
input.setCheck(getAllowedTypesForSetCheck(this.mrcArgs[i].type));
112-
}
104+
updateBlock_: function (this: MechanismBlock): void {
105+
// Add input sockets for the arguments.
106+
for (let i = 0; i < this.mrcArgs.length; i++) {
107+
const input = this.appendValueInput('ARG' + i)
108+
.setAlign(Blockly.inputs.Align.RIGHT)
109+
.appendField(this.mrcArgs[i].name);
110+
if (this.mrcArgs[i].type) {
111+
input.setCheck(getAllowedTypesForSetCheck(this.mrcArgs[i].type));
113112
}
114113
}
114+
}
115115
}
116116

117117
export const setup = function () {
@@ -126,14 +126,15 @@ export const pythonFromBlock = function (
126126
generator.addImport(block.mrcImportModule);
127127
}
128128
let code = 'self.' + block.getFieldValue('NAME') + ' = ' + block.getFieldValue('TYPE') + '(';
129-
129+
130130
for (let i = 0; i < block.mrcArgs.length; i++) {
131-
const fieldName = 'ARG' + i;
132-
if(i != 0){
133-
code += ', '
134-
}
135-
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, fieldName, Order.NONE);
136-
}
137-
code += ')';
138-
return [code, Order.ATOMIC];
131+
const fieldName = 'ARG' + i;
132+
if (i != 0) {
133+
code += ', '
134+
}
135+
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, fieldName, Order.NONE);
136+
}
137+
code += ')\n' + "self.hardware.append(self." + block.getFieldValue('NAME') + ")\n";
138+
139+
return code;
139140
}

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 26 additions & 161 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,19 @@ import { MRC_STYLE_MECHANISMS } from '../themes/styles';
2525
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2626
import { OUTPUT_NAME as MECHANISM_OUTPUT } from './mrc_mechanism';
2727
import { 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

3329
export const BLOCK_NAME = 'mrc_mechanism_component_holder';
3430

3531
export const MECHANISM = 'mechanism';
3632
export const COMPONENT = 'component';
3733

3834
type MechanismComponentHolderExtraState = {
39-
numMechanismInputs?: number,
40-
numComponentInputs?: number,
35+
hideMechanims?: boolean;
4136
}
4237

4338
type MechanismComponentHolderBlock = Blockly.Block & MechanismComponentHolderMixin;
4439
interface MechanismComponentHolderMixin extends MechanismComponentHolderMixinType {
45-
mrcNumMechanismInputs: number,
46-
mrcNumComponentInputs: number,
47-
lastMechanism: Blockly.Input,
48-
lastComponent: Blockly.Input
40+
mrcHideMechanisms : boolean;
4941
}
5042
type 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

Comments
 (0)