Skip to content

Commit

Permalink
Inital testing (may be buggy)
Browse files Browse the repository at this point in the history
  • Loading branch information
AshimeeAlt committed Nov 6, 2024
1 parent 29f1ff7 commit c5eda94
Showing 1 changed file with 77 additions and 5 deletions.
82 changes: 77 additions & 5 deletions src/engine/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ class Blocks {
*/
this._scripts = [];

/**
* All boxes in the workspace.
* Metadata about existing boxes.
* @type {Object.<string, Object>}
*/
this._boxes = {};

/**
* Runtime Cache
* @type {{inputs: {}, procedureParamNames: {}, procedureDefinitions: {}}}
Expand Down Expand Up @@ -88,7 +95,7 @@ class Blocks {
* @type {object.<string, object>}
*/
compiledScripts: {},

/**
* tw: A cache of procedure code opcodes to a parsed intermediate representation
* @type {object.<string, object>}
Expand Down Expand Up @@ -175,6 +182,15 @@ class Blocks {
return this._scripts;
}

/**
* Provide a object with box data for the requested box ID.
* @param {!string} boxId ID of the box.
* @return {?object} Metadata about this box.
*/
getBox (boxId) {
return this._boxes[boxId];
}

/**
* Get the next block for a particular block
* @param {?string} id ID of block to get the next block for
Expand Down Expand Up @@ -386,6 +402,7 @@ class Blocks {
const newBlocks = new Blocks(this.runtime, this.forceNoGlow);
newBlocks._blocks = Clone.simple(this._blocks);
newBlocks._scripts = Clone.simple(this._scripts);
newBlocks._boxes = Clone.simple(this._boxes);
return newBlocks;
}
// ---------------------------------------------------------------------
Expand Down Expand Up @@ -498,7 +515,7 @@ class Blocks {
}
}
stage.createVariable(e.varId, e.varName, e.varType, e.isCloud);

this.runtime.addPendingMonitor(e.varId);

// TODO same as above, this should probably be batched
Expand Down Expand Up @@ -648,6 +665,35 @@ class Blocks {
}
}

/**
* Block management: create a box from a `createbox` event.
* @param {!object} box Blockly create event to be processed.
*/
createBox (box) {
if (
Object.prototype.hasOwnProperty.call(this._boxes, box.id) ||
!Object.prototype.hasOwnProperty.call(this._blocks, box.scripts[0])
) {
return;
}
// Create new box.
this._boxes[box.id] = box;
this.emitProjectChanged();
}

/**
* Util function to delete a box and its blocks.
* @param {!string} boxId The box to delete.
*/
deleteBox (boxId) {
if (!this.getBox(boxId)) return;
for (const block of this.getBox(boxId).blocks) {
this.deleteBlock(block);
}
delete this._boxes[boxId];
this.emitProjectChanged();
}

/**
* Block management: create blocks and scripts from a `create` event
* @param {!object} block Blockly create event to be processed
Expand All @@ -660,6 +706,9 @@ class Blocks {
}
// Create new block.
this._blocks[block.id] = block;
if (block.box && this.getBox(block.box)) {
this.getBox(block.box).blocks.push(block.id);
}
// Push block id to scripts array.
// Blocks are added as a top-level stack if they are marked as a top-block
// (if they were top-level XML in the event).
Expand Down Expand Up @@ -939,6 +988,13 @@ class Blocks {
return;
}

// Remove the block from its box
if (block.box && this.getBox(block.box)) {
const box = this.getBox(block.box);
const index = box.blocks.indexOf(block.id);
box.blocks.splice(index, index === -1 ? 0 : 1);
}

// Delete children
if (block.next !== null) {
this.deleteBlock(block.next);
Expand Down Expand Up @@ -1007,6 +1063,7 @@ class Blocks {
*/
deleteAllBlocks () {
const blockIds = Object.keys(this._blocks);
this._boxes = {};
blockIds.forEach(blockId => this.deleteBlock(blockId));
}

Expand Down Expand Up @@ -1253,6 +1310,7 @@ class Blocks {
id="${xmlEscape(block.id)}"
type="${xmlEscape(block.opcode)}"
${block.topLevel ? `x="${block.x}" y="${block.y}"` : ''}
${block.box ? `box="${xmlEscape(block.box)}"` : ''}
>`;
const commentId = block.comment;
if (commentId) {
Expand Down Expand Up @@ -1388,8 +1446,13 @@ class Blocks {
const i = this._scripts.indexOf(topBlockId);
if (i > -1) return; // Already in scripts.
this._scripts.push(topBlockId);
const block = this._blocks[topBlockId];
// Update `topLevel` property on the top block.
this._blocks[topBlockId].topLevel = true;
block.topLevel = true;
// If this block is in a box then add it to the boxes scripts.
if (block.box && this.getBox(block.box)) {
this.getBox(block.box).scripts.push(topBlockId);
}
}

/**
Expand All @@ -1399,8 +1462,17 @@ class Blocks {
_deleteScript (topBlockId) {
const i = this._scripts.indexOf(topBlockId);
if (i > -1) this._scripts.splice(i, 1);
// Update `topLevel` property on the top block.
if (this._blocks[topBlockId]) this._blocks[topBlockId].topLevel = false;
const block = this._blocks[topBlockId];
if (block) {
// Update `topLevel` property on the top block.
block.topLevel = false;
// Remove the block from its box
if (block.box && this.getBox(block.box)) {
const box = this.getBox(block.box);
const index = box.scripts.indexOf(block.id);
box.scripts.splice(index, index === -1 ? 0 : 1);
}
}
}
}

Expand Down

0 comments on commit c5eda94

Please sign in to comment.