Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/PenguinMod/PenguinMod-Vm
Browse files Browse the repository at this point in the history
…into develop
  • Loading branch information
Ianyourgod committed Dec 13, 2024
2 parents 4ccdb92 + e600001 commit 898cd35
Show file tree
Hide file tree
Showing 8 changed files with 699 additions and 67 deletions.
2 changes: 1 addition & 1 deletion src/blocks/scratch3_sensing.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class Scratch3SensingBlocks {
getDirectionToFrom (args) {
const dx = args.x2 - args.x1;
const dy = args.y2 - args.y1;
const direction = 90 - MathUtil.radToDeg(Math.atan2(dy, dx));
const direction = MathUtil.wrapClamp(90 - MathUtil.radToDeg(Math.atan2(dy, dx)), -179, 180);
return direction;
}

Expand Down
2 changes: 1 addition & 1 deletion src/engine/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -1692,7 +1692,7 @@ class Runtime extends EventEmitter {
blockJSON.checkboxInFlyout = true;
}
}
if (blockInfo.blockType === BlockType.LOOP || ('branchIndicator' in blockInfo || 'branchIconURI' in blockInfo)) {
if (blockInfo.blockType === BlockType.LOOP && ('branchIndicator' in blockInfo || 'branchIconURI' in blockInfo)) {
// Add icon to the bottom right of a loop block
blockJSON[`lastDummyAlign${outLineNum}`] = 'RIGHT';
blockJSON[`message${outLineNum}`] = '%1';
Expand Down
158 changes: 147 additions & 11 deletions src/extensions/jwArray/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const BlockShape = require('../../extension-support/block-shape')
const ArgumentType = require('../../extension-support/argument-type')
const Cast = require('../../util/cast')

let arrayLimit = 2 ** 32 - 1
let arrayLimit = 2 ** 32

/**
* @param {number} x
Expand All @@ -18,6 +18,10 @@ function formatNumber(x) {
}
}

function clampIndex(x) {
return Math.min(Math.max(x, 1), arrayLimit)
}

function span(text) {
let el = document.createElement('span')
el.innerHTML = text
Expand All @@ -37,14 +41,22 @@ class ArrayType {
this.array = array
}

static toArray(x) {
if (x instanceof ArrayType) return x
if (x instanceof Array) return new ArrayType(x)
if (x === "" || x === null || x === undefined) return new ArrayType()
return new ArrayType([x])
}

static display(x) {
try {
switch (typeof x) {
case "object":
if (x === null) return "null"
if (typeof x.jwArrayHandler == "function") {
return x.jwArrayHandler()
}
return "Object"
return Cast.toString(x)
case "undefined":
return "null"
case "number":
Expand Down Expand Up @@ -84,6 +96,10 @@ class ArrayType {

return root
}

get length() {
return this.array.length
}
}

const jwArray = {
Expand All @@ -103,6 +119,39 @@ const jwArray = {
class Extension {
constructor() {
vm.jwArray = jwArray
vm.runtime.registerSerializer( //this basically copies variable serialization
"jwArray",
v => v.array.map(w => {
if (typeof w == "object" && w != null && w.customId) {
return {
customType: true,
typeId: w.customId,
serialized: vm.runtime.serializers[w.customId].serialize(w)
};
}
return w
}),
v => new jwArray.Type(v.map(w => {
if (typeof w == "object" && w != null && w.customType) {
return vm.runtime.serializers[w.typeId].deserialize(w.serialized)
}
return w
}))
);

//patch square shape
if (ScratchBlocks !== undefined) {
ScratchBlocks.BlockSvg.INPUT_SHAPE_SQUARE =
ScratchBlocks.BlockSvg.TOP_LEFT_CORNER_START +
ScratchBlocks.BlockSvg.TOP_LEFT_CORNER +
' h ' + (4 * ScratchBlocks.BlockSvg.GRID_UNIT - 2 * ScratchBlocks.BlockSvg.CORNER_RADIUS) +
ScratchBlocks.BlockSvg.TOP_RIGHT_CORNER +
' v ' + (8 * ScratchBlocks.BlockSvg.GRID_UNIT - 2 * ScratchBlocks.BlockSvg.CORNER_RADIUS) +
ScratchBlocks.BlockSvg.BOTTOM_RIGHT_CORNER +
' h ' + (-4 * ScratchBlocks.BlockSvg.GRID_UNIT + 2 * ScratchBlocks.BlockSvg.CORNER_RADIUS) +
ScratchBlocks.BlockSvg.BOTTOM_LEFT_CORNER +
' z';
}
}

getInfo() {
Expand All @@ -127,10 +176,21 @@ class Extension {
},
...jwArray.Block
},
{
opcode: 'fromList',
text: 'array from list [LIST]',
arguments: {
LIST: {
menu: "list"
}
},
hideFromPalette: true, //doesn't work for some reason
...jwArray.Block
},
"---",
{
opcode: 'get',
text: 'get [ARRAY] at [INDEX]',
text: 'get [INDEX] in [ARRAY]',
blockType: BlockType.REPORTER,
arguments: {
ARRAY: jwArray.Argument,
Expand All @@ -140,10 +200,30 @@ class Extension {
}
}
},
{
opcode: 'has',
text: '[ARRAY] has [VALUE]',
blockType: BlockType.BOOLEAN,
arguments: {
ARRAY: jwArray.Argument,
VALUE: {
type: ArgumentType.STRING,
exemptFromNormalization: true
}
}
},
{
opcode: 'length',
text: 'length of [ARRAY]',
blockType: BlockType.REPORTER,
arguments: {
ARRAY: jwArray.Argument
}
},
"---",
{
opcode: 'set',
text: 'set [ARRAY] at [INDEX] to [VALUE]',
text: 'set [INDEX] in [ARRAY] to [VALUE]',
arguments: {
ARRAY: jwArray.Argument,
INDEX: {
Expand All @@ -152,32 +232,88 @@ class Extension {
},
VALUE: {
type: ArgumentType.STRING,
defaultValue: "foo"
defaultValue: "foo",
exemptFromNormalization: true
}
},
...jwArray.Block
},
{
opcode: 'append',
text: 'append [VALUE] to [ARRAY]',
arguments: {
ARRAY: jwArray.Argument,
VALUE: {
type: ArgumentType.STRING,
defaultValue: "foo",
exemptFromNormalization: true
}
},
...jwArray.Block
}
]
],
menus: {
list: {
acceptReporters: false,
items: "getLists",
},
}
};
}

getLists() {
const globalLists = Object.values(vm.runtime.getTargetForStage().variables)
.filter((x) => x.type == "list");
const localLists = Object.values(vm.editingTarget.variables)
.filter((x) => x.type == "list");
const uniqueLists = [...new Set([...globalLists, ...localLists])];
if (uniqueLists.length === 0) return [{ text: "", value: "" }];
return uniqueLists.map((v) => ({ text: v.name, value: new jwArray.Type(v.value) }));
}

blank() {
return new jwArray.Type()
}

blankLength({LENGTH}) {
LENGTH = Cast.toNumber(LENGTH)
LENGTH = Math.min(Math.max(LENGTH, 1), arrayLimit)
LENGTH = clampIndex(Cast.toNumber(LENGTH))

return new jwArray.Type(Array(LENGTH))
return new jwArray.Type(Array(LENGTH).fill(undefined))
}

fromList({LIST}) {
return jwArray.Type.toArray(LIST)
}

get({ARRAY, INDEX}) {
return ARRAY.array[Cast.toNumber(INDEX)]
ARRAY = jwArray.Type.toArray(ARRAY)

return ARRAY.array[Cast.toNumber(INDEX)-1] || ""
}

has({ARRAY, VALUE}) {
ARRAY = jwArray.Type.toArray(ARRAY)

return ARRAY.array.includes(VALUE)
}

length({ARRAY}) {
ARRAY = jwArray.Type.toArray(ARRAY)

return ARRAY.length
}

set({ARRAY, INDEX, VALUE}) {
ARRAY.array[Cast.toNumber(INDEX)] = VALUE
ARRAY = jwArray.Type.toArray(ARRAY)

ARRAY.array[clampIndex(Cast.toNumber(INDEX))-1] = VALUE
return ARRAY
}

append({ARRAY, VALUE}) {
ARRAY = jwArray.Type.toArray(ARRAY)

ARRAY.array.push(VALUE)
return ARRAY
}
}
Expand Down
Loading

0 comments on commit 898cd35

Please sign in to comment.