Implement pen blocks

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.
This commit is contained in:
Christopher Willis-Ford 2017-01-19 11:26:38 -08:00
parent d33af47a87
commit 369c02b5d5
7 changed files with 382 additions and 6 deletions
src/engine

View file

@ -1,3 +1,6 @@
var EventEmitter = require('events');
var util = require('util');
var Blocks = require('./blocks');
var Variable = require('../engine/variable');
var List = require('../engine/list');
@ -14,6 +17,8 @@ var uid = require('../util/uid');
* @constructor
*/
var Target = function (blocks) {
EventEmitter.call(this);
if (!blocks) {
blocks = new Blocks(this);
}
@ -39,8 +44,20 @@ var Target = function (blocks) {
* @type {Object.<string,*>}
*/
this.lists = {};
/**
* Dictionary of custom state for this target.
* This can be used to store target-specific custom state for blocks which need it.
* TODO: do we want to persist this in SB3 files?
* @type {Object.<string,*>}
*/
this._customState = {};
};
/**
* Inherit from EventEmitter
*/
util.inherits(Target, EventEmitter);
/**
* Called when the project receives a "green flag."
* @abstract
@ -112,10 +129,20 @@ Target.prototype.lookupOrCreateList = function (name) {
*/
Target.prototype.postSpriteInfo = function () {};
Target.prototype.getCustomState = function (stateId) {
return this._customState[stateId];
};
Target.prototype.setCustomState = function (stateId, newValue) {
this._customState[stateId] = newValue;
};
/**
* Call to destroy a target.
* @abstract
*/
Target.prototype.dispose = function () {};
Target.prototype.dispose = function () {
this._customState = {};
};
module.exports = Target;