PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/src/com/greensock/loading/display/FlexContentDisplay.as

https://bitbucket.org/tshubbard/zfstarlingtutorial
ActionScript | 471 lines | 279 code | 28 blank | 164 comment | 97 complexity | a16c3155b60887c1533876bc49d3acc2 MD5 | raw file
  1. /**
  2. * VERSION: 1.895
  3. * DATE: 2011-11-27
  4. * AS3
  5. * UPDATES AND DOCS AT: http://www.greensock.com/loadermax/
  6. **/
  7. package com.greensock.loading.display {
  8. import com.greensock.loading.core.LoaderItem;
  9. import flash.display.DisplayObject;
  10. import flash.display.DisplayObjectContainer;
  11. import flash.display.Loader;
  12. import flash.display.LoaderInfo;
  13. import flash.display.Sprite;
  14. import flash.geom.Matrix;
  15. import flash.geom.Rectangle;
  16. import flash.media.Video;
  17. import mx.core.UIComponent;
  18. /**
  19. * A container for visual content that is loaded by any of the following: ImageLoaders, SWFLoaders,
  20. * or VideoLoaders which is to be used in Flex. It is essentially a UIComponent that has a <code>loader</code>
  21. * property for easily referencing the original loader, as well as several other useful properties for
  22. * controling the placement of <code>rawContent</code> and the way it is scaled to fit (if at all). That way,
  23. * you can add a FlexContentDisplay to the display list or populate an array with as many as you want and then if
  24. * you ever need to unload() the content or reload it or figure out its url, etc., you can reference your
  25. * FlexContentDisplay's <code>loader</code> property like <code>myContent.loader.url</code> or
  26. * <code>(myContent.loader as SWFLoader).getClass("com.greensock.TweenLite");</code>. For
  27. * <br /><br />
  28. *
  29. * <strong>IMPORTANT</strong>: In order for the LoaderMax loaders to use FlexContentDisplay instead of
  30. * the regular ContentDisplay class, you must set the <code>LoaderMax.contentDisplayClass</code> property
  31. * to FlexContentDisplay once like:
  32. * @example Example AS3 code:<listing version="3.0">
  33. import com.greensock.loading.~~;
  34. import com.greensock.loading.display.~~;
  35. LoaderMax.contentDisplayClass = FlexContentDisplay;
  36. </listing>
  37. *
  38. * After that, all ImageLoaders, SWFLoaders, and VideoLoaders will return FlexContentDisplay objects
  39. * as their <code>content</code> instead of regular ContentDisplay objects. <br /><br />
  40. *
  41. * <b>Copyright 2011, 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.
  42. *
  43. * @author Jack Doyle, jack@greensock.com
  44. */
  45. public class FlexContentDisplay extends UIComponent {
  46. /** @private **/
  47. protected static var _transformProps:Object = {x:1, y:1, z:1, rotationX:1, rotationY:1, rotationZ:1, scaleX:1, scaleY:1, rotation:1, alpha:1, visible:true, blendMode:"normal", centerRegistration:false, crop:false, scaleMode:"stretch", hAlign:"center", vAlign:"center"};
  48. /** @private **/
  49. protected var _loader:LoaderItem;
  50. /** @private **/
  51. protected var _rawContent:DisplayObject;
  52. /** @private **/
  53. protected var _centerRegistration:Boolean;
  54. /** @private **/
  55. protected var _crop:Boolean;
  56. /** @private **/
  57. protected var _scaleMode:String = "stretch";
  58. /** @private **/
  59. protected var _hAlign:String = "center";
  60. /** @private **/
  61. protected var _vAlign:String = "center";
  62. /** @private **/
  63. protected var _bgColor:uint;
  64. /** @private **/
  65. protected var _bgAlpha:Number = 0;
  66. /** @private **/
  67. protected var _fitWidth:Number;
  68. /** @private **/
  69. protected var _fitHeight:Number;
  70. /** @private only used when crop is true - works around bugs in Flash with the way it reports getBounds() on objects with a scrollRect. **/
  71. protected var _cropContainer:Sprite;
  72. /** @private Primarily for Video objects which don't act like anything else - we must store the original width/height ratio in this variable so that we can properly apply scaleModes **/
  73. protected var _nativeRect:Rectangle;
  74. /** @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. **/
  75. public var gcProtect:*;
  76. /** Arbitrary data that you can associate with the FlexContentDisplay instance. For example, you could set <code>data</code> to be an object containing various other properties or set it to an index number related to an array in your application. It is completely optional and arbitrary. **/
  77. public var data:*;
  78. /**
  79. * Constructor
  80. *
  81. * @param loader The Loader object that will populate the FlexContentDisplay's <code>rawContent</code>.
  82. */
  83. public function FlexContentDisplay(loader:LoaderItem) {
  84. super();
  85. this.loader = loader;
  86. }
  87. /**
  88. * Removes the FlexContentDisplay from the display list (if necessary), dumps the <code>rawContent</code>,
  89. * and calls <code>unload()</code> and <code>dispose()</code> on the loader (unless you define otherwise with
  90. * the optional parameters). This essentially destroys the FlexContentDisplay and makes it eligible for garbage
  91. * collection internally, although if you added any listeners manually, you should remove them as well.
  92. *
  93. * @param unloadLoader If <code>true</code>, <code>unload()</code> will be called on the loader. It is <code>true</code> by default.
  94. * @param disposeLoader If <code>true</code>, <code>dispose()</code> will be called on the loader. It is <code>true</code> by default.
  95. */
  96. public function dispose(unloadLoader:Boolean=true, disposeLoader:Boolean=true):void {
  97. if (this.parent != null) {
  98. if (this.parent.hasOwnProperty("removeElement")) {
  99. (this.parent as Object).removeElement(this);
  100. } else {
  101. this.parent.removeChild(this);
  102. }
  103. }
  104. this.rawContent = null;
  105. this.gcProtect = null;
  106. _cropContainer = null;
  107. if (_loader != null) {
  108. if (unloadLoader) {
  109. _loader.unload();
  110. }
  111. if (disposeLoader) {
  112. _loader.dispose(false);
  113. _loader = null;
  114. }
  115. }
  116. }
  117. /** @private **/
  118. protected function _update():void {
  119. var left:Number = (_centerRegistration && _fitWidth > 0) ? _fitWidth / -2 : 0;
  120. var top:Number = (_centerRegistration && _fitHeight > 0) ? _fitHeight / -2 : 0;
  121. graphics.clear();
  122. if (_fitWidth > 0 && _fitHeight > 0) {
  123. graphics.beginFill(_bgColor, _bgAlpha);
  124. graphics.drawRect(left, top, _fitWidth, _fitHeight);
  125. graphics.endFill();
  126. }
  127. if (_rawContent == null) {
  128. measure();
  129. return;
  130. }
  131. var mc:DisplayObject = _rawContent;
  132. var m:Matrix = mc.transform.matrix;
  133. var nativeBounds:Object, contentWidth:Number, contentHeight:Number;
  134. if (mc is Video) {//Video objects don't accurately report getBounds() - they act like their native dimension is always 160x320.
  135. nativeBounds = _nativeRect;
  136. contentWidth = mc.width;
  137. contentHeight = mc.height;
  138. } else {
  139. if (mc is Loader) {
  140. nativeBounds = Loader(mc).contentLoaderInfo;
  141. } else if (_loader != null && _loader.hasOwnProperty("getClass")) {
  142. nativeBounds = mc.loaderInfo; //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)
  143. } else {
  144. nativeBounds = mc.getBounds(mc);
  145. }
  146. contentWidth = nativeBounds.width * Math.abs(m.a) + nativeBounds.height * Math.abs(m.b);
  147. contentHeight = nativeBounds.width * Math.abs(m.c) + nativeBounds.height * Math.abs(m.d);
  148. }
  149. if (_fitWidth > 0 && _fitHeight > 0) {
  150. var w:Number = _fitWidth;
  151. var h:Number = _fitHeight;
  152. var wGap:Number = w - contentWidth;
  153. var hGap:Number = h - contentHeight;
  154. if (_scaleMode != "none") {
  155. var displayRatio:Number = w / h;
  156. var contentRatio:Number = nativeBounds.width / nativeBounds.height;
  157. if ((contentRatio < displayRatio && _scaleMode == "proportionalInside") || (contentRatio > displayRatio && _scaleMode == "proportionalOutside")) {
  158. w = h * contentRatio;
  159. }
  160. if ((contentRatio > displayRatio && _scaleMode == "proportionalInside") || (contentRatio < displayRatio && _scaleMode == "proportionalOutside")) {
  161. h = w / contentRatio;
  162. }
  163. if (_scaleMode != "heightOnly") {
  164. mc.width *= w / contentWidth;
  165. wGap = _fitWidth - w;
  166. }
  167. if (_scaleMode != "widthOnly") {
  168. mc.height *= h / contentHeight;
  169. hGap = _fitHeight - h;
  170. }
  171. }
  172. if (_hAlign == "left") {
  173. wGap = 0;
  174. } else if (_hAlign != "right") {
  175. wGap /= 2;
  176. }
  177. if (_vAlign == "top") {
  178. hGap = 0;
  179. } else if (_vAlign != "bottom") {
  180. hGap /= 2;
  181. }
  182. if (_crop) {
  183. //due to bugs in the way Flash reports getBounds() on objects with a scrollRect, we need to just wrap the rawContent in a container and apply the scrollRect to the container.
  184. if (_cropContainer == null || mc.parent != _cropContainer) {
  185. _cropContainer = new Sprite();
  186. this.addChildAt(_cropContainer, this.getChildIndex(mc));
  187. _cropContainer.addChild(mc);
  188. }
  189. _cropContainer.x = left;
  190. _cropContainer.y = top;
  191. _cropContainer.scrollRect = new Rectangle(0, 0, _fitWidth, _fitHeight);
  192. mc.x = wGap;
  193. mc.y = hGap;
  194. } else {
  195. if (_cropContainer != null) {
  196. this.addChildAt(mc, this.getChildIndex(_cropContainer));
  197. _cropContainer = null;
  198. }
  199. mc.x = left + wGap;
  200. mc.y = top + hGap;
  201. }
  202. } else {
  203. mc.x = (_centerRegistration) ? contentWidth / -2 : 0;
  204. mc.y = (_centerRegistration) ? contentHeight / -2 : 0;
  205. }
  206. measure();
  207. }
  208. /** @private **/
  209. override protected function measure():void {
  210. var bounds:Rectangle;
  211. if (this.parent) {
  212. bounds = this.getBounds(this.parent);
  213. this.width = bounds.width;
  214. this.height = bounds.height;
  215. }
  216. bounds = this.getBounds(this);
  217. this.explicitWidth = bounds.width;
  218. this.explicitHeight = bounds.height;
  219. super.measure();
  220. }
  221. //---- GETTERS / SETTERS -------------------------------------------------------------------------
  222. /**
  223. * The width to which the <code>rawContent</code> should be fit according to the FlexContentDisplay's <code>scaleMode</code>
  224. * (this width is figured before rotation, scaleX, and scaleY). When a "width" property is defined in the loader's <code>vars</code>
  225. * property/parameter, it is automatically applied to this <code>fitWidth</code> property. For example, the following code will
  226. * set the loader's FlexContentDisplay <code>fitWidth</code> to 100:<code><br /><br />
  227. *
  228. * var loader:ImageLoader = new ImageLoader("photo.jpg", {width:100, height:80, container:this});</code>
  229. *
  230. * @see #fitHeight
  231. * @see #scaleMode
  232. **/
  233. public function get fitWidth():Number {
  234. return _fitWidth;
  235. }
  236. public function set fitWidth(value:Number):void {
  237. _fitWidth = value;
  238. _update();
  239. }
  240. /**
  241. * The height to which the <code>rawContent</code> should be fit according to the FlexContentDisplay's <code>scaleMode</code>
  242. * (this height is figured before rotation, scaleX, and scaleY). When a "height" property is defined in the loader's <code>vars</code>
  243. * property/parameter, it is automatically applied to this <code>fitHeight</code> property. For example, the following code will
  244. * set the loader's FlexContentDisplay <code>fitHeight</code> to 80:<code><br /><br />
  245. *
  246. * var loader:ImageLoader = new ImageLoader("photo.jpg", {width:100, height:80, container:this});</code>
  247. *
  248. * @see #fitWidth
  249. * @see #scaleMode
  250. **/
  251. public function get fitHeight():Number {
  252. return _fitHeight;
  253. }
  254. public function set fitHeight(value:Number):void {
  255. _fitHeight = value;
  256. _update();
  257. }
  258. /**
  259. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  260. * and <code>height</code> in the loader's <code>vars</code> property/parameter), the <code>scaleMode</code> controls how
  261. * the <code>rawContent</code> will be scaled to fit the area. The following values are recognized (you may use the
  262. * <code>com.greensock.layout.ScaleMode</code> constants if you prefer):
  263. * <ul>
  264. * <li><code>"stretch"</code> (the default) - The <code>rawContent</code> will fill the width/height exactly.</li>
  265. * <li><code>"proportionalInside"</code> - The <code>rawContent</code> will be scaled proportionally to fit inside the area defined by the width/height</li>
  266. * <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>
  267. * <li><code>"widthOnly"</code> - Only the width of the <code>rawContent</code> will be adjusted to fit.</li>
  268. * <li><code>"heightOnly"</code> - Only the height of the <code>rawContent</code> will be adjusted to fit.</li>
  269. * <li><code>"none"</code> - No scaling of the <code>rawContent</code> will occur.</li>
  270. * </ul>
  271. **/
  272. public function get scaleMode():String {
  273. return _scaleMode;
  274. }
  275. public function set scaleMode(value:String):void {
  276. if (_rawContent != null) {
  277. _rawContent.scaleX = _rawContent.scaleY = 1;
  278. }
  279. _scaleMode = value;
  280. _update();
  281. }
  282. /**
  283. * If <code>true</code>, the FlexContentDisplay's registration point will be placed in the center of the <code>rawContent</code>
  284. * which can be useful if, for example, you want to animate its scale and have it grow/shrink from its center.
  285. * @see #scaleMode
  286. **/
  287. public function get centerRegistration():Boolean {
  288. return _centerRegistration;
  289. }
  290. public function set centerRegistration(value:Boolean):void {
  291. _centerRegistration = value;
  292. _update();
  293. }
  294. /**
  295. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  296. * and <code>height</code> in the loader's <code>vars</code> property/parameter), setting <code>crop</code> to
  297. * <code>true</code> will cause the <code>rawContent</code> to be cropped within that area (by applying a <code>scrollRect</code>
  298. * for maximum performance). This is typically useful when the <code>scaleMode</code> is <code>"proportionalOutside"</code>
  299. * or <code>"none"</code> so that any parts of the <code>rawContent</code> that exceed the dimensions defined by
  300. * <code>fitWidth</code> and <code>fitHeight</code> are visually chopped off. Use the <code>hAlign</code> and
  301. * <code>vAlign</code> properties to control the vertical and horizontal alignment within the cropped area.
  302. *
  303. * @see #scaleMode
  304. **/
  305. public function get crop():Boolean {
  306. return _crop;
  307. }
  308. public function set crop(value:Boolean):void {
  309. _crop = value;
  310. _update();
  311. }
  312. /**
  313. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  314. * and <code>height</code> in the loader's <code>vars</code> property/parameter), the <code>hAlign</code> determines how
  315. * the <code>rawContent</code> is horizontally aligned within that area. The following values are recognized (you may use the
  316. * <code>com.greensock.layout.AlignMode</code> constants if you prefer):
  317. * <ul>
  318. * <li><code>"center"</code> (the default) - The <code>rawContent</code> will be centered horizontally in the FlexContentDisplay</li>
  319. * <li><code>"left"</code> - The <code>rawContent</code> will be aligned with the left side of the FlexContentDisplay</li>
  320. * <li><code>"right"</code> - The <code>rawContent</code> will be aligned with the right side of the FlexContentDisplay</li>
  321. * </ul>
  322. * @see #scaleMode
  323. * @see #vAlign
  324. **/
  325. public function get hAlign():String {
  326. return _hAlign;
  327. }
  328. public function set hAlign(value:String):void {
  329. _hAlign = value;
  330. _update();
  331. }
  332. /**
  333. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  334. * and <code>height</code> in the loader's <code>vars</code> property/parameter), the <code>vAlign</code> determines how
  335. * the <code>rawContent</code> is vertically aligned within that area. The following values are recognized (you may use the
  336. * <code>com.greensock.layout.AlignMode</code> constants if you prefer):
  337. * <ul>
  338. * <li><code>"center"</code> (the default) - The <code>rawContent</code> will be centered vertically in the FlexContentDisplay</li>
  339. * <li><code>"top"</code> - The <code>rawContent</code> will be aligned with the top of the FlexContentDisplay</li>
  340. * <li><code>"bottom"</code> - The <code>rawContent</code> will be aligned with the bottom of the FlexContentDisplay</li>
  341. * </ul>
  342. * @see #scaleMode
  343. * @see #hAlign
  344. **/
  345. public function get vAlign():String {
  346. return _vAlign;
  347. }
  348. public function set vAlign(value:String):void {
  349. _vAlign = value;
  350. _update();
  351. }
  352. /**
  353. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  354. * and <code>height</code> in the loader's <code>vars</code> property/parameter), a rectangle will be drawn inside the
  355. * FlexContentDisplay object immediately in order to ease the development process (for example, you can add <code>ROLL_OVER/ROLL_OUT</code>
  356. * event listeners immediately). It is transparent by default, but you may define a <code>bgAlpha</code> if you prefer.
  357. * @see #bgAlpha
  358. * @see #fitWidth
  359. * @see #fitHeight
  360. **/
  361. public function get bgColor():uint {
  362. return _bgColor;
  363. }
  364. public function set bgColor(value:uint):void {
  365. _bgColor = value;
  366. _update();
  367. }
  368. /**
  369. * Controls the alpha of the rectangle that is drawn when the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code>
  370. * properties are defined (or <code>width</code> and <code>height</code> in the loader's <code>vars</code> property/parameter).
  371. * @see #bgColor
  372. * @see #fitWidth
  373. * @see #fitHeight
  374. **/
  375. public function get bgAlpha():Number {
  376. return _bgAlpha;
  377. }
  378. public function set bgAlpha(value:Number):void {
  379. _bgAlpha = value;
  380. _update();
  381. }
  382. /** 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. **/
  383. public function get rawContent():* {
  384. return _rawContent;
  385. }
  386. public function set rawContent(value:*):void {
  387. if (_rawContent != null && _rawContent != value) {
  388. if (_rawContent.parent == this) {
  389. removeChild(_rawContent);
  390. } else if (_cropContainer != null && _rawContent.parent == _cropContainer) {
  391. _cropContainer.removeChild(_rawContent);
  392. removeChild(_cropContainer);
  393. _cropContainer = null;
  394. }
  395. }
  396. _rawContent = value as DisplayObject;
  397. if (_rawContent == null) {
  398. return;
  399. } else if (_rawContent.parent == null || (_rawContent.parent != this && _rawContent.parent != _cropContainer)) {
  400. addChildAt(_rawContent as DisplayObject, 0);
  401. }
  402. _nativeRect = new Rectangle(0, 0, _rawContent.width, _rawContent.height);
  403. _update();
  404. }
  405. /** The loader whose rawContent populates this FlexContentDisplay. If you get the loader's <code>content</code>, it will return this FlexContentDisplay object. **/
  406. public function get loader():LoaderItem {
  407. return _loader;
  408. }
  409. public function set loader(value:LoaderItem):void {
  410. _loader = value;
  411. if (value == null) {
  412. return;
  413. } else if (!_loader.hasOwnProperty("setContentDisplay")) {
  414. throw new Error("Incompatible loader used for a FlexContentDisplay");
  415. }
  416. this.name = _loader.name;
  417. var type:String;
  418. for (var p:String in _transformProps) {
  419. if (p in _loader.vars) {
  420. type = typeof(_transformProps[p]);
  421. this[p] = (type == "number") ? Number(_loader.vars[p]) : (type == "string") ? String(_loader.vars[p]) : Boolean(_loader.vars[p]);
  422. }
  423. }
  424. _bgColor = uint(_loader.vars.bgColor);
  425. _bgAlpha = ("bgAlpha" in _loader.vars) ? Number(_loader.vars.bgAlpha) : ("bgColor" in _loader.vars) ? 1 : 0;
  426. _fitWidth = ("fitWidth" in _loader.vars) ? Number(_loader.vars.fitWidth) : Number(_loader.vars.width);
  427. _fitHeight = ("fitHeight" in _loader.vars) ? Number(_loader.vars.fitHeight) : Number(_loader.vars.height);
  428. _update();
  429. if (_loader.vars.container is DisplayObjectContainer) {
  430. if (_loader.vars.container.hasOwnProperty("addElement")) {
  431. (_loader.vars.container as Object).addElement(this);
  432. } else {
  433. (_loader.vars.container as DisplayObjectContainer).addChild(this);
  434. }
  435. }
  436. if (_loader.content != this) {
  437. (_loader as Object).setContentDisplay(this);
  438. }
  439. this.rawContent = (_loader as Object).rawContent;
  440. }
  441. }
  442. }