CCTransitionProgress.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2008-2010 Ricardo Quesada
  4. Copyright (c) 2011 Zynga Inc.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. /**
  23. * tag for scene redial
  24. * @constant
  25. * @type Number
  26. */
  27. cc.SCENE_RADIAL = 0xc001;
  28. /**
  29. * cc.TransitionProgress transition.
  30. * @class
  31. * @extends cc.TransitionScene
  32. */
  33. cc.TransitionProgress = cc.TransitionScene.extend(/** @lends cc.TransitionProgress# */{
  34. _to:0,
  35. _from:0,
  36. _sceneToBeModified:null,
  37. /**
  38. * @override
  39. */
  40. onEnter:function () {
  41. cc.TransitionScene.prototype.onEnter.call(this);
  42. this._setupTransition();
  43. // create a transparent color layer
  44. // in which we are going to add our rendertextures
  45. var winSize = cc.Director.getInstance().getWinSize();
  46. // create the second render texture for outScene
  47. var texture = cc.RenderTexture.create(winSize.width, winSize.height);
  48. texture.getSprite().setAnchorPoint(0.5, 0.5);
  49. texture.setPosition(winSize.width / 2, winSize.height / 2);
  50. texture.setAnchorPoint(0.5, 0.5);
  51. // render outScene to its texturebuffer
  52. texture.clear(0, 0, 0, 1);
  53. texture.begin();
  54. this._sceneToBeModified.visit();
  55. texture.end();
  56. // Since we've passed the outScene to the texture we don't need it.
  57. if (this._sceneToBeModified == this._outScene)
  58. this.hideOutShowIn();
  59. // We need the texture in RenderTexture.
  60. var pNode = this._progressTimerNodeWithRenderTexture(texture);
  61. // create the blend action
  62. var layerAction = cc.Sequence.create(
  63. cc.ProgressFromTo.create(this._duration, this._from, this._to),
  64. cc.CallFunc.create(this.finish, this));
  65. // run the blend action
  66. pNode.runAction(layerAction);
  67. // add the layer (which contains our two rendertextures) to the scene
  68. this.addChild(pNode, 2, cc.SCENE_RADIAL);
  69. },
  70. /**
  71. * @override
  72. */
  73. onExit:function () {
  74. // remove our layer and release all containing objects
  75. this.removeChildByTag(cc.SCENE_RADIAL, true);
  76. cc.TransitionScene.prototype.onExit.call(this);
  77. },
  78. _setupTransition:function () {
  79. this._sceneToBeModified = this._outScene;
  80. this._from = 100;
  81. this._to = 0;
  82. },
  83. _progressTimerNodeWithRenderTexture:function (texture) {
  84. cc.log("cc.TransitionProgress._progressTimerNodeWithRenderTexture(): should be overridden in subclass");
  85. return null;
  86. },
  87. _sceneOrder:function () {
  88. this._isInSceneOnTop = false;
  89. }
  90. });
  91. /**
  92. * create a cc.TransitionProgress object
  93. * @function
  94. * @param {Number} t time
  95. * @param {cc.Scene} scene
  96. * @return {cc.TransitionProgress}
  97. */
  98. cc.TransitionProgress.create = function (t, scene) {
  99. var tempScene = new cc.TransitionProgress();
  100. if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
  101. return tempScene;
  102. }
  103. return null;
  104. };
  105. /**
  106. * cc.TransitionRadialCCW transition.<br/>
  107. * A counter clock-wise radial transition to the next scene
  108. * @class
  109. * @extends cc.TransitionProgress
  110. */
  111. cc.TransitionProgressRadialCCW = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressRadialCCW# */{
  112. _progressTimerNodeWithRenderTexture:function (texture) {
  113. var size = cc.Director.getInstance().getWinSize();
  114. var pNode = cc.ProgressTimer.create(texture.getSprite());
  115. // but it is flipped upside down so we flip the sprite
  116. if (cc.renderContextType === cc.WEBGL)
  117. pNode.getSprite().setFlippedY(true);
  118. pNode.setType(cc.PROGRESS_TIMER_TYPE_RADIAL);
  119. // Return the radial type that we want to use
  120. pNode.setReverseDirection(false);
  121. pNode.setPercentage(100);
  122. pNode.setPosition(size.width / 2, size.height / 2);
  123. pNode.setAnchorPoint(0.5, 0.5);
  124. return pNode;
  125. }
  126. });
  127. /**
  128. * create a cc.TransitionProgressRadialCCW object
  129. * @function
  130. * @param {Number} t time
  131. * @param {cc.Scene} scene
  132. * @return {cc.TransitionProgressRadialCCW}
  133. */
  134. cc.TransitionProgressRadialCCW.create = function (t, scene) {
  135. var tempScene = new cc.TransitionProgressRadialCCW();
  136. if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
  137. return tempScene;
  138. }
  139. return null;
  140. };
  141. /**
  142. * cc.TransitionRadialCW transition.<br/>
  143. * A counter colock-wise radial transition to the next scene
  144. * @class
  145. * @extends cc.TransitionProgress
  146. */
  147. cc.TransitionProgressRadialCW = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressRadialCW# */{
  148. _progressTimerNodeWithRenderTexture:function (texture) {
  149. var size = cc.Director.getInstance().getWinSize();
  150. var pNode = cc.ProgressTimer.create(texture.getSprite());
  151. // but it is flipped upside down so we flip the sprite
  152. if (cc.renderContextType === cc.WEBGL)
  153. pNode.getSprite().setFlippedY(true);
  154. pNode.setType(cc.PROGRESS_TIMER_TYPE_RADIAL);
  155. // Return the radial type that we want to use
  156. pNode.setReverseDirection(true);
  157. pNode.setPercentage(100);
  158. pNode.setPosition(size.width / 2, size.height / 2);
  159. pNode.setAnchorPoint(0.5, 0.5);
  160. return pNode;
  161. }
  162. });
  163. /**
  164. * create a cc.TransitionProgressRadialCW object
  165. * @function
  166. * @param {Number} t time
  167. * @param {cc.Scene} scene
  168. * @return {cc.TransitionProgressRadialCW}
  169. */
  170. cc.TransitionProgressRadialCW.create = function (t, scene) {
  171. var tempScene = new cc.TransitionProgressRadialCW();
  172. if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
  173. return tempScene;
  174. }
  175. return null;
  176. };
  177. /**
  178. * cc.TransitionProgressHorizontal transition.<br/>
  179. * A colock-wise radial transition to the next scene
  180. * @class
  181. * @extends cc.TransitionProgress
  182. */
  183. cc.TransitionProgressHorizontal = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressHorizontal# */{
  184. _progressTimerNodeWithRenderTexture:function (texture) {
  185. var size = cc.Director.getInstance().getWinSize();
  186. var pNode = cc.ProgressTimer.create(texture.getSprite());
  187. // but it is flipped upside down so we flip the sprite
  188. if (cc.renderContextType === cc.WEBGL)
  189. pNode.getSprite().setFlippedY(true);
  190. pNode.setType(cc.PROGRESS_TIMER_TYPE_BAR);
  191. pNode.setMidpoint(cc.p(1, 0));
  192. pNode.setBarChangeRate(cc.p(1, 0));
  193. pNode.setPercentage(100);
  194. pNode.setPosition(size.width / 2, size.height / 2);
  195. pNode.setAnchorPoint(0.5, 0.5);
  196. return pNode;
  197. }
  198. });
  199. /**
  200. * create a cc.TransitionProgressHorizontal object
  201. * @function
  202. * @param {Number} t time
  203. * @param {cc.Scene} scene
  204. * @return {cc.TransitionProgressHorizontal}
  205. */
  206. cc.TransitionProgressHorizontal.create = function (t, scene) {
  207. var tempScene = new cc.TransitionProgressHorizontal();
  208. if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
  209. return tempScene;
  210. }
  211. return null;
  212. };
  213. /**
  214. * cc.TransitionProgressVertical transition.
  215. * @class
  216. * @extends cc.TransitionProgress
  217. */
  218. cc.TransitionProgressVertical = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressVertical# */{
  219. _progressTimerNodeWithRenderTexture:function (texture) {
  220. var size = cc.Director.getInstance().getWinSize();
  221. var pNode = cc.ProgressTimer.create(texture.getSprite());
  222. // but it is flipped upside down so we flip the sprite
  223. if (cc.renderContextType === cc.WEBGL)
  224. pNode.getSprite().setFlippedY(true);
  225. pNode.setType(cc.PROGRESS_TIMER_TYPE_BAR);
  226. pNode.setMidpoint(cc.p(0, 0));
  227. pNode.setBarChangeRate(cc.p(0, 1));
  228. pNode.setPercentage(100);
  229. pNode.setPosition(size.width / 2, size.height / 2);
  230. pNode.setAnchorPoint(0.5, 0.5);
  231. return pNode;
  232. }
  233. });
  234. /**
  235. * create a cc.TransitionProgressVertical object
  236. * @function
  237. * @param {Number} t time
  238. * @param {cc.Scene} scene
  239. * @return {cc.TransitionProgressVertical}
  240. */
  241. cc.TransitionProgressVertical.create = function (t, scene) {
  242. var tempScene = new cc.TransitionProgressVertical();
  243. if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
  244. return tempScene;
  245. }
  246. return null;
  247. };
  248. /**
  249. * cc.TransitionProgressInOut transition.
  250. * @class
  251. * @extends cc.TransitionProgress
  252. */
  253. cc.TransitionProgressInOut = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressInOut# */{
  254. _progressTimerNodeWithRenderTexture:function (texture) {
  255. var size = cc.Director.getInstance().getWinSize();
  256. var pNode = cc.ProgressTimer.create(texture.getSprite());
  257. // but it is flipped upside down so we flip the sprite
  258. if (cc.renderContextType === cc.WEBGL)
  259. pNode.getSprite().setFlippedY(true);
  260. pNode.setType(cc.PROGRESS_TIMER_TYPE_BAR);
  261. pNode.setMidpoint(cc.p(0.5, 0.5));
  262. pNode.setBarChangeRate(cc.p(1, 1));
  263. pNode.setPercentage(0);
  264. pNode.setPosition(size.width / 2, size.height / 2);
  265. pNode.setAnchorPoint(0.5, 0.5);
  266. return pNode;
  267. },
  268. _sceneOrder:function () {
  269. this._isInSceneOnTop = false;
  270. },
  271. _setupTransition:function () {
  272. this._sceneToBeModified = this._inScene;
  273. this._from = 0;
  274. this._to = 100;
  275. }
  276. });
  277. /**
  278. * create a cc.TransitionProgressInOut object
  279. * @function
  280. * @param {Number} t time
  281. * @param {cc.Scene} scene
  282. * @return {cc.TransitionProgressInOut}
  283. */
  284. cc.TransitionProgressInOut.create = function (t, scene) {
  285. var tempScene = new cc.TransitionProgressInOut();
  286. if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
  287. return tempScene;
  288. }
  289. return null;
  290. };
  291. /**
  292. * cc.TransitionProgressOutIn transition.
  293. * @class
  294. * @extends cc.TransitionProgress
  295. */
  296. cc.TransitionProgressOutIn = cc.TransitionProgress.extend(/** @lends cc.TransitionProgressOutIn# */{
  297. _progressTimerNodeWithRenderTexture:function (texture) {
  298. var size = cc.Director.getInstance().getWinSize();
  299. var pNode = cc.ProgressTimer.create(texture.getSprite());
  300. // but it is flipped upside down so we flip the sprite
  301. if (cc.renderContextType === cc.WEBGL)
  302. pNode.getSprite().setFlippedY(true);
  303. pNode.setType(cc.PROGRESS_TIMER_TYPE_BAR);
  304. pNode.setMidpoint(cc.p(0.5, 0.5));
  305. pNode.setBarChangeRate(cc.p(1, 1));
  306. pNode.setPercentage(100);
  307. pNode.setPosition(size.width / 2, size.height / 2);
  308. pNode.setAnchorPoint(0.5, 0.5);
  309. return pNode;
  310. }
  311. });
  312. /**
  313. * create a cc.TransitionProgressOutIn object
  314. * @function
  315. * @param {Number} t time
  316. * @param {cc.Scene} scene
  317. * @return {cc.TransitionProgressOutIn}
  318. */
  319. cc.TransitionProgressOutIn.create = function (t, scene) {
  320. var tempScene = new cc.TransitionProgressOutIn();
  321. if ((tempScene != null) && (tempScene.initWithDuration(t, scene))) {
  322. return tempScene;
  323. }
  324. return null;
  325. };