How to enable blocks in the mutator

126 views
Skip to first unread message

fu6...@gmail.com

unread,
Nov 23, 2021, 11:56:24 AM11/23/21
to Blockly
Hi, 

The blocks will be disabled if not to connect to the "mutation_container" block on top in the mutator.
I want to remove the "mutation_container" block.
How to modify it?
Thank you.


Blockly.Blocks["mutation_container"] = {
  init: function() {
    this.appendDummyInput()   
.appendField("Hello World");   
    this.setNextStatement(true, null);
  }
};

Blockly.Blocks["mutation_test"] = {
  init: function() {
    this.appendDummyInput()
        .appendField(new Blockly.FieldLabelSerializable(""), "code");
this.getField("code").setVisible(false);
    this.appendDummyInput()
        .appendField(new Blockly.FieldLabelSerializable(""), "xml");
this.getField("xml").setVisible(false);
    this.setInputsInline(true);
    this.setPreviousStatement(true, null);
    this.setNextStatement(true, null);
    this.setColour(100);
    this.updateShape_();    
    this.setMutator(new Blockly.Mutator1(["controls_if","logic_compare","math_number"]));
  },
  mutationToDom: function (workspace) {
  },
  domToMutation: function (xmlElement) {
  },
  decompose: function (workspace) {
  },
  compose: function(containerBlock) {
  },
  saveConnections: function(containerBlock) {
  },
  updateShape_: function() {
  }
};
mutation.mp4

Beka Westberg

unread,
Nov 23, 2021, 3:43:32 PM11/23/21
to blo...@googlegroups.com
Hello,

As far as I know it's not supported to have a mutator without some kind of top block :/ The compose and decompose methods explicitly expect a top block.

Sorry I can't be more helpful :/ Someone else here might have a different suggestion.

Best wishes,
Beka

--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/blockly/73f009cb-140a-483c-a0be-0412e24c711cn%40googlegroups.com.

fu6...@gmail.com

unread,
Nov 24, 2021, 6:50:49 AM11/24/21
to Blockly
Hi, Beka. 

I did it.  Thank you for your encouragement.

Share my source code.
test_myMutation.mp4

fu6...@gmail.com

unread,
Jan 1, 2022, 12:21:30 PM1/1/22
to Blockly
 I wrote a useful sample code.

myMutator.PNG

Blockly.Blocks.procedures_callnoreturn={
        init:function(){
                etc...
                this.setMutator(new Blockly.myMutator([]));       
                etc...
        },
        myWorkspaceInitial: function(myWorkspace) {
                var xml = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace());
                xml = new XMLSerializer().serializeToString(xml);
                xml = new DOMParser().parseFromString(xml,"text/xml").firstChild.childNodes;
                for (var i=0;i<xml.length;i++) {
                        if (xml[i].getAttribute("type")=="procedures_defnoreturn") {
                                if (xml[i].childNodes[0].textContent==this.getFieldValue("NAME")) {
                                        if (xml[i].childNodes[2]) {
                                                if (xml[i].childNodes[2].childNodes[0]) {
                                                        xml = Blockly.Xml.domToText(xml[i].childNodes[2].childNodes[0], true);
                                                        xml = Blockly.Xml.textToDom('<xml xmlns="https://developers.google.com/blockly/xml">'+xml+'</xml>');
                                                        myWorkspace.clear();
                                                        Blockly.Xml.domToWorkspace(xml, myWorkspace);                                                        
                                                        break;
                                                }
                                        }
                                }
                        }
                }
        },
        myWorkspaceChanged: function(myWorkspace) {}

        etc...
};

Blockly.Blocks.procedures_callreturn={
        init:function(){
                etc...
                this.setMutator(new Blockly.myMutator([]));       
                etc...
        },
        myWorkspaceInitial: function(myWorkspace) {
                var xml = Blockly.Xml.workspaceToDom(Blockly.getMainWorkspace());
                xml = new XMLSerializer().serializeToString(xml);
                xml = new DOMParser().parseFromString(xml,"text/xml").firstChild.childNodes;
                for (var i=0;i<xml.length;i++) {
                        if (xml[i].getAttribute("type")=="procedures_defreturn") {
                                if (xml[i].childNodes[1].textContent==this.getFieldValue("NAME")) {
                                        console.log(xml[i].childNodes[3]);
                                        if (xml[i].childNodes[3]) {
                                                if (xml[i].childNodes[3].childNodes[0]) {
                                                        xml = Blockly.Xml.domToText(xml[i].childNodes[3].childNodes[0], true);
                                                        xml = Blockly.Xml.textToDom('<xml xmlns="https://developers.google.com/blockly/xml">'+xml+'</xml>');
                                                        myWorkspace.clear();
                                                        Blockly.Xml.domToWorkspace(xml, myWorkspace);                                                        
                                                        break;
                                                }
                                        }
                                }
                        }
                }
        },myWorkspaceChanged: function(myWorkspace) {} 
       
        etc...
};
myMutator.mp4

fu6...@gmail.com

unread,
Jan 2, 2022, 10:35:38 AM1/2/22
to Blockly
myMutationStatement.mp4
Reply all
Reply to author
Forward
0 new messages