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

fromSb3: Peculiarity with pen_menu_colorParam as shadow input (etc?) #144

Closed
towerofnix opened this issue May 30, 2024 · 3 comments · Fixed by #145
Closed

fromSb3: Peculiarity with pen_menu_colorParam as shadow input (etc?) #144

towerofnix opened this issue May 30, 2024 · 3 comments · Fixed by #145
Assignees
Labels
bug Something isn't working compatibility Mismatch or currently unsupported language behavior fmt: SB3 Pertains to SB3 format (Scratch 3.0)

Comments

@towerofnix
Copy link
Member

Investigating #141.

The basic processing for shadow inputs is to copy the inputs and fields of a shadow input into the parent block:

// In most cases, just copy the shadow block's fields and inputs
// into its parent
result = {
  ...result,
  ...translateInputs(blocks[value].inputs),
  ...translateFields(blocks[value].fields, blocks[value].opcode)
};

This is usually good enough, but there can be discrepancies between the names of inputs/fields on a shadow input and inputs/fields on the parent block. These need to be handled manually and aren't detected by TypeScript.

One such came up with pen_menu_colorParam, which has a strangely camelCased field:

export const fieldTypeMap = {
  ...
  [OpCode.pen_menu_colorParam]: { colorParam: "penColorParam" },
}

pen_menu_colorParam is a CONSTANT_CASED input on two blocks:

export const inputPrimitiveOrShadowMap = {
  ...
  [OpCode.pen_changePenColorParamBy]: { COLOR_PARAM: OpCode.pen_menu_colorParam, VALUE: BIS.MATH_NUM_PRIMITIVE },
  [OpCode.pen_setPenColorParamTo]: { COLOR_PARAM: OpCode.pen_menu_colorParam, VALUE: BIS.MATH_NUM_PRIMITIVE },
};

This results in COLOR_PARAM being undefined (possibly only in certain project.json scenarios), which breaks output formats that expect it to be there. Also a surprise colorParam input existing, of course, which isn't referenced by any output formats (it's not part of the sb-edit intermediate representation).

It would be nice if TypeScript could flag cases like this automatically. Right now inputPrimitiveOrShadowMap isn't typed at all, so it looks like some work could be done here.

@towerofnix towerofnix added bug Something isn't working compatibility Mismatch or currently unsupported language behavior fmt: SB3 Pertains to SB3 format (Scratch 3.0) labels May 30, 2024
@towerofnix towerofnix self-assigned this May 30, 2024
@towerofnix
Copy link
Member Author

It turns out pen_menu_colorParam is the only menu (currently implemented by sb-edit) which has this quirk.

@towerofnix
Copy link
Member Author

Tested with sb-edit-playground and this project, no behavior change is needed for toSb3. However, toSb3, like toLeopard, requires the COLOR_INPUT input on Block to be present, and crashes without it. So this fix does affect both output formats.

Sample output with fixed fromSb3 and unmodified toSb3:

"$cZ9-1*N^|Dvl=,7gQ5(-COLOR_PARAM": {
	"opcode": "pen_menu_colorParam",
	"next": null,
	"parent": "$cZ9-1*N^|Dvl=,7gQ5(",
	"fields": {
		"colorParam": ["color"]
	},
	"inputs": {
	},
	"shadow": true,
	"topLevel": false
},

"$cZ9-1*N^|Dvl=,7gQ5(": {
	"opcode": "pen_setPenColorParamTo",
	"parent": "wJIc)f-`T0lDk!j+[VR@",
	"next": ",e]CCiibG}7f}6ZHo3G|",
	"topLevel": false,
	"inputs": {
		"COLOR_PARAM": [1, "$cZ9-1*N^|Dvl=,7gQ5(-COLOR_PARAM"],
		"VALUE": [1, [4, "50"]]
	},
	"fields": {
	},
	"shadow": false
},

@towerofnix
Copy link
Member Author

Also, Leopard's implementation of these component-based pen color blocks is working correctly! 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working compatibility Mismatch or currently unsupported language behavior fmt: SB3 Pertains to SB3 format (Scratch 3.0)
Projects
None yet
1 participant