PageRenderTime 50ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/source/temple/utils/FrameDelay.as

http://templelibrary.googlecode.com/
ActionScript | 213 lines | 91 code | 23 blank | 99 comment | 11 complexity | 8c44100bb74efdb9d4672d4f0bf3d863 MD5 | raw file
  1. /*
  2. * Temple Library for ActionScript 3.0
  3. * Copyright Š MediaMonks B.V.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by MediaMonks B.V.
  16. * 4. Neither the name of MediaMonks B.V. nor the
  17. * names of its contributors may be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY MEDIAMONKS B.V. ''AS IS'' AND ANY
  21. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  22. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL MEDIAMONKS B.V. BE LIABLE FOR ANY
  24. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  25. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  27. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  29. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. *
  32. * Note: This license does not apply to 3rd party classes inside the Temple
  33. * repository with their own license!
  34. */
  35. package temple.utils
  36. {
  37. import temple.common.interfaces.IPauseable;
  38. import temple.core.CoreObject;
  39. import temple.core.debug.IDebuggable;
  40. import temple.utils.types.FunctionUtils;
  41. import flash.events.Event;
  42. /**
  43. * Delay a function call with one or more frames. Use this when initializing a SWF or a bunch of DisplayObjects, to enable the player to do its thing. Usually a single frame delay will do the job, since the next enterFrame will come when all other jobs are finished.
  44. * To execute function 'init' after 1 frame, use:
  45. *
  46. * @example
  47. * <listing version="3.0">
  48. * new FrameDelay(init);
  49. * </listing>
  50. *
  51. * To execute function 'init' after 10 frames, use:
  52. * <listing version="3.0">
  53. * new FrameDelay(init, 10);
  54. * </listing>
  55. *
  56. * To call function 'setProps' with parameters, executed after 1 frame:
  57. * <listing version="3.0">
  58. * new FrameDelay(setProps, 1, [shape, 'alpha', 0]);
  59. * </listing>
  60. *
  61. * <listing version="3.0">
  62. * private function setProps (shape:Shape, property:String, value:Number):void
  63. * {
  64. * shape[property] = value;
  65. * }
  66. * </listing>
  67. *
  68. * @author ASAPLibrary, Thijs Broerse
  69. */
  70. public class FrameDelay extends CoreObject implements IPauseable, IDebuggable
  71. {
  72. /**
  73. * Make frame-delayed callback: (eg: a closure to .resume() of a paused FrameDelay)
  74. */
  75. public static function closure(callback:Function, frameCount:int = 1, params:Array = null):Function
  76. {
  77. var fd:FrameDelay = new FrameDelay(callback, frameCount, params);
  78. fd.pause();
  79. return fd.resume;
  80. }
  81. private var _isDone:Boolean = false;
  82. private var _currentFrame:int;
  83. private var _callback:Function;
  84. private var _params:Array;
  85. private var _paused:Boolean;
  86. private var _debug:Boolean;
  87. private var _callbackString:String;
  88. /**
  89. * Creates a new FrameDelay. Starts the delay immediately.
  90. * @param callback the callback function to be called when done waiting
  91. * @param frameCount the number of frames to wait; when left out, or set to 1 or 0, one frame is waited
  92. * @param params list of parameters to pass to the callback function
  93. * @param debug if set to true, debug information will be logged.
  94. */
  95. public function FrameDelay(callback:Function, frameCount:int = 1, params:Array = null, debug:Boolean = false)
  96. {
  97. this.toStringProps.push('callback');
  98. this._currentFrame = frameCount;
  99. this._callback = callback;
  100. this._params = params;
  101. this._isDone = frameCount <= 1;
  102. FramePulse.addEnterFrameListener(this.handleEnterFrame);
  103. this.debug = debug;
  104. }
  105. /**
  106. * Returns the callback as a String, useful for debug purposes.
  107. */
  108. public function get callback():String
  109. {
  110. return this._callbackString ||= FunctionUtils.functionToString(this._callback);
  111. }
  112. /**
  113. * @inheritDoc
  114. */
  115. public function pause():void
  116. {
  117. if (this.debug) this.logDebug("pause: ");
  118. FramePulse.removeEnterFrameListener(this.handleEnterFrame);
  119. this._paused = true;
  120. }
  121. /**
  122. * @inheritDoc
  123. */
  124. public function resume():void
  125. {
  126. if (this.debug) this.logDebug("resume: ");
  127. if (!this.isDestructed && this._paused)
  128. {
  129. FramePulse.addEnterFrameListener(handleEnterFrame);
  130. }
  131. }
  132. /**
  133. * @inheritDoc
  134. */
  135. public function get paused():Boolean
  136. {
  137. return this._paused;
  138. }
  139. /**
  140. * @inheritDoc
  141. */
  142. public function get debug():Boolean
  143. {
  144. return this._debug;
  145. }
  146. /**
  147. * @inheritDoc
  148. */
  149. public function set debug(value:Boolean):void
  150. {
  151. this._debug = value;
  152. }
  153. /**
  154. * Handle the Event.ENTER_FRAME event.
  155. * Checks if still waiting - when true: calls callback function.
  156. * @param event not used
  157. */
  158. private function handleEnterFrame(event:Event):void
  159. {
  160. if (this._isDone)
  161. {
  162. FramePulse.removeEnterFrameListener(this.handleEnterFrame);
  163. if (this._callback != null)
  164. {
  165. if (this.debug) this.logDebug("Done, execute callback: ");
  166. this._callback.apply(null, this._params);
  167. }
  168. this.destruct();
  169. }
  170. else
  171. {
  172. this._currentFrame--;
  173. this._isDone = (this._currentFrame <= 1);
  174. if (this.debug) this.logDebug("handleEnterFrame: wait for " + this._currentFrame + " frames...");
  175. }
  176. }
  177. /**
  178. * Release reference to creating object.
  179. * Use this to remove a FrameDelay object that is still running when the creating object will be removed.
  180. */
  181. override public function destruct():void
  182. {
  183. if (this.debug) this.logDebug("destruct: ");
  184. FramePulse.removeEnterFrameListener(this.handleEnterFrame);
  185. this._callback = null;
  186. this._params = null;
  187. super.destruct();
  188. }
  189. }
  190. }