PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/!src/com/greensock12/loading/display/FlexContentDisplay.as

https://github.com/tluczyns/tluczyns-as3-base
ActionScript | 478 lines | 286 code | 28 blank | 164 comment | 101 complexity | fcfcfa798abd8d2dfe82bb6927816d23 MD5 | raw file
  1. /**
  2. * VERSION: 1.896
  3. * DATE: 2012-01-06
  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>.
  27. *
  28. *
  29. * <p><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:</p>
  32. * <listing version="3.0">
  33. import com.greensock.loading.~~;
  34. import com.greensock.loading.display.~~;
  35. LoaderMax.contentDisplayClass = FlexContentDisplay;
  36. </listing>
  37. *
  38. * <p>After that, all ImageLoaders, SWFLoaders, and VideoLoaders will return FlexContentDisplay objects
  39. * as their <code>content</code> instead of regular ContentDisplay objects. </p>
  40. *
  41. * <p><strong>Copyright 2009-2013, GreenSock. All rights reserved.</strong> 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 <a href="http://www.greensock.com/club/">Club GreenSock</a> members, the software agreement that was issued with the membership.</p>
  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. if (nativeBounds is LoaderInfo && _loader != null && _loader.progress < 1) {
  147. try {
  148. contentWidth = nativeBounds.width; //if not enough of the file has loaded, this can throw a runtime error saying that the "width" isn't known yet.
  149. } catch (error:Error) {
  150. nativeBounds = mc.getBounds(mc);
  151. }
  152. }
  153. contentWidth = nativeBounds.width * Math.abs(m.a) + nativeBounds.height * Math.abs(m.b);
  154. contentHeight = nativeBounds.width * Math.abs(m.c) + nativeBounds.height * Math.abs(m.d);
  155. }
  156. if (_fitWidth > 0 && _fitHeight > 0) {
  157. var w:Number = _fitWidth;
  158. var h:Number = _fitHeight;
  159. var wGap:Number = w - contentWidth;
  160. var hGap:Number = h - contentHeight;
  161. if (_scaleMode != "none") {
  162. var displayRatio:Number = w / h;
  163. var contentRatio:Number = nativeBounds.width / nativeBounds.height;
  164. if ((contentRatio < displayRatio && _scaleMode == "proportionalInside") || (contentRatio > displayRatio && _scaleMode == "proportionalOutside")) {
  165. w = h * contentRatio;
  166. }
  167. if ((contentRatio > displayRatio && _scaleMode == "proportionalInside") || (contentRatio < displayRatio && _scaleMode == "proportionalOutside")) {
  168. h = w / contentRatio;
  169. }
  170. if (_scaleMode != "heightOnly") {
  171. mc.width *= w / contentWidth;
  172. wGap = _fitWidth - w;
  173. }
  174. if (_scaleMode != "widthOnly") {
  175. mc.height *= h / contentHeight;
  176. hGap = _fitHeight - h;
  177. }
  178. }
  179. if (_hAlign == "left") {
  180. wGap = 0;
  181. } else if (_hAlign != "right") {
  182. wGap /= 2;
  183. }
  184. if (_vAlign == "top") {
  185. hGap = 0;
  186. } else if (_vAlign != "bottom") {
  187. hGap /= 2;
  188. }
  189. if (_crop) {
  190. //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.
  191. if (_cropContainer == null || mc.parent != _cropContainer) {
  192. _cropContainer = new Sprite();
  193. this.addChildAt(_cropContainer, this.getChildIndex(mc));
  194. _cropContainer.addChild(mc);
  195. }
  196. _cropContainer.x = left;
  197. _cropContainer.y = top;
  198. _cropContainer.scrollRect = new Rectangle(0, 0, _fitWidth, _fitHeight);
  199. mc.x = wGap;
  200. mc.y = hGap;
  201. } else {
  202. if (_cropContainer != null) {
  203. this.addChildAt(mc, this.getChildIndex(_cropContainer));
  204. _cropContainer = null;
  205. }
  206. mc.x = left + wGap;
  207. mc.y = top + hGap;
  208. }
  209. } else {
  210. mc.x = (_centerRegistration) ? contentWidth / -2 : 0;
  211. mc.y = (_centerRegistration) ? contentHeight / -2 : 0;
  212. }
  213. measure();
  214. }
  215. /** @private **/
  216. override protected function measure():void {
  217. var bounds:Rectangle;
  218. if (this.parent) {
  219. bounds = this.getBounds(this.parent);
  220. this.width = bounds.width;
  221. this.height = bounds.height;
  222. }
  223. bounds = this.getBounds(this);
  224. this.explicitWidth = bounds.width;
  225. this.explicitHeight = bounds.height;
  226. super.measure();
  227. }
  228. //---- GETTERS / SETTERS -------------------------------------------------------------------------
  229. /**
  230. * The width to which the <code>rawContent</code> should be fit according to the FlexContentDisplay's <code>scaleMode</code>
  231. * (this width is figured before rotation, scaleX, and scaleY). When a "width" property is defined in the loader's <code>vars</code>
  232. * property/parameter, it is automatically applied to this <code>fitWidth</code> property. For example, the following code will
  233. * set the loader's FlexContentDisplay <code>fitWidth</code> to 100:<p><code>
  234. *
  235. * var loader:ImageLoader = new ImageLoader("photo.jpg", {width:100, height:80, container:this});</code></p>
  236. *
  237. * @see #fitHeight
  238. * @see #scaleMode
  239. **/
  240. public function get fitWidth():Number {
  241. return _fitWidth;
  242. }
  243. public function set fitWidth(value:Number):void {
  244. _fitWidth = value;
  245. _update();
  246. }
  247. /**
  248. * The height to which the <code>rawContent</code> should be fit according to the FlexContentDisplay's <code>scaleMode</code>
  249. * (this height is figured before rotation, scaleX, and scaleY). When a "height" property is defined in the loader's <code>vars</code>
  250. * property/parameter, it is automatically applied to this <code>fitHeight</code> property. For example, the following code will
  251. * set the loader's FlexContentDisplay <code>fitHeight</code> to 80:<p><code>
  252. *
  253. * var loader:ImageLoader = new ImageLoader("photo.jpg", {width:100, height:80, container:this});</code></p>
  254. *
  255. * @see #fitWidth
  256. * @see #scaleMode
  257. **/
  258. public function get fitHeight():Number {
  259. return _fitHeight;
  260. }
  261. public function set fitHeight(value:Number):void {
  262. _fitHeight = value;
  263. _update();
  264. }
  265. /**
  266. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  267. * and <code>height</code> in the loader's <code>vars</code> property/parameter), the <code>scaleMode</code> controls how
  268. * the <code>rawContent</code> will be scaled to fit the area. The following values are recognized (you may use the
  269. * <code>com.greensock.layout.ScaleMode</code> constants if you prefer):
  270. * <ul>
  271. * <li><code>"stretch"</code> (the default) - The <code>rawContent</code> will fill the width/height exactly.</li>
  272. * <li><code>"proportionalInside"</code> - The <code>rawContent</code> will be scaled proportionally to fit inside the area defined by the width/height</li>
  273. * <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>
  274. * <li><code>"widthOnly"</code> - Only the width of the <code>rawContent</code> will be adjusted to fit.</li>
  275. * <li><code>"heightOnly"</code> - Only the height of the <code>rawContent</code> will be adjusted to fit.</li>
  276. * <li><code>"none"</code> - No scaling of the <code>rawContent</code> will occur.</li>
  277. * </ul>
  278. **/
  279. public function get scaleMode():String {
  280. return _scaleMode;
  281. }
  282. public function set scaleMode(value:String):void {
  283. if (_rawContent != null) {
  284. _rawContent.scaleX = _rawContent.scaleY = 1;
  285. }
  286. _scaleMode = value;
  287. _update();
  288. }
  289. /**
  290. * If <code>true</code>, the FlexContentDisplay's registration point will be placed in the center of the <code>rawContent</code>
  291. * which can be useful if, for example, you want to animate its scale and have it grow/shrink from its center.
  292. * @see #scaleMode
  293. **/
  294. public function get centerRegistration():Boolean {
  295. return _centerRegistration;
  296. }
  297. public function set centerRegistration(value:Boolean):void {
  298. _centerRegistration = value;
  299. _update();
  300. }
  301. /**
  302. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  303. * and <code>height</code> in the loader's <code>vars</code> property/parameter), setting <code>crop</code> to
  304. * <code>true</code> will cause the <code>rawContent</code> to be cropped within that area (by applying a <code>scrollRect</code>
  305. * for maximum performance). This is typically useful when the <code>scaleMode</code> is <code>"proportionalOutside"</code>
  306. * or <code>"none"</code> so that any parts of the <code>rawContent</code> that exceed the dimensions defined by
  307. * <code>fitWidth</code> and <code>fitHeight</code> are visually chopped off. Use the <code>hAlign</code> and
  308. * <code>vAlign</code> properties to control the vertical and horizontal alignment within the cropped area.
  309. *
  310. * @see #scaleMode
  311. **/
  312. public function get crop():Boolean {
  313. return _crop;
  314. }
  315. public function set crop(value:Boolean):void {
  316. _crop = value;
  317. _update();
  318. }
  319. /**
  320. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  321. * and <code>height</code> in the loader's <code>vars</code> property/parameter), the <code>hAlign</code> determines how
  322. * the <code>rawContent</code> is horizontally aligned within that area. The following values are recognized (you may use the
  323. * <code>com.greensock.layout.AlignMode</code> constants if you prefer):
  324. * <ul>
  325. * <li><code>"center"</code> (the default) - The <code>rawContent</code> will be centered horizontally in the FlexContentDisplay</li>
  326. * <li><code>"left"</code> - The <code>rawContent</code> will be aligned with the left side of the FlexContentDisplay</li>
  327. * <li><code>"right"</code> - The <code>rawContent</code> will be aligned with the right side of the FlexContentDisplay</li>
  328. * </ul>
  329. * @see #scaleMode
  330. * @see #vAlign
  331. **/
  332. public function get hAlign():String {
  333. return _hAlign;
  334. }
  335. public function set hAlign(value:String):void {
  336. _hAlign = value;
  337. _update();
  338. }
  339. /**
  340. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  341. * and <code>height</code> in the loader's <code>vars</code> property/parameter), the <code>vAlign</code> determines how
  342. * the <code>rawContent</code> is vertically aligned within that area. The following values are recognized (you may use the
  343. * <code>com.greensock.layout.AlignMode</code> constants if you prefer):
  344. * <ul>
  345. * <li><code>"center"</code> (the default) - The <code>rawContent</code> will be centered vertically in the FlexContentDisplay</li>
  346. * <li><code>"top"</code> - The <code>rawContent</code> will be aligned with the top of the FlexContentDisplay</li>
  347. * <li><code>"bottom"</code> - The <code>rawContent</code> will be aligned with the bottom of the FlexContentDisplay</li>
  348. * </ul>
  349. * @see #scaleMode
  350. * @see #hAlign
  351. **/
  352. public function get vAlign():String {
  353. return _vAlign;
  354. }
  355. public function set vAlign(value:String):void {
  356. _vAlign = value;
  357. _update();
  358. }
  359. /**
  360. * When the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code> properties are defined (or <code>width</code>
  361. * and <code>height</code> in the loader's <code>vars</code> property/parameter), a rectangle will be drawn inside the
  362. * FlexContentDisplay object immediately in order to ease the development process (for example, you can add <code>ROLL_OVER/ROLL_OUT</code>
  363. * event listeners immediately). It is transparent by default, but you may define a <code>bgAlpha</code> if you prefer.
  364. * @see #bgAlpha
  365. * @see #fitWidth
  366. * @see #fitHeight
  367. **/
  368. public function get bgColor():uint {
  369. return _bgColor;
  370. }
  371. public function set bgColor(value:uint):void {
  372. _bgColor = value;
  373. _update();
  374. }
  375. /**
  376. * Controls the alpha of the rectangle that is drawn when the FlexContentDisplay's <code>fitWidth</code> and <code>fitHeight</code>
  377. * properties are defined (or <code>width</code> and <code>height</code> in the loader's <code>vars</code> property/parameter).
  378. * @see #bgColor
  379. * @see #fitWidth
  380. * @see #fitHeight
  381. **/
  382. public function get bgAlpha():Number {
  383. return _bgAlpha;
  384. }
  385. public function set bgAlpha(value:Number):void {
  386. _bgAlpha = value;
  387. _update();
  388. }
  389. /** 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. **/
  390. public function get rawContent():* {
  391. return _rawContent;
  392. }
  393. public function set rawContent(value:*):void {
  394. if (_rawContent != null && _rawContent != value) {
  395. if (_rawContent.parent == this) {
  396. removeChild(_rawContent);
  397. } else if (_cropContainer != null && _rawContent.parent == _cropContainer) {
  398. _cropContainer.removeChild(_rawContent);
  399. removeChild(_cropContainer);
  400. _cropContainer = null;
  401. }
  402. }
  403. _rawContent = value as DisplayObject;
  404. if (_rawContent == null) {
  405. return;
  406. } else if (_rawContent.parent == null || (_rawContent.parent != this && _rawContent.parent != _cropContainer)) {
  407. addChildAt(_rawContent as DisplayObject, 0);
  408. }
  409. _nativeRect = new Rectangle(0, 0, _rawContent.width, _rawContent.height);
  410. _update();
  411. }
  412. /** The loader whose rawContent populates this FlexContentDisplay. If you get the loader's <code>content</code>, it will return this FlexContentDisplay object. **/
  413. public function get loader():LoaderItem {
  414. return _loader;
  415. }
  416. public function set loader(value:LoaderItem):void {
  417. _loader = value;
  418. if (value == null) {
  419. return;
  420. } else if (!_loader.hasOwnProperty("setContentDisplay")) {
  421. throw new Error("Incompatible loader used for a FlexContentDisplay");
  422. }
  423. this.name = _loader.name;
  424. var type:String;
  425. for (var p:String in _transformProps) {
  426. if (p in _loader.vars) {
  427. type = typeof(_transformProps[p]);
  428. this[p] = (type == "number") ? Number(_loader.vars[p]) : (type == "string") ? String(_loader.vars[p]) : Boolean(_loader.vars[p]);
  429. }
  430. }
  431. _bgColor = uint(_loader.vars.bgColor);
  432. _bgAlpha = ("bgAlpha" in _loader.vars) ? Number(_loader.vars.bgAlpha) : ("bgColor" in _loader.vars) ? 1 : 0;
  433. _fitWidth = ("fitWidth" in _loader.vars) ? Number(_loader.vars.fitWidth) : Number(_loader.vars.width);
  434. _fitHeight = ("fitHeight" in _loader.vars) ? Number(_loader.vars.fitHeight) : Number(_loader.vars.height);
  435. _update();
  436. if (_loader.vars.container is DisplayObjectContainer) {
  437. if (_loader.vars.container.hasOwnProperty("addElement")) {
  438. (_loader.vars.container as Object).addElement(this);
  439. } else {
  440. (_loader.vars.container as DisplayObjectContainer).addChild(this);
  441. }
  442. }
  443. if (_loader.content != this) {
  444. (_loader as Object).setContentDisplay(this);
  445. }
  446. this.rawContent = (_loader as Object).rawContent;
  447. }
  448. }
  449. }