-
Notifications
You must be signed in to change notification settings - Fork 0
/
stretch.js
99 lines (89 loc) · 2.75 KB
/
stretch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
(function (Scratch) {
'use strict';
const STRETCH_X = Symbol('stretch.x');
const STRETCH_Y = Symbol('stretch.y');
const vm = Scratch.vm;
/**
* @param {VM.RenderedTarget} target
* @param {VM.RenderedTarget} [originalTarget] If target is a clone, the original to copy from.
*/
const implementStretchForTarget = (target, originalTarget) => {
if (STRETCH_X in target) {
// Target already has stretch. Don't implement again.
return;
}
target[STRETCH_X] = originalTarget ? originalTarget[STRETCH_X] : 100;
target[STRETCH_Y] = originalTarget ? originalTarget[STRETCH_Y] : 100;
const original = target._getRenderedDirectionAndScale;
target._getRenderedDirectionAndScale = function () {
const result = original.call(this);
result.scale[0] *= this[STRETCH_X] / 100;
result.scale[1] *= this[STRETCH_Y] / 100;
return result;
};
};
vm.runtime.targets.forEach((target) => implementStretchForTarget(target));
vm.runtime.on('targetWasCreated', (target, originalTarget) => implementStretchForTarget(target, originalTarget));
vm.runtime.on('PROJECT_LOADED', () => {
vm.runtime.targets.forEach((target) => implementStretchForTarget(target));
});
/**
* @param {VM.RenderedTarget} target
*/
const forceUpdateDirectionAndScale = (target) => {
target.setDirection(target.direction);
};
class Stretch {
getInfo() {
return {
id: 'stretch',
name: 'Stretch',
color1: '#4287f5',
color2: '#2b62ba',
color3: '#204785',
blocks: [
{
opcode: 'setStretch',
blockType: Scratch.BlockType.COMMAND,
text: 'set stretch to x: [X] y: [Y]',
arguments: {
X: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 100,
},
Y: {
type: Scratch.ArgumentType.NUMBER,
defaultValue: 100,
},
},
},
{
opcode: 'getX',
blockType: Scratch.BlockType.REPORTER,
text: 'x stretch',
disableMonitor: true,
},
{
opcode: 'getY',
blockType: Scratch.BlockType.REPORTER,
text: 'y stretch',
disableMonitor: true,
},
],
};
}
setStretch(args, util) {
// TODO: move to Scratch.Cast when it's merged
util.target[STRETCH_X] = +args.X || 0;
util.target[STRETCH_Y] = +args.Y || 0;
forceUpdateDirectionAndScale(util.target);
}
getX(args, util) {
return util.target[STRETCH_X];
}
getY(args, util) {
return util.target[STRETCH_Y];
}
}
Scratch.extensions.register(new Stretch());
})(Scratch);