Beispiel #1
0
        /// <summary>
        /// Fetch the type(s) defined in the given input.
        /// </summary>
        /// <param name="block"> Block with input.</param>
        /// <param  name="name">Name of the input.</param>
        /// <returns>List of types.</returns>
        private static JsArray <string> getTypesFrom_(Blockly.Block block, string name)
        {
            var typeBlock = block.getInputTargetBlock(name);
            JsArray <string> types;

            if (typeBlock == null || typeBlock.disabled)
            {
                types = new JsArray <string>();
            }
            else if (typeBlock.type == "type_other")
            {
                types = new JsArray <string> {
                    FactoryUtils.escapeString(typeBlock.getFieldValue("TYPE"))
                };
            }
            else if (typeBlock.type == "type_group")
            {
                types = new JsArray <string>();
                for (var n = 0; n < ((TypeGroup)typeBlock).typeCount_; n++)
                {
                    types = types.Concat(FactoryUtils.getTypesFrom_(typeBlock, "TYPE" + n));
                }
                // Remove duplicates.
                var hash = new Dictionary <string, object>();
                for (var n = types.Length - 1; n >= 0; n--)
                {
                    if (hash.ContainsKey(types[n]))
                    {
                        types.Splice(n, 1);
                    }
                    hash[types[n]] = true;
                }
            }
            else
            {
                var fi = typeBlock.GetType().GetField("valueType", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Public);
                types = new JsArray <string> {
                    FactoryUtils.escapeString((string)fi.GetValue(null))
                };
            }
            return(types);
        }
Beispiel #2
0
        /// <summary>
        /// Returns field strings and any config.
        /// </summary>
        /// <param  name="block">Input block.</param>
        /// <returns>Field strings.</returns>
        private static JsArray <string> getFieldsJs_(Blockly.Block block)
        {
            var fields = new JsArray <string>();

            while (block != null)
            {
                if (!block.disabled && !block.getInheritedDisabled())
                {
                    switch (block.type)
                    {
                    case "field_static":
                        // Result: "hello"
                        fields.Push(FactoryUtils.escapeString(block.getFieldValue("TEXT")));
                        break;

                    case "field_input":
                        // Result: new Blockly.FieldTextInput("Hello"), "GREET"
                        fields.Push("new Blockly.FieldTextInput(" +
                                    FactoryUtils.escapeString(block.getFieldValue("TEXT")) + "), " +
                                    FactoryUtils.escapeString(block.getFieldValue("FIELDNAME")));
                        break;

                    case "field_number":
                        // Result: new Blockly.FieldNumber(10, 0, 100, 1), "NUMBER"
                        var args = new JsArray <double> {
                            Script.ParseFloat(block.getFieldValue("VALUE")),
                            Script.ParseFloat(block.getFieldValue("MIN")),
                            Script.ParseFloat(block.getFieldValue("MAX")),
                            Script.ParseFloat(block.getFieldValue("PRECISION"))
                        };
                        // Remove any trailing arguments that aren't needed.
                        if (args[3] == 0)
                        {
                            args.Pop();
                            if (args[2] == Double.PositiveInfinity)
                            {
                                args.Pop();
                                if (args[1] == Double.NegativeInfinity)
                                {
                                    args.Pop();
                                }
                            }
                        }
                        fields.Push("new Blockly.FieldNumber(" + args.Join(", ") + "), " +
                                    FactoryUtils.escapeString(block.getFieldValue("FIELDNAME")));
                        break;

                    case "field_angle":
                        // Result: new Blockly.FieldAngle(90), "ANGLE"
                        fields.Push("new Blockly.FieldAngle(" +
                                    Script.ParseFloat(block.getFieldValue("ANGLE")) + "), " +
                                    FactoryUtils.escapeString(block.getFieldValue("FIELDNAME")));
                        break;

                    case "field_checkbox":
                        // Result: new Blockly.FieldCheckbox("TRUE"), "CHECK"
                        fields.Push("new Blockly.FieldCheckbox(" +
                                    FactoryUtils.escapeString(block.getFieldValue("CHECKED")) +
                                    "), " +
                                    FactoryUtils.escapeString(block.getFieldValue("FIELDNAME")));
                        break;

                    case "field_colour":
                        // Result: new Blockly.FieldColour("#ff0000"), "COLOUR"
                        fields.Push("new Blockly.FieldColour(" +
                                    FactoryUtils.escapeString(block.getFieldValue("COLOUR")) +
                                    "), " +
                                    FactoryUtils.escapeString(block.getFieldValue("FIELDNAME")));
                        break;

                    case "field_date":
                        // Result: new Blockly.FieldDate("2015-02-04"), "DATE"
                        fields.Push("new Blockly.FieldDate(" +
                                    FactoryUtils.escapeString(block.getFieldValue("DATE")) + "), " +
                                    FactoryUtils.escapeString(block.getFieldValue("FIELDNAME")));
                        break;

                    case "field_variable":
                        // Result: new Blockly.FieldVariable("item"), "VAR"
                        var varname
                            = FactoryUtils.escapeString(block.getFieldValue("TEXT") ?? null);
                        fields.Push("new Blockly.FieldVariable(" + varname + "), " +
                                    FactoryUtils.escapeString(block.getFieldValue("FIELDNAME")));
                        break;

                    case "field_dropdown":
                        // Result:
                        // new Blockly.FieldDropdown([["yes", "1"], ["no", "0"]]), "TOGGLE"
                        var options = new JsArray <string>();
                        for (var i = 0; i < ((FieldDropdown)block).optionCount_; i++)
                        {
                            options[i] = "[" +
                                         FactoryUtils.escapeString(block.getFieldValue("USER" + i)) +
                                         ", " +
                                         FactoryUtils.escapeString(block.getFieldValue("CPU" + i)) + "]";
                        }
                        if (options.Length > 0)
                        {
                            fields.Push("new Blockly.FieldDropdown([" +
                                        options.Join(", ") + "]), " +
                                        FactoryUtils.escapeString(block.getFieldValue("FIELDNAME")));
                        }
                        break;

                    case "field_image":
                        // Result: new Blockly.FieldImage("http://...", 80, 60)
                        var src    = FactoryUtils.escapeString(block.getFieldValue("SRC"));
                        var width  = Convert.ToDouble(block.getFieldValue("WIDTH"));
                        var height = Convert.ToDouble(block.getFieldValue("HEIGHT"));
                        var alt    = FactoryUtils.escapeString(block.getFieldValue("ALT"));
                        fields.Push("new Blockly.FieldImage(" +
                                    src + ", " + width + ", " + height + ", " + alt + ")");
                        break;
                    }
                }
                block = block.nextConnection == null ? null : block.nextConnection.targetBlock();
            }
            return(fields);
        }
Beispiel #3
0
        /// <summary>
        /// Update the language code as JavaScript.
        /// </summary>
        /// <param name="blockType"> Name of block.</param>
        /// <param name="rootBlock"> Factory_base block.</param>
        /// <param name="workspace"></param> Where the root block lives.
        /// <returns>Generated language code.</returns>
        private static string formatJavaScript_(string blockType, Blockly.Block rootBlock, Blockly.Workspace workspace)
        {
            var code = new JsArray <string>();

            code.Push("Blockly.Core.Blocks[\"" + blockType + "\"] = {");
            code.Push("  init: () => {");
            // Generate inputs.
            var TYPES = new Dictionary <string, string>()
            {
                { "input_value", "appendValueInput" },
                { "input_statement", "appendStatementInput" },
                { "input_dummy", "appendDummyInput" }
            };
            var contentsBlock = rootBlock.getInputTargetBlock("INPUTS");

            while (contentsBlock != null)
            {
                if (!contentsBlock.disabled && !contentsBlock.getInheritedDisabled())
                {
                    var name = "";
                    // Dummy inputs don't have names.  Other inputs do.
                    if (contentsBlock.type != "input_dummy")
                    {
                        name =
                            FactoryUtils.escapeString(contentsBlock.getFieldValue("INPUTNAME"));
                    }
                    code.Push("    this." + TYPES[contentsBlock.type] + "(" + name + ")");
                    var check = FactoryUtils.getOptTypesFrom(contentsBlock, "TYPE");
                    if (!String.IsNullOrEmpty(check))
                    {
                        code.Push("        .setCheck(" + check + ")");
                    }
                    var align = contentsBlock.getFieldValue("ALIGN");
                    if (align != "LEFT")
                    {
                        code.Push("        .setAlign(Blockly.ALIGN_" + align + ")");
                    }
                    var fields = FactoryUtils.getFieldsJs_(
                        contentsBlock.getInputTargetBlock("FIELDS"));
                    for (var i = 0; i < fields.Length; i++)
                    {
                        code.Push("        .appendField(" + fields[i] + ")");
                    }
                    // Add semicolon to last line to finish the statement.
                    code[code.Length - 1] += ";";
                }
                contentsBlock = contentsBlock.nextConnection == null ? null :
                                contentsBlock.nextConnection.targetBlock();
            }
            // Generate inline/external switch.
            if (rootBlock.getFieldValue("INLINE") == "EXT")
            {
                code.Push("    this.setInputsInline(false);");
            }
            else if (rootBlock.getFieldValue("INLINE") == "INT")
            {
                code.Push("    this.setInputsInline(true);");
            }
            // Generate output, or next/previous connections.
            switch (rootBlock.getFieldValue("CONNECTIONS"))
            {
            case "LEFT":
                code.Push(FactoryUtils.connectionLineJs_("setOutput", "OUTPUTTYPE", workspace));
                break;

            case "BOTH":
                code.Push(
                    FactoryUtils.connectionLineJs_("setPreviousStatement", "TOPTYPE", workspace));
                code.Push(
                    FactoryUtils.connectionLineJs_("setNextStatement", "BOTTOMTYPE", workspace));
                break;

            case "TOP":
                code.Push(
                    FactoryUtils.connectionLineJs_("setPreviousStatement", "TOPTYPE", workspace));
                break;

            case "BOTTOM":
                code.Push(
                    FactoryUtils.connectionLineJs_("setNextStatement", "BOTTOMTYPE", workspace));
                break;
            }
            // Generate colour.
            var colourBlock = rootBlock.getInputTargetBlock("COLOUR");

            if (colourBlock != null && !colourBlock.disabled)
            {
                var hue = Script.ParseFloat(colourBlock.getFieldValue("HUE"));
                if (!Double.IsNaN(hue))
                {
                    code.Push("    this.setColour(" + hue + ");");
                }
            }
            code.Push("    this.setTooltip(\"\");");
            code.Push("    this.setHelpUrl(\"http://www.example.com/\");");
            code.Push("  }");
            code.Push("};");
            return(code.Join("\n"));
        }