API Documentation for: 1.1.2
Show:

File:BaseState.js

  1. /**
  2. * @module cloudkid
  3. */
  4. (function(){
  5. "use strict";
  6.  
  7. // Imports
  8. var Animator = cloudkid.Animator,
  9. PixiAnimator = cloudkid.PixiAnimator,
  10. StateManager = cloudkid.StateManager;
  11. /**
  12. * Defines the base functionality for a state used by the state manager
  13. *
  14. * @class BaseState
  15. * @constructor
  16. * @param {createjs.MovieClip|PIXI.DisplayObjectContainer} panel The panel to associate with this panel
  17. */
  18. var BaseState = function(panel)
  19. {
  20. this.initialize(panel);
  21. };
  22. var p = BaseState.prototype;
  23. /**
  24. * Adds the specified event listener
  25. * @function addEventListener
  26. * @param {String} type The string type of the event
  27. * @param {function|object} listener An object with a handleEvent method, or a function that will be called when the event is dispatched
  28. * @return {function|object} Returns the listener for chaining or assignment
  29. */
  30. p.addEventListener = null;
  31.  
  32. /**
  33. * Removes the specified event listener
  34. * @function removeEventListener
  35. * @param {String} type The string type of the event
  36. * @param {function|object} listener The listener function or object
  37. */
  38. p.removeEventListener = null;
  39.  
  40. /**
  41. * Removes all listeners for the specified type, or all listeners of all types
  42. * @function removeAllEventListeners
  43. * @param {String} type The string type of the event. If omitted, all listeners for all types will be removed.
  44. */
  45. p.removeAllEventListeners = null;
  46.  
  47. /**
  48. * Dispatches the specified event
  49. * @function dispatchEvent
  50. * @param {Object|String} enventObj An object with a "type" property, or a string type
  51. * @param {object} target The object to use as the target property of the event object
  52. * @return {bool} Returns true if any listener returned true
  53. */
  54. p.dispatchEvent = null;
  55.  
  56. /**
  57. * Indicates whether there is at least one listener for the specified event type
  58. * @function hasEventListener
  59. * @param {String} type The string type of the event
  60. * @return {bool} Returns true if there is at least one listener for the specified event
  61. */
  62. p.hasEventListener = null;
  63.  
  64. /**
  65. * Createjs EventDispatcher method
  66. * @property {Array} _listeners description
  67. * @private
  68. */
  69. p._listeners = null;
  70. // we only use EventDispatcher if it's available:
  71. if (createjs.EventDispatcher) createjs.EventDispatcher.initialize(p); // inject EventDispatcher methods.
  72. /**
  73. * The id reference
  74. *
  75. * @property {String} stateID
  76. */
  77. p.stateId = null;
  78. /**
  79. * A reference to the state manager
  80. *
  81. * @property {StateManager} manager
  82. */
  83. p.manager = null;
  84. /**
  85. * Save the panel
  86. *
  87. * @property {createjs.MovieClip|PIXI.DisplayObjectContainer} panel
  88. */
  89. p.panel = null;
  90. /**
  91. * Check to see if we've been destroyed
  92. *
  93. * @property {bool} _destroyed
  94. * @private
  95. */
  96. p._destroyed = false;
  97. /**
  98. * If the manager considers this the active panel
  99. *
  100. * @property {bool} _active
  101. * @private
  102. */
  103. p._active = false;
  104. /**
  105. * If we are pre-loading the state
  106. *
  107. * @property {bool} _isLoading
  108. * @private
  109. */
  110. p._isLoading = false;
  111. /**
  112. * If we canceled entering the state
  113. *
  114. * @property {bool} _canceled
  115. * @private
  116. */
  117. p._canceled = false;
  118. /**
  119. * When we're finishing loading
  120. *
  121. * @property {function} _onEnterStateProceed
  122. * @private
  123. */
  124. p._onEnterStateProceed = null;
  125. /** If we start doing a load in enterState, assign the onEnterStateComplete here
  126. *
  127. * @property {function} _onLoadingComplete
  128. * @private
  129. */
  130. p._onLoadingComplete = null;
  131. /** If the state is enabled that means it click ready
  132. *
  133. * @property {bool} _enabled
  134. * @private
  135. */
  136. p._enabled = false;
  137.  
  138. /**
  139. * If we are currently transitioning
  140. *
  141. * @property {bool} isTransitioning
  142. * @private
  143. */
  144. p._isTransitioning = false;
  145. /**
  146. * Initialize the Base State
  147. * @function initialize
  148. * @param {createjs.MovieClip|PIXI.DisplayObjectContaner} panel The panel
  149. */
  150. p.initialize = function(panel)
  151. {
  152. this.panel = panel;
  153. };
  154. /**
  155. * Status of whether the panel load was canceled
  156. *
  157. * @function getCanceled
  158. * @return {bool} If the load was canceled
  159. */
  160. p.getCanceled = function()
  161. {
  162. return this._canceled;
  163. };
  164. /**
  165. * This is called by the State Manager to exit the state
  166. *
  167. * @function _internalExitState
  168. * @private
  169. */
  170. p._internalExitState = function()
  171. {
  172. if (this._isTransitioning)
  173. {
  174. this._isTransitioning = false;
  175. var animator = (CONFIG_CREATEJS) ? Animator : PixiAnimator.instance;
  176. animator.stop(this.panel);
  177. }
  178. this._enabled = false;
  179. this.panel.visible = false;
  180. this._active = false;
  181. this.exitState();
  182. };
  183. /**
  184. * When the state is exited
  185. *
  186. * @function exitState
  187. */
  188. p.exitState = function(){};
  189. /**
  190. * Exit the state start, called by the State Manager
  191. *
  192. * @function _internalExitStateStart
  193. * @private
  194. */
  195. p._internalExitStateStart = function()
  196. {
  197. this.exitStateStart();
  198. };
  199. /**
  200. * When the state has requested to be exit, pre-transition
  201. *
  202. * @function exitStateStart
  203. */
  204. p.exitStateStart = function(){};
  205. /**
  206. * Exit the state start, called by the State Manager
  207. *
  208. * @function _internalEnterState
  209. * @param {functon} proceed The function to call after enterState has been called
  210. * @private
  211. */
  212. p._internalEnterState = function(proceed)
  213. {
  214. if (this._isTransitioning)
  215. {
  216. this._isTransitioning = false;
  217. var animator = (CONFIG_CREATEJS) ? Animator : PixiAnimator.instance;
  218. animator.stop(this.panel);
  219. }
  220. this._enabled = false;
  221. this._active = true;
  222. this._canceled = false;
  223. this._onEnterStateProceed = proceed;
  224. this.enterState();
  225. if (this._onEnterStateProceed)
  226. {
  227. this._onEnterStateProceed();
  228. this._onEnterStateProceed = null;
  229. }
  230. };
  231. /**
  232. * Internal function to start the preloading
  233. *
  234. * @function loadingStart
  235. */
  236. p.loadingStart = function()
  237. {
  238. if (this._isLoading)
  239. {
  240. Debug.warn("loadingStart() was called while we're already loading");
  241. return;
  242. }
  243. this._isLoading = true;
  244. this.manager.loadingStart();
  245. // Starting a load is optional and
  246. // need to be called from the enterState function
  247. // We'll override the existing behavior
  248. // of internalEnterState, by passing
  249. // the complete function to onLoadingComplete
  250. this._onLoadingComplete = this._onEnterStateProceed;
  251. this._onEnterStateProceed = null;
  252. };
  253. /**
  254. * Internal function to finish the preloading
  255. *
  256. * @function loadingDone
  257. */
  258. p.loadingDone = function()
  259. {
  260. if (!this._isLoading)
  261. {
  262. Debug.warn("loadingDone() was called without a load started, call loadingStart() first");
  263. return;
  264. }
  265. this._isLoading = false;
  266. this.manager.loadingDone();
  267. if (this._onLoadingComplete)
  268. {
  269. this._onLoadingComplete();
  270. this._onLoadingComplete = null;
  271. }
  272. };
  273. /**
  274. * Cancel the loading of this state
  275. *
  276. * @function _internalCancel
  277. * @private
  278. */
  279. p._internalCancel = function()
  280. {
  281. this._active = false;
  282. this._canceled = true;
  283. this._isLoading = false;
  284. this._internalExitState();
  285. this.cancel();
  286. };
  287. /**
  288. * Cancel the load, implementation-specific
  289. * this is where any async actions are removed
  290. *
  291. * @function cancel
  292. */
  293. p.cancel = function(){};
  294. /**
  295. * When the state is entered
  296. *
  297. * @function enterState
  298. */
  299. p.enterState = function(){};
  300. /**
  301. * Exit the state start, called by the State Manager
  302. *
  303. * @function _internalEnterStateDone
  304. * @private
  305. */
  306. p._internalEnterStateDone = function()
  307. {
  308. if (this._canceled) return;
  309. this.setEnabled(true);
  310. this.enterStateDone();
  311. };
  312. /**
  313. * When the state is visually entered fully
  314. * that is, after the transition is done
  315. *
  316. * @function enterStateDone
  317. */
  318. p.enterStateDone = function(){};
  319. /**
  320. * StateManager updates the state
  321. *
  322. * @function update
  323. * @param {int} elasped The second since the last frame
  324. */
  325. p.update = function(){};
  326. /**
  327. * Get if this is the active state
  328. *
  329. * @function getActive
  330. * @return {bool} If this is the active state
  331. */
  332. p.getActive = function()
  333. {
  334. return this._active;
  335. };
  336. /**
  337. * Transition the panel in
  338. *
  339. * @function transitionIn
  340. * @param {function} callback
  341. */
  342. p.transitionIn = function(callback)
  343. {
  344. this._isTransitioning = true;
  345. var s = this;
  346. var animator = (CONFIG_CREATEJS) ? Animator : PixiAnimator.instance;
  347. animator.play(
  348. this.panel,
  349. StateManager.TRANSITION_IN,
  350. function()
  351. {
  352. s._isTransitioning = false;
  353. callback();
  354. }
  355. );
  356. };
  357. /**
  358. * Transition the panel out
  359. *
  360. * @function transitionOut
  361. * @param {function} callback
  362. */
  363. p.transitionOut = function(callback)
  364. {
  365. this._enabled = false;
  366. this._isTransitioning = true;
  367. var s = this;
  368. var animator = (CONFIG_CREATEJS) ? Animator : PixiAnimator.instance;
  369. animator.play(
  370. this.panel,
  371. StateManager.TRANSITION_OUT,
  372. function()
  373. {
  374. s._isTransitioning = false;
  375. callback();
  376. }
  377. );
  378. };
  379. /**
  380. * Get if this State has been destroyed
  381. *
  382. * @function getDestroyed
  383. * @return {bool} If this has been destroyed
  384. */
  385. p.getDestroyed = function()
  386. {
  387. return this._destroyed;
  388. };
  389. /**
  390. * Enable this panel, true is only non-loading and non-transitioning state
  391. *
  392. * @function setEnabled
  393. * @param {bool} enabled The enabled state
  394. */
  395. p.setEnabled = function(enabled)
  396. {
  397. this._enabled = enabled;
  398. };
  399. /**
  400. * Get the enabled status
  401. *
  402. * @function getEnabled
  403. * @return {bool} If this state is enabled
  404. */
  405. p.getEnabled = function()
  406. {
  407. return this._enabled;
  408. };
  409. /**
  410. * Don't use the state object after this
  411. *
  412. * @function destroy
  413. */
  414. p.destroy = function()
  415. {
  416. this.exitState();
  417. this.panel = null;
  418. this.manager = null;
  419. this._destroyed = true;
  420. this._onEnterStateProceed = null;
  421. this._onLoadingComplete = null;
  422. };
  423. // Add to the name space
  424. namespace('cloudkid').BaseState = BaseState;
  425. }());