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

/DiegoRay/actionscripts/org/asapframework/util/loader/LoaderWorker.as

https://github.com/joemaffia/flash-junk
ActionScript | 207 lines | 76 code | 40 blank | 91 comment | 3 complexity | c81268f388ad7e7fcd61b593fb4389d8 MD5 | raw file
  1. /*
  2. Copyright 2005-2006 by the authors of asapframework, http://asapframework.org
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // ASAP classes
  14. import org.asapframework.events.Dispatcher;
  15. import org.asapframework.util.debug.Log;
  16. import org.asapframework.util.FrameDelay;
  17. import org.asapframework.util.loader.FileData;
  18. import org.asapframework.util.loader.LoaderWorkerEvent;
  19. /**
  20. * Used internally by {@link Loader}.
  21. * @author Arthur Clemens
  22. * @author Martijn de Visser, added check for already loaded movies. These are now unloaded first.
  23. * @author Martijn de Visser, implemented MovieClipLoader instead of MovieClip.loadMovie - major refactoring of code.
  24. */
  25. class org.asapframework.util.loader.LoaderWorker extends Dispatcher {
  26. private var mNumber:Number; /**< Loader's worker number, counting from zero (used for debugging). */
  27. private var mLoadingState:Boolean = false;
  28. private var mLoadingData:FileData;
  29. private var mLoader:MovieClipLoader;
  30. private static var TIMEOUT:Number = 10000; /**< Initial timeout when waiting for data from server (10 seconds). */
  31. /**
  32. Creates a new LoaderWorker object.
  33. */
  34. function LoaderWorker (inNumber:Number) {
  35. super();
  36. mNumber = inNumber;
  37. // setup MovieClipLoader
  38. mLoader = new MovieClipLoader();
  39. mLoader.addListener(this);
  40. }
  41. /**
  42. Starts loading content
  43. @param data: a {@link FileData} object
  44. */
  45. public function load (inData:FileData) : Void {
  46. //delete mLoadingData;
  47. mLoadingData = inData;
  48. mLoadingState = true;
  49. // was a movie already loaded in target?
  50. var progress:Object = mLoader.getProgress(inData.location);
  51. var b:Number = progress.bytesLoaded;
  52. if (b > 4) {
  53. unloadContent(true);
  54. } else {
  55. startLoading();
  56. }
  57. }
  58. /**
  59. Stops loading the asset
  60. */
  61. public function stopLoading () : Void {
  62. // unload clip
  63. mLoader.unloadClip(mLoadingData.location);
  64. // reset
  65. resetState();
  66. sendDoneMessage();
  67. }
  68. /**
  69. The loading state of the worker, true: the worker is currently loading; false: the worker has not yet started loading, or has finished or has encountered an error.
  70. */
  71. public function get loading () : Boolean {
  72. return mLoadingState;
  73. }
  74. /**
  75. The {@link FileData} object.
  76. */
  77. public function get loadingData () : FileData {
  78. return mLoadingData;
  79. }
  80. // EVENTS
  81. /**
  82. Invoked when a file loaded with MovieClipLoader.loadClip() has failed to load.
  83. @sends LoaderWorkerEvent#ON_ERROR
  84. */
  85. public function onLoadError ( target_mc:MovieClip, errorCode:String, httpStatus:Number ) : Void {
  86. // debug message
  87. Log.error("onLoadError - '" + httpStatus + "' error while loading file: " + mLoadingData.url, toString());
  88. // reset
  89. resetState();
  90. // dispatch onWorkerLoadError event
  91. dispatchEvent(new LoaderWorkerEvent(LoaderWorkerEvent.ON_ERROR, this, mLoadingData.name, mLoadingData.location, httpStatus));
  92. }
  93. /**
  94. Invoked every time the loading content is written to the hard disk during the loading process (that is, between MovieClipLoader.onLoadStart and MovieClipLoader.onLoadComplete).
  95. @sends LoaderWorkerEvent#ON_PROGRESS
  96. */
  97. public function onLoadProgress ( target_mc:MovieClip, loadedBytes:Number, totalBytes:Number ) : Void {
  98. // update data
  99. mLoadingData.bytesLoaded = loadedBytes;
  100. mLoadingData.bytesTotal = totalBytes;
  101. // dispatch onWorkerLoadProgress event
  102. dispatchEvent(new LoaderWorkerEvent(LoaderWorkerEvent.ON_PROGRESS, this, mLoadingData.name, mLoadingData.location));
  103. }
  104. /**
  105. Invoked when the actions on the first frame of the loaded clip have been executed.
  106. */
  107. public function onLoadInit ( target_mc:MovieClip ) : Void {
  108. // reset
  109. resetState();
  110. // set visibility
  111. mLoadingData.location._visible = mLoadingData.visible;
  112. sendDoneMessage();
  113. }
  114. /**
  115. Invoked when a call to MovieClipLoader.loadClip() has begun to download a file.
  116. @sends Nothing, not yet implemented.
  117. */
  118. public function onLoadStart ( target_mc:MovieClip ) : Void {
  119. // unused
  120. }
  121. /**
  122. Invoked when a file that was loaded with MovieClipLoader.loadClip() is completely downloaded.
  123. @sends Nothing, not implemented.
  124. */
  125. public function onLoadComplete ( target_mc:MovieClip, httpStatus:Number ) : Void {
  126. // unused
  127. }
  128. // PRIVATE METHODS
  129. /**
  130. @sends LoaderWorkerEvent#ON_DONE
  131. */
  132. private function sendDoneMessage () : Void {
  133. dispatchEvent(new LoaderWorkerEvent(LoaderWorkerEvent.ON_DONE, this, mLoadingData.name, mLoadingData.location));
  134. }
  135. /**
  136. Resets the state of this worker
  137. */
  138. private function resetState () : Void {
  139. // set flag
  140. mLoadingState = false;
  141. // reset loaded bytes
  142. mLoadingData.bytesTotal = mLoadingData.bytesLoaded = 0;
  143. }
  144. /**
  145. Starts the actual loading process
  146. */
  147. private function startLoading () : Void {
  148. mLoader.loadClip(mLoadingData.url, mLoadingData.location);
  149. }
  150. /**
  151. Unloads the current movie if a movie was already loaded in mc
  152. @param start, set to true to start loading new content after 1 frame dealy
  153. */
  154. private function unloadContent (inStart:Boolean) : Void {
  155. mLoader.unloadClip(mLoadingData.location);
  156. if (inStart) var d:FrameDelay = new FrameDelay(this,startLoading,1);
  157. }
  158. /**
  159. @return Package and class name
  160. */
  161. public function toString () : String {
  162. return "org.asapframework.util.loader.LoaderWorker: " + mNumber;
  163. }
  164. }