PageRenderTime 29ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

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