FlexContentDisplay.as 18.1 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
/**
 * VERSION: 1.46
 * DATE: 2010-09-15
 * AS3
 * UPDATES AND DOCS AT: http://www.greensock.com/loadermax/
 **/
package com.greensock.loading.display {
	import com.greensock.loading.core.LoaderItem;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.geom.Matrix;
	import flash.geom.Rectangle;
	import flash.media.Video;
	
	import mx.core.UIComponent;
/**
 * A container for visual content that is loaded by any of the following: ImageLoaders, SWFLoaders, 
 * or VideoLoaders which is to be used in Flex. It is essentially a UIComponent that has a <code>loader</code> 
 * property for easily referencing the original loader, as well as several other useful properties for 
 * controling the placement of <code>rawContent</code> and the way it is scaled to fit (if at all). That way, 
 * you can add a FlexContentDisplay to the display list or populate an array with as many as you want and then if 
 * you ever need to unload() the content or reload it or figure out its url, etc., you can reference your 
 * FlexContentDisplay's <code>loader</code> property like <code>myContent.loader.url</code> or 
 * <code>(myContent.loader as SWFLoader).getClass("com.greensock.TweenLite");</code>. For
 * <br /><br />
 * 
 * <strong>IMPORTANT</strong>: In order for the LoaderMax loaders to use FlexContentDisplay instead of 
 * the regular ContentDisplay class, you must set the <code>LoaderMax.contentDisplayClass</code> property 
 * to FlexContentDisplay once like:
 * @example Example AS3 code:<listing version="3.0">
 import com.greensock.loading.~~;
 import com.greensock.loading.display.~~;
 
 LoaderMax.contentDisplayClass = FlexContentDisplay;
 </listing>
 * 
 * After that, all ImageLoaders, SWFLoaders, and VideoLoaders will return FlexContentDisplay objects 
 * as their <code>content</code> instead of regular ContentDisplay objects. <br /><br />
 * 
 * <b>Copyright 2010, GreenSock. All rights reserved.</b> This work is subject to the terms in <a href="http://www.greensock.com/terms_of_use.html">http://www.greensock.com/terms_of_use.html</a> or for corporate Club GreenSock members, the software agreement that was issued with the corporate membership.
 * 
 * @author Jack Doyle, jack@greensock.com
 */	
	public class FlexContentDisplay extends UIComponent {
		/** @private **/
		protected static var _transformProps:Object = {x:1, y:1, scaleX:1, scaleY:1, rotation:1, alpha:1, visible:true, blendMode:"normal", centerRegistration:false, crop:false, scaleMode:"stretch", hAlign:"center", vAlign:"center"};
		/** @private **/
		protected var _loader:LoaderItem;
		/** @private **/
		protected var _rawContent:DisplayObject;
		/** @private **/
		protected var _centerRegistration:Boolean;
		/** @private **/
		protected var _crop:Boolean;
		/** @private **/
		protected var _scaleMode:String = "stretch";
		/** @private **/
		protected var _hAlign:String = "center";
		/** @private **/
		protected var _vAlign:String = "center";
		/** @private **/
		protected var _bgColor:uint;
		/** @private **/
		protected var _bgAlpha:Number = 0;
		/** @private **/
		protected var _fitWidth:Number;
		/** @private **/
		protected var _fitHeight:Number;
		
		/** @private A place to reference an object that should be protected from gc - this is used in VideoLoader in order to protect the NetStream object when the loader is disposed. **/
		public var gcProtect:*;
		
		/**
		 * Constructor
		 * 
		 * @param loader The Loader object that will populate the FlexContentDisplay's <code>rawContent</code>.
		 */
		public function FlexContentDisplay(loader:LoaderItem) {
			super();
			this.loader = loader;
		}
		/**
		 * Removes the FlexContentDisplay from the display list (if necessary), dumps the <code>rawContent</code>,
		 * and calls <code>unload()</code> and <code>dispose()</code> on the loader (unless you define otherwise with 
		 * the optional parameters). This essentially destroys the FlexContentDisplay and makes it eligible for garbage 
		 * collection internally, although if you added any listeners manually, you should remove them as well.
		 * 
		 * @param unloadLoader If <code>true</code>, <code>unload()</code> will be called on the loader. It is <code>true</code> by default.
		 * @param disposeLoader If <code>true</code>, <code>dispose()</code> will be called on the loader. It is <code>true</code> by default.
		 */
		public function dispose(unloadLoader:Boolean=true, disposeLoader:Boolean=true):void {
			if (this.parent != null) {
				if (this.parent.hasOwnProperty("addElement")) {
					(this.parent as Object).removeElement(this);
				} else {
					this.parent.removeChild(this);
				}
			}
			this.rawContent = null;
			this.gcProtect = null;
			if (_loader != null) {
				if (unloadLoader) {
					_loader.unload();
				}
				if (disposeLoader) {
					_loader.dispose(false);
					_loader = null;
				}
			}
		}
		
		/** @private **/
		protected function _update():void {
			var left:Number = (_centerRegistration && _fitWidth > 0) ? _fitWidth / -2 : 0;
			var top:Number = (_centerRegistration && _fitHeight > 0) ? _fitHeight / -2 : 0;
			if (_fitWidth > 0 && _fitHeight > 0) {
				graphics.beginFill(_bgColor, _bgAlpha);
				graphics.drawRect(left, top, _fitWidth, _fitHeight);
				graphics.endFill();
			}
			if (_rawContent == null) {
				return;
			}
			var mc:DisplayObject = _rawContent;
			var contentWidth:Number =  mc.width;
			var contentHeight:Number = mc.height;
			if (_loader.hasOwnProperty("getClass") && !_loader.scriptAccessDenied) { //for SWFLoaders, use loaderInfo.width/height so that everything is based on the stage size, not the bounding box of the DisplayObjects that happen to be on the stage (which could be much larger or smaller than the swf's stage)
				contentWidth = mc.loaderInfo.width;
				contentHeight = mc.loaderInfo.height;
			}
			
			if (_fitWidth > 0 && _fitHeight > 0) {
				var w:Number = _fitWidth;
				var h:Number = _fitHeight;
				
				var wGap:Number = w - contentWidth;
				var hGap:Number = h - contentHeight;
				
				if (_scaleMode != "none") {
					var displayRatio:Number = w / h;
					var contentRatio:Number = contentWidth / contentHeight;
					if ((contentRatio < displayRatio && _scaleMode == "proportionalInside") || (contentRatio > displayRatio && _scaleMode == "proportionalOutside")) {
						w = h * contentRatio;
					}
					if ((contentRatio > displayRatio && _scaleMode == "proportionalInside") || (contentRatio < displayRatio && _scaleMode == "proportionalOutside")) {
						h = w / contentRatio;
					}
					
					if (_scaleMode != "heightOnly") {
						mc.width *= w / contentWidth;
						wGap = _fitWidth - w;
					} 
					if (_scaleMode != "widthOnly") {
						mc.height *= h / contentHeight;
						hGap = _fitHeight - h;
					}
				}
				
				if (_hAlign == "left") {
					wGap = 0;
				} else if (_hAlign != "right") {
					wGap *= 0.5;
				}
				if (_vAlign == "top") {
					hGap = 0;
				} else if (_vAlign != "bottom") {
					hGap *= 0.5;
				}
				
				mc.x = left;
				mc.y = top;
				
				if (_crop) {
					mc.scrollRect = new Rectangle(-wGap / mc.scaleX, -hGap / mc.scaleY, _fitWidth / mc.scaleX, _fitHeight / mc.scaleY);
				} else {
					mc.x += wGap;
					mc.y += hGap;
				}
			} else {
				mc.x = (_centerRegistration) ? -contentWidth / 2 : 0;
				mc.y = (_centerRegistration) ? -contentHeight / 2 : 0;
			}
			measure();
		}
		
		override protected function measure():void {
			var bounds:Rectangle = this.getBounds(this);
			this.explicitWidth = bounds.width;
			this.explicitHeight = bounds.height;
			if (this.parent) {
				bounds = this.getBounds(this.parent);
				this.width = bounds.width;
				this.height = bounds.height;
			}
			super.measure();
		}
		
//---- GETTERS / SETTERS -------------------------------------------------------------------------

		/** 
		 * The width to which the <code>rawContent</code> should be fit according to the FlexContentDisplay's <code>scaleMode</code>
		 * (this width is figured before rotation, scaleX, and scaleY). When a "width" property is defined in the loader's <code>vars</code>
		 * property/parameter, it is automatically applied to this <code>fitWidth</code> property. For example, the following code will
		 * set the loader's FlexContentDisplay <code>fitWidth</code> to 100:<code><br /><br />
		 * 
		 * var loader:ImageLoader = new ImageLoader("photo.jpg", {width:100, height:80, container:this});</code>
		 * 
		 * @see #fitHeight
		 * @see #scaleMode
		 **/
		public function get fitWidth():Number {
			return _fitWidth;
		}
		public function set fitWidth(value:Number):void {
			_fitWidth = value;
			_update();
		}
		
		/** 
		 * The height to which the <code>rawContent</code> should be fit according to the FlexContentDisplay's <code>scaleMode</code>
		 * (this height is figured before rotation, scaleX, and scaleY). When a "height" property is defined in the loader's <code>vars</code>
		 * property/parameter, it is automatically applied to this <code>fitHeight</code> property. For example, the following code will
		 * set the loader's FlexContentDisplay <code>fitHeight</code> to 80:<code><br /><br />
		 * 
		 * var loader:ImageLoader = new ImageLoader("photo.jpg", {width:100, height:80, container:this});</code>
		 * 
		 * @see #fitWidth
		 * @see #scaleMode
		 **/
		public function get fitHeight():Number {
			return _fitHeight;
		}
		public function set fitHeight(value:Number):void {
			_fitHeight = value;
			_update();
		}
		
		/** 
		 * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code> 
		 * and <code>height</code> in the loader's <code>vars</code> property/parameter), the <code>scaleMode</code> controls how 
		 * the <code>rawContent</code> will be scaled to fit the area. The following values are recognized (you may use the 
		 * <code>com.greensock.layout.ScaleMode</code> constants if you prefer):
		 * <ul>
		 * 		<li><code>"stretch"</code> (the default) - The <code>rawContent</code> will fill the width/height exactly.</li>
		 * 		<li><code>"proportionalInside"</code> - The <code>rawContent</code> will be scaled proportionally to fit inside the area defined by the width/height</li>
		 * 		<li><code>"proportionalOutside"</code> - The <code>rawContent</code> will be scaled proportionally to completely fill the area, allowing portions of it to exceed the bounds defined by the width/height.</li>
		 * 		<li><code>"widthOnly"</code> - Only the width of the <code>rawContent</code> will be adjusted to fit.</li>
		 * 		<li><code>"heightOnly"</code> - Only the height of the <code>rawContent</code> will be adjusted to fit.</li>
		 * 		<li><code>"none"</code> - No scaling of the <code>rawContent</code> will occur.</li>
		 * </ul> 
		 **/
		public function get scaleMode():String {
			return _scaleMode;
		}
		public function set scaleMode(value:String):void {
			if (value == "none" && _rawContent != null) {
				_rawContent.scaleX = _rawContent.scaleY = 1;
			}
			_scaleMode = value;
			_update();
		}
		
		/** 
		 * If <code>true</code>, the FlexContentDisplay's registration point will be placed in the center of the <code>rawContent</code> 
		 * which can be useful if, for example, you want to animate its scale and have it grow/shrink from its center. 
		 * @see #scaleMode
		 **/
		public function get centerRegistration():Boolean {
			return _centerRegistration;
		}
		public function set centerRegistration(value:Boolean):void {
			_centerRegistration = value;
			_update();
		}
		
		/** 
		 * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code> 
		 * and <code>height</code> in the loader's <code>vars</code> property/parameter), setting <code>crop</code> to 
		 * <code>true</code> will cause the <code>rawContent</code> to be cropped within that area (by applying a <code>scrollRect</code> 
		 * for maximum performance). This is typically useful when the <code>scaleMode</code> is <code>"proportionalOutside"</code> 
		 * or <code>"none"</code> so that any parts of the <code>rawContent</code> that exceed the dimensions defined by 
		 * <code>fitWidth</code> and <code>fitHeight</code> are visually chopped off. Use the <code>hAlign</code> and 
		 * <code>vAlign</code> properties to control the vertical and horizontal alignment within the cropped area. 
		 * 
		 * @see #scaleMode
		 **/
		public function get crop():Boolean {
			return _crop;
		}
		public function set crop(value:Boolean):void {
			_crop = value;
			_update();
		}
		
		/** 
		 * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code> 
		 * and <code>height</code> in the loader's <code>vars</code> property/parameter), the <code>hAlign</code> determines how 
		 * the <code>rawContent</code> is horizontally aligned within that area. The following values are recognized (you may use the 
		 * <code>com.greensock.layout.AlignMode</code> constants if you prefer):
		 * <ul>
		 * 		<li><code>"center"</code> (the default) - The <code>rawContent</code> will be centered horizontally in the FlexContentDisplay</li>
		 * 		<li><code>"left"</code> - The <code>rawContent</code> will be aligned with the left side of the FlexContentDisplay</li>
		 * 		<li><code>"right"</code> - The <code>rawContent</code> will be aligned with the right side of the FlexContentDisplay</li>
		 * </ul> 
		 * @see #scaleMode
		 * @see #vAlign
		 **/
		public function get hAlign():String {
			return _hAlign;
		}
		public function set hAlign(value:String):void {
			_hAlign = value;
			_update();
		}
		
		/** 
		 * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code> 
		 * and <code>height</code> in the loader's <code>vars</code> property/parameter), the <code>vAlign</code> determines how 
		 * the <code>rawContent</code> is vertically aligned within that area. The following values are recognized (you may use the 
		 * <code>com.greensock.layout.AlignMode</code> constants if you prefer):
		 * <ul>
		 * 		<li><code>"center"</code> (the default) - The <code>rawContent</code> will be centered vertically in the FlexContentDisplay</li>
		 * 		<li><code>"top"</code> - The <code>rawContent</code> will be aligned with the top of the FlexContentDisplay</li>
		 * 		<li><code>"bottom"</code> - The <code>rawContent</code> will be aligned with the bottom of the FlexContentDisplay</li>
		 * </ul> 
		 * @see #scaleMode
		 * @see #hAlign
		 **/
		public function get vAlign():String {
			return _vAlign;
		}
		public function set vAlign(value:String):void {
			_vAlign = value;
			_update();
		}
		
		/** 
		 * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code> 
		 * and <code>height</code> in the loader's <code>vars</code> property/parameter), a rectangle will be drawn inside the 
		 * FlexContentDisplay object immediately in order to ease the development process (for example, you can add <code>ROLL_OVER/ROLL_OUT</code>
		 * event listeners immediately). It is transparent by default, but you may define a <code>bgAlpha</code> if you prefer. 
		 * @see #bgAlpha
		 * @see #fitWidth
		 * @see #fitHeight
		 **/
		public function get bgColor():uint {
			return _bgColor;
		}
		public function set bgColor(value:uint):void {
			_bgColor = value;
			_update();
		}
		
		/** 
		 * Controls the alpha of the rectangle that is drawn when the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> 
		 * properties are defined (or <code>width</code> and <code>height</code> in the loader's <code>vars</code> property/parameter). 
		 * @see #bgColor
		 * @see #fitWidth
		 * @see #fitHeight
		 **/
		public function get bgAlpha():Number {
			return _bgAlpha;
		}
		public function set bgAlpha(value:Number):void {
			_bgAlpha = value;
			_update();
		}
		
		
		/** The raw content which can be a Bitmap, a MovieClip, a Loader, or a Video depending on the type of loader associated with the FlexContentDisplay. **/
		public function get rawContent():* {
			return _rawContent;
		}
		
		public function set rawContent(value:*):void {
			if (_rawContent != null && _rawContent != value && _rawContent.parent == this) {
				removeChild(_rawContent);
			}
			_rawContent = value as DisplayObject;
			if (_rawContent == null) {
				return;
			}
			addChildAt(_rawContent as DisplayObject, 0);
			_update();
		}
		
		/** The loader whose rawContent populates this FlexContentDisplay. If you get the loader's <code>content</code>, it will return this FlexContentDisplay object. **/
		public function get loader():LoaderItem {
			return _loader;
		}
		
		public function set loader(value:LoaderItem):void {
			_loader = value;
			if (value == null) {
				return;
			} else if (!_loader.hasOwnProperty("setContentDisplay")) {
				throw new Error("Incompatible loader used for a FlexContentDisplay");
			}
			this.name = _loader.name;
			if (_loader.vars.container is DisplayObjectContainer) {
				if (_loader.vars.container.hasOwnProperty("addElement")) {
					(_loader.vars.container as Object).addElement(this);
				} else {
					(_loader.vars.container as DisplayObjectContainer).addChild(this);
				}
			}
			var type:String;
			for (var p:String in _transformProps) {
				if (p in _loader.vars) {
					type = typeof(_transformProps[p]);
					this[p] = (type == "number") ? Number(_loader.vars[p]) : (type == "string") ? String(_loader.vars[p]) : Boolean(_loader.vars[p]);
				}
			}
			_bgColor = uint(_loader.vars.bgColor);
			_bgAlpha = ("bgAlpha" in _loader.vars) ? Number(_loader.vars.bgAlpha) : ("bgColor" in _loader.vars) ? 1 : 0;
			_fitWidth = ("fitWidth" in _loader.vars) ? Number(_loader.vars.fitWidth) : Number(_loader.vars.width);
			_fitHeight = ("fitHeight" in _loader.vars) ? Number(_loader.vars.fitHeight) : Number(_loader.vars.height);
			_update();
			if (_loader.content != this) {
				(_loader as Object).setContentDisplay(this);
			}
			this.rawContent = (_loader as Object).rawContent;
		}
	}
}