PageRenderTime 129ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/blogs.dir/2/files/2011/02/20110220-dancemarathon/SatPM/com/greensock/loading/display/ContentDisplay.as

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