diff --git a/boards/default_src/micropython_esp32/template.xml b/boards/default_src/micropython_esp32/template.xml index ccb6bbc2..273529a6 100644 --- a/boards/default_src/micropython_esp32/template.xml +++ b/boards/default_src/micropython_esp32/template.xml @@ -899,19 +899,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/micropython_esp32c2/template.xml b/boards/default_src/micropython_esp32c2/template.xml index 77e05008..a3a9793f 100644 --- a/boards/default_src/micropython_esp32c2/template.xml +++ b/boards/default_src/micropython_esp32c2/template.xml @@ -872,19 +872,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/micropython_esp32c3/template.xml b/boards/default_src/micropython_esp32c3/template.xml index b21c6247..c6044d45 100644 --- a/boards/default_src/micropython_esp32c3/template.xml +++ b/boards/default_src/micropython_esp32c3/template.xml @@ -872,19 +872,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/micropython_esp32s2/template.xml b/boards/default_src/micropython_esp32s2/template.xml index a8c553ac..cc780c75 100644 --- a/boards/default_src/micropython_esp32s2/template.xml +++ b/boards/default_src/micropython_esp32s2/template.xml @@ -899,19 +899,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/micropython_esp32s3/template.xml b/boards/default_src/micropython_esp32s3/template.xml index 9f1659e9..3da14ca6 100644 --- a/boards/default_src/micropython_esp32s3/template.xml +++ b/boards/default_src/micropython_esp32s3/template.xml @@ -899,19 +899,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/micropython_k210_mixgoai/template.xml b/boards/default_src/micropython_k210_mixgoai/template.xml index 56674dc9..31793106 100644 --- a/boards/default_src/micropython_k210_mixgoai/template.xml +++ b/boards/default_src/micropython_k210_mixgoai/template.xml @@ -894,19 +894,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/micropython_nrf51822_microbit/template.xml b/boards/default_src/micropython_nrf51822_microbit/template.xml index f6fcf09d..86aeb2db 100644 --- a/boards/default_src/micropython_nrf51822_microbit/template.xml +++ b/boards/default_src/micropython_nrf51822_microbit/template.xml @@ -742,19 +742,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/micropython_nrf51822_mithoncc/template.xml b/boards/default_src/micropython_nrf51822_mithoncc/template.xml index cbe12914..e57f13c1 100644 --- a/boards/default_src/micropython_nrf51822_mithoncc/template.xml +++ b/boards/default_src/micropython_nrf51822_mithoncc/template.xml @@ -658,19 +658,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/micropython_robot/template.xml b/boards/default_src/micropython_robot/template.xml index 873f5c07..9ff4785e 100644 --- a/boards/default_src/micropython_robot/template.xml +++ b/boards/default_src/micropython_robot/template.xml @@ -886,19 +886,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/python/blocks/lists.js b/boards/default_src/python/blocks/lists.js index b47a80f6..554cb264 100644 --- a/boards/default_src/python/blocks/lists.js +++ b/boards/default_src/python/blocks/lists.js @@ -775,127 +775,6 @@ export const lists_del_general = { } }; -export const lists_zip = { - init: function () { - this.setColour(LISTS_HUE); - - this.itemCount_ = 2; - this.updateShape_(); - this.setInputsInline(true); - this.setPreviousStatement(false); - this.setNextStatement(false); - this.setOutput(true, "List") - this.setMutator(new Blockly.icons.MutatorIcon(['lists_zip_item'], this)); - this.setTooltip(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_TOOLTIP); - }, - - mutationToDom: function () { - var container = document.createElement('mutation'); - container.setAttribute('items', this.itemCount_); - return container; - }, - - domToMutation: function (xmlElement) { - this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10); - this.updateShape_(); - }, - - decompose: function (workspace) { - var containerBlock = - workspace.newBlock('lists_zip_container'); - containerBlock.initSvg(); - var connection = containerBlock.getInput('STACK').connection; - for (var i = 0; i < this.itemCount_; i++) { - var itemBlock = workspace.newBlock('lists_zip_item'); - itemBlock.initSvg(); - connection.connect(itemBlock.previousConnection); - connection = itemBlock.nextConnection; - } - return containerBlock; - }, - - compose: function (containerBlock) { - var itemBlock = containerBlock.getInputTargetBlock('STACK'); - // Count number of inputs. - var connections = []; - var i = 0; - while (itemBlock) { - connections[i] = itemBlock.valueConnection_; - itemBlock = itemBlock.nextConnection && - itemBlock.nextConnection.targetBlock(); - i++; - } - this.itemCount_ = i; - this.updateShape_(); - // Reconnect any child blocks. - for (var i = 0; i < this.itemCount_; i++) { - if (connections[i]) { - this.getInput('ADD' + i).connection.connect(connections[i]); - } - } - }, - - saveConnections: function (containerBlock) { - var itemBlock = containerBlock.getInputTargetBlock('STACK'); - var i = 0; - while (itemBlock) { - var input = this.getInput('ADD' + i); - itemBlock.valueConnection_ = input && input.connection.targetConnection; - i++; - itemBlock = itemBlock.nextConnection && - itemBlock.nextConnection.targetBlock(); - } - }, - - updateShape_: function () { - // Delete everything. - if (this.getInput('EMPTY')) { - this.removeInput('EMPTY'); - } else { - var i = 0; - while (this.getInput('ADD' + i)) { - this.removeInput('ADD' + i); - i++; - } - } - // Rebuild block. - if (this.itemCount_ == 0) { - this.appendDummyInput('EMPTY') - .appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP); - } else { - for (var i = 0; i < this.itemCount_; i++) { - var input = this.appendValueInput('ADD' + i); - if (i == 0) { - input.appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP); - } - } - } - } -}; -export const lists_zip_container = { - init: function () { - this.setColour(LISTS_HUE); - this.appendDummyInput() - .appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP) - .appendField('[]'); - this.appendStatementInput('STACK'); - this.setTooltip(Blockly.Msg.MIXLY_MIXPY_INOUT_PRINT_MANY_CONTAINER_TOOLTIP); - this.contextMenu = false; - } -}; - -export const lists_zip_item = { - init: function () { - this.setColour(LISTS_HUE); - this.appendDummyInput() - .appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_ITEM); - this.setPreviousStatement(true); - this.setNextStatement(true); - this.setTooltip(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_ITEM_TOOLTIP); - this.contextMenu = false; - } -}; - export const list_tolist = { init: function () { this.setColour(LISTS_HUE); diff --git a/boards/default_src/python/blocks/variables.js b/boards/default_src/python/blocks/variables.js index 4468e77b..0eb4ec84 100644 --- a/boards/default_src/python/blocks/variables.js +++ b/boards/default_src/python/blocks/variables.js @@ -199,4 +199,139 @@ export const controls_typeLists = { return mode0 + TOOLTIPS[mode]; }); } +}; + +export const lists_zip = { + init: function () { + this.setColour(VARIABLES_HUE); + + this.itemCount_ = 2; + this.updateShape_(); + this.setInputsInline(true); + this.setPreviousStatement(false); + this.setNextStatement(false); + this.setOutput(true, "List") + this.setMutator(new Blockly.icons.MutatorIcon(['lists_zip_item'], this)); + this.setTooltip(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_TOOLTIP); + }, + + mutationToDom: function () { + var container = document.createElement('mutation'); + container.setAttribute('items', this.itemCount_); + return container; + }, + + domToMutation: function (xmlElement) { + this.itemCount_ = parseInt(xmlElement.getAttribute('items'), 10); + this.updateShape_(); + }, + + decompose: function (workspace) { + var containerBlock = + workspace.newBlock('lists_zip_container'); + containerBlock.initSvg(); + var connection = containerBlock.getInput('STACK').connection; + for (var i = 0; i < this.itemCount_; i++) { + var itemBlock = workspace.newBlock('lists_zip_item'); + itemBlock.initSvg(); + connection.connect(itemBlock.previousConnection); + connection = itemBlock.nextConnection; + } + return containerBlock; + }, + + compose: function (containerBlock) { + var itemBlock = containerBlock.getInputTargetBlock('STACK'); + // Count number of inputs. + var connections = []; + var i = 0; + while (itemBlock) { + connections[i] = itemBlock.valueConnection_; + itemBlock = itemBlock.nextConnection && + itemBlock.nextConnection.targetBlock(); + i++; + } + this.itemCount_ = i; + this.updateShape_(); + // Reconnect any child blocks. + for (var i = 0; i < this.itemCount_; i++) { + if (connections[i]) { + this.getInput('ADD' + i).connection.connect(connections[i]); + } + } + }, + + saveConnections: function (containerBlock) { + var itemBlock = containerBlock.getInputTargetBlock('STACK'); + var i = 0; + while (itemBlock) { + var input = this.getInput('ADD' + i); + itemBlock.valueConnection_ = input && input.connection.targetConnection; + i++; + itemBlock = itemBlock.nextConnection && + itemBlock.nextConnection.targetBlock(); + } + }, + + updateShape_: function () { + // Delete everything. + if (this.getInput('EMPTY')) { + this.removeInput('EMPTY'); + } else { + var i = 0; + while (this.getInput('ADD' + i)) { + this.removeInput('ADD' + i); + i++; + } + } + // Rebuild block. + if (this.itemCount_ == 0) { + this.appendDummyInput('EMPTY') + .appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP); + } else { + for (var i = 0; i < this.itemCount_; i++) { + var input = this.appendValueInput('ADD' + i); + if (i == 0) { + input.appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP); + } + } + } + } +}; +export const lists_zip_container = { + init: function () { + this.setColour(VARIABLES_HUE); + this.appendDummyInput() + .appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP) + .appendField('[]'); + this.appendStatementInput('STACK'); + this.setTooltip(Blockly.Msg.MIXLY_MIXPY_INOUT_PRINT_MANY_CONTAINER_TOOLTIP); + this.contextMenu = false; + } +}; + +export const lists_zip_item = { + init: function () { + this.setColour(VARIABLES_HUE); + this.appendDummyInput() + .appendField(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_ITEM); + this.setPreviousStatement(true); + this.setNextStatement(true); + this.setTooltip(Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_ITEM_TOOLTIP); + this.contextMenu = false; + } +}; + +export const unpack_iterable_object = { + init: function () { + this.setColour(VARIABLES_HUE); + this.appendValueInput('VAR') + .appendField(Blockly.Msg.MIXLY_VARIABLE_UNPACK) + .appendField(new Blockly.FieldDropdown([ + [Blockly.Msg.MIXLY_PYTHON_LISTS_ZIP_ITEM, '*'], + [Blockly.Msg.MIXLY_MICROBIT_TYPE_DICT, '**'] + ]), 'TYPE'); + this.setTooltip(''); + this.setOutput(true); + } }; \ No newline at end of file diff --git a/boards/default_src/python/generators/lists.js b/boards/default_src/python/generators/lists.js index 2bed7d40..7b89c1f8 100644 --- a/boards/default_src/python/generators/lists.js +++ b/boards/default_src/python/generators/lists.js @@ -245,17 +245,6 @@ export const lists_setIndex3 = lists_set_index export const lists_insert_value2 = lists_insert_value export const lists_remove_at2 = lists_remove_at -export const lists_zip = function (_, generator) { - var code = new Array(this.itemCount_); - var default_value = '[]'; - for (var n = 0; n < this.itemCount_; n++) { - code[n] = generator.valueToCode(this, 'ADD' + n, - generator.ORDER_NONE) || default_value; - } - var code = 'zip(' + code.join(', ') + ')'; - return [code, generator.ORDER_ATOMIC]; -} - export const list_tolist = function (_, generator) { var str = generator.valueToCode(this, 'VAR', generator.ORDER_ATOMIC) || '0' return ['list(' + str + ')', generator.ORDER_ATOMIC]; diff --git a/boards/default_src/python/generators/variables.js b/boards/default_src/python/generators/variables.js index 0242bfea..512dc26d 100644 --- a/boards/default_src/python/generators/variables.js +++ b/boards/default_src/python/generators/variables.js @@ -69,4 +69,22 @@ export const controls_typeLists = function (_, generator) { var type = this.getFieldValue('type'); // generator.definitions_['func_type' + type] = code; return [type, generator.ORDER_ATOMIC]; +} + +export const lists_zip = function (_, generator) { + var code = new Array(this.itemCount_); + var default_value = '[]'; + for (var n = 0; n < this.itemCount_; n++) { + code[n] = generator.valueToCode(this, 'ADD' + n, + generator.ORDER_NONE) || default_value; + } + var code = 'zip(' + code.join(', ') + ')'; + return [code, generator.ORDER_ATOMIC]; +} + +export const unpack_iterable_object = function (_, generator) { + const varName = generator.valueToCode(this, 'VAR', generator.ORDER_ASSIGNMENT) || '[]'; + const type = this.getFieldValue('TYPE'); + const code = `${type}(${varName})`; + return [code, generator.ORDER_ATOMIC]; } \ No newline at end of file diff --git a/boards/default_src/python/others/variables.js b/boards/default_src/python/others/variables.js index 2a3226d5..a9f8bf99 100644 --- a/boards/default_src/python/others/variables.js +++ b/boards/default_src/python/others/variables.js @@ -134,6 +134,33 @@ Variables.flyoutCategory = function (workspace) { block.setAttribute('type', 'controls_typeLists'); xmlList.push(block); } + if (Blockly.Blocks['lists_zip']) { + var block = Blockly.utils.xml.createElement('block'); + block.setAttribute('type', 'lists_zip'); + block.setAttribute('inline', 'false'); + var mutation = Blockly.utils.xml.createElement('mutation'); + mutation.setAttribute('items', 2); + block.appendChild(mutation); + for (let i = 0; i < 2; i++) { + let add = Blockly.utils.xml.createElement('value'); + add.setAttribute('name', `ADD${i}`); + // let childBlock = Blockly.utils.xml.createElement('block'); + // childBlock.setAttribute('type', 'list_many_input'); + // let field = Blockly.utils.xml.createElement('field'); + // let value = Blockly.utils.xml.createTextNode('0,1,2,3'); + // field.setAttribute('name', 'CONTENT'); + // field.appendChild(value); + // childBlock.appendChild(field); + // add.appendChild(childBlock); + block.appendChild(add); + } + xmlList.push(block); + } + if (Blockly.Blocks['unpack_iterable_object']) { + var block = Blockly.utils.xml.createElement('block'); + block.setAttribute('type', 'unpack_iterable_object'); + xmlList.push(block); + } for (var i = 0; i < variableList.length; i++) { // alert(variableList) // if(i==0&& !(Blockly.Python.setups_['variables_set'+''])){ diff --git a/boards/default_src/python_mixpy/template.xml b/boards/default_src/python_mixpy/template.xml index b28281b1..dbc0db8c 100644 --- a/boards/default_src/python_mixpy/template.xml +++ b/boards/default_src/python_mixpy/template.xml @@ -894,19 +894,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/python_pyodide/template.xml b/boards/default_src/python_pyodide/template.xml index b28281b1..dbc0db8c 100644 --- a/boards/default_src/python_pyodide/template.xml +++ b/boards/default_src/python_pyodide/template.xml @@ -894,19 +894,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/python_skulpt/template.xml b/boards/default_src/python_skulpt/template.xml index 54bbe9c0..9978da0b 100644 --- a/boards/default_src/python_skulpt/template.xml +++ b/boards/default_src/python_skulpt/template.xml @@ -846,19 +846,6 @@ - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/python_skulpt_car/template.xml b/boards/default_src/python_skulpt_car/template.xml index 099a727e..3732e064 100644 --- a/boards/default_src/python_skulpt_car/template.xml +++ b/boards/default_src/python_skulpt_car/template.xml @@ -817,19 +817,6 @@ type="application/javascript"> --> - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/boards/default_src/python_skulpt_mixtoy/template.xml b/boards/default_src/python_skulpt_mixtoy/template.xml index b87fd3d1..43d7794d 100644 --- a/boards/default_src/python_skulpt_mixtoy/template.xml +++ b/boards/default_src/python_skulpt_mixtoy/template.xml @@ -804,19 +804,6 @@ type="application/javascript"> --> - - - - - 0,1,2,3 - - - - - 0,1,2,3 - - - diff --git a/common/msg/blockly/en.js b/common/msg/blockly/en.js index d3bab69a..bebd126c 100644 --- a/common/msg/blockly/en.js +++ b/common/msg/blockly/en.js @@ -3748,4 +3748,5 @@ En.MIXLY_BATTERY ="battery level"; En.MIXLY_RANGE_OF_BATTERY = "the range of battery level is 0~100%"; En.MIXLY_MOUSE = "mouse"; En.MIXLY_SEND = "send"; +En.MIXLY_VARIABLE_UNPACK = "Unpack"; })(); \ No newline at end of file diff --git a/common/msg/blockly/zh-hans.js b/common/msg/blockly/zh-hans.js index 1520a17c..709c2255 100644 --- a/common/msg/blockly/zh-hans.js +++ b/common/msg/blockly/zh-hans.js @@ -3896,4 +3896,5 @@ ZhHans.MIXLY_BATTERY ="电池电量"; ZhHans.MIXLY_RANGE_OF_BATTERY = "电池的电量范围为 0~100%"; ZhHans.MIXLY_MOUSE = "鼠标"; ZhHans.MIXLY_SEND = "发送"; +ZhHans.MIXLY_VARIABLE_UNPACK = "解包"; })(); \ No newline at end of file diff --git a/common/msg/blockly/zh-hant.js b/common/msg/blockly/zh-hant.js index 2cd9c4d2..0202ed51 100644 --- a/common/msg/blockly/zh-hant.js +++ b/common/msg/blockly/zh-hant.js @@ -3751,4 +3751,5 @@ ZhHant.MIXLY_BATTERY ="電池電量"; ZhHant.MIXLY_RANGE_OF_BATTERY = "電池的電量範圍為 0~100%"; ZhHant.MIXLY_MOUSE = "鼠標"; ZhHant.MIXLY_SEND = "發送"; +ZhHant.MIXLY_VARIABLE_UNPACK = "解包"; })(); \ No newline at end of file