/// <summary> /// Check to see if more than one input has this name. /// Highly inefficient (On^2), but n is small. /// </summary> /// <param name="referenceBlock">Block to check.</param> public static void inputNameCheck(Blockly.Block referenceBlock) { if (referenceBlock.workspace == null) { // Block has been deleted. return; } var name = referenceBlock.getFieldValue("INPUTNAME").ToLowerCase(); var count = 0; var blocks = referenceBlock.workspace.getAllBlocks(); for (var i = 0; i < blocks.Length; i++) { var block = blocks[i]; var otherName = block.getFieldValue("INPUTNAME"); if (!block.disabled && !block.getInheritedDisabled() && otherName != null && otherName.ToLowerCase() == name) { count++; } } var msg = (count > 1) ? "There are " + count + " input blocks\n with this name." : null; referenceBlock.setWarningText(msg); }
/// <summary> /// Returns field strings and any config. /// </summary> /// <param name="block"></param> Input block. /// <returns></returns> Array of static text and field configs. private static JsArray <object> getFieldsJson_(Blockly.Block block) { var fields = new JsArray <object>(); while (block != null) { if (!block.disabled && !block.getInheritedDisabled()) { switch (block.type) { case "field_static": // Result: "hello" fields.Push(block.getFieldValue("TEXT")); break; case "field_input": fields.Push(new { type = block.type, name = block.getFieldValue("FIELDNAME"), text = block.getFieldValue("TEXT") }); break; case "field_number": var obj = new Dictionary <string, object>(); obj["type"] = block.type; obj["name"] = block.getFieldValue("FIELDNAME"); obj["value"] = Script.ParseFloat(block.getFieldValue("VALUE")); var min = Script.ParseFloat(block.getFieldValue("MIN")); if (min > Double.NegativeInfinity) { obj["min"] = min; } var max = Script.ParseFloat(block.getFieldValue("MAX")); if (max < Double.PositiveInfinity) { obj["max"] = max; } var precision = block.getFieldValue("PRECISION"); if (!String.IsNullOrEmpty(precision)) { obj["precision"] = Script.ParseFloat(precision); } fields.Push(obj); break; case "field_angle": fields.Push(new { type = block.type, name = block.getFieldValue("FIELDNAME"), angle = Convert.ToDouble(block.getFieldValue("ANGLE")) }); break; case "field_checkbox": fields.Push(new { type = block.type, name = block.getFieldValue("FIELDNAME"), @checked = block.getFieldValue("CHECKED") == "TRUE" }); break; case "field_colour": fields.Push(new { type = block.type, name = block.getFieldValue("FIELDNAME"), colour = block.getFieldValue("COLOUR") }); break; case "field_date": fields.Push(new { type = block.type, name = block.getFieldValue("FIELDNAME"), date = block.getFieldValue("DATE") }); break; case "field_variable": fields.Push(new { type = block.type, name = block.getFieldValue("FIELDNAME"), variable = block.getFieldValue("TEXT") ?? null }); break; case "field_dropdown": var count = ((FieldDropdown)block).optionCount_; var options = new JsArray <DropdownItemInfo>(count); for (var i = 0; i < count; i++) { options[i] = new DropdownItemInfo( block.getFieldValue("USER" + i), block.getFieldValue("CPU" + i) ); } if (options.Length >= 0) { fields.Push(new { type = block.type, name = block.getFieldValue("FIELDNAME"), options = options }); } break; case "field_image": fields.Push(new { type = block.type, src = block.getFieldValue("SRC"), width = Convert.ToDouble(block.getFieldValue("WIDTH")), height = Convert.ToDouble(block.getFieldValue("HEIGHT")), alt = block.getFieldValue("ALT") }); break; } } block = block.nextConnection == null ? null : block.nextConnection.targetBlock(); } return(fields); }
/// <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); }
/// <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")); }
/// <summary> /// Update the language code as JSON. /// </summary> /// <param name="blockType"> Name of block.</param> /// <param name="rootBlock">Factory_base block.</param> /// <returns>Generanted language code.</returns> private static string formatJson_(string blockType, Blockly.Block rootBlock) { var JS = new Dictionary <string, object>(); // Type is not used by Blockly, but may be used by a loader. JS["type"] = blockType; // Generate inputs. var message = new JsArray <string>(); var args = new JsArray <object>(); var contentsBlock = rootBlock.getInputTargetBlock("INPUTS"); Blockly.Block lastInput = null; while (contentsBlock != null) { if (!contentsBlock.disabled && !contentsBlock.getInheritedDisabled()) { var fields = FactoryUtils.getFieldsJson_( contentsBlock.getInputTargetBlock("FIELDS")); for (var i = 0; i < fields.Length; i++) { if (fields[i] is string str) { message.Push(str.Replace(new Regex("%", RegexOptions.Multiline), "%%")); } else { args.Push(fields[i]); message.Push("%" + args.Length); } } var input = new Dictionary <string, object>(); input.Add("type", contentsBlock.type); // Dummy inputs don't have names. Other inputs do. if (contentsBlock.type != "input_dummy") { input["name"] = contentsBlock.getFieldValue("INPUTNAME"); } var check = JSON.Parse( FactoryUtils.getOptTypesFrom(contentsBlock, "TYPE") ?? "null"); if (check != null) { input["check"] = check; } var align = contentsBlock.getFieldValue("ALIGN"); if (align != "LEFT") { input["align"] = align; } args.Push(input); message.Push("%" + args.Length); lastInput = contentsBlock; } contentsBlock = contentsBlock.nextConnection == null ? null : contentsBlock.nextConnection.targetBlock(); } // Remove last input if dummy and not empty. if (lastInput != null && lastInput.type == "input_dummy") { var fields = lastInput.getInputTargetBlock("FIELDS"); if (fields != null && FactoryUtils.getFieldsJson_(fields).Join("").Trim() != "") { var align = lastInput.getFieldValue("ALIGN"); if (align != "LEFT") { JS["lastDummyAlign0"] = align; } args.Pop(); message.Pop(); } } JS["message0"] = message.Join(" "); if (args.Length > 0) { JS["args0"] = args; } // Generate inline/external switch. if (rootBlock.getFieldValue("INLINE") == "EXT") { JS["inputsInline"] = false; } else if (rootBlock.getFieldValue("INLINE") == "INT") { JS["inputsInline"] = true; } // Generate output, or next/previous connections. switch (rootBlock.getFieldValue("CONNECTIONS")) { case "LEFT": JS["output"] = JSON.Parse( FactoryUtils.getOptTypesFrom(rootBlock, "OUTPUTTYPE") ?? "null"); break; case "BOTH": JS["previousStatement"] = JSON.Parse( FactoryUtils.getOptTypesFrom(rootBlock, "TOPTYPE") ?? "null"); JS["nextStatement"] = JSON.Parse( FactoryUtils.getOptTypesFrom(rootBlock, "BOTTOMTYPE") ?? "null"); break; case "TOP": JS["previousStatement"] = JSON.Parse( FactoryUtils.getOptTypesFrom(rootBlock, "TOPTYPE") ?? "null"); break; case "BOTTOM": JS["nextStatement"] = JSON.Parse( FactoryUtils.getOptTypesFrom(rootBlock, "BOTTOMTYPE") ?? "null"); break; } // Generate colour. var colourBlock = rootBlock.getInputTargetBlock("COLOUR"); if (colourBlock != null && !colourBlock.disabled) { var hue = Script.ParseFloat(colourBlock.getFieldValue("HUE")); JS["colour"] = hue; } JS["tooltip"] = ""; JS["helpUrl"] = "http://www.example.com/"; return(JSON.Stringify(JS, null, " ")); }