Skip to content

Commit 8fa5612

Browse files
committed
No longer allow multiple components or mechanisms named the same
Fixes #115
1 parent b9b53c7 commit 8fa5612

File tree

1 file changed

+54
-17
lines changed

1 file changed

+54
-17
lines changed

src/blocks/mrc_mechanism_component_holder.ts

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,13 @@
2222
import * as Blockly from 'blockly';
2323

2424
import { MRC_STYLE_MECHANISMS } from '../themes/styles';
25+
import * as ChangeFramework from './utils/change_framework';
26+
import { getLegalName } from './utils/python';
2527
import { ExtendedPythonGenerator } from '../editor/extended_python_generator';
2628
import { OUTPUT_NAME as MECHANISM_OUTPUT } from './mrc_mechanism';
29+
import { BLOCK_NAME as MRC_MECHANISM_NAME } from './mrc_mechanism';
30+
import { BLOCK_NAME as MRC_COMPONENT_NAME } from './mrc_component';
31+
2732
import { OUTPUT_NAME as COMPONENT_OUTPUT } from './mrc_component';
2833

2934
export const BLOCK_NAME = 'mrc_mechanism_component_holder';
@@ -37,10 +42,25 @@ type MechanismComponentHolderExtraState = {
3742

3843
type MechanismComponentHolderBlock = Blockly.Block & MechanismComponentHolderMixin;
3944
interface MechanismComponentHolderMixin extends MechanismComponentHolderMixinType {
40-
mrcHideMechanisms : boolean;
45+
mrcHideMechanisms: boolean;
4146
}
4247
type MechanismComponentHolderMixinType = typeof MECHANISM_COMPONENT_HOLDER;
4348

49+
function setName(block: Blockly.BlockSvg){
50+
const parentBlock = ChangeFramework.getParentOfType(block, BLOCK_NAME);
51+
if (parentBlock) {
52+
const variableBlocks = parentBlock!.getDescendants(true)
53+
const otherNames: string[] = []
54+
variableBlocks?.forEach(function (variableBlock) {
55+
if (variableBlock != block) {
56+
otherNames.push(variableBlock.getFieldValue('NAME'));
57+
}
58+
});
59+
const currentName = block.getFieldValue('NAME');
60+
block.setFieldValue(getLegalName(currentName, otherNames), 'NAME');
61+
}
62+
}
63+
4464
const MECHANISM_COMPONENT_HOLDER = {
4565
/**
4666
* Block initialization.
@@ -52,6 +72,9 @@ const MECHANISM_COMPONENT_HOLDER = {
5272

5373
this.setOutput(false);
5474
this.setStyle(MRC_STYLE_MECHANISMS);
75+
ChangeFramework.registerCallback(MRC_COMPONENT_NAME, [Blockly.Events.BLOCK_MOVE, Blockly.Events.BLOCK_CHANGE], this.onBlockChanged);
76+
ChangeFramework.registerCallback(MRC_MECHANISM_NAME, [Blockly.Events.BLOCK_MOVE, Blockly.Events.BLOCK_CHANGE], this.onBlockChanged);
77+
5578
},
5679
saveExtraState: function (this: MechanismComponentHolderBlock): MechanismComponentHolderExtraState {
5780
const extraState: MechanismComponentHolderExtraState = {
@@ -72,25 +95,39 @@ const MECHANISM_COMPONENT_HOLDER = {
7295
* Update the block to reflect the newly loaded extra state.
7396
*/
7497
updateBlock_: function (this: MechanismComponentHolderBlock): void {
75-
if(this.mrcHideMechanisms){
76-
if(this.getInput('MECHANISMS')){
98+
if (this.mrcHideMechanisms) {
99+
if (this.getInput('MECHANISMS')) {
77100
this.removeInput('MECHANISMS')
78-
}
101+
}
79102
}
80-
else{
81-
if(this.getInput('MECHANISMS') == null){
103+
else {
104+
if (this.getInput('MECHANISMS') == null) {
82105
this.appendStatementInput('MECHANISMS').setCheck(MECHANISM_OUTPUT).appendField('Mechanisms');
83106
this.moveInputBefore('MECHANISMS', 'COMPONENTS')
84107
}
85108
}
86-
}
109+
},
110+
onBlockChanged: function (block: Blockly.BlockSvg, blockEvent: Blockly.Events.BlockBase) {
111+
if (blockEvent.type == Blockly.Events.BLOCK_MOVE) {
112+
let blockMoveEvent = blockEvent as Blockly.Events.BlockMove;
113+
if (blockMoveEvent.reason?.includes('connect')) {
114+
setName(block);
115+
}
116+
}
117+
else {
118+
if (blockEvent.type == Blockly.Events.BLOCK_CHANGE) {
119+
setName(block);
120+
}
121+
}
122+
},
123+
87124
}
88125

89126
export const setup = function () {
90127
Blockly.Blocks[BLOCK_NAME] = MECHANISM_COMPONENT_HOLDER;
91128
}
92129

93-
function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator){
130+
function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator) {
94131
let code = 'def define_hardware(self):\n' + generator.INDENT + 'self.hardware = []\n';
95132

96133
let mechanisms = '';
@@ -100,25 +137,25 @@ function pythonFromBlockInRobot(block: MechanismComponentHolderBlock, generator:
100137
components = generator.statementToCode(block, 'COMPONENTS');
101138

102139
const body = mechanisms + components;
103-
if(body != ''){
140+
if (body != '') {
104141
code += body;
105-
}else{
142+
} else {
106143
code += generator.INDENT + 'pass';
107144
}
108145
generator.addClassMethodDefinition('define_hardware', code);
109146
}
110147

111-
function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator){
148+
function pythonFromBlockInMechanism(block: MechanismComponentHolderBlock, generator: ExtendedPythonGenerator) {
112149
let components = '';
113150

114151
components = generator.statementToCode(block, 'COMPONENTS');
115152

116-
let code = 'def define_hardware(self' + generator.getListOfPorts(false) + '):\n' +
117-
generator.INDENT + 'self.hardware = []\n';
153+
let code = 'def define_hardware(self' + generator.getListOfPorts(false) + '):\n' +
154+
generator.INDENT + 'self.hardware = []\n';
118155

119-
if(components != ''){
156+
if (components != '') {
120157
code += components;
121-
}else{
158+
} else {
122159
code += generator.INDENT + 'pass';
123160
}
124161
generator.addClassMethodDefinition('define_hardware', code);
@@ -128,10 +165,10 @@ export const pythonFromBlock = function (
128165
block: MechanismComponentHolderBlock,
129166
generator: ExtendedPythonGenerator,
130167
) {
131-
if(block.getInput('MECHANISMS')){
168+
if (block.getInput('MECHANISMS')) {
132169
pythonFromBlockInRobot(block, generator);
133170
}
134-
else{
171+
else {
135172
pythonFromBlockInMechanism(block, generator);
136173
}
137174
return ''

0 commit comments

Comments
 (0)