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

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

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