Example #1
0
        /**
         * Block for prompt function (internal message).
         * The "text_prompt_ext" block is preferred as it is more flexible.
         * @this Blockly.Block
         */
        public override void init()
        {
            var TYPES = new JsArray <DropdownItemInfo> {
                new DropdownItemInfo(Msg.TEXT_PROMPT_TYPE_TEXT, "TEXT"),
                new DropdownItemInfo(Msg.TEXT_PROMPT_TYPE_NUMBER, "NUMBER")
            };
            // Assign "this" to a variable for use in the closures below.
            var thisBlock = this;

            this.setHelpUrl(Msg.TEXT_PROMPT_HELPURL);
            this.setColour(Texts.HUE);
            var dropdown = new FieldDropdown(TYPES, (field, newOp) => {
                thisBlock.updateType_(newOp);
                return(Script.Undefined);
            });

            this.appendDummyInput()
            .appendField(dropdown, "TYPE")
            .appendField(this.newQuote_(true))
            .appendField(new FieldTextInput(""), "TEXT")
            .appendField(this.newQuote_(false));
            this.setOutput(true, "String");
            this.setTooltip(new Func <string>(() => {
                return((thisBlock.getFieldValue("TYPE") == "TEXT") ?
                       Msg.TEXT_PROMPT_TOOLTIP_TEXT :
                       Msg.TEXT_PROMPT_TOOLTIP_NUMBER);
            }));
        }
Example #2
0
        /**
         * Block for checking if a number is even, odd, prime, whole, positive,
         * negative or if it is divisible by certain number.
         * @this Blockly.Block
         */
        public override void init()
        {
            var PROPERTIES = new JsArray <DropdownItemInfo> {
                new DropdownItemInfo(Msg.MATH_IS_EVEN, "EVEN"),
                new DropdownItemInfo(Msg.MATH_IS_ODD, "ODD"),
                new DropdownItemInfo(Msg.MATH_IS_PRIME, "PRIME"),
                new DropdownItemInfo(Msg.MATH_IS_WHOLE, "WHOLE"),
                new DropdownItemInfo(Msg.MATH_IS_POSITIVE, "POSITIVE"),
                new DropdownItemInfo(Msg.MATH_IS_NEGATIVE, "NEGATIVE"),
                new DropdownItemInfo(Msg.MATH_IS_DIVISIBLE_BY, "DIVISIBLE_BY")
            };

            this.setColour(Math.HUE);
            this.appendValueInput("NUMBER_TO_CHECK")
            .setCheck("Number");
            var dropdown = new FieldDropdown(PROPERTIES, (field, option) => {
                var divisorInput = (option == "DIVISIBLE_BY");
                this.updateShape_(divisorInput);
                return(Script.Undefined);
            });

            this.appendDummyInput()
            .appendField(dropdown, "PROPERTY");
            this.setInputsInline(true);
            this.setOutput(true, "Boolean");
            this.setTooltip(Msg.MATH_IS_TOOLTIP);
        }
Example #3
0
        /**
         * Create or delete an input for a numeric index.
         * This block has two such inputs, independant of each other.
         * @param {number} n Specify first or second input (1 or 2).
         * @param {boolean} isAt True if the input should exist.
         * @private
         * @this Blockly.Block
         */
        public void updateAt_(int n, bool isAt)
        {
            // Create or delete an input for the numeric index.
            // Destroy old "AT" and "ORDINAL" inputs.
            this.removeInput("AT" + n);
            this.removeInput("ORDINAL" + n, true);
            // Create either a value "AT" input or a dummy input.
            if (isAt)
            {
                this.appendValueInput("AT" + n).setCheck("Number");
                if (!String.IsNullOrEmpty(Msg.ORDINAL_NUMBER_SUFFIX))
                {
                    this.appendDummyInput("ORDINAL" + n)
                    .appendField(Msg.ORDINAL_NUMBER_SUFFIX);
                }
            }
            else
            {
                this.appendDummyInput("AT" + n);
            }
            var menu = new FieldDropdown(WHERE_OPTIONS[n - 1], (field, value) => {
                var newAt = (value == "FROM_START") || (value == "FROM_END");
                // The "isAt" variable is available due to this function being a
                // closure.
                if (newAt != isAt)
                {
                    this.updateAt_(n, newAt);
                    // This menu has been destroyed and replaced.
                    // Update the replacement.
                    this.setFieldValue(value, "WHERE" + n);
                    return(null);
                }
                return(Script.Undefined);
            });

            this.getInput("AT" + n)
            .appendField(menu, "WHERE" + n);
            if (n == 1)
            {
                this.moveInputBefore("AT1", "AT2");
                if (this.getInput("ORDINAL1") != null)
                {
                    this.moveInputBefore("ORDINAL1", "AT2");
                }
            }
            if (!String.IsNullOrEmpty(Msg.LISTS_GET_SUBLIST_TAIL))
            {
                this.moveInputBefore("TAIL", null);
            }
        }
Example #4
0
        /**
         * Block for evaluating a list of numbers to return sum, average, min, max,
         * etc.  Some functions also work on text (min, max, mode, median).
         * @this Blockly.Block
         */
        public override void init()
        {
            var OPERATORS = new JsArray <DropdownItemInfo> {
                new DropdownItemInfo(Msg.MATH_ONLIST_OPERATOR_SUM, "SUM"),
                new DropdownItemInfo(Msg.MATH_ONLIST_OPERATOR_MIN, "MIN"),
                new DropdownItemInfo(Msg.MATH_ONLIST_OPERATOR_MAX, "MAX"),
                new DropdownItemInfo(Msg.MATH_ONLIST_OPERATOR_AVERAGE, "AVERAGE"),
                new DropdownItemInfo(Msg.MATH_ONLIST_OPERATOR_MEDIAN, "MEDIAN"),
                new DropdownItemInfo(Msg.MATH_ONLIST_OPERATOR_MODE, "MODE"),
                new DropdownItemInfo(Msg.MATH_ONLIST_OPERATOR_STD_DEV, "STD_DEV"),
                new DropdownItemInfo(Msg.MATH_ONLIST_OPERATOR_RANDOM, "RANDOM")
            };
            // Assign "this" to a variable for use in the closures below.
            var thisBlock = this;

            this.setHelpUrl(Msg.MATH_ONLIST_HELPURL);
            this.setColour(Math.HUE);
            this.setOutput(true, "Number");
            var dropdown = new FieldDropdown(OPERATORS, (field, newOp) => {
                thisBlock.updateType_(newOp);
                return(Script.Undefined);
            });

            this.appendValueInput("LIST")
            .setCheck("Array")
            .appendField(dropdown, "OP");
            this.setTooltip(new Func <string>(() => {
                switch (thisBlock.getFieldValue("OP"))
                {
                case "SUM": return(Msg.MATH_ONLIST_TOOLTIP_SUM);

                case "MIN": return(Msg.MATH_ONLIST_TOOLTIP_MIN);

                case "MAX": return(Msg.MATH_ONLIST_TOOLTIP_MAX);

                case "AVERAGE": return(Msg.MATH_ONLIST_TOOLTIP_AVERAGE);

                case "MEDIAN": return(Msg.MATH_ONLIST_TOOLTIP_MEDIAN);

                case "MODE": return(Msg.MATH_ONLIST_TOOLTIP_MODE);

                case "STD_DEV": return(Msg.MATH_ONLIST_TOOLTIP_STD_DEV);

                case "RANDOM": return(Msg.MATH_ONLIST_TOOLTIP_RANDOM);
                }
                return("");
            }));
        }
Example #5
0
        public override void init()
        {
            this.setColour(120);
            this.appendDummyInput()
            .appendField("name")
            .appendField(new Blockly.FieldTextInput("block_type"), "NAME");
            this.appendStatementInput("INPUTS")
            .setCheck("Input")
            .appendField("inputs");
            var dropdown = new Blockly.FieldDropdown(new JsArray <Blockly.DropdownItemInfo>()
            {
                new Blockly.DropdownItemInfo("automatic inputs", "AUTO"),
                new Blockly.DropdownItemInfo("external inputs", "EXT"),
                new Blockly.DropdownItemInfo("inline inputs", "INT")
            });

            this.appendDummyInput()
            .appendField(dropdown, "INLINE");
            dropdown = new Blockly.FieldDropdown(new JsArray <Blockly.DropdownItemInfo>()
            {
                new Blockly.DropdownItemInfo("no connections", "NONE"),
                new Blockly.DropdownItemInfo("← left output", "LEFT"),
                new Blockly.DropdownItemInfo("↕ top+bottom connections", "BOTH"),
                new Blockly.DropdownItemInfo("↑ top connection", "TOP"),
                new Blockly.DropdownItemInfo("↓ bottom connection", "BOTTOM")
            },
                                                 (field, option) => {
                ((FactoryBase)field.sourceBlock_).updateShape_(option);
                // Connect a shadow block to this new input.
                ((FactoryBase)field.sourceBlock_).spawnOutputShadow_(option);
                return(Script.Undefined);
            });
            this.appendDummyInput()
            .appendField(dropdown, "CONNECTIONS");
            this.appendValueInput("COLOUR")
            .setCheck("Colour")
            .appendField("colour");
            this.setTooltip("Build a custom block by plugging\n" +
                            "fields, inputs and other blocks here.");
            this.setHelpUrl(
                "https://developers.google.com/blockly/guides/create-custom-blocks/block-factory");
        }
Example #6
0
        /**
         * Create or delete an input for the numeric index.
         * @param {boolean} isAt True if the input should exist.
         * @private
         * @this Blockly.Block
         */
        public void updateAt_(bool isAt)
        {
            // Destroy old "AT" and "ORDINAL" input.
            this.removeInput("AT");
            this.removeInput("ORDINAL", true);
            // Create either a value "AT" input or a dummy input.
            if (isAt)
            {
                this.appendValueInput("AT").setCheck("Number");
                if (!String.IsNullOrEmpty(Msg.ORDINAL_NUMBER_SUFFIX))
                {
                    this.appendDummyInput("ORDINAL")
                    .appendField(Msg.ORDINAL_NUMBER_SUFFIX);
                }
            }
            else
            {
                this.appendDummyInput("AT");
            }
            var menu = new FieldDropdown(this.WHERE_OPTIONS, (field, value) => {
                var newAt = (value == "FROM_START") || (value == "FROM_END");
                // The "isAt" variable is available due to this function being a closure.
                if (newAt != isAt)
                {
                    this.updateAt_(newAt);
                    // This menu has been destroyed and replaced.  Update the replacement.
                    this.setFieldValue(value, "WHERE");
                    return(null);
                }
                return(Script.Undefined);
            });

            this.moveInputBefore("AT", "TO");
            if (this.getInput("ORDINAL") != null)
            {
                this.moveInputBefore("ORDINAL", "TO");
            }

            this.getInput("AT").appendField(menu, "WHERE");
        }
Example #7
0
        /**
         * Block for splitting text into a list, or joining a list into text.
         * @this Blockly.Block
         */
        public override void init()
        {
            // Assign "this" to a variable for use in the closures below.
            var thisBlock = this;
            var dropdown  = new FieldDropdown(new JsArray <DropdownItemInfo> {
                new DropdownItemInfo(Msg.LISTS_SPLIT_LIST_FROM_TEXT, "SPLIT"),
                new DropdownItemInfo(Msg.LISTS_SPLIT_TEXT_FROM_LIST, "JOIN")
            },
                                              (field, newMode) => {
                thisBlock.updateType_(newMode);
                return(Script.Undefined);
            });

            this.setHelpUrl(Msg.LISTS_SPLIT_HELPURL);
            this.setColour(Lists.HUE);
            this.appendValueInput("INPUT")
            .setCheck("String")
            .appendField(dropdown, "MODE");
            this.appendValueInput("DELIM")
            .setCheck("String")
            .appendField(Msg.LISTS_SPLIT_WITH_DELIMITER);
            this.setInputsInline(true);
            this.setOutput(true, "Array");
            this.setTooltip(new Func <string>(() => {
                var mode = thisBlock.getFieldValue("MODE");
                if (mode == "SPLIT")
                {
                    return(Msg.LISTS_SPLIT_TOOLTIP_SPLIT);
                }
                else if (mode == "JOIN")
                {
                    return(Msg.LISTS_SPLIT_TOOLTIP_JOIN);
                }
                throw new Exception("Unknown mode: " + mode);
            }));
        }
Example #8
0
        /**
         * Block for getting element at index.
         * @this Blockly.Block
         */
        public override void init()
        {
            var MODE = new JsArray <DropdownItemInfo> {
                new DropdownItemInfo(Msg.LISTS_GET_INDEX_GET, "GET"),
                new DropdownItemInfo(Msg.LISTS_GET_INDEX_GET_REMOVE, "GET_REMOVE"),
                new DropdownItemInfo(Msg.LISTS_GET_INDEX_REMOVE, "REMOVE")
            };

            this.WHERE_OPTIONS = new JsArray <DropdownItemInfo> {
                new DropdownItemInfo(Msg.LISTS_GET_INDEX_FROM_START, "FROM_START"),
                new DropdownItemInfo(Msg.LISTS_GET_INDEX_FROM_END, "FROM_END"),
                new DropdownItemInfo(Msg.LISTS_GET_INDEX_FIRST, "FIRST"),
                new DropdownItemInfo(Msg.LISTS_GET_INDEX_LAST, "LAST"),
                new DropdownItemInfo(Msg.LISTS_GET_INDEX_RANDOM, "RANDOM")
            };
            this.setHelpUrl(Msg.LISTS_GET_INDEX_HELPURL);
            this.setColour(Lists.HUE);
            var modeMenu = new FieldDropdown(MODE, (field, value) => {
                var isStatement = (value == "REMOVE");
                this.updateStatement_(isStatement);
                return(Script.Undefined);
            });

            this.appendValueInput("VALUE")
            .setCheck("Array")
            .appendField(Msg.LISTS_GET_INDEX_INPUT_IN_LIST);
            this.appendDummyInput()
            .appendField(modeMenu, "MODE")
            .appendField("", "SPACE");
            this.appendDummyInput("AT");
            if (!String.IsNullOrEmpty(Msg.LISTS_GET_INDEX_TAIL))
            {
                this.appendDummyInput("TAIL")
                .appendField(Msg.LISTS_GET_INDEX_TAIL);
            }
            this.setInputsInline(true);
            this.setOutput(true);
            this.updateAt_(true);
            // Assign "this" to a variable for use in the tooltip closure below.
            var thisBlock = this;

            this.setTooltip(new Func <string>(() => {
                var mode    = thisBlock.getFieldValue("MODE");
                var where   = thisBlock.getFieldValue("WHERE");
                var tooltip = "";
                switch (mode + " " + where)
                {
                case "GET FROM_START":
                case "GET FROM_END":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_GET_FROM;
                    break;

                case "GET FIRST":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_GET_FIRST;
                    break;

                case "GET LAST":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_GET_LAST;
                    break;

                case "GET RANDOM":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_GET_RANDOM;
                    break;

                case "GET_REMOVE FROM_START":
                case "GET_REMOVE FROM_END":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_FROM;
                    break;

                case "GET_REMOVE FIRST":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_FIRST;
                    break;

                case "GET_REMOVE LAST":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_LAST;
                    break;

                case "GET_REMOVE RANDOM":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_GET_REMOVE_RANDOM;
                    break;

                case "REMOVE FROM_START":
                case "REMOVE FROM_END":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_REMOVE_FROM;
                    break;

                case "REMOVE FIRST":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_REMOVE_FIRST;
                    break;

                case "REMOVE LAST":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_REMOVE_LAST;
                    break;

                case "REMOVE RANDOM":
                    tooltip = Msg.LISTS_GET_INDEX_TOOLTIP_REMOVE_RANDOM;
                    break;
                }
                if (where == "FROM_START" || where == "FROM_END")
                {
                    var msg = (where == "FROM_START") ?
                              Msg.LISTS_INDEX_FROM_START_TOOLTIP :
                              Msg.LISTS_INDEX_FROM_END_TOOLTIP;
                    tooltip += "  " + msg.Replace("%1",
                                                  thisBlock.workspace.options.oneBasedIndex ? "#1" : "#0");
                }
                return(tooltip);
            }));
        }