Skip to content

Commit

Permalink
add boolean return block
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Dec 8, 2024
1 parent adca2f5 commit ce05ac8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 12 deletions.
24 changes: 23 additions & 1 deletion blocks/Blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ javascript.javascriptGenerator.forBlock['blocks_blockproperty'] = function() {
return code;
}



const blocks_blockswitch = {
init: function() {
this.appendDummyInput('PROPERTY')
Expand Down Expand Up @@ -77,4 +79,24 @@ javascript.javascriptGenerator.forBlock['blocks_blockswitch'] = function() {
const checkbox_value = this.getFieldValue('VALUE') ? 1 : 0;
const code = `this["$${dropdown_property}"] = ${checkbox_value};`;
return code;
}
}


const handle_BlockBreak = {
init: function() {
this.appendDummyInput('ID')
.appendField('Handler ID:')
.appendField(new Blockly.FieldTextInput('block break 1'), 'ID');
this.appendStatementInput('CODE')
.appendField('Block Break Handler');
this.setInputsInline(false)
this.setTooltip('');
this.setHelpUrl('');
this.setColour(0);
}
};
Blockly.common.defineBlocks({handle_BlockBreak: handle_BlockBreak});
javascript.javascriptGenerator.forBlock['handle_BlockBreak'] = function (block) {
const statement = javascript.javascriptGenerator.statementToCode(this, 'CODE');
return statement;
}
24 changes: 22 additions & 2 deletions blocks/ReturnBlock.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,30 @@ const proc_returnvalue = {
};
Blockly.common.defineBlocks({ proc_returnvalue: proc_returnvalue });



javascript.javascriptGenerator.forBlock['proc_returnvalue'] = function () {
const value = javascript.javascriptGenerator.valueToCode(this, 'VALUE', javascript.Order.ATOMIC);
const code = 'return ' + value + ';';
return code;
}



const proc_returnbool = {
init: function () {
this.appendValueInput('VALUE').setCheck('Boolean')
.appendField('return boolean');
this.setInputsInline(false)
this.setPreviousStatement(true, null);
this.setNextStatement(false, null);
this.setTooltip('');
this.setHelpUrl('');
this.setColour(195);
}
};
Blockly.common.defineBlocks({ proc_returnbool: proc_returnbool });

javascript.javascriptGenerator.forBlock['proc_returnbool'] = function () {
const value = javascript.javascriptGenerator.valueToCode(this, 'VALUE', javascript.Order.ATOMIC);
const code = `return (${value}) ? 1 : 0;`;
return code;
}
15 changes: 9 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,9 @@
</shadow>
</value>
</block>
<block type="proc_returnbool">
<field name="VALUE"> </field>
</block>
</category>
<!-- <category name="Events" colour="#a59f5b">
<block type="events_onModLoads">
Expand All @@ -358,20 +361,21 @@
<block type="events_onKeyReleased">
</block>
</category>
<sep></sep>
<category name="Variables" colour="#a55b80" custom="VARIABLE"></category> -->
<sep></sep> -->
<category name="Variables" colour="#a55b80" custom="VARIABLE"></category>
<!-- <category name="Functions" colour="#995ba5" custom="PROCEDURE"></category> -->
<sep></sep>
<category name="Blocks" colour="#ff00ff">
<block type="handle_BlockConstructor"> </block>
<block type="blocks_blockproperty">
<value name="VALUE">
<shadow type="math_number">
<field name="NUM">0.5</field>
</shadow>
<block type="logic_boolean">
<field name="BOOL">TRUE</field>
</block>
</value>
</block>
<block type="blocks_blockswitch"> </block>
<block type="handle_BlockBreak"> </block>
</category>
</xml>
</head>
Expand Down Expand Up @@ -436,7 +440,6 @@ <h4>Dirt Block</h4>
<!-- Datablock Primitives -->
<script src="primitives/Metadata.js"></script>
<script src="primitives/Icon.js"></script>
<script src="primitives/Block.js"></script>
<script src="primitives/AdvancedBlock.js"></script>

<!-- Web App GUI and Engine -->
Expand Down
13 changes: 12 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ function getHandlers(type) {
function getHandler(type, name) {
return handlerMapDict["handle_" + type]?.[name] || null;
}
function getHandlerCode(type, tag) {
var handler = getHandler(type, tag);
var usedVariableSet = new Set();
handler.getDescendants(true).forEach(block => {
block.getVars().forEach(varId => {
usedVariableSet.add(varId);
});
});
var variableCode = "var " + [...usedVariableSet].map(varId => { return javascript.javascriptGenerator.getVariableName(varId) }).join(",") + ";"
return variableCode + javascript.javascriptGenerator.blockToCode(handler);
}
const supportedEvents = new Set([
Blockly.Events.BLOCK_CHANGE,
Blockly.Events.BLOCK_CREATE,
Expand All @@ -37,7 +48,7 @@ function updateHandlers() {
handlers[block.type] = [id];
handlerMapDict[block.type] = Object.fromEntries([[id, block]]);
} else {

if (handlers[block.type].includes(id)) {
return;
}
Expand Down
4 changes: 2 additions & 2 deletions primitives/AdvancedBlock.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// Todo: add support for more events, and returning stuff in events.
PRIMITIVES["block_advanced"] = {
name: "Advanced Block",
uses: ["fixup_block_ids"],
Expand All @@ -11,7 +10,8 @@ PRIMITIVES["block_advanced"] = {
Constructor: VALUE_ENUMS.ABSTRACT_HANDLER + "BlockConstructor",
},
asJavaScript: function () {
var constructorHandler = javascript.javascriptGenerator.blockToCode(getHandler("BlockConstructor", this.tags.Constructor));
console.log(this);
var constructorHandler = getHandlerCode("BlockConstructor", this.tags.Constructor);
return `(function AdvancedBlockDatablock() {
const blockTexture = "${this.tags.texture}";
Expand Down
5 changes: 5 additions & 0 deletions types.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ function getPrimitive(type) {
var cloned = Object.assign({}, PRIMITIVES[type]);
delete cloned.asJavaScript;
cloned = structuredClone(cloned);
Object.keys(cloned.tags).forEach(key => {
if (Array.isArray(cloned.tags[key])) {
cloned.tags[key] = cloned.tags[key][0];
}
});
return cloned;
}

Expand Down

0 comments on commit ce05ac8

Please sign in to comment.