Skip to content
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

Editor & Compiler support for variable sized bitmaps #50

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added blockly/alien.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added blockly/bubble.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
113 changes: 110 additions & 3 deletions blockly/compile.js
Expand Up @@ -4,24 +4,131 @@ const Blockly = require('blockly/core');
const libraryBlocks = require('blockly/blocks'); // not entirely sure if this is necessary
const {pythonGenerator} = require('blockly/python');
const en = require('blockly/msg/en');
const savePixels = require("save-pixels")
const ndarray = require("ndarray");

Blockly.setLocale(en);

const primaryColor = [54, 61, 128, 255]
const secondaryColor = [194, 60, 30, 255]
const backgroundColor = [0, 0, 0, 0]


const pixel_colors = {
0: backgroundColor,
1: primaryColor,
2: secondaryColor
}

var saveBitmap = (bitmap, size, name) => {

// old squish approach

// var squished = [];
// for(var string of bitmap) {
// for(bit of string) {
// squished.push(bit * 255);
// }
// }

// var d = {
// data: squished,
// shape: size,
// stride: [size[0], 1],
// offset: 0
// }

var data = bitmap.map(function(arr) {
return bitmap.slice();
});

var d = new ndarray(data, [size[0], size[1], 4]);

for(var x in bitmap) {
for(var y in bitmap[x]) {
var color = pixel_colors[bitmap[x][y]];

for(var i=0;i<4;i++) {
d.set(y, x, i, color[i])
}
}
}

var out = fs.createWriteStream(`${name}.png`);
savePixels(d, "png").pipe(out)
}

var unrollWorkspaceBlocks = (workspace) => {
var workspaceBlocks = [];

var saveBlock = (block) => {
workspaceBlocks.push(block)
if(block.next) {
saveBlock(block.next.block)
}
}

for(var block of workspace.blocks.blocks) {
saveBlock(block);
}

return workspaceBlocks;
}

var getVariableName = (block_id, workspace) => {
console.log("looking",block_id);
var variable_match = workspace.variables.filter((variable)=>variable.id == block_id)
console.log(workspace.variables)
if(variable_match.length == 0) { return block_id }

return variable_match[0].name;
}

var getBitmapSize = (bitmap_type, definitions) => {
var size = definitions.blocks.filter((definition)=>definition.type==bitmap_type)[0]["args0"][1];
return [size.width, size.height]
}

try {
const file = jsonfile.readFileSync(process.argv[2]);
var filename = process.argv[2].replace(/^.*[\\/]/, '')
console.log(process.argv[3]);
var output = process.argv[3] || filename.split('.')[0]+".py"

// SAVE BITMAPS
// PROBABLY SHOULD BE IN THEIR OWN GAME FOLDER
var allWorkspaceBlocks = unrollWorkspaceBlocks(file)

// THIS IS HOW WE GET BLOCK DEFINITIONS
var definitionsArray = jsonfile.readFileSync('./src/blocks/game.json')
var definitions = Blockly.common.createBlockDefinitionsFromJsonArray(definitionsArray.blocks);
Blockly.common.defineBlocks(definitions);

// THIS IS HOW WE GET BLOCK GENERATION

for(var block of allWorkspaceBlocks) {
// save orphaned bitmaps
// if(["small_bitmap", "large_bitmap"].includes(block.type)) {
// saveBitmap(block.fields.field, block.id);
// }
if(block.type == "variables_set") {
for(var key in block.inputs) {
var variable_id = block.fields.VAR.id;
var block = block.inputs[key].block;
if(["small_bitmap", "large_bitmap"].includes(block.type)) {
var size = getBitmapSize(block.type, definitionsArray)
var name = getVariableName(variable_id, file)
saveBitmap(block.fields.field, size, name);
}
}
}
}


return;

// THIS IS HOW WE GET BLOCK GENERATION
const forBlock = require('./src/generators/python.js')
Object.assign(pythonGenerator.forBlock, forBlock);

try {
var workspace = new Blockly.Workspace();
Blockly.serialization.workspaces.load(file, workspace);
Expand Down
2 changes: 1 addition & 1 deletion blockly/dist/bundle.js

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions blockly/dist/bundle.js.LICENSE.txt
Expand Up @@ -9,3 +9,9 @@
* Copyright 2020 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/