-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Make "switch costume" and "switch backdrop" blocks compatible with 2.0 #1517
Make "switch costume" and "switch backdrop" blocks compatible with 2.0 #1517
Conversation
Regarding the unit tests added, would it be a good idea to split them into a separate file -- and, since the backdrop and costume tests are quite similar, use a loop/function for the common bits? |
src/blocks/scratch3_looks.js
Outdated
const numCostumes = target.getCostumes().length; | ||
} else if (requestedCostume === 'previous costume') { | ||
target.setCostume(target.currentCostume - 1); | ||
} else if (!isNaN(requestedCostume) && |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
src/blocks/scratch3_looks.js
Outdated
} else if (requestedCostume === 'previous costume') { | ||
target.setCostume(target.currentCostume - 1); | ||
} else if (!isNaN(requestedCostume) && | ||
(typeof requestedCostume !== 'string' || /\d/g.test(requestedCostume))) { |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
src/blocks/scratch3_looks.js
Outdated
} else if (requestedBackdrop === 'next backdrop') { | ||
stage.setCostume(stage.currentCostume + 1); | ||
} else if (requestedBackdrop === 'previous backdrop') { | ||
stage.setCostume(stage.currentCostume - 1); |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
test/unit/blocks_looks.js
Outdated
// Non-existant costumes do nothing | ||
t.strictEqual(testCostume(['a', 'b', 'c', 'd'], 'e', 3), 3); | ||
|
||
// Difference between string and numeric arguments |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
test/unit/blocks_looks.js
Outdated
* @param {number} [currentCostume=1] The 1-indexed default costume for the sprite to start at. | ||
* @return {number} The 1-indexed costume index on which the sprite lands. | ||
*/ | ||
const testCostume = (costumes, arg, currentCostume = 1) => { |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joker314 Thanks for making this pull request and for detailing the Scratch 2.0 behavior in the comment on #1490. I apologize for the delay on this review.
I am requesting some minor changes/code cleanup, and would also like to request adding more comments in the code to illustrate what kinds of cases you're trying to address in which parts of the code.
Thanks for adding the thorough unit tests, they're awesome!
I added a comment about maybe making the switch backdrop
behavior similar to switch costume
in how it handles backdrops named 'previous backdrop' and 'next backdrop' but I think it's also okay to file that as a follow-up separate issue since what you have now is consistent with Scratch 2.0.
Move the helper functions `testCostume` and `testBackdrop` outside of their test definitions. In addition, define the latter in terms of the former.
The `switch costume` block accepts special values like "next costume" and "previous costume". If you create a costume with these names, these take priority over the special values. However, the `switch backdrop` block keeps these special values for values like "next backdrop", "previous backdrop", "random backdrop". It is impossible to navigate to such a backdrop by name via block. This commit also modifies tests to allow for this. BREAKING CHANGE: specially-named backdrops can now be navigated
Thank you very much for the review @kchadha. I've addressed your thoughtful feedback, and have introduced the breaking change which makes backdrop special arguments be handled in a similar way to costumes. This pull request is ready for re-review. Here's a base for what one might want to include in the Compatibility wiki page, if it's useful:
|
We ensure that all code whose purpose may be confusing to grasp is commented; and we remove information that is no longer required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joker314 Thank you for making the requested changes! This PR looks good to go.
Resolves
Resolves #1490
Proposed Changes
switch backdrop to
block is (EDIT: almost) compatible with 2.0 behaviourswitch costume to
block is compatible with 2.0 behaviourSee 2.0 behaviour description.
Reason for Changes
Making sure that 2.0 projects still work in 3.0.
I placed the "random backdrop" feature as less important than a costume name. This means that if a 2.0 project already had a backdrop with the name "random backdrop", references to it by name will still hold. Was this a good idea?
This is different to how "previous backdrop" and "next backdrop" behave -- they can't be overridden (per 2.0); however this is the same behaviour as "previous costume" and "next costume", which can.
Test Coverage
Assertions which failed prior to changes
Costumes
switch costume to NaN
(e.g.0/0
) should flawlessly switch to the first costume.switch costume to Infinity
(e.g.1/0
) should flawlessly switch to the first costume.switch costume to -Infinity
(e.g.-1/0
) should flawlessly switch to the first costume.switch costume to previous backdrop
switch costume to next backdrop
should do nothingswitch costume to <1 = 0>
should switch to the costume named "false" if it exists Switch costume/backdrop to boolean value switches to costume nr. 0 or 1 #1490Backdrops
switch backdrop to [previous backdrop]
decrements the backdrop even if there's a backdrop with that nameswitch backdrop to [next backdrop]
increments the backdrop even if there's a backdrop with that nameswitch backdrop to NaN
(e.g.0/0
) should flawlessly switch to the first backdrop.switch backdrop to Infinity
(e.g.1/0
) should flawlessly switch to the first backdrop.switch backdrop to -Infinity
(e.g.-1/0
) should flawlessly switch to the first backdrop.