-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parametros andando - Proceds blockly parte 1 #1
Conversation
/** | ||
* Plugin description. | ||
*/ | ||
export class Plugin { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Se tomo la decisión de ir por un objeto en lugar de una clase porque habían conflictos con las definiciones de this por como estaban definidas ciertas cosas.
Con @rgonzalezt habíamos intentado hacerlo andar pero fallaba, podemos darle otra vuelta si consideran necesario.
updateParams_: () => { }, | ||
/* customContextMenu: makeProcedureCustomMenu(), | ||
domToMutation: makeProcedureDomToMutation(), */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
El updateParams
estaba así en https://github.com/Program-AR/proceds-blockly, las otras dos son las que faltan.
export const allProcedures = (workspace) => workspace.getAllBlocks().filter(isProcedure) | ||
|
||
const getName = (procedureBlock) => procedureBlock.getFieldValue('NAME') | ||
|
||
const isProcedure = (block) => block.type === 'Procedimiento' | ||
|
||
export const allProcedureNames = (workspace) => allProcedures(workspace).map(getName) | ||
|
||
//TODO no anda | ||
const findLegalName = (name, block, index) => allProcedureNames(block.workspace).includes(name) ? findLegalName(name + " " + index, block, index + 1) : name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
El findLegalName que viene con Blockly no está funcionando, yo asumo que puede ser porque al momento de chequear, los procedimientos tienen la definición de Procedimiento y no de procedure_defnoreturn, pero no lo sé. Intenté hacer un propio findLegalName, pero no anduvo, lo voy a revisar en el proximo PR.
var addParameterButton = new Blockly.FieldImage( | ||
PLUS, | ||
16, | ||
16, | ||
Blockly.Msg.PROCEDURES_ADD_PARAMETER, | ||
() => addParameter(block, Blockly, undefined) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Este es el botón del +.
Proceds Blockly tocaba la definicion de FieldImage para solucionar algo pero como anda bien el click no lo agregué, si es necesario lo agregamos. Puede que sea algo que la nueva versión de blockly ya tenga solucionada
var input = block.appendDummyInput() | ||
.appendField(title) | ||
.appendField(nameField, 'NAME') | ||
.appendField('', 'PARAMS') | ||
|
||
block.appendStatementInput("STACK").setCheck(null) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Esto crea la estructura visible, agrega los fields que uno ve en el parámetro.
const getAvailableName = (block, name) => { | ||
const isTaken = block.arguments_.some(arg => arg === name); | ||
return isTaken ? getAvailableName(block, name + "_") : name | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lo cambie un poco a como estaba pero por cuestión de gusto personal jeje
const callers = Blockly.Procedures.getCallers(self.getFieldValue('NAME'), self.workspace); | ||
callers.forEach(caller => { | ||
caller.arguments_.push(name); | ||
caller.updateShape_() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Esto teoricamente (falta probarlo) toma los bloques que son callers del procedimiento para agregarles el parámetro nuevo
const removeParameterButton = new Blockly.FieldImage( | ||
MINUS, | ||
16, | ||
16, | ||
Blockly.Msg.PROCEDURES_REMOVE_PARAMETER, | ||
() => removeParameter(self, argsAmount, Blockly) | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Botón para quitar un parámetro, se le da el argsAmount como identificador del parámetro (que al agregarlo siempre es el último, o sea la cantidad de argumentos), para que luego sepa cual quitar.
const nameField = new Blockly.FieldTextInput(name, function (newName) { | ||
const oldName = self.arguments_[argsAmount]; | ||
|
||
if (oldName !== newName) | ||
newName = getAvailableName(self, newName); | ||
|
||
self.arguments_[argsAmount] = newName; | ||
|
||
callers.forEach(caller => { | ||
caller.arguments_ = caller.arguments_.map(argName => argName === oldName ? newName : argName) | ||
caller.updateShape_() | ||
}) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Esta función se ejecuta cuando se cambia el text field de un parámetro, es decir el nombre. Si el nombre existe se le agrega un _ al final.
Cambia también el nombre en los callers del procedimiento.
TODO: this changes the argument blocks when the name of the argument changes | ||
const varBlocks = self.workspace.getAllBlocks().filter(block.type === "variables_get" && block.$parent === self.id) | ||
varBlocks.forEach(varBlock => { | ||
var varField = varBlock.getField("VAR"); | ||
if (varField.getValue() === oldName) { | ||
varField.setValue(newName); | ||
} | ||
}) */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Esto no andaba, pero como no logré generar los bloques de los parámetros tampoco se puede probar mucho.
self | ||
.appendDummyInput(id) | ||
.appendField(Blockly.Msg.PROCEDURES_BEFORE_PARAMS) | ||
.appendField(nameField, 'ARG' + argsAmount) | ||
.appendField(createCallButton) | ||
.appendField(removeParameterButton); | ||
|
||
self.moveInputBefore(id, 'STACK'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Crea la estructura de los parámetros y los mueve antes del stack (el espacio abierto donde van los bloques hijos).
const removeParameter = (self, argsAmount, Blockly) => { | ||
let arguments_ = self.arguments_ | ||
self.arguments_.forEach((_, i) => self.removeInput("INPUTARG" + i)) | ||
self.arguments_ = [] | ||
arguments_.splice(argsAmount, 1) | ||
|
||
arguments_.forEach(arg => addParameter(self, Blockly, arg)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Esto también lo cambie un poco, como traía problemas sacar uno del medio directamente hice que se borraran todos y se agregaran nuevamente todos excepto al que se quito. La estrategia de proceds-blockly era similar pero me mareaba un poco más.
const createCall = (self, Blockly) => { | ||
console.log("create call") | ||
} | ||
|
||
const createParameterCaller = (procedureBlock, name) => { | ||
console.log("create parameter call") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Faltan
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quizá la accion de la manito (el createCall) del procedimiento (o de los parametros) pueda resolverse rapidamente con algo asi como
Blockly.serialization.blocks.append({
'type': self.type,
}, self.workspace)
aunque esto duplica el bloque, en relidad lo siguiente:
Blockly.serialization.blocks.append({
'type': "procedures_callnoreturn",
'fields': { 'NAME' :getName(self) }
}, self.workspace)
logra que genere el bloque del procedimiento a usar.
vokoscreenNG-2024-07-15_10-30-33.mp4
Hay que ver cómo sumarlo al toolbox
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Todo esta funcional. hay un error en
const createCallButton = new Blockly.FieldImage(
HAND,
16,
16,
Blockly.Msg.VARIABLES_SET_CREATE_GET.replace('%1', name),
() => createParameterCaller(self, self.getVars()[argsAmount])()
)
que la funcion getVars() no existe. Como para lograr generar los bloques de los parametros.
Vamos bien !!!
const createCall = (self, Blockly) => { | ||
console.log("create call") | ||
} | ||
|
||
const createParameterCaller = (procedureBlock, name) => { | ||
console.log("create parameter call") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
quizá la accion de la manito (el createCall) del procedimiento (o de los parametros) pueda resolverse rapidamente con algo asi como
Blockly.serialization.blocks.append({
'type': self.type,
}, self.workspace)
aunque esto duplica el bloque, en relidad lo siguiente:
Blockly.serialization.blocks.append({
'type': "procedures_callnoreturn",
'fields': { 'NAME' :getName(self) }
}, self.workspace)
logra que genere el bloque del procedimiento a usar.
vokoscreenNG-2024-07-15_10-30-33.mp4
Hay que ver cómo sumarlo al toolbox
Related Program-AR/pilas-bloques-app#248
Por ahora solo andan los parámetros (crear y eliminar) pero pense en ir tirando ya un PR con esto para que no sea una cosa enorme para hacer review mas adelante.
Se puede probar en la branch proceds-blockly de pilas.
Falta:
makeProcedureInit
que se pisan al definir los procedimientos nuestros.