[Sprite Lab] add behaviorPicker block and field type #50702
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context and Request
In Sprite Lab, students have to open the toolbox multiple times to set up a behavior-related command. It is only possible to switch behaviors by deleting a block and dragging out a new one.
The curriculum team would like to streamline this process by using a pre-populated dropdown, similar to the experience in Poetry:
We cannot simply copy this block into the GameLabJr pool. This is because the behavior list in Poetry is hard-coded. (In Poetry, students cannot edit/add/delete behaviors.)
Because Sprite Lab has a higher ceiling, the desired block requires a new field that can dynamically look for all behaviors and auto-populate the dropdown. It also needs to be linked up to the modal function editor.
How it works
The new block works (and generates JavaScript code) just like the existing behavior blocks.
In a typical use case, the dropdown will be populated with options based on all behaviors that defined in the main workspace, including shared behaviors and user-created behaviors.
bp1-standard.use.mp4
If the modal function editor is enabled, the block also includes an edit button that will open the BehaviorEditor with the selected behavior option. There is also a "Create new behavior" option that will open the editor with a new blank behavior that is immediately added to the dropdown and selected using the name chosen by the user.
bp2-modal.mp4
If the modal editor is disabled, we do not show the button or the option to create a new behavior. It is not possible to create new behaviors using this block under this scenario, nor is it possible through other means.
If there are no behaviors currently defined on the workspace we will either show the option to create one (modal editor enabled) or an option that simply generated undefined (no modal). This is possible within a level; it's also the normal expectation when viewing the block in a levelbuilder context, such as on the edit and index pages for the GameLabJr block pool.
Blocks in a read-only workspace such as the level instructions will not find any behaviors to populate the list, however a levelbuilder can still list whatever behavior they want by specifying it directly in the block XML. This is similar to the process of customizing the default option in start blocks or a toolbox.
While workspace-defined behaviors are currently unsupported in Sprite Lab (or Poetry) using Google Blockly, the new block still renders and generates code correctly. Shown here in a Poetry project:
To work correctly with the modal Function Editor, this feature also requires an update to our CDO Blockly repo.
When the name of a behavior is changed in the modal editor, we need to find each
gamelab_behaviorPicker
block and detect whether it is selected the current behavior. For user-created behaviors, the behavior name and id are interchangeable and therefore we must update the selected option with the new id. For shared behaviors, the ids are locked. Updating the selected option to use the current id ensures that the user will see the edited behavior name as the dropdown option label. Other blocks should not be touched.bp3-rename.mp4
If a user-created behavior is deleted, we detected blocks that are selecting that behavior and update them to show the undefined (Create new behavior) option.
bp4-delete.mp4
Links
Jira: https://codedotorg.atlassian.net/browse/SL-643
Testing story
Deployment strategy
This PR requires a change to the Blockly repo. It should be merged alongside a bump to the Blockly version in this repo.
PR Checklist: