PageRenderTime 32ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

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

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