GIRA / PhysicalBits

A web-based programming environment for educational robotics that supports live coding and autonomy using a hybrid blocks/text programming language.
https://gira.github.io/PhysicalBits/
MIT License
18 stars 5 forks source link

tried changing block 'number-random-int' to allow re-ording of fields #8

Closed kristiank closed 4 years ago

kristiank commented 4 years ago

This does not work yet, don't merge it!

My aim is to allow translations reorder the input fields, but this is not working yet. I think the definiton of the input fields (on line 839) creates the input fields before the code goes through the msg.

RichoM commented 4 years ago

This does not work yet, don't merge it!

:)

My aim is to allow translations reorder the input fields, but this is not working yet. I think the definiton of the input fields (on line 839) creates the input fields before the code goes through the msg.

You're right. A possible solution is to wrap the input definitions in functions and apply them at the right time. The following code seems to work as expected:

    Blockly.Blocks['number_random_int'] = {
      init: function() {
        let msg = i18n.translate("random integer from %0 to %1");
        let parts = msg.split(" ").map(trim);
        let inputFields = [
          () => this.appendValueInput("from")
                    .setCheck("Number")
                    .setAlign(Blockly.ALIGN_RIGHT),
          () => this.appendValueInput("to")
                    .setCheck("Number")
                    .setAlign(Blockly.ALIGN_RIGHT)
        ];
        let isInputField = /^%\d+$/; // for testing %1, %2 etc
        for (let i = 0; i < parts.length; i++) {
          let part = parts[i];
          if (isInputField.test(part)) { // if translation part is an input field
            let fieldNumber = Number(part.substring(1)); // convert to int
            let inputField = inputFields[fieldNumber](); // get correct input field
            inputField.appendField(); // not sure?
          } else {
            this.appendDummyInput().appendField(part); // not sure
          }
        }

        this.setInputsInline(true);
        this.setOutput(true, "Number");
        this.setColour(230);
        this.setTooltip("");
        this.setHelpUrl("");
      }
    };
kristiank commented 4 years ago

I'm sorry, I didn't see you comment until after I updated the code. Your version is nicer and seems more Smalltalkish. I'll experiment some more. Or do you have any objections for adding this kind of feature?

RichoM commented 4 years ago

No, this is great. I really appreciate it.

BTW, I finished adding the procedure and function blocks (and I made a few changes along the way) so I would suggest you to update your branch soon in order to avoid a painful merge later.

kristiank commented 4 years ago

Cool. I will fetch your commits. I'm currently working out the details to get all consequent text together in one block field. Also this will be needed for blocks that have "multiple" lines (like the 'for' and 'while' blocks). Will report progress tomorrow.

kristiank commented 4 years ago

I started a new pull request in #9 after fetching and merging your commits. I'll close this pull request here.