Skip to content

Commit d48aebe

Browse files
committed
Changes to add ports, get things more like we want
1 parent bfbdd22 commit d48aebe

File tree

8 files changed

+460
-132
lines changed

8 files changed

+460
-132
lines changed

src/blocks/mrc_component.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ export type ConstructorArg = {
3737

3838
type ComponentExtraState = {
3939
importModule?: string,
40+
hideParams?: boolean,
4041
params?: ConstructorArg[],
4142
}
4243

4344
type ComponentBlock = Blockly.Block & ComponentMixin;
4445
interface ComponentMixin extends ComponentMixinType {
4546
mrcArgs: ConstructorArg[],
47+
hideParams: boolean,
4648
mrcImportModule: string,
4749
}
4850
type ComponentMixinType = typeof COMPONENT;
@@ -59,7 +61,6 @@ const COMPONENT = {
5961
.appendField(createFieldNonEditableText(''), 'TYPE');
6062
this.setPreviousStatement(true, OUTPUT_NAME);
6163
this.setNextStatement(true, OUTPUT_NAME);
62-
// this.setOutput(true, OUTPUT_NAME);
6364
},
6465

6566
/**
@@ -74,20 +75,24 @@ const COMPONENT = {
7475
'name': arg.name,
7576
'type': arg.type,
7677
});
77-
});
78+
});
7879
if (this.mrcImportModule) {
7980
extraState.importModule = this.mrcImportModule;
8081
}
82+
if (this.hideParams) {
83+
extraState.hideParams = this.hideParams;
84+
}
8185
return extraState;
8286
},
8387
/**
8488
* Applies the given state to this block.
8589
*/
8690
loadExtraState: function (this: ComponentBlock, extraState: ComponentExtraState): void {
8791
this.mrcImportModule = extraState.importModule ? extraState.importModule : '';
92+
this.hideParams = extraState.hideParams ? extraState.hideParams : false;
8893
this.mrcArgs = [];
8994

90-
if(extraState.params){
95+
if (extraState.params) {
9196
extraState.params.forEach((arg) => {
9297
this.mrcArgs.push({
9398
'name': arg.name,
@@ -101,17 +106,19 @@ const COMPONENT = {
101106
/**
102107
* Update the block to reflect the newly loaded extra state.
103108
*/
104-
updateBlock_: function(this: ComponentBlock): void {
109+
updateBlock_: function (this: ComponentBlock): void {
110+
if (this.hideParams == false) {
105111
// Add input sockets for the arguments.
106112
for (let i = 0; i < this.mrcArgs.length; i++) {
107113
const input = this.appendValueInput('ARG' + i)
108-
.setAlign(Blockly.inputs.Align.RIGHT)
109-
.appendField(this.mrcArgs[i].name);
114+
.setAlign(Blockly.inputs.Align.RIGHT)
115+
.appendField(this.mrcArgs[i].name);
110116
if (this.mrcArgs[i].type) {
111117
input.setCheck(getAllowedTypesForSetCheck(this.mrcArgs[i].type));
112118
}
113119
}
114120
}
121+
}
115122
}
116123

117124
export const setup = function () {
@@ -122,18 +129,26 @@ export const pythonFromBlock = function (
122129
block: ComponentBlock,
123130
generator: ExtendedPythonGenerator,
124131
) {
125-
if(block.mrcImportModule){
132+
if (block.mrcImportModule) {
126133
generator.addImport(block.mrcImportModule);
127134
}
128135
let code = 'self.' + block.getFieldValue('NAME') + ' = ' + block.getFieldValue('TYPE') + '(';
129-
136+
130137
for (let i = 0; i < block.mrcArgs.length; i++) {
131-
const fieldName = 'ARG' + i;
138+
const fieldName = 'ARG' + i;
139+
if (i != 0) {
140+
code += ', '
141+
}
142+
if(block.hideParams){
143+
let extension = '';
132144
if(i != 0){
133-
code += ', '
145+
extension = '_' + (i + 1).toString();
134146
}
147+
code += block.mrcArgs[i].name + " = " + block.getFieldValue('NAME') + extension;
148+
}else{
135149
code += block.mrcArgs[i].name + ' = ' + generator.valueToCode(block, fieldName, Order.NONE);
136-
}
150+
}
151+
}
137152
code += ')\n' + "self.hardware.append(self." + block.getFieldValue('NAME') + ")\n";
138153
return code;
139154
}

src/blocks/mrc_port.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* @license
3+
* Copyright 2025 Porpoiseful LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* @fileoverview Create a component with a name of a certain type
20+
* @author [email protected] (Alan Smith)
21+
*/
22+
import * as Blockly from 'blockly';
23+
import { Order } from 'blockly/python';
24+
25+
import { MRC_STYLE_PORTS } from '../themes/styles'
26+
import { createFieldNonEditableText } from '../fields/FieldNonEditableText';
27+
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
28+
import { createFieldDropdown } from '../fields/FieldDropdown';
29+
30+
export const BLOCK_NAME = 'mrc_port';
31+
export const OUTPUT_NAME = 'mrc_port';
32+
33+
34+
type PortBlock = Blockly.Block & PortMixin;
35+
interface PortMixin extends PortMixinType {
36+
}
37+
type PortMixinType = typeof COMPONENT;
38+
39+
const COMPONENT = {
40+
/**
41+
* Block initialization.
42+
*/
43+
init: function (this: PortBlock): void {
44+
this.setStyle(MRC_STYLE_PORTS);
45+
this.appendDummyInput()
46+
.appendField(createFieldNonEditableText(''), 'TYPE')
47+
.appendField(new Blockly.FieldTextInput(''), 'PORT_NUM');
48+
49+
//this.setOutput(true, OUTPUT_NAME);
50+
this.setOutput(true);
51+
},
52+
}
53+
54+
export const setup = function () {
55+
Blockly.Blocks[BLOCK_NAME] = COMPONENT;
56+
}
57+
58+
export const pythonFromBlock = function (
59+
block: PortBlock,
60+
generator: ExtendedPythonGenerator,
61+
) {
62+
let code = "";
63+
//TODO(Alan) - Make this real
64+
65+
return [code, Order.ATOMIC];
66+
}

src/blocks/setup_custom_blocks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import * as ClassMethodDef from './mrc_class_method_def';
1111
import * as Mechanism from './mrc_mechanism';
1212
import * as Component from './mrc_component';
1313
import * as MechanismContainerHolder from './mrc_mechanism_component_holder';
14+
import * as Port from './mrc_port';
1415

1516
const customBlocks = [
1617
CallPythonFunction,
@@ -25,6 +26,7 @@ const customBlocks = [
2526
Mechanism,
2627
Component,
2728
MechanismContainerHolder,
29+
Port,
2830
];
2931

3032
export const setup = function(forBlock: any) {

src/modules/mechanism_start.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@
1313
"canBeCalledWithinClass": false,
1414
"canBeCalledOutsideClass": false,
1515
"returnType": "None",
16-
"params": [],
16+
"params": [
17+
{
18+
"name": "port_my_motor",
19+
"type": "int"
20+
},
21+
{
22+
"name": "port_my_color_range_sensor",
23+
"type": "int"
24+
}
25+
],
1726
"pythonMethodName": "__init__"
1827
},
1928
"fields": {

src/themes/styles.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const MRC_STYLE_CLASS_BLOCKS = 'mrc_style_class_blocks';
3030
export const MRC_CATEGORY_STYLE_METHODS = 'mrc_category_style_methods';
3131
export const MRC_STYLE_MECHANISMS = 'mrc_style_mechanisms';
3232
export const MRC_STYLE_COMPONENTS = 'mrc_style_components';
33+
export const MRC_STYLE_PORTS = 'mrc_style_ports';
3334

3435
export const add_mrc_styles = function (theme: Blockly.Theme): Blockly.Theme {
3536
theme.setBlockStyle(MRC_STYLE_FUNCTIONS, {
@@ -85,6 +86,12 @@ export const add_mrc_styles = function (theme: Blockly.Theme): Blockly.Theme {
8586
"colourTertiary": "#496684",
8687
hat: ""
8788
});
89+
theme.setBlockStyle(MRC_STYLE_PORTS, {
90+
colourPrimary: "#5ba55b",
91+
colourSecondary: "#deedde",
92+
colourTertiary: "#498449",
93+
hat: ""
94+
});
8895

8996
return theme;
9097
}

0 commit comments

Comments
 (0)