2017-09-26 23:34:07 -04:00
|
|
|
const ArgumentType = require('../extension-support/argument-type');
|
|
|
|
const BlockType = require('../extension-support/block-type');
|
2017-04-17 15:10:04 -04:00
|
|
|
const Cast = require('../util/cast');
|
|
|
|
const Clone = require('../util/clone');
|
|
|
|
const Color = require('../util/color');
|
|
|
|
const MathUtil = require('../util/math-util');
|
|
|
|
const RenderedTarget = require('../sprites/rendered-target');
|
2017-10-18 11:33:25 -04:00
|
|
|
const log = require('../util/log');
|
2017-10-12 10:43:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum for pen color parameters.
|
|
|
|
* @readonly
|
|
|
|
* @enum {string}
|
|
|
|
*/
|
|
|
|
const ColorParam = {
|
2017-10-17 12:22:53 -04:00
|
|
|
COLOR: 'color',
|
2017-10-12 10:43:20 -04:00
|
|
|
SATURATION: 'saturation',
|
|
|
|
BRIGHTNESS: 'brightness',
|
|
|
|
TRANSPARENCY: 'transparency'
|
|
|
|
};
|
|
|
|
|
2017-01-19 14:26:38 -05:00
|
|
|
/**
|
|
|
|
* @typedef {object} PenState - the pen state associated with a particular target.
|
|
|
|
* @property {Boolean} penDown - tracks whether the pen should draw for this target.
|
2017-10-17 12:22:53 -04:00
|
|
|
* @property {number} color - the current color (hue) of the pen.
|
2017-01-19 14:26:38 -05:00
|
|
|
* @property {PenAttributes} penAttributes - cached pen attributes for the renderer. This is the authoritative value for
|
|
|
|
* diameter but not for pen color.
|
|
|
|
*/
|
|
|
|
|
2017-01-20 14:26:18 -05:00
|
|
|
/**
|
|
|
|
* Host for the Pen-related blocks in Scratch 3.0
|
|
|
|
* @param {Runtime} runtime - the runtime instantiating this block package.
|
|
|
|
* @constructor
|
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
class Scratch3PenBlocks {
|
|
|
|
constructor (runtime) {
|
|
|
|
/**
|
|
|
|
* The runtime instantiating this block package.
|
|
|
|
* @type {Runtime}
|
|
|
|
*/
|
|
|
|
this.runtime = runtime;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the renderer Drawable corresponding to the pen layer.
|
|
|
|
* @type {int}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._penDrawableId = -1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ID of the renderer Skin corresponding to the pen layer.
|
|
|
|
* @type {int}
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
this._penSkinId = -1;
|
|
|
|
|
2017-06-16 17:22:51 -04:00
|
|
|
this._onTargetCreated = this._onTargetCreated.bind(this);
|
2017-04-17 19:42:48 -04:00
|
|
|
this._onTargetMoved = this._onTargetMoved.bind(this);
|
2017-06-16 17:22:51 -04:00
|
|
|
|
|
|
|
runtime.on('targetWasCreated', this._onTargetCreated);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
|
|
|
|
2017-01-20 14:26:18 -05:00
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* The default pen state, to be used when a target has no existing pen state.
|
|
|
|
* @type {PenState}
|
2017-01-20 14:26:18 -05:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get DEFAULT_PEN_STATE () {
|
|
|
|
return {
|
|
|
|
penDown: false,
|
2017-10-17 23:10:58 -04:00
|
|
|
color: 66.66,
|
2017-10-12 10:43:20 -04:00
|
|
|
saturation: 100,
|
|
|
|
brightness: 100,
|
2017-09-05 18:00:34 -04:00
|
|
|
transparency: 0,
|
2017-10-31 17:24:12 -04:00
|
|
|
_shade: 50, // Used only for legacy `change shade by` blocks
|
2017-04-17 19:42:48 -04:00
|
|
|
penAttributes: {
|
|
|
|
color4f: [0, 0, 1, 1],
|
|
|
|
diameter: 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2017-01-20 14:26:18 -05:00
|
|
|
|
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* 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?
|
2017-01-20 14:26:18 -05:00
|
|
|
* @type {int}
|
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get PEN_ORDER () {
|
|
|
|
return 1;
|
|
|
|
}
|
2017-01-20 14:26:18 -05:00
|
|
|
|
|
|
|
/**
|
2017-04-17 19:42:48 -04:00
|
|
|
* The minimum and maximum allowed pen size.
|
|
|
|
* @type {{min: number, max: number}}
|
2017-01-20 14:26:18 -05:00
|
|
|
*/
|
2017-04-17 19:42:48 -04:00
|
|
|
static get PEN_SIZE_RANGE () {
|
|
|
|
return {min: 1, max: 255};
|
|
|
|
}
|
2017-01-20 14:26:18 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* The key to load & store a target's pen-related state.
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
static get STATE_KEY () {
|
|
|
|
return 'Scratch.pen';
|
|
|
|
}
|
2017-01-20 14:26:18 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Clamp a pen size value to the range allowed by the pen.
|
|
|
|
* @param {number} requestedSize - the requested pen size.
|
|
|
|
* @returns {number} the clamped size.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_clampPenSize (requestedSize) {
|
|
|
|
return MathUtil.clamp(
|
|
|
|
requestedSize,
|
|
|
|
Scratch3PenBlocks.PEN_SIZE_RANGE.min,
|
|
|
|
Scratch3PenBlocks.PEN_SIZE_RANGE.max
|
|
|
|
);
|
2017-01-19 14:26:38 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Retrieve the ID of the renderer "Skin" corresponding to the pen layer. If
|
|
|
|
* the pen Skin doesn't yet exist, create it.
|
|
|
|
* @returns {int} the Skin ID of the pen layer, or -1 on failure.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getPenLayerID () {
|
|
|
|
if (this._penSkinId < 0 && this.runtime.renderer) {
|
|
|
|
this._penSkinId = this.runtime.renderer.createPenSkin();
|
|
|
|
this._penDrawableId = this.runtime.renderer.createDrawable();
|
|
|
|
this.runtime.renderer.setDrawableOrder(this._penDrawableId, Scratch3PenBlocks.PEN_ORDER);
|
|
|
|
this.runtime.renderer.updateDrawableProperties(this._penDrawableId, {skinId: this._penSkinId});
|
|
|
|
}
|
|
|
|
return this._penSkinId;
|
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* @param {Target} target - collect pen state for this target. Probably, but not necessarily, a RenderedTarget.
|
|
|
|
* @returns {PenState} the mutable pen state associated with that target. This will be created if necessary.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_getPenState (target) {
|
|
|
|
let penState = target.getCustomState(Scratch3PenBlocks.STATE_KEY);
|
|
|
|
if (!penState) {
|
|
|
|
penState = Clone.simple(Scratch3PenBlocks.DEFAULT_PEN_STATE);
|
|
|
|
target.setCustomState(Scratch3PenBlocks.STATE_KEY, penState);
|
|
|
|
}
|
|
|
|
return penState;
|
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-06-16 17:22:51 -04:00
|
|
|
/**
|
|
|
|
* When a pen-using Target is cloned, clone the pen state.
|
|
|
|
* @param {Target} newTarget - the newly created target.
|
|
|
|
* @param {Target} [sourceTarget] - the target used as a source for the new clone, if any.
|
|
|
|
* @listens Runtime#event:targetWasCreated
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onTargetCreated (newTarget, sourceTarget) {
|
|
|
|
if (sourceTarget) {
|
|
|
|
const penState = sourceTarget.getCustomState(Scratch3PenBlocks.STATE_KEY);
|
|
|
|
if (penState) {
|
|
|
|
newTarget.setCustomState(Scratch3PenBlocks.STATE_KEY, Clone.simple(penState));
|
|
|
|
if (penState.penDown) {
|
|
|
|
newTarget.addListener(RenderedTarget.EVENT_TARGET_MOVED, this._onTargetMoved);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* Handle a target which has moved. This only fires when the pen is down.
|
|
|
|
* @param {RenderedTarget} target - the target which has moved.
|
|
|
|
* @param {number} oldX - the previous X position.
|
|
|
|
* @param {number} oldY - the previous Y position.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_onTargetMoved (target, oldX, oldY) {
|
|
|
|
const penSkinId = this._getPenLayerID();
|
|
|
|
if (penSkinId >= 0) {
|
|
|
|
const penState = this._getPenState(target);
|
|
|
|
this.runtime.renderer.penLine(penSkinId, penState.penAttributes, oldX, oldY, target.x, target.y);
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-10-18 11:48:45 -04:00
|
|
|
/**
|
|
|
|
* Wrap a color input into the range (0,100).
|
|
|
|
* @param {number} value - the value to be wrapped.
|
|
|
|
* @returns {number} the wrapped value.
|
|
|
|
* @private
|
|
|
|
*/
|
2017-10-17 12:22:53 -04:00
|
|
|
_wrapColor (value) {
|
2017-10-12 10:43:20 -04:00
|
|
|
return MathUtil.wrapClamp(value, 0, 100);
|
|
|
|
}
|
|
|
|
|
2017-09-01 02:11:09 -04:00
|
|
|
/**
|
2017-10-12 10:43:20 -04:00
|
|
|
* Clamp a pen color parameter to the range (0,100).
|
|
|
|
* @param {number} value - the value to be clamped.
|
2017-09-05 18:00:34 -04:00
|
|
|
* @returns {number} the clamped value.
|
2017-09-01 02:11:09 -04:00
|
|
|
* @private
|
|
|
|
*/
|
2017-10-12 10:43:20 -04:00
|
|
|
_clampColorParam (value) {
|
2017-09-05 18:00:34 -04:00
|
|
|
return MathUtil.clamp(value, 0, 100);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an alpha value to a pen transparency value.
|
|
|
|
* Alpha ranges from 0 to 1, where 0 is transparent and 1 is opaque.
|
|
|
|
* Transparency ranges from 0 to 100, where 0 is opaque and 100 is transparent.
|
|
|
|
* @param {number} alpha - the input alpha value.
|
|
|
|
* @returns {number} the transparency value.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_alphaToTransparency (alpha) {
|
|
|
|
return (1.0 - alpha) * 100.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert a pen transparency value to an alpha value.
|
|
|
|
* Alpha ranges from 0 to 1, where 0 is transparent and 1 is opaque.
|
|
|
|
* Transparency ranges from 0 to 100, where 0 is opaque and 100 is transparent.
|
|
|
|
* @param {number} transparency - the input transparency value.
|
|
|
|
* @returns {number} the alpha value.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_transparencyToAlpha (transparency) {
|
|
|
|
return 1.0 - (transparency / 100.0);
|
2017-09-01 02:11:09 -04:00
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
2017-10-04 15:16:27 -04:00
|
|
|
* @returns {object} metadata for this extension and its blocks.
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2017-09-26 23:34:07 -04:00
|
|
|
getInfo () {
|
2017-04-17 19:42:48 -04:00
|
|
|
return {
|
2017-09-26 23:34:07 -04:00
|
|
|
id: 'pen',
|
|
|
|
name: 'Pen',
|
|
|
|
blocks: [
|
|
|
|
{
|
|
|
|
opcode: 'clear',
|
2017-10-12 10:37:02 -04:00
|
|
|
blockType: BlockType.COMMAND
|
2017-09-26 23:34:07 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'stamp',
|
|
|
|
blockType: BlockType.COMMAND
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'penDown',
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
text: 'pen down'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'penUp',
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
text: 'pen up'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'setPenColorToColor',
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
text: 'set pen color to [COLOR]',
|
|
|
|
arguments: {
|
|
|
|
COLOR: {
|
|
|
|
type: ArgumentType.COLOR
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-10-12 10:43:20 -04:00
|
|
|
opcode: 'changePenColorParamBy',
|
2017-09-26 23:34:07 -04:00
|
|
|
blockType: BlockType.COMMAND,
|
2017-10-12 10:43:20 -04:00
|
|
|
text: 'change pen [COLOR_PARAM] by [VALUE]',
|
2017-09-26 23:34:07 -04:00
|
|
|
arguments: {
|
2017-10-12 10:43:20 -04:00
|
|
|
COLOR_PARAM: {
|
|
|
|
type: ArgumentType.STRING,
|
|
|
|
menu: 'colorParam',
|
2017-10-17 12:22:53 -04:00
|
|
|
defaultValue: ColorParam.COLOR
|
2017-10-12 10:43:20 -04:00
|
|
|
},
|
|
|
|
VALUE: {
|
2017-09-26 23:34:07 -04:00
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 10
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-10-12 10:43:20 -04:00
|
|
|
opcode: 'setPenColorParamTo',
|
2017-09-26 23:34:07 -04:00
|
|
|
blockType: BlockType.COMMAND,
|
2017-10-12 10:43:20 -04:00
|
|
|
text: 'set pen [COLOR_PARAM] to [VALUE]',
|
2017-09-26 23:34:07 -04:00
|
|
|
arguments: {
|
2017-10-12 10:43:20 -04:00
|
|
|
COLOR_PARAM: {
|
|
|
|
type: ArgumentType.STRING,
|
|
|
|
menu: 'colorParam',
|
2017-10-17 12:22:53 -04:00
|
|
|
defaultValue: ColorParam.COLOR
|
2017-10-12 10:43:20 -04:00
|
|
|
},
|
|
|
|
VALUE: {
|
2017-09-26 23:34:07 -04:00
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 50
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'changePenSizeBy',
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
text: 'change pen size by [SIZE]',
|
|
|
|
arguments: {
|
|
|
|
SIZE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'setPenSizeTo',
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
text: 'set pen size to [SIZE]',
|
|
|
|
arguments: {
|
|
|
|
SIZE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 1
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 17:24:12 -04:00
|
|
|
},
|
|
|
|
/* Legacy blocks, should not be shown in flyout */
|
|
|
|
{
|
|
|
|
opcode: 'setPenShadeToNumber',
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
text: 'set pen shade to [SHADE]',
|
|
|
|
arguments: {
|
|
|
|
SHADE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 1
|
|
|
|
}
|
2017-11-01 11:07:26 -04:00
|
|
|
},
|
|
|
|
hideFromPalette: true
|
2017-10-31 17:24:12 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'changePenShadeBy',
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
text: 'change pen shade by [SHADE]',
|
|
|
|
arguments: {
|
|
|
|
SHADE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 1
|
|
|
|
}
|
2017-11-01 11:07:26 -04:00
|
|
|
},
|
|
|
|
hideFromPalette: true
|
2017-10-31 17:24:12 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'setPenHueToNumber',
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
text: 'set pen hue to [HUE]',
|
|
|
|
arguments: {
|
|
|
|
HUE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 1
|
|
|
|
}
|
2017-11-01 11:07:26 -04:00
|
|
|
},
|
|
|
|
hideFromPalette: true
|
2017-10-31 17:24:12 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
opcode: 'changePenHueBy',
|
|
|
|
blockType: BlockType.COMMAND,
|
|
|
|
text: 'change pen hue by [HUE]',
|
|
|
|
arguments: {
|
|
|
|
HUE: {
|
|
|
|
type: ArgumentType.NUMBER,
|
|
|
|
defaultValue: 1
|
|
|
|
}
|
2017-11-01 11:07:26 -04:00
|
|
|
},
|
|
|
|
hideFromPalette: true
|
2017-09-26 23:34:07 -04:00
|
|
|
}
|
2017-10-12 10:43:20 -04:00
|
|
|
],
|
|
|
|
menus: {
|
|
|
|
colorParam:
|
2017-10-17 12:22:53 -04:00
|
|
|
[ColorParam.COLOR, ColorParam.SATURATION,
|
2017-10-18 13:16:24 -04:00
|
|
|
ColorParam.BRIGHTNESS, ColorParam.TRANSPARENCY]
|
2017-10-12 10:43:20 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
};
|
2017-01-19 14:26:38 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* The pen "clear" block clears the pen layer's contents.
|
|
|
|
*/
|
|
|
|
clear () {
|
|
|
|
const penSkinId = this._getPenLayerID();
|
|
|
|
if (penSkinId >= 0) {
|
|
|
|
this.runtime.renderer.penClear(penSkinId);
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2017-01-20 14:26:18 -05:00
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* The pen "stamp" block stamps the current drawable's image onto the pen layer.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
stamp (args, util) {
|
|
|
|
const penSkinId = this._getPenLayerID();
|
|
|
|
if (penSkinId >= 0) {
|
|
|
|
const target = util.target;
|
|
|
|
this.runtime.renderer.penStamp(penSkinId, target.drawableID);
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* The pen "pen down" block causes the target to leave pen trails on future motion.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
penDown (args, util) {
|
|
|
|
const target = util.target;
|
|
|
|
const penState = this._getPenState(target);
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
if (!penState.penDown) {
|
|
|
|
penState.penDown = true;
|
|
|
|
target.addListener(RenderedTarget.EVENT_TARGET_MOVED, this._onTargetMoved);
|
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
const penSkinId = this._getPenLayerID();
|
|
|
|
if (penSkinId >= 0) {
|
|
|
|
this.runtime.renderer.penPoint(penSkinId, penState.penAttributes, target.x, target.y);
|
|
|
|
this.runtime.requestRedraw();
|
|
|
|
}
|
2017-01-20 14:26:18 -05:00
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* The pen "pen up" block stops the target from leaving pen trails.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
penUp (args, util) {
|
2017-04-17 15:10:04 -04:00
|
|
|
const target = util.target;
|
2017-04-17 19:42:48 -04:00
|
|
|
const penState = this._getPenState(target);
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
if (penState.penDown) {
|
|
|
|
penState.penDown = false;
|
|
|
|
target.removeListener(RenderedTarget.EVENT_TARGET_MOVED, this._onTargetMoved);
|
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* The pen "set pen color to {color}" block sets the pen to a particular RGB color.
|
2017-10-17 23:13:05 -04:00
|
|
|
* The transparency is reset to 0.
|
2017-04-17 19:42:48 -04:00
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @property {int} COLOR - the color to set, expressed as a 24-bit RGB value (0xRRGGBB).
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
setPenColorToColor (args, util) {
|
|
|
|
const penState = this._getPenState(util.target);
|
|
|
|
const rgb = Cast.toRgbColorObject(args.COLOR);
|
|
|
|
const hsv = Color.rgbToHsv(rgb);
|
2017-10-17 12:22:53 -04:00
|
|
|
penState.color = (hsv.h / 360) * 100;
|
2017-10-12 10:43:20 -04:00
|
|
|
penState.saturation = hsv.s * 100;
|
|
|
|
penState.brightness = hsv.v * 100;
|
2017-11-03 21:24:47 -04:00
|
|
|
if (rgb.hasOwnProperty('a')) {
|
|
|
|
penState.transparency = 100 * (1 - rgb.a / 255.0);
|
|
|
|
} else {
|
|
|
|
penState.transparency = 0;
|
|
|
|
}
|
2017-10-31 17:24:12 -04:00
|
|
|
|
|
|
|
// Set the legacy "shade" value the same way scratch 2 did.
|
|
|
|
penState._shade = penState.brightness / 2;
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
this._updatePenColor(penState);
|
2017-01-19 14:26:38 -05:00
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
2017-10-17 12:22:53 -04:00
|
|
|
* Update the cached color from the color, saturation, brightness and transparency values
|
2017-10-12 10:43:20 -04:00
|
|
|
* in the provided PenState object.
|
|
|
|
* @param {PenState} penState - the pen state to update.
|
|
|
|
* @private
|
2017-04-17 19:42:48 -04:00
|
|
|
*/
|
2017-10-12 10:43:20 -04:00
|
|
|
_updatePenColor (penState) {
|
2017-10-18 13:16:24 -04:00
|
|
|
const rgb = Color.hsvToRgb({
|
2017-10-17 12:22:53 -04:00
|
|
|
h: penState.color * 360 / 100,
|
2017-10-12 10:43:20 -04:00
|
|
|
s: penState.saturation / 100,
|
|
|
|
v: penState.brightness / 100
|
|
|
|
});
|
|
|
|
penState.penAttributes.color4f[0] = rgb.r / 255.0;
|
|
|
|
penState.penAttributes.color4f[1] = rgb.g / 255.0;
|
|
|
|
penState.penAttributes.color4f[2] = rgb.b / 255.0;
|
|
|
|
penState.penAttributes.color4f[3] = this._transparencyToAlpha(penState.transparency);
|
2017-01-27 07:49:32 -05:00
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-10-18 11:48:45 -04:00
|
|
|
/**
|
|
|
|
* Set or change a single color parameter on the pen state, and update the pen color.
|
|
|
|
* @param {ColorParam} param - the name of the color parameter to set or change.
|
|
|
|
* @param {number} value - the value to set or change the param by.
|
|
|
|
* @param {PenState} penState - the pen state to update.
|
|
|
|
* @param {boolean} change - if true change param by value, if false set param to value.
|
|
|
|
* @private
|
|
|
|
*/
|
2017-10-17 19:31:51 -04:00
|
|
|
_setOrChangeColorParam (param, value, penState, change) {
|
|
|
|
switch (param) {
|
2017-10-18 13:16:24 -04:00
|
|
|
case ColorParam.COLOR:
|
|
|
|
penState.color = this._wrapColor(value + (change ? penState.color : 0));
|
|
|
|
break;
|
|
|
|
case ColorParam.SATURATION:
|
|
|
|
penState.saturation = this._clampColorParam(value + (change ? penState.saturation : 0));
|
|
|
|
break;
|
|
|
|
case ColorParam.BRIGHTNESS:
|
|
|
|
penState.brightness = this._clampColorParam(value + (change ? penState.brightness : 0));
|
|
|
|
break;
|
|
|
|
case ColorParam.TRANSPARENCY:
|
|
|
|
penState.transparency = this._clampColorParam(value + (change ? penState.transparency : 0));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
log.warn(`Tried to set or change unknown color parameter: ${param}`);
|
2017-10-12 10:43:20 -04:00
|
|
|
}
|
2017-04-17 19:42:48 -04:00
|
|
|
this._updatePenColor(penState);
|
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-10-18 11:48:45 -04:00
|
|
|
/**
|
|
|
|
* The "change pen {ColorParam} by {number}" block changes one of the pen's color parameters
|
|
|
|
* by a given amound.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @property {ColorParam} COLOR_PARAM - the name of the selected color parameter.
|
|
|
|
* @property {number} VALUE - the amount to change the selected parameter by.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
2017-10-17 19:31:51 -04:00
|
|
|
changePenColorParamBy (args, util) {
|
|
|
|
const penState = this._getPenState(util.target);
|
|
|
|
this._setOrChangeColorParam(args.COLOR_PARAM, Cast.toNumber(args.VALUE), penState, true);
|
|
|
|
}
|
|
|
|
|
2017-10-18 11:48:45 -04:00
|
|
|
/**
|
|
|
|
* The "set pen {ColorParam} to {number}" block sets one of the pen's color parameters
|
|
|
|
* to a given amound.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @property {ColorParam} COLOR_PARAM - the name of the selected color parameter.
|
|
|
|
* @property {number} VALUE - the amount to set the selected parameter to.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
2017-10-12 10:43:20 -04:00
|
|
|
setPenColorParamTo (args, util) {
|
2017-04-17 19:42:48 -04:00
|
|
|
const penState = this._getPenState(util.target);
|
2017-10-17 19:31:51 -04:00
|
|
|
this._setOrChangeColorParam(args.COLOR_PARAM, Cast.toNumber(args.VALUE), penState, false);
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* The pen "change pen size by {number}" block changes the pen size by the given amount.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @property {number} SIZE - the amount of desired size change.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
changePenSizeBy (args, util) {
|
|
|
|
const penAttributes = this._getPenState(util.target).penAttributes;
|
|
|
|
penAttributes.diameter = this._clampPenSize(penAttributes.diameter + Cast.toNumber(args.SIZE));
|
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
/**
|
|
|
|
* The pen "set pen size to {number}" block sets the pen size to the given amount.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @property {number} SIZE - the amount of desired size change.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
setPenSizeTo (args, util) {
|
|
|
|
const penAttributes = this._getPenState(util.target).penAttributes;
|
|
|
|
penAttributes.diameter = this._clampPenSize(Cast.toNumber(args.SIZE));
|
|
|
|
}
|
2017-10-31 17:24:12 -04:00
|
|
|
|
|
|
|
/* LEGACY OPCODES */
|
|
|
|
/**
|
|
|
|
* Scratch 2 "hue" param is equivelant to twice the new "color" param.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @property {number} HUE - the amount to set the hue to.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
setPenHueToNumber (args, util) {
|
|
|
|
const penState = this._getPenState(util.target);
|
|
|
|
const hueValue = Cast.toNumber(args.HUE);
|
|
|
|
const colorValue = hueValue / 2;
|
|
|
|
this._setOrChangeColorParam(ColorParam.COLOR, colorValue, penState, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scratch 2 "hue" param is equivelant to twice the new "color" param.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @property {number} HUE - the amount of desired hue change.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
changePenHueBy (args, util) {
|
|
|
|
const penState = this._getPenState(util.target);
|
|
|
|
const hueChange = Cast.toNumber(args.HUE);
|
|
|
|
const colorChange = hueChange / 2;
|
|
|
|
this._setOrChangeColorParam(ColorParam.COLOR, colorChange, penState, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use legacy "set shade" code to calculate RGB value for shade,
|
|
|
|
* then convert back to HSV and store those components.
|
|
|
|
* It is important to also track the given shade in penState._shade
|
|
|
|
* because it cannot be accurately backed out of the new HSV later.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @property {number} SHADE - the amount to set the shade to.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
setPenShadeToNumber (args, util) {
|
|
|
|
const penState = this._getPenState(util.target);
|
|
|
|
let newShade = Cast.toNumber(args.SHADE);
|
|
|
|
|
|
|
|
// Wrap clamp the new shade value the way scratch 2 did.
|
|
|
|
newShade = newShade % 200;
|
|
|
|
if (newShade < 0) newShade += 200;
|
|
|
|
|
|
|
|
// Create the new color in RGB using the scratch 2 "shade" model
|
|
|
|
let rgb = Color.hsvToRgb({h: penState.color * 360 / 100, s: 1, v: 1});
|
|
|
|
const shade = (newShade > 100) ? 200 - newShade : newShade;
|
|
|
|
if (shade < 50) {
|
|
|
|
rgb = Color.mixRgb(Color.RGB_BLACK, rgb, (10 + shade) / 60);
|
|
|
|
} else {
|
|
|
|
rgb = Color.mixRgb(rgb, Color.RGB_WHITE, (shade - 50) / 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the pen state according to new color
|
|
|
|
const hsv = Color.rgbToHsv(rgb);
|
|
|
|
penState.color = 100 * hsv.h / 360;
|
|
|
|
penState.saturation = 100 * hsv.s;
|
|
|
|
penState.brightness = 100 * hsv.v;
|
|
|
|
|
|
|
|
// And store the shade that was used to compute this new color for later use.
|
|
|
|
penState._shade = newShade;
|
|
|
|
|
|
|
|
this._updatePenColor(penState);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Because "shade" cannot be backed out of hsv consistently, use the previously
|
|
|
|
* stored penState._shade to make the shade change.
|
|
|
|
* @param {object} args - the block arguments.
|
|
|
|
* @property {number} SHADE - the amount of desired shade change.
|
|
|
|
* @param {object} util - utility object provided by the runtime.
|
|
|
|
*/
|
|
|
|
changePenShadeBy (args, util) {
|
|
|
|
const penState = this._getPenState(util.target);
|
|
|
|
const shadeChange = Cast.toNumber(args.SHADE);
|
|
|
|
this.setPenShadeToNumber({SHADE: penState._shade + shadeChange}, util);
|
|
|
|
}
|
|
|
|
|
2017-04-17 19:42:48 -04:00
|
|
|
}
|
2017-01-19 14:26:38 -05:00
|
|
|
|
|
|
|
module.exports = Scratch3PenBlocks;
|