PageRenderTime 58ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

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

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