Re: Saving values when using a mutator

131 views
Skip to first unread message
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Coda Highland

unread,
Mar 25, 2020, 9:47:13 AM3/25/20
to blo...@googlegroups.com
No, that's not quite true. It's also used when restoring a saved workspace. You almost certainly want to have that function even if it doesn't solve your immediate problem.

/s/ Adam

On Wed, Mar 25, 2020 at 8:45 AM Yasser Benfoughal <star...@gmail.com> wrote:
So it seems that domToMutation executes ONLY when constructing the block from XML aka dragging from the toolbox, which is not the goal of my block. so I removed domToMutation and kept mutationToDom to save the attributes then passed those attributes through another variable to a custom function, which I called at the end of the compose function. It successfully kept values when I use the mutator to add new fields. Though when you refresh the browser everything is lost.
Not very important for now but If you know how to do that you'll save me a lot of time
Here's the new block

var global_filters = 1;
let filtersXML;
Blockly.Blocks['filters'] = {
    init: function () {
        this.filtersCount_ = 1;
        this.appendDummyInput()
            .setAlign(Blockly.ALIGN_CENTER)
            .appendField("Filters");
        this.appendDummyInput('condition')
            .appendField(new Blockly.FieldDropdown(minecraft.all_any), "conditionDrop");
        this.appendDummyInput('filter0')
            .setAlign(Blockly.ALIGN_LEFT)
            .appendField("Test")
            .appendField(new Blockly.FieldDropdown(minecraft.filters), "test0")
            .appendField("Operator")
            .appendField(new Blockly.FieldDropdown(minecraft.operator), "operator0")
            .appendField("Subject")
            .appendField(new Blockly.FieldDropdown(minecraft.subject), "subject0")
            .appendField("Value")
            .appendField(new Blockly.FieldNumber(00), "value0");
        this.setMutator(new Blockly.Mutator(['filtersItems']));
        this.setPreviousStatement(truenull);
        this.setNextStatement(truenull);
        this.setColour(230);
        this.setTooltip("");
        this.setHelpUrl("");
    },
    mutationToDom: function () {
        var container = Blockly.utils.xml.createElement('filters_mutation');
        container.setAttribute('filterCount'this.filtersCount_);
        for (var i = 0i < this.filtersCount_i++) {
            var conditionDrop = this.getFieldValue('conditionDrop');
            var test = this.getFieldValue('test' + i);
            var operator = this.getFieldValue('operator' + i);
            var subject = this.getFieldValue('subject' + i);
            var value = this.getFieldValue('value' + i);
            container.setAttribute('conditionDrop'conditionDrop);
            container.setAttribute('test' + itest);
            container.setAttribute('operator' + ioperator);
            container.setAttribute('subject' + isubject);
            container.setAttribute('value' + ivalue);
        }
        filtersXML = container;
        return container;
    },
    decompose: function (workspace) {
        var containerBlock = workspace.newBlock('filtersContainer');
        containerBlock.initSvg();
        var connection = containerBlock.getInput('filtersList').connection;
        for (var x = 0x < this.filtersCount_x++) {
            var itemBlock = workspace.newBlock('filtersItems');
            itemBlock.initSvg();
            connection.connect(itemBlock.previousConnection);
            connection = itemBlock.nextConnection;
        }
        return containerBlock;
    },
    compose: function (containerBlock) {
        if (this.filtersCount_ == 0) {
            this.removeInput('filter0');
        } else {
            for (var x = this.filtersCount_ - 1x >= 0x--) {
                this.removeInput('filter' + x);
            }
        }
        this.filtersCount_ = 0;
        var ItemBlock = containerBlock.getInputTargetBlock('filtersList');
        while (ItemBlock) {
            var input = this.appendDummyInput('filter' + this.filtersCount_)
            input.setAlign(Blockly.ALIGN_LEFT)
                .appendField("Test")
                .appendField(new Blockly.FieldDropdown(minecraft.filters), "test" + this.filtersCount_)
                .appendField("Operator")
                .appendField(new Blockly.FieldDropdown(minecraft.operator), "operator" + this.filtersCount_)
                .appendField("Subject")
                .appendField(new Blockly.FieldDropdown(minecraft.subject), "subject" + this.filtersCount_)
                .appendField("Value")
                .appendField(new Blockly.FieldNumber(00), "value" + this.filtersCount_);
            if (ItemBlock.valueConnection_) {
                input.connection.connect(ItemBlock.valueConnection_);
            }
            this.filtersCount_++;
            global_filters = this.filtersCount_;
            ItemBlock = ItemBlock.nextConnection &&
                ItemBlock.nextConnection.targetBlock();
        }
        if (this.filtersCount_ == 0) {
            this.appendDummyInput('filter0')
                .setAlign(Blockly.ALIGN_LEFT)
                .appendField("Test")
                .appendField(new Blockly.FieldDropdown(minecraft.filters), "test0")
                .appendField("Operator")
                .appendField(new Blockly.FieldDropdown(minecraft.operator), "operator0")
                .appendField("Subject")
                .appendField(new Blockly.FieldDropdown(minecraft.subject), "subject0")
                .appendField("Value")
                .appendField(new Blockly.FieldNumber(00), "value0");
        }
        this.replace(filtersXML)
    },
    replace: function (container) {
        for (var i = 0i < this.filtersCount_i++) {
            var conditionDrop = container.getAttribute('conditionDrop');
            var test = container.getAttribute('test' + i);
            var operator = container.getAttribute('operator' + i);
            var subject = container.getAttribute('subject' + i);
            var value = container.getAttribute('value' + i);
            this.setFieldValue(conditionDrop'conditionDrop');
            this.setFieldValue(test'test' + i);
            this.setFieldValue(operator'operator' + i);
            this.setFieldValue(subject'subject' + i);
            this.setFieldValue(value'value' + i);
        }
    }
};

--
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/c1265e21-9055-40af-b9e2-57f89beb9327%40googlegroups.com.
Message has been deleted

Coda Highland

unread,
Mar 25, 2020, 11:02:08 AM3/25/20
to blo...@googlegroups.com
Yes, as I said, it doesn't solve your immediate problem.

As its name implies, the purpose of the function is to read a mutation from an XML DOM and apply it to the block. This is used when constructing the toolbox, as you have discovered already, but it's also used when loading a workspace from saved XML using domToWorkspace. (You can save the current contents of the workspace using workspaceToDom.) Importantly, Blockly will run this function during the creation of the block, before loading the attached values.

If the block has already been created and you're mutating it interactively, then Blockly doesn't require you to use any specific method for preserving the values. Rather, since Blockly assumes that the mutator can change anything at all about a block, it's up to you as the developer to keep track of what you need to know. You can do this in whatever way you see fit.

It's true that mutators are probably the hardest part of integrating Blockly. The documentation tries its best, but since it's so flexible and everyone is going to want something different out of it, there's no one best solution that can be written there.

/s/ Adam

On Wed, Mar 25, 2020 at 9:40 AM Yasser Benfoughal <star...@gmail.com> wrote:
I am confused, domToMutation didn't seem to run at all while editing the block with the mutator.
This currently solved my issue but if there's a proper way to use it I'd like to know. Is there any example with a mutator that can restore values? I'd like to check it out because the documentations aren't quite clear

--
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.
Message has been deleted

Coda Highland

unread,
Mar 25, 2020, 1:54:07 PM3/25/20
to blo...@googlegroups.com
Happy to help! Glad you were able to get things working.

/s/ Adam

On Wed, Mar 25, 2020 at 10:50 AM Yasser Benfoughal <star...@gmail.com> wrote:
Understood, makes more sense now. I've been digging the docs looking for a possible function that might solve my issue then I decided to do it myself. Anyways you're right I have plans to use workspaceToDom domToWorkspace in the future so I'll be needing domToMutation
Thanks for explaining it, really appreciated

--
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.
Reply all
Reply to author
Forward
0 new messages