forked from arpit/openpyro
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImage.as
More file actions
244 lines (205 loc) · 5.67 KB
/
Image.as
File metadata and controls
244 lines (205 loc) · 5.67 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
package org.openPyro.controls
{
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import org.openPyro.core.UIControl;
/**
* Complete event is dispatched when the image load
* is completed.
*/
[Event(name="complete", type="flash.events.Event")]
/**
* The Image control is used to display images that
* are loaded from a remote URL. Unlike the Flex
* equivalent, OpenPyro Images cannot be used to
* render embedded content.
*/
public class Image extends UIControl
{
private var _sourceURL:String = "";
private var _loader:Loader;
public function Image() {
super();
}
override protected function createChildren():void
{
super.createChildren();
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
addChild(_loader);
_loader.visible = false;
if(!_loaderContext)
{
_loaderContext = new LoaderContext(true);
}
if(_autoLoad && (_sourceURL != "")){
_loader.load(new URLRequest(_sourceURL), _loaderContext);
}
}
protected var _autoLoad:Boolean = true;
public function set autoLoad(b:Boolean):void
{
_autoLoad = b;
}
public function get autoLoad():Boolean
{
return _autoLoad;
}
protected var _isContentLoaded:Boolean = false;
/**
* Flag to check if the remote content is loaded
* or not.
*/
public function isContentLoaded():Boolean{
return _isContentLoaded;
}
public function set source(url:String):void
{
if(url == _sourceURL) return;
_sourceURL = url;
if(_loader && _autoLoad){
load();
}
}
public function get source():String{
return _sourceURL;
}
protected function load():void{
_loader.unload();
needsRecalculation = true;
_loader.visible = false;
_isContentLoaded = false;
_loader.load(new URLRequest(_sourceURL), _loaderContext);
}
private var _loaderContext:LoaderContext
/**
* The LoaderContext that is used when loading an
* image.
*/
public function set loaderContext(context:LoaderContext):void
{
_loaderContext = context;
}
/**
* @private
*/
public function get loaderContext():LoaderContext
{
return _loaderContext;
}
/**
* Returns the raw loader being used to load the image
*/
public function get loader():Loader
{
return _loader;
}
protected var _originalContentWidth:Number = NaN;
protected var _originalContentHeight:Number = NaN;
protected function onLoadComplete(event:Event):void
{
_originalContentWidth = _loader.content.width;
_originalContentHeight = _loader.content.height;
_isContentLoaded = true;
dispatchEvent(new Event(Event.COMPLETE));
forceInvalidateDisplayList = true;
invalidateSize();
invalidateDisplayList()
}
override protected function doChildBasedValidation():void{
if(!_loader || !_loader.content) return;
if(isNaN(this._explicitWidth) && isNaN(this._percentWidth) && isNaN(_percentUnusedWidth)){
measuredWidth = _originalContentWidth + _padding.left + _padding.right;
}
if(isNaN(this._explicitHeight) && isNaN(this._percentHeight) && isNaN(_percentUnusedHeight))
{
measuredHeight = _originalContentHeight + _padding.top + _padding.bottom;
}
}
public function get contentWidth():Number{
return _loader.content.width;
}
public function get contentHeight():Number{
return _loader.content.height;
}
public function get originalContentWidth():Number{
return _originalContentWidth
}
public function get originalContentHeight():Number{
return _originalContentHeight;
}
protected function onIOError(event:IOErrorEvent):void
{
//todo: Put broken thumb skin here//
}
protected var _scaleToFit:Boolean = true;
public function get scaleToFit():Boolean
{
return _scaleToFit;
}
public function set scaleToFit(value:Boolean):void
{
_scaleToFit = value;
if(_scaleToFit && _loader && _loader.content)
{
scaleImageContent()
}
}
protected var _maintainAspectRatio:Boolean = true;
public function set maintainAspectRatio(value:Boolean):void
{
_maintainAspectRatio = value;
}
public function get maintainAspectRatio():Boolean
{
return _maintainAspectRatio;
}
/**
* Unloads the image loaded by the <code>Loader</code>
* instance
*/
public function unload():void{
this._loader.unload();
}
protected function scaleImageContent():void
{
var scaleX:Number;
var scaleY:Number;
scaleX = width / (_originalContentWidth+_padding.left+_padding.right);
scaleY = height / (_originalContentHeight+_padding.top+_padding.bottom);
if(_maintainAspectRatio)
{
var scale:Number = Math.min(scaleX, scaleY);
_loader.content.width = _originalContentWidth*scale;
_loader.content.height = _originalContentHeight*scale;
}
else
{
_loader.content.width = _originalContentWidth*scaleX;
_loader.content.height = _originalContentHeight*scaleY;
}
}
public var needsRecalculation:Boolean = false;
override public function resizeHandler() : void{
needsRecalculation = true;
super.resizeHandler();
}
override public function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
{
super.updateDisplayList(unscaledWidth, unscaledHeight);
if(_loader && _loader.content){
if(_scaleToFit && needsRecalculation){
scaleImageContent();
needsRecalculation = false;
}
_loader.visible = true;
}
_loader.x = _padding.left
_loader.y = _padding.top;
}
}
}