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

Implement pen blocks #384

Merged
merged 6 commits into from Jan 20, 2017
Merged

Implement pen blocks #384

merged 6 commits into from Jan 20, 2017

Conversation

cwillisf
Copy link
Contributor

@cwillisf cwillisf commented Jan 19, 2017

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 emitter
  • RenderedTarget now emits an event when it moves
  • Target can now store additional arbitrary 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.

Reason for Changes

Implementing pen blocks brings us closer to Scratch 2.0 compatibility.

Test Coverage

All newly-added util functions are covered:

  • toRbgColorObject from util/cast.js is covered in test/unit/util_cast.js
  • hsvToRgb, rgbToHsv, and mixRgb from util/color.js are covered in test/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 are clear, pen down, and stamp.

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.
@cwillisf cwillisf added this to the February 16 milestone Jan 19, 2017
@cwillisf cwillisf self-assigned this Jan 19, 2017
g = p;
b = q;
break;
}

This comment was marked as abuse.

This comment was marked as abuse.

Christopher Willis-Ford added 2 commits January 19, 2017 15:24
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
Copy link
Contributor

@thisandagain thisandagain left a 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.runtime = runtime;

this._penSkinId = -1;

This comment was marked as abuse.

* @private
*/
Scratch3PenBlocks.prototype._clampPenSize = function (requestedSize) {
return MathUtil.clamp(requestedSize, 1, 255);

This comment was marked as abuse.

return MathUtil.clamp(requestedSize, 1, 255);
};

Scratch3PenBlocks.prototype._getPenLayerID = function () {

This comment was marked as abuse.

Scratch3PenBlocks.prototype._getPenState = function (target) {
var penState = target.getCustomState(stateKey);
if (!penState) {
penState = JSON.parse(JSON.stringify(defaultPenState));

This comment was marked as abuse.


Scratch3PenBlocks.prototype.clear = function () {
var penSkinId = this._getPenLayerID();
this.runtime.renderer.penClear(penSkinId);

This comment was marked as abuse.

@@ -112,10 +129,20 @@ Target.prototype.lookupOrCreateList = function (name) {
*/
Target.prototype.postSpriteInfo = function () {};

Target.prototype.getCustomState = function (stateId) {

This comment was marked as abuse.

return this._customState[stateId];
};

Target.prototype.setCustomState = function (stateId, newValue) {

This comment was marked as abuse.

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.
@cwillisf cwillisf merged commit bdabe2e into scratchfoundation:develop Jan 20, 2017
@cwillisf cwillisf deleted the pen branch January 20, 2017 21:22
@SillyInventor
Copy link
Contributor

For pen clear to work you need to set the skin to dirty:
this.runtime.renderer._allSkins[penSkinId]._canvasDirty=true;

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.

Pen support
5 participants