Skip to content

Commit de4d667

Browse files
authored
Expandable Join Block (Block Side)
1 parent ddfdbd6 commit de4d667

File tree

5 files changed

+145
-142
lines changed

5 files changed

+145
-142
lines changed

blocks_vertical/live_tests.js

Lines changed: 0 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,6 @@ goog.require('Blockly.Blocks');
66
goog.require('Blockly.Colours');
77
goog.require('Blockly.ScratchBlocks.VerticalExtensions');
88

9-
Blockly.Blocks['sensing_set_of'] = {
10-
/**
11-
* Block to report properties of sprites.
12-
* @this Blockly.Block
13-
*/
14-
init: function() {
15-
this.jsonInit({
16-
"message0": 'set %1 of %2 to %3',
17-
"args0": [
18-
{
19-
"type": "field_dropdown",
20-
"name": "PROPERTY",
21-
"options": [
22-
[Blockly.Msg.SENSING_OF_XPOSITION, 'x position'],
23-
[Blockly.Msg.SENSING_OF_YPOSITION, 'y position'],
24-
[Blockly.Msg.SENSING_OF_DIRECTION, 'direction'],
25-
[Blockly.Msg.SENSING_OF_COSTUMENUMBER, 'costume #'],
26-
[Blockly.Msg.SENSING_OF_COSTUMENAME, 'costume name'],
27-
[Blockly.Msg.SENSING_OF_SIZE, 'size'],
28-
[Blockly.Msg.SENSING_OF_VOLUME, 'volume'],
29-
[Blockly.Msg.SENSING_OF_BACKDROPNUMBER, 'backdrop #'],
30-
[Blockly.Msg.SENSING_OF_BACKDROPNAME, 'backdrop name']
31-
]
32-
},
33-
{
34-
"type": "input_value",
35-
"name": "OBJECT"
36-
},
37-
{
38-
"type": "input_value",
39-
"name": "VALUE"
40-
}
41-
],
42-
"category": Blockly.Categories.sensing,
43-
"extensions": ["colours_sensing", "shape_statement"]
44-
});
45-
}
46-
};
47-
489
Blockly.Blocks['looks_setVertTransform'] = {
4910
/**
5011
* Block to report properties of sprites.
@@ -320,101 +281,3 @@ Blockly.Blocks['control_dualblock'] = {
320281
});
321282
}
322283
};
323-
324-
Blockly.Blocks['operators_expandablejoininputs'] = {
325-
/*
326-
default block texts
327-
*/
328-
messageList: ["apple", "banana", "pear", "orange", "mango", "strawberry", "pineapple", "grape", "kiwi"],
329-
330-
/**
331-
* @this Blockly.Block
332-
*/
333-
init: function () {
334-
this.jsonInit({
335-
"message0": 'join %1 %2',
336-
"args0": [
337-
{
338-
"type": "field_expandable_remove",
339-
"name": "REMOVE"
340-
},
341-
{
342-
"type": "field_expandable_add",
343-
"name": "ADD"
344-
}
345-
],
346-
"category": Blockly.Categories.operators,
347-
"extensions": ["colours_operators", "output_string"]
348-
});
349-
350-
this.inputs_ = 2;
351-
if (this.isInFlyout) {
352-
for (let i = 0; i < this.inputs_; i++) {
353-
const input = this.appendValueInput(`INPUT${i + 1}`);
354-
this.fillInBlock(input.connection, i);
355-
}
356-
}
357-
},
358-
359-
fillInBlock: function (connection, index) {
360-
if (connection.sourceBlock_.isInsertionMarker_) return;
361-
const block = this.workspace.newBlock('text');
362-
const text = this.messageList[index];
363-
block.setFieldValue(text ? text : "...", "TEXT");
364-
block.setShadow(true);
365-
block.initSvg();
366-
block.render(true);
367-
block.outputConnection.connect(connection);
368-
},
369-
370-
mutationToDom: function () {
371-
// on save
372-
const container = document.createElement("mutation");
373-
let number = Number(this.inputs_);
374-
if (isNaN(number)) number = 1;
375-
container.setAttribute("inputcount", String(number));
376-
return container;
377-
},
378-
379-
domToMutation: function (xmlElement) {
380-
// on load
381-
const inputCount = Number(xmlElement.getAttribute("inputcount"));
382-
this.inputs_ = isNaN(inputCount) ? 0 : inputCount;
383-
for (let i = 0; i < this.inputs_; i++) {
384-
const input = this.appendValueInput(`INPUT${i + 1}`);
385-
this.fillInBlock(input.connection, i);
386-
}
387-
queueMicrotask(() => {
388-
const connections = this.getConnections_();
389-
for (let i = 1; i < connections.length; i++) {
390-
const block = connections[i].targetBlock();
391-
if (!block) continue;
392-
if (!block.category_ && !block.isShadow()) block.dispose();
393-
}
394-
});
395-
},
396-
397-
onExpandableButtonClicked_: function (isAdding) {
398-
// Create an event group to keep field value and mutator in sync
399-
// Return null at the end because setValue is called here already.
400-
Blockly.Events.setGroup(true);
401-
var oldMutation = Blockly.Xml.domToText(this.mutationToDom());
402-
if (isAdding) {
403-
this.inputs_++;
404-
const number = this.inputs_;
405-
const newInput = this.appendValueInput(`INPUT${number}`);
406-
this.fillInBlock(newInput.connection, number - 1);
407-
} else if (this.inputs_ > 1) {
408-
const number = this.inputs_;
409-
this.removeInput(`INPUT${number}`);
410-
this.inputs_--;
411-
}
412-
this.initSvg();
413-
if (this.rendered) this.render();
414-
415-
var newMutation = Blockly.Xml.domToText(this.mutationToDom());
416-
Blockly.Events.fire(new Blockly.Events.BlockChange(this,
417-
'mutation', null, oldMutation, newMutation));
418-
Blockly.Events.setGroup(false);
419-
}
420-
};

blocks_vertical/operators.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,102 @@ Blockly.Blocks['operator_join3'] = {
518518
}
519519
};
520520

521+
522+
Blockly.Blocks['operators_expandablejoininputs'] = {
523+
/**
524+
* pm: Block for joining strings together (determined by user)
525+
* @this Blockly.Block
526+
*/
527+
init: function () {
528+
this.jsonInit({
529+
"message0": 'join %1 %2',
530+
"args0": [
531+
{
532+
"type": "field_expandable_remove",
533+
"name": "REMOVE"
534+
},
535+
{
536+
"type": "field_expandable_add",
537+
"name": "ADD"
538+
}
539+
],
540+
"category": Blockly.Categories.operators,
541+
"extensions": ["colours_operators", "output_string"]
542+
});
543+
544+
this.messageList = ["apple", "banana", "pear", "orange", "mango", "strawberry", "pineapple", "grape", "kiwi"];
545+
this.inputs_ = 2;
546+
if (this.isInFlyout) {
547+
for (let i = 0; i < this.inputs_; i++) {
548+
const input = this.appendValueInput(`INPUT${i + 1}`);
549+
this.fillInBlock(input.connection, i);
550+
}
551+
}
552+
},
553+
554+
fillInBlock: function (connection, index) {
555+
if (connection.sourceBlock_.isInsertionMarker_) return;
556+
const block = this.workspace.newBlock('text');
557+
const text = this.messageList[index];
558+
block.setFieldValue(text ? text : "...", "TEXT");
559+
block.setShadow(true);
560+
block.initSvg();
561+
block.render(true);
562+
block.outputConnection.connect(connection);
563+
},
564+
565+
mutationToDom: function () {
566+
// on save
567+
const container = document.createElement("mutation");
568+
let number = Number(this.inputs_);
569+
if (isNaN(number)) number = 1;
570+
container.setAttribute("inputcount", String(number));
571+
return container;
572+
},
573+
574+
domToMutation: function (xmlElement) {
575+
// on load
576+
const inputCount = Number(xmlElement.getAttribute("inputcount"));
577+
this.inputs_ = isNaN(inputCount) ? 0 : inputCount;
578+
for (let i = 0; i < this.inputs_; i++) {
579+
const input = this.appendValueInput(`INPUT${i + 1}`);
580+
this.fillInBlock(input.connection, i);
581+
}
582+
queueMicrotask(() => {
583+
const connections = this.getConnections_();
584+
for (let i = 1; i < connections.length; i++) {
585+
const block = connections[i].targetBlock();
586+
if (!block) continue;
587+
if (!block.category_ && !block.isShadow()) block.dispose();
588+
}
589+
});
590+
},
591+
592+
onExpandableButtonClicked_: function (isAdding) {
593+
// Create an event group to keep field value and mutator in sync
594+
// Return null at the end because setValue is called here already.
595+
Blockly.Events.setGroup(true);
596+
var oldMutation = Blockly.Xml.domToText(this.mutationToDom());
597+
if (isAdding) {
598+
this.inputs_++;
599+
const number = this.inputs_;
600+
const newInput = this.appendValueInput(`INPUT${number}`);
601+
this.fillInBlock(newInput.connection, number - 1);
602+
} else if (this.inputs_ > 1) {
603+
const number = this.inputs_;
604+
this.removeInput(`INPUT${number}`);
605+
this.inputs_--;
606+
}
607+
this.initSvg();
608+
if (this.rendered) this.render();
609+
610+
var newMutation = Blockly.Xml.domToText(this.mutationToDom());
611+
Blockly.Events.fire(new Blockly.Events.BlockChange(this,
612+
'mutation', null, oldMutation, newMutation));
613+
Blockly.Events.setGroup(false);
614+
}
615+
};
616+
521617
Blockly.Blocks['operator_letter_of'] = {
522618
/**
523619
* Block for "letter _ of _" operator.

blocks_vertical/sensing.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,45 @@ Blockly.Blocks['sensing_of'] = {
795795
}
796796
};
797797

798+
Blockly.Blocks['sensing_set_of'] = {
799+
/**
800+
* Block to set properties of sprites.
801+
* @this Blockly.Block
802+
*/
803+
init: function() {
804+
this.jsonInit({
805+
"message0": 'set %1 of %2 to %3',
806+
"args0": [
807+
{
808+
"type": "field_dropdown",
809+
"name": "PROPERTY",
810+
"options": [
811+
[Blockly.Msg.SENSING_OF_XPOSITION, 'x position'],
812+
[Blockly.Msg.SENSING_OF_YPOSITION, 'y position'],
813+
[Blockly.Msg.SENSING_OF_DIRECTION, 'direction'],
814+
[Blockly.Msg.SENSING_OF_COSTUMENUMBER, 'costume #'],
815+
[Blockly.Msg.SENSING_OF_COSTUMENAME, 'costume name'],
816+
[Blockly.Msg.SENSING_OF_SIZE, 'size'],
817+
[Blockly.Msg.SENSING_OF_VOLUME, 'volume'],
818+
[Blockly.Msg.SENSING_OF_BACKDROPNUMBER, 'backdrop #'],
819+
[Blockly.Msg.SENSING_OF_BACKDROPNAME, 'backdrop name']
820+
]
821+
},
822+
{
823+
"type": "input_value",
824+
"name": "OBJECT"
825+
},
826+
{
827+
"type": "input_value",
828+
"name": "VALUE"
829+
}
830+
],
831+
"category": Blockly.Categories.sensing,
832+
"extensions": ["colours_sensing", "shape_statement"]
833+
});
834+
}
835+
};
836+
798837
Blockly.Blocks['sensing_current'] = {
799838
/**
800839
* Block to Report the current option.

core/field_expandable_add.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Blockly.FieldExpandableAdd.prototype.init = function() {
7272
}
7373
Blockly.FieldExpandableAdd.superClass_.init.call(this);
7474

75+
this.size_.width = 32;
76+
this.overrideSep = 1;
7577
this.boxGroup_ = Blockly.utils.createSvgElement('g', {}, null);
7678
this.box_ = Blockly.utils.createSvgElement('rect',
7779
{
@@ -89,10 +91,11 @@ Blockly.FieldExpandableAdd.prototype.init = function() {
8991
);
9092
this.icon_ = Blockly.utils.createSvgElement('image',
9193
{
92-
'x': 2.5,
94+
'x': 5,
9395
'y': 5,
94-
'width': this.size_.height / 1.5,
96+
'width': this.size_.width / 1.5,
9597
'height': this.size_.height / 1.5,
98+
'xlink:href': plusIcon,
9699
'href': plusIcon,
97100
},
98101
this.boxGroup_

core/field_expandable_remove.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ Blockly.FieldExpandableRemove.prototype.init = function() {
7272
}
7373
Blockly.FieldExpandableRemove.superClass_.init.call(this);
7474

75+
this.size_.width = 32;
76+
this.overrideSep = 1;
7577
this.boxGroup_ = Blockly.utils.createSvgElement('g', {}, null);
7678
this.box_ = Blockly.utils.createSvgElement('rect',
7779
{
@@ -89,18 +91,18 @@ Blockly.FieldExpandableRemove.prototype.init = function() {
8991
);
9092
this.icon_ = Blockly.utils.createSvgElement('image',
9193
{
92-
'x': 2.5,
94+
'x': 5,
9395
'y': 5,
94-
'width': this.size_.height / 1.5,
96+
'width': this.size_.width / 1.5,
9597
'height': this.size_.height / 1.5,
98+
'xlink:href': removeIcon,
9699
'href': removeIcon,
97100
},
98101
this.boxGroup_
99102
);
100103
this.fieldGroup_.insertBefore(this.boxGroup_, this.textElement_);
101104
};
102105

103-
104106
/**
105107
* Returns an empty string.
106108
* @return {string}

0 commit comments

Comments
 (0)