Skip to content

Commit

Permalink
Merge pull request #50 from Capstone-Projects-2024-Spring/BP-139-Add-…
Browse files Browse the repository at this point in the history
…support-for-bitmaps-in-editor-and-compiler

Editor & Compiler support for variable sized bitmaps
  • Loading branch information
tuk05348 committed Mar 13, 2024
2 parents 07f5768 + c9d56a1 commit e67fc4c
Show file tree
Hide file tree
Showing 13 changed files with 1,556 additions and 12 deletions.
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
*/

0 comments on commit e67fc4c

Please sign in to comment.