/** * Reconfigure this block based on the mutator dialog's components. * @param {!Blockly.Block} containerBlock Root block in mutator. * @this Blockly.Block */ public override void compose(Block containerBlock) { var itemBlock = (TextCreateJoinItemBlock)containerBlock.getInputTargetBlock("STACK"); // Count number of inputs. var connections = new JsArray <Connection>(); while (itemBlock != null) { connections.Push(itemBlock.valueConnection_); itemBlock = (itemBlock.nextConnection != null) ? (TextCreateJoinItemBlock)itemBlock.nextConnection.targetBlock() : null; } // Disconnect any children that don't belong. for (var i = 0; i < this.itemCount_; i++) { var connection = this.getInput("ADD" + i).connection.targetConnection; if (connection != null && Array.IndexOf(connections, connection) == -1) { connection.disconnect(); } } this.itemCount_ = connections.Length; this.updateShape_(); // Reconnect any child blocks. for (var i = 0; i < this.itemCount_; i++) { Mutator.reconnect(connections[i], this, "ADD" + i); } }
/** * Reconfigure this block based on the mutator dialog's components. * @param {!Blockly.Block} containerBlock Root block in mutator. * @this Blockly.Block */ public override void compose(Block containerBlock) { // Parameter list. this.arguments_ = new JsArray <string>(); this.paramIds_ = new JsArray <string>(); var paramBlock = containerBlock.getInputTargetBlock("STACK"); while (paramBlock != null) { this.arguments_.Push(paramBlock.getFieldValue("NAME")); this.paramIds_.Push(paramBlock.id); paramBlock = (paramBlock.nextConnection != null) ? paramBlock.nextConnection.targetBlock() : null; } this.updateParams_(); Core.Procedures.mutateCallers(this); // Show/hide the statement input. var hasStatements_ = containerBlock.getFieldValue("STATEMENTS"); if (hasStatements_ != null) { var hasStatements = hasStatements_ == "TRUE"; if (this.hasStatements_ != hasStatements) { if (hasStatements) { this.setStatements_(true); // Restore the stack, if one was saved. Mutator.reconnect(this.statementConnection_, this, "STACK"); this.statementConnection_ = null; } else { // Save the stack, then disconnect it. var stackConnection = this.getInput("STACK").connection; this.statementConnection_ = stackConnection.targetConnection; if (this.statementConnection_ != null) { var stackBlock = stackConnection.targetBlock(); stackBlock.unplug(); stackBlock.bumpNeighbours_(); } this.setStatements_(false); } } } }
/** * Reconfigure this block based on the mutator dialog's components. * @param {!Blockly.Block} containerBlock Root block in mutator. * @this Blockly.Block */ public override void compose(Block containerBlock) { var clauseBlock = containerBlock.nextConnection.targetBlock(); // Count number of inputs. this.elseifCount_ = 0; this.elseCount_ = 0; var valueConnections = new JsArray <Connection> { null }; var statementConnections = new JsArray <Connection> { null }; var elseStatementConnection = (Connection)null; while (clauseBlock != null) { switch (clauseBlock.type) { case ControlsIfElseIfBlock.type_name: this.elseifCount_++; valueConnections.Push(((ControlsIfElseIfBlock)clauseBlock).valueConnection_); statementConnections.Push(((ControlsIfElseIfBlock)clauseBlock).statementConnection_); break; case ControlsIfElseBlock.type_name: this.elseCount_++; elseStatementConnection = ((ControlsIfElseBlock)clauseBlock).statementConnection_; break; default: throw new Exception("Unknown block type."); } clauseBlock = (clauseBlock.nextConnection != null) ? clauseBlock.nextConnection.targetBlock() : null; } this.updateShape_(); // Reconnect any child blocks. for (var i = 1; i <= this.elseifCount_; i++) { Mutator.reconnect(valueConnections[i], this, "IF" + i); Mutator.reconnect(statementConnections[i], this, "DO" + i); } Mutator.reconnect(elseStatementConnection, this, "ELSE"); }
/** * Notification that the procedure's parameters have changed. * @param {!Array.<string>} paramNames New param names, e.g. ["x", "y", "z"]. * @param {!Array.<string>} paramIds IDs of params (consistent for each * parameter through the life of a mutator, regardless of param renaming), * e.g. ["piua", "f8b_", "oi.o"]. * @private * @this Blockly.Block */ public void setProcedureParameters_(string[] paramNames, string[] paramIds) { // Data structures: // this.arguments = ["x", "y"] // Existing param names. // this.quarkConnections_ {piua: null, f8b_: Blockly.Connection} // Look-up of paramIds to connections plugged into the call block. // this.quarkIds_ = ["piua", "f8b_"] // Existing param IDs. // Note that quarkConnections_ may include IDs that no longer exist, but // which might reappear if a param is reattached in the mutator. var defBlock = (BlockSvg)Core.Procedures.getDefinition(this.getProcedureCall(), this.workspace); var mutatorOpen = defBlock != null && defBlock.mutator != null && defBlock.mutator.isVisible(); if (!mutatorOpen) { this.quarkConnections_ = new Dictionary <string, Connection>(); this.quarkIds_ = null; } if (paramIds == null) { // Reset the quarks (a mutator is about to open). return; } if (Array.Equals(this.arguments_, paramNames)) { // No change. this.quarkIds_ = paramIds; return; } if (paramIds.Length != paramNames.Length) { throw new Exception("Error: paramNames and paramIds must be the same length."); } this.setCollapsed(false); if (this.quarkIds_ == null) { // Initialize tracking for this block. this.quarkConnections_ = new Dictionary <string, Connection>(); if (paramNames.Join("\n") == this.arguments_.Join("\n")) { // No change to the parameters, allow quarkConnections_ to be // populated with the existing connections. this.quarkIds_ = paramIds; } else { this.quarkIds_ = new string[0]; } } // Switch off rendering while the block is rebuilt. var savedRendered = this.rendered; this.rendered = false; // Update the quarkConnections_ with existing connections. for (var i = 0; i < this.arguments_.Length; i++) { var input = this.getInput("ARG" + i); if (input != null) { var connection = input.connection.targetConnection; this.quarkConnections_[this.quarkIds_[i]] = connection; if (mutatorOpen && connection != null && Array.IndexOf(paramIds, this.quarkIds_[i]) == -1) { // This connection should no longer be attached to this block. connection.disconnect(); connection.getSourceBlock().bumpNeighbours_(); } } } // Rebuild the block's arguments. this.arguments_ = (string[])(new string[0]).Concat(paramNames); this.updateShape_(); this.quarkIds_ = paramIds; // Reconnect any child blocks. if (this.quarkIds_ != null) { for (var i = 0; i < this.arguments_.Length; i++) { var quarkId = this.quarkIds_[i]; Connection connection; if (this.quarkConnections_.TryGetValue(quarkId, out connection)) { if (!Mutator.reconnect(connection, this, "ARG" + i)) { // Block no longer exists or has been attached elsewhere. this.quarkConnections_.Remove(quarkId); //Script.Delete(this.quarkConnections_[quarkId]); } } } } // Restore rendering and show the changes. this.rendered = savedRendered; if (this.rendered) { this.render(); } }