-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAssetLoader.hx
More file actions
570 lines (491 loc) · 17.6 KB
/
AssetLoader.hx
File metadata and controls
570 lines (491 loc) · 17.6 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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
/*
Feathers UI
Copyright 2026 Bowler Hat LLC. All Rights Reserved.
This program is free software. You can redistribute and/or modify it in
accordance with the terms of the accompanying license agreement.
*/
package feathers.controls;
import feathers.core.FeathersControl;
import feathers.core.IValidating;
import feathers.events.FeathersEvent;
import feathers.layout.Measurements;
import feathers.skins.RectangleSkin;
import feathers.utils.ScaleUtil;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.BitmapData;
import openfl.display.DisplayObject;
import openfl.display.Loader;
import openfl.display.MovieClip;
import openfl.display.PixelSnapping;
import openfl.display.StageScaleMode;
import openfl.errors.SecurityError;
import openfl.events.Event;
import openfl.events.IOErrorEvent;
import openfl.events.ProgressEvent;
import openfl.events.SecurityErrorEvent;
import openfl.geom.Rectangle;
import openfl.net.URLRequest;
import openfl.utils.AssetType;
import openfl.utils.Future;
/**
Loads and displays an asset using either OpenFL's asset management system or
from a URL.
Supports assets of the following types:
- [`AssetType.IMAGE`](https://api.openfl.org/openfl/utils/AssetType.html#IMAGE)
- [`AssetType.MOVIE_CLIP`](https://api.openfl.org/openfl/utils/AssetType.html#MOVIE_CLIP)
@event openfl.events.Event.COMPLETE Dispatched when `AssetLoader.source`
successfully completes loading asynchronously. If `AssetLoader.source` is
pre-loaded by `openfl.utils.Assets`, this event will not be dispatched.
@event openfl.events.ProgressEvent.PROGRESS Dispatched periodically as
`AssetLoader.source` loads asynchronously. If `AssetLoader.source` is
pre-loaded by `openfl.utils.Assets`, this event will not be dispatched.
@event openfl.events.IOErrorEvent.IO_ERROR Dispatched if an IO error occurs
while loading `AssetLoader.source`.
@event openfl.events.SecurityErrorEvent.SECURITY_ERROR Dispatched if a
security error occurs while loading `AssetLoader.source`.
@see [Tutorial: How to use the AssetLoader component](https://feathersui.com/learn/haxe-openfl/asset-loader/)
@see [`openfl.utils.Assets`](https://api.openfl.org/openfl/utils/Assets.html)
@see `feathers.controls.BitmapImage`
@since 1.0.0
**/
@:event(openfl.events.Event.COMPLETE)
@:event(openfl.events.ProgressEvent.PROGRESS)
@:event(openfl.events.IOErrorEvent.IO_ERROR)
@:event(openfl.events.SecurityErrorEvent.SECURITY_ERROR)
@:styleContext
class AssetLoader extends FeathersControl {
/**
Creates a new `AssetLoader` object.
@since 1.0.0
**/
public function new(?source:String, ?completeListener:(Event) -> Void) {
initializeAssetLoaderTheme();
super();
this.source = source;
if (completeListener != null) {
this.addEventListener(Event.COMPLETE, completeListener);
}
}
private var content:DisplayObject;
private var loader:Loader;
private var _contentMeasurements:Measurements = new Measurements();
private var _pendingFuture:Future<Dynamic>;
private var _sourceScale:Float = 1.0;
/**
Scales the source content for measurement. For example, if assets are
designed for a scale factor of 2.0, they can be displayed at 0.5
scale to appear crisp (because displays bitmaps at the original
dimensions, as if the scale factor were 1.0).
```haxe
loader.sourceScale = 0.5;
```
@default 1.0
@since 1.3.0
**/
public var sourceScale(get, set):Float;
private function get_sourceScale():Float {
return this._sourceScale;
}
private function set_sourceScale(value:Float):Float {
if (this._sourceScale == value) {
return this._sourceScale;
}
this._sourceScale = value;
this.setInvalid(SIZE);
return this._sourceScale;
}
private var _source:String;
/**
Sets the loader's source, which may be either the name of an asset or a
URL to load the asset from the web instead.
The following example sets the source to an asset name:
```haxe
loader.source = "my-asset-name";
```
The following example sets the source to a URL:
```haxe
loader.source = "https://example.com/my-asset.png";
```
@since 1.0.0
**/
@:inspectable
public var source(get, set):String;
private function get_source():String {
return this._source;
}
private function set_source(value:String):String {
if (this._source == value) {
return this._source;
}
if (this.loader != null) {
this.loader.unloadAndStop();
}
if (this.content != null) {
this.removeChild(this.content);
this.content = null;
}
this._source = value;
if (this._source == null) {
this._pendingFuture = null;
this.cleanupLoader();
} else {
if (Assets.exists(this._source, AssetType.IMAGE)) {
this.cleanupLoader();
if (Assets.isLocal(this._source, AssetType.IMAGE)) {
this._pendingFuture = null;
var bitmapData = Assets.getBitmapData(this._source);
var bitmap = this.createBitmap(bitmapData);
this._contentMeasurements.save(bitmap);
this.addChild(bitmap);
this.content = bitmap;
FeathersEvent.dispatch(this, Event.COMPLETE);
} else // async
{
var future = Assets.loadBitmapData(this._source);
future.onProgress((progress, total) -> {
this.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, progress, total));
}).onComplete(function(bitmapData:BitmapData):Void {
if (future != this._pendingFuture) {
// cancelled
return;
}
var bitmap = this.createBitmap(bitmapData);
this._contentMeasurements.save(bitmap);
this.addChild(bitmap);
this.content = bitmap;
this.setInvalid(DATA);
FeathersEvent.dispatch(this, Event.COMPLETE);
}).onError((event:Dynamic) -> {
this.dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
});
this._pendingFuture = future;
}
} else if (Assets.exists(this._source, AssetType.MOVIE_CLIP)) {
this.cleanupLoader();
if (Assets.isLocal(this._source, AssetType.MOVIE_CLIP)) {
this._pendingFuture = null;
var movieClip = Assets.getMovieClip(this._source);
this._contentMeasurements.save(movieClip);
this.addChild(movieClip);
this.content = movieClip;
FeathersEvent.dispatch(this, Event.COMPLETE);
} else // async
{
var future = Assets.loadMovieClip(this._source);
future.onProgress((progress, total) -> {
this.dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, progress, total));
}).onComplete(function(movieClip:MovieClip):Void {
if (future != this._pendingFuture) {
// cancelled
return;
}
this._contentMeasurements.save(movieClip);
this.addChild(movieClip);
this.content = movieClip;
this.setInvalid(DATA);
FeathersEvent.dispatch(this, Event.COMPLETE);
}).onError((event:Dynamic) -> {
this.dispatchEvent(new IOErrorEvent(IOErrorEvent.IO_ERROR));
});
this._pendingFuture = future;
}
} else {
this._pendingFuture = null;
if (this.loader == null) {
this.loader = new Loader();
this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_contentLoaderInfo_completeHandler);
this.loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loader_contentLoaderInfo_progressHandler);
this.loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, loader_contentLoaderInfo_ioErrorHandler);
this.loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_contentLoaderInfo_securityErrorHandler);
}
try {
this.loader.load(new URLRequest(this._source));
} catch (e:Dynamic) {
if ((e is SecurityError)) {
var securityError:SecurityError = cast e;
this.dispatchEvent(new SecurityErrorEvent(SecurityErrorEvent.SECURITY_ERROR, false, false, securityError.message,
securityError.errorID));
}
}
}
}
this.setInvalid(DATA);
return this._source;
}
/**
The original width of the `source` asset, measured in pixels. May return
`null`, if the `source` is `null`, or if the `source` has not yet
completed loading.
@see `AssetLoader.source`
@see `AssetLoader.originalSourceHeight`
@since 1.0.0
**/
public var originalSourceWidth(get, never):Null<Float>;
private function get_originalSourceWidth():Null<Float> {
if (this._contentMeasurements == null) {
return null;
}
return this._contentMeasurements.width;
}
/**
The original height of the source asset, measured in pixels. May return
`null`, if the `source` is `null`, or if the `source` has not yet
completed loading.
@see `AssetLoader.source`
@see `AssetLoader.originalSourceWidth`
@since 1.0.0
**/
public var originalSourceHeight(get, never):Null<Float>;
private function get_originalSourceHeight():Null<Float> {
if (this._contentMeasurements == null) {
return null;
}
return this._contentMeasurements.height;
}
private var _scaleModeMask:DisplayObject = null;
private var _scaleMode:StageScaleMode = StageScaleMode.SHOW_ALL;
/**
Determines how the asset will be scaled within the width and height of
the `AssetLoader` instance. Uses the same constants from
[`StageScaleMode`](https://api.openfl.org/openfl/display/StageScaleMode.html)
that are used to scale the OpenFL stage.
The following example maintains the aspect ratio of the asset, but
displays no border, and may crop it to fit:
```haxe
loader.scaleMode = StageScaleMode.NO_BORDER
```
@see [`openfl.display.StageScaleMode`](https://api.openfl.org/openfl/display/StageScaleMode.html)
@since 1.0.0
**/
public var scaleMode(get, set):StageScaleMode;
private function get_scaleMode():StageScaleMode {
return this._scaleMode;
}
private function set_scaleMode(value:StageScaleMode):StageScaleMode {
if (this._scaleMode == value) {
return this._scaleMode;
}
this._scaleMode = value;
this.setInvalid(LAYOUT);
return this._scaleMode;
}
/**
Applies pixel snapping to the loaded content, if the loaded content is a
bitmap. For other types of loaded content, the `pixelSnapping` property
is ignored.
@see [`openfl.display.Bitmap.pixelSnapping`](https://api.openfl.org/openfl/display/Bitmap.html#pixelSnapping)
**/
@:style
public var pixelSnapping:PixelSnapping = AUTO;
/**
Applies smoothing to the loaded content, if the loaded content is a
bitmap. For other types of loaded content, the `smoothing` property is
ignored.
@see [`openfl.display.Bitmap.smoothing`](https://api.openfl.org/openfl/display/Bitmap.html#smoothing)
**/
@:style
public var smoothing:Bool = false;
private function initializeAssetLoaderTheme():Void {
#if !feathersui_disable_default_theme
feathers.themes.steel.components.SteelAssetLoaderStyles.initialize();
#end
}
override public function dispose():Void {
this.source = null;
super.dispose();
}
override private function update():Void {
var dataInvalid = this.isInvalid(DATA);
var layoutInvalid = this.isInvalid(LAYOUT);
var stylesInvalid = this.isInvalid(STYLES);
if (dataInvalid || stylesInvalid) {
if (this.content != null) {
var bitmap = Std.downcast(this.content, Bitmap);
if (bitmap == null) {
var loader = Std.downcast(this.content, Loader);
bitmap = Std.downcast(loader.content, Bitmap);
}
if (bitmap != null) {
bitmap.pixelSnapping = this.pixelSnapping;
bitmap.smoothing = this.smoothing;
}
}
}
this.measure();
this.layoutChildren();
}
private function measure():Bool {
var needsWidth = this.explicitWidth == null;
var needsHeight = this.explicitHeight == null;
var needsMinWidth = this.explicitMinWidth == null;
var needsMinHeight = this.explicitMinHeight == null;
var needsMaxWidth = this.explicitMaxWidth == null;
var needsMaxHeight = this.explicitMaxHeight == null;
if (!needsWidth && !needsHeight && !needsMinWidth && !needsMinHeight && !needsMaxWidth && !needsMaxHeight) {
return false;
}
var contentWidth = this._contentMeasurements.width;
var contentHeight = this._contentMeasurements.height;
if (contentWidth != null && this._sourceScale != 1.0) {
contentWidth *= this._sourceScale;
}
if (contentHeight != null && this._sourceScale != 1.0) {
contentHeight *= this._sourceScale;
}
var widthScale = 1.0;
var heightScale = 1.0;
if (this.content != null && this._scaleMode != StageScaleMode.NO_SCALE) {
if (!needsWidth) {
widthScale = this.explicitWidth / contentWidth;
} else if (this.explicitMaxWidth != null && this.explicitMaxWidth < contentWidth) {
widthScale = this.explicitMaxWidth / contentWidth;
} else if (this.explicitMinWidth != null && this.explicitMinWidth > contentWidth) {
widthScale = this.explicitMinWidth / contentWidth;
}
if (!needsHeight) {
heightScale = this.explicitHeight / contentHeight;
} else if (this.explicitMaxHeight != null && this.explicitMaxHeight < contentHeight) {
heightScale = this.explicitMaxHeight / contentHeight;
} else if (this.explicitMinHeight != null && this.explicitMinHeight > contentHeight) {
heightScale = this.explicitMinHeight / contentHeight;
}
}
var newWidth = this.explicitWidth;
if (needsWidth) {
if (this.content != null) {
newWidth = contentWidth * heightScale;
} else {
newWidth = 0.0;
}
}
var newHeight = this.explicitHeight;
if (needsHeight) {
if (this.content != null) {
newHeight = contentHeight * widthScale;
} else {
newHeight = 0.0;
}
}
var newMinWidth = this.explicitMinWidth;
if (needsMinWidth) {
if (this.content != null) {
newMinWidth = contentWidth * heightScale;
} else {
newMinWidth = 0.0;
}
}
var newMinHeight = this.explicitMinHeight;
if (needsMinHeight) {
if (this.content != null) {
newMinHeight = contentHeight * widthScale;
} else {
newMinHeight = 0.0;
}
}
var newMaxWidth = this.explicitMaxWidth;
if (needsMaxWidth) {
if (this.content != null) {
newMaxWidth = contentWidth * heightScale;
} else {
newMaxWidth = 1.0 / 0.0; // Math.POSITIVE_INFINITY bug workaround for swf
}
}
var newMaxHeight = this.explicitMaxHeight;
if (needsMaxHeight) {
if (this.content != null) {
newMaxHeight = contentHeight * widthScale;
} else {
newMaxHeight = 1.0 / 0.0; // Math.POSITIVE_INFINITY bug workaround for swf
}
}
return this.saveMeasurements(newWidth, newHeight, newMinWidth, newMinHeight, newMaxWidth, newMaxHeight);
}
private function layoutChildren():Void {
if (this.content == null) {
return;
}
var needsMask = false;
switch (this._scaleMode) {
case StageScaleMode.EXACT_FIT:
this.content.x = 0.0;
this.content.y = 0.0;
this.content.width = this.actualWidth;
this.content.height = this.actualHeight;
case StageScaleMode.NO_SCALE:
this.content.x = 0.0;
this.content.y = 0.0;
this._contentMeasurements.restore(this.content);
if ((this.content is IValidating)) {
(cast this.content : IValidating).validateNow();
}
needsMask = this.content.width > this.actualWidth || this.content.height > this.actualHeight;
case StageScaleMode.NO_BORDER:
var original = new Rectangle(0.0, 0.0, this._contentMeasurements.width, this._contentMeasurements.height);
var into = new Rectangle(0.0, 0.0, this.actualWidth, this.actualHeight);
var scaled = ScaleUtil.fillRectangle(original, into, into);
this.content.x = scaled.x;
this.content.y = scaled.y;
this.content.width = scaled.width;
this.content.height = scaled.height;
needsMask = this.content.width > this.actualWidth || this.content.height > this.actualHeight;
default: // showAll
var original = new Rectangle(0.0, 0.0, this._contentMeasurements.width, this._contentMeasurements.height);
var into = new Rectangle(0.0, 0.0, this.actualWidth, this.actualHeight);
ScaleUtil.fitRectangle(original, into, into);
this.content.x = into.x;
this.content.y = into.y;
this.content.width = into.width;
this.content.height = into.height;
}
if (needsMask) {
if (this._scaleModeMask == null) {
this._scaleModeMask = new RectangleSkin(SolidColor(0xff00ff));
this.addChild(this._scaleModeMask);
}
this._scaleModeMask.width = this.actualWidth;
this._scaleModeMask.height = this.actualHeight;
this.content.mask = this._scaleModeMask;
} else {
if (this._scaleModeMask != null) {
this.removeChild(this._scaleModeMask);
this._scaleModeMask = null;
}
this.content.mask = null;
}
}
private function createBitmap(bitmapData:BitmapData):Bitmap {
return new Bitmap(bitmapData, this.pixelSnapping, this.smoothing);
}
private function cleanupLoader():Void {
if (this.loader == null) {
return;
}
this.loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, loader_contentLoaderInfo_completeHandler);
this.loader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, loader_contentLoaderInfo_progressHandler);
this.loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, loader_contentLoaderInfo_ioErrorHandler);
this.loader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, loader_contentLoaderInfo_securityErrorHandler);
this.loader = null;
}
private function loader_contentLoaderInfo_progressHandler(event:ProgressEvent):Void {
this.dispatchEvent(event);
}
private function loader_contentLoaderInfo_ioErrorHandler(event:IOErrorEvent):Void {
this.dispatchEvent(event);
}
private function loader_contentLoaderInfo_securityErrorHandler(event:SecurityErrorEvent):Void {
this.dispatchEvent(event);
}
private function loader_contentLoaderInfo_completeHandler(event:Event):Void {
var bitmap = Std.downcast(this.loader.content, Bitmap);
if (bitmap != null) {
bitmap.pixelSnapping = this.pixelSnapping;
bitmap.smoothing = this.smoothing;
}
this.addChild(this.loader);
this.content = this.loader;
this._contentMeasurements.save(this.content);
this.setInvalid(LAYOUT);
this.dispatchEvent(event);
}
}