forked from microsoft/pxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfield_animation.ts
More file actions
282 lines (225 loc) · 8.92 KB
/
field_animation.ts
File metadata and controls
282 lines (225 loc) · 8.92 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/// <reference path="../../built/pxtlib.d.ts" />
/// <reference path="./field_asset.ts" />
namespace pxtblockly {
import svg = pxt.svgUtil;
export interface FieldAnimationOptions {
initWidth: string;
initHeight: string;
disableResize: string;
filter?: string;
lightMode: boolean;
}
export interface ParsedFieldAnimationOptions {
initWidth: number;
initHeight: number;
disableResize: boolean;
filter?: string;
lightMode: boolean;
}
// 32 is specifically chosen so that we can scale the images for the default
// sprite sizes without getting browser anti-aliasing
const PREVIEW_WIDTH = 32;
const X_PADDING = 5;
const Y_PADDING = 1;
const BG_PADDING = 4;
const BG_WIDTH = BG_PADDING * 2 + PREVIEW_WIDTH;
const ICON_WIDTH = 30;
const TOTAL_HEIGHT = Y_PADDING * 2 + BG_PADDING * 2 + PREVIEW_WIDTH;
const TOTAL_WIDTH = X_PADDING * 2 + BG_PADDING * 2 + PREVIEW_WIDTH + ICON_WIDTH;
export class FieldAnimationEditor extends FieldAssetEditor<FieldAnimationOptions, ParsedFieldAnimationOptions> {
protected frames: string[];
protected preview: svg.Image;
protected animateRef: any;
protected asset: pxt.Animation;
protected initInterval: number;
init() {
if (this.fieldGroup_) {
// Field has already been initialized once.
return;
}
super.init();
// Register mouseover events for animating preview
(this.sourceBlock_ as Blockly.BlockSvg).getSvgRoot().addEventListener("mouseenter", this.onMouseEnter);
(this.sourceBlock_ as Blockly.BlockSvg).getSvgRoot().addEventListener("mouseleave", this.onMouseLeave);
}
showEditor_() {
// Read parent interval
if (this.asset) {
this.asset.interval = this.getParentInterval() || this.asset.interval;
}
super.showEditor_();
}
render_() {
super.render_();
this.size_.height = TOTAL_HEIGHT
this.size_.width = TOTAL_WIDTH;
}
protected getAssetType(): pxt.AssetType {
return pxt.AssetType.Animation;
}
protected createNewAsset(text?: string): pxt.Asset {
const project = pxt.react.getTilemapProject();
if (text) {
const existing = pxt.lookupProjectAssetByTSReference(text, project);
if (existing) return existing;
const frames = parseImageArrayString(text);
if (frames && frames.length) {
const id = this.sourceBlock_.id;
const newAnimation: pxt.Animation = {
internalID: -1,
id,
type: pxt.AssetType.Animation,
frames,
interval: this.getParentInterval(),
meta: { },
};
return newAnimation;
}
const asset = project.lookupAssetByName(pxt.AssetType.Animation, text.trim());
if (asset) return asset;
}
const id = this.sourceBlock_.id;
const bitmap = new pxt.sprite.Bitmap(this.params.initWidth, this.params.initHeight).data()
const newAnimation: pxt.Animation = {
internalID: -1,
id,
type: pxt.AssetType.Animation,
frames: [bitmap],
interval: 500,
meta: {},
};
return newAnimation;
}
protected onEditorClose(newValue: pxt.Animation) {
this.setParentInterval(newValue.interval);
}
protected getValueText(): string {
if (!this.asset) return "[]";
if (this.isTemporaryAsset()) {
return "[" + this.asset.frames.map(frame =>
pxt.sprite.bitmapToImageLiteral(pxt.sprite.Bitmap.fromData(frame), pxt.editor.FileType.TypeScript)
).join(",") + "]"
}
return pxt.getTSReferenceForAsset(this.asset);
}
protected redrawPreview() {
if (!this.fieldGroup_) return;
pxsim.U.clear(this.fieldGroup_);
const bg = new svg.Rect()
.at(X_PADDING + ICON_WIDTH, Y_PADDING)
.size(BG_WIDTH, BG_WIDTH)
.corner(4)
.setClass("blocklyAnimationField");
this.fieldGroup_.appendChild(bg.el);
const icon = new svg.Text("\uf008")
.at(X_PADDING, 5 + (TOTAL_HEIGHT >> 1))
.fill((this.sourceBlock_ as Blockly.BlockSvg).getColourSecondary())
.setClass("semanticIcon");
this.fieldGroup_.appendChild(icon.el);
if (this.asset) {
this.frames = this.asset.frames.map(frame => bitmapToImageURI(pxt.sprite.Bitmap.fromData(frame), PREVIEW_WIDTH, this.lightMode));
this.preview = new svg.Image()
.src(this.frames[0])
.at(X_PADDING + BG_PADDING + ICON_WIDTH, Y_PADDING + BG_PADDING)
.size(PREVIEW_WIDTH, PREVIEW_WIDTH);
this.fieldGroup_.appendChild(this.preview.el);
}
}
protected onMouseEnter = () => {
if (this.animateRef || !this.asset) return;
const assetInterval = this.getParentInterval() || this.asset.interval;
const interval = assetInterval > 50 ? assetInterval : 50;
let index = 0;
this.animateRef = setInterval(() => {
if (this.preview && this.frames[index]) this.preview.src(this.frames[index]);
index = (index + 1) % this.frames.length;
}, interval);
}
protected onMouseLeave = () => {
if (this.animateRef) clearInterval(this.animateRef);
this.animateRef = undefined;
if (this.preview && this.frames[0]) {
this.preview.src(this.frames[0]);
}
}
protected getParentIntervalBlock(): Blockly.Block {
const s = this.sourceBlock_;
if (s.parentBlock_) {
const p = s.parentBlock_;
for (const input of p.inputList) {
if (input.name === "frameInterval") {
return input.connection.targetBlock();
}
}
}
return undefined;
}
protected setParentInterval(interval: number) {
const target = this.getParentIntervalBlock();
if (target) {
const fieldName = getFieldName(target);
if (fieldName) {
target.setFieldValue(String(interval), fieldName);
}
}
}
protected getParentInterval() {
const target = this.getParentIntervalBlock();
if (target) {
const fieldName = getFieldName(target);
if (fieldName) {
return Number(target.getFieldValue(fieldName))
}
}
return 100;
}
protected parseFieldOptions(opts: FieldAnimationOptions): ParsedFieldAnimationOptions {
return parseFieldOptions(opts);
}
}
function parseFieldOptions(opts: FieldAnimationOptions) {
const parsed: ParsedFieldAnimationOptions = {
initWidth: 16,
initHeight: 16,
disableResize: false,
lightMode: false
};
if (!opts) {
return parsed;
}
parsed.lightMode = opts.lightMode;
if (opts.filter) {
parsed.filter = opts.filter;
}
parsed.initWidth = withDefault(opts.initWidth, parsed.initWidth);
parsed.initHeight = withDefault(opts.initHeight, parsed.initHeight);
return parsed;
function withDefault(raw: string, def: number) {
const res = parseInt(raw);
if (isNaN(res)) {
return def;
}
return res;
}
}
function parseImageArrayString(str: string): pxt.sprite.BitmapData[] {
if (str.indexOf("[") === -1) return null;
str = str.replace(/[\[\]]/mg, "");
return str.split(",").map(s => pxt.sprite.imageLiteralToBitmap(s).data()).filter(b => b.height && b.width);
}
function isNumberType(type: string) {
return type === "math_number" || type === "math_integer" || type === "math_whole_number";
}
function getFieldName(target: Blockly.Block) {
if (target.type === "math_number_minmax") {
return "SLIDER";
}
else if (isNumberType(target.type)) {
return "NUM";
}
else if (target.type === "timePicker") {
return "ms";
}
return null;
}
}