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

Preload Sprite Lab background images #29358

Merged
merged 2 commits into from Jun 27, 2019
Merged

Preload Sprite Lab background images #29358

merged 2 commits into from Jun 27, 2019

Conversation

ajpal
Copy link
Contributor

@ajpal ajpal commented Jun 25, 2019

This change preloads the background images in the dropdown for the setBackgroundImage block in the preload phase of the P5 lifecycle. This change is necessary to show the background images in preview mode.

Old:
image

New:
image

Currently, the name field in backgrounds.json is unused, since the setBackgroundImage block uses explicit URLs (the legacyParam field in backgrounds.json). Ideally, over the longterm, we can make a plan to migrate the block XML for that block, and then use the name field instead of legacyParam

return new Promise(resolve => {
backgrounds.forEach(background => {
this.p5.loadImage(background.sourceUrl, image => {
this.p5._predefinedBackgrounds[background.legacyParam] = image;
Copy link
Contributor

@jmkulwik jmkulwik Jun 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this still work if there is no legacyParam? i.e. if we add more background images

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, worth adding a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we add more background images, then we will need additional logic to key by name in those cases. I think it's fine to hold off on adding that until we actually add new images, though.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

let backgroundImage = this.loadImage(img);
backgroundImage.name = img;
spriteUtils.background = backgroundImage;
if (this._predefinedBackgrounds[img]) {
Copy link
Contributor

@jmkulwik jmkulwik Jun 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two questions:

  1. Should there be an else here?
  2. Do we use loadImage anywhere anymore?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. No, I think we decided yesterday not to try to live-load the image if we don't have it preloaded. That case should never come up, so it would basically just be untested/unused code. The image parameter can only be set by the values in the dropdown, so as long as we have all of those images preloaded, we will never reach an else case.
  2. loadImage is only called during the preload stage now.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Contributor

@jmkulwik jmkulwik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ajpal ajpal merged commit d015a62 into staging Jun 27, 2019
@ajpal ajpal deleted the jun25-backgrounds branch June 27, 2019 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants