示例#1
0
        /// <summary>
        /// Return the uneditable container block that everything else attaches to in
        /// given workspace.
        /// </summary>
        /// <param name="workspace"> Where the root block lives.</param>
        /// <returns>Root block.</returns>
        public static Blockly.Block getRootBlock(Blockly.Workspace workspace)
        {
            var blocks = workspace.getTopBlocks(false);

            for (var i = 0; i < blocks.Length; i++)
            {
                var block = blocks[i];
                if (block.type == "factory_base")
                {
                    return(block);
                }
            }
            return(null);
        }
示例#2
0
 public FieldImage(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#3
0
 public FieldVariable(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#4
0
 public FieldColour(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#5
0
 public TypeString(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#6
0
 public TypeNull(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#7
0
        /// <summary>
        /// Create JS code required to create a top, bottom, or value connection.
        /// </summary>
        /// <param name="functionName"> JavaScript function name.</param>
        /// <param name="typeName"> Name of type input.</param>
        /// <param  name="workspace"></param> Where the root block lives.
        /// <returns>Line of JavaScript code to create connection.</returns>
        private static string connectionLineJs_(string functionName, string typeName, Blockly.Workspace workspace)
        {
            var type = FactoryUtils.getOptTypesFrom(
                FactoryUtils.getRootBlock(workspace), typeName);

            if (!String.IsNullOrEmpty(type))
            {
                type = ", " + type;
            }
            else
            {
                type = "";
            }
            return("    this." + functionName + "(true" + type + ");");
        }
示例#8
0
 public FieldStatic(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#9
0
 public FactoryBase(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#10
0
 public InputStatement(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#11
0
 public InputDummy(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#12
0
 public InputValue(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#13
0
 public ColourHue(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#14
0
 public TypeOther(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#15
0
 public TypeGroupContainer(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#16
0
 public FieldNumber(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#17
0
 public TypeGroupItem(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#18
0
 public FieldDropdownContainer(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#19
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"));
        }
示例#20
0
 public FieldDropdownOption(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#21
0
        /// <summary>
        /// Get block definition code for the current block.
        /// </summary>
        /// <param  name="blockType"></param> Type of block.
        /// <param name="rootBlock"> RootBlock from main workspace in which
        /// user uses Block Factory Blocks to create a custom block.</param>
        /// <param  name="format">"JSON" or "JavaScript".</param>
        /// <param name="workspace"> Where the root block lives.</param>
        /// <returns>Block definition.</returns>
        public static string getBlockDefinition(string blockType, Blockly.Block rootBlock, string format, Blockly.Workspace workspace)
        {
            string code = null;

            blockType = blockType.Replace(new Regex(@"\W", RegexOptions.Multiline), "_").Replace(new Regex(@"^(\d)"), "_\\1");
            switch (format)
            {
            case "JSON":
                code = FactoryUtils.formatJson_(blockType, rootBlock);
                break;

            case "JavaScript":
                code = FactoryUtils.formatJavaScript_(blockType, rootBlock, workspace);
                break;
            }
            return(code);
        }
示例#22
0
 public FieldCheckbox(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }
示例#23
0
 /// <summary>
 /// Get Blockly Block by rendering pre-defined block in workspace.
 /// </summary>
 /// <param name="blockType"> Type of block that has already been defined.</param>
 /// <param name="workspace">Workspace on which to render</param>
 /// the block.
 /// <returns>The Blockly.Block of desired type.</returns>
 public static Blockly.Block getDefinedBlock(string blockType, Blockly.Workspace workspace)
 {
     workspace.clear();
     return(workspace.newBlock(blockType));
 }
示例#24
0
 public TypeBoolean(Blockly.Workspace workspace)
     : base(workspace, type_name)
 {
 }