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
Implement pen blocks #384
Implement pen blocks #384
Conversation
These blocks implement pen features as found in Scratch 2.0 Supporting changes include: - `Target` is now an event emitter - `RenderedTarget` now emits an event when it moves - `Target` can now store arbitrary "extra" data, called "custom state" in the code, using a `Target`'s `setCustomState` and `getCustomState` methods. This is used to store per-target pen state without requiring `Target` or `RenderedTarget` to know anything about the pen. - `Cast` can now cast to an RGB color object. - `Color` now has functions to convert between RGB and HSV, constants for for black & white, and a `mixRgb` function to lerp between two colors.
g = p; | ||
b = q; | ||
break; | ||
} |
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.
Also, fix a math error in `Color.rgbToHsv`. Newly covered functions: - `toRbgColorObject` from `util/cast.js` - `hsvToRgb` from `util/colors.js` - `rgbToHsv` from `util/colors.js` - `mixRgb` from `util/colors.js`
See `pen.sb2` for details
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.
This looks great overall. Thanks for expanding the test coverage. Some fairly minor notes for the most part.
// Place the pen layer in front of the backdrop but behind everything else | ||
// We should probably handle this somewhere else... somewhere central that knows about pen, backdrop, video, etc. | ||
// Maybe it should be in the GUI? | ||
var penOrder = 1; |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
*/ | ||
this.runtime = runtime; | ||
|
||
this._penSkinId = -1; |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
* @private | ||
*/ | ||
Scratch3PenBlocks.prototype._clampPenSize = function (requestedSize) { | ||
return MathUtil.clamp(requestedSize, 1, 255); |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
return MathUtil.clamp(requestedSize, 1, 255); | ||
}; | ||
|
||
Scratch3PenBlocks.prototype._getPenLayerID = function () { |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
Scratch3PenBlocks.prototype._getPenState = function (target) { | ||
var penState = target.getCustomState(stateKey); | ||
if (!penState) { | ||
penState = JSON.parse(JSON.stringify(defaultPenState)); |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
|
||
Scratch3PenBlocks.prototype.clear = function () { | ||
var penSkinId = this._getPenLayerID(); | ||
this.runtime.renderer.penClear(penSkinId); |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
@@ -112,10 +129,20 @@ Target.prototype.lookupOrCreateList = function (name) { | |||
*/ | |||
Target.prototype.postSpriteInfo = function () {}; | |||
|
|||
Target.prototype.getCustomState = function (stateId) { |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
return this._customState[stateId]; | ||
}; | ||
|
||
Target.prototype.setCustomState = function (stateId, newValue) { |
This comment was marked as abuse.
This comment was marked as abuse.
Sorry, something went wrong.
Changes include: - Added missing JSDoc for items in `scratch3_pen.js` and `target.js`. - Moved constants used by `Scratch3PenBlocks` into the class. - Created a constant for minimum and maximum pen size. - Added `util/clone.js` to host cloning functionality. - Pen blocks now check for the renderer before trying to use it. - The pen integration test covers all blocks, though `clear`, `stamp`, and `pen down` will skip some of their functionality when there is no renderer.
For pen clear to work you need to set the skin to dirty: |
Resolves
Along with scratchfoundation/scratch-render#72, this resolves scratchfoundation/scratch-render#6
Proposed Changes
These blocks implement pen features as found in Scratch 2.0
Supporting changes include:
Target
is now an event emitterRenderedTarget
now emits an event when it movesTarget
can now store additional arbitrary data, called "custom state" in the code, using aTarget
'ssetCustomState
andgetCustomState
methods. This is used to store per-target pen state without requiringTarget
orRenderedTarget
to know anything about the pen.Cast
can now cast to an RGB color object.Color
now has functions to convert between RGB and HSV, constants for for black & white, and amixRgb
function to lerp between two colors.Reason for Changes
Implementing pen blocks brings us closer to Scratch 2.0 compatibility.
Test Coverage
All newly-added
util
functions are covered:toRbgColorObject
fromutil/cast.js
is covered intest/unit/util_cast.js
hsvToRgb
,rgbToHsv
, andmixRgb
fromutil/color.js
are covered intest/unit/util_color.js
All pen blocks are covered by the new
pen.js
integration test. Unfortunately our current integration test setup makes it very difficult (impossible?) to hook to the renderer, so the blocks which actually draw something will skip parts of their functionality during this test. The three affected pen blocks areclear
,pen down
, andstamp
.