1 /****************************************************************************
  2  Copyright (c) 2008-2010 Ricardo Quesada
  3  Copyright (c) 2011-2012 cocos2d-x.org
  4  Copyright (c) 2013-2014 Chukong Technologies Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * Instant actions are immediate actions. They don't have a duration like
 29  * the CCIntervalAction actions.
 30  * @class
 31  * @extends cc.FiniteTimeAction
 32  */
 33 cc.ActionInstant = cc.FiniteTimeAction.extend(/** @lends cc.ActionInstant# */{
 34     /**
 35      * @return {Boolean}
 36      */
 37     isDone:function () {
 38         return true;
 39     },
 40 
 41     /**
 42      * @param {Number} dt
 43      */
 44     step:function (dt) {
 45         this.update(1);
 46     },
 47 
 48     /**
 49      * @param {Number} time
 50      */
 51     update:function (time) {
 52         //nothing
 53     },
 54 
 55     reverse:function(){
 56         return this.clone();
 57     },
 58 
 59     clone:function(){
 60         return new cc.ActionInstant();
 61     }
 62 });
 63 
 64 /**  Show the node
 65  * @class
 66  * @extends cc.ActionInstant
 67  */
 68 cc.Show = cc.ActionInstant.extend(/** @lends cc.Show# */{
 69     /**
 70      * @param {Number} time
 71      */
 72     update:function (time) {
 73         this.target.visible = true;
 74     },
 75 
 76     /**
 77      * @return {cc.FiniteTimeAction}
 78      */
 79     reverse:function () {
 80         return cc.Hide.create();
 81     },
 82 
 83     clone:function(){
 84         return new cc.Show();
 85     }
 86 });
 87 /**
 88  * @return {cc.Show}
 89  * @example
 90  * // example
 91  * var showAction = cc.Show.create();
 92  */
 93 cc.Show.create = function () {
 94     return new cc.Show();
 95 };
 96 
 97 /**
 98  * Hide the node
 99  * @class
100  * @extends cc.ActionInstant
101  */
102 cc.Hide = cc.ActionInstant.extend(/** @lends cc.Hide# */{
103     /**
104      * @param {Number} time
105      */
106     update:function (time) {
107         this.target.visible = false;
108     },
109 
110     /**
111      * @return {cc.FiniteTimeAction}
112      */
113     reverse:function () {
114         return cc.Show.create();
115     },
116 
117     clone:function(){
118         return new cc.Hide();
119     }
120 });
121 /**
122  * @return {cc.Hide}
123  * @example
124  * // example
125  * var hideAction = cc.Hide.create();
126  */
127 cc.Hide.create = function () {
128     return new cc.Hide();
129 };
130 
131 
132 /** Toggles the visibility of a node
133  * @class
134  * @extends cc.ActionInstant
135  */
136 cc.ToggleVisibility = cc.ActionInstant.extend(/** @lends cc.ToggleVisibility# */{
137     /**
138      * @param {Number} time
139      */
140     update:function (time) {
141         this.target.visible = !this.target.visible;
142     },
143 
144     /**
145      * @return {cc.ToggleVisibility}
146      */
147     reverse:function () {
148         return new cc.ToggleVisibility();
149     },
150 
151     clone:function(){
152         return new cc.ToggleVisibility();
153     }
154 });
155 
156 /**
157  * @return {cc.ToggleVisibility}
158  * @example
159  * // example
160  * var toggleVisibilityAction = cc.ToggleVisibility.create();
161  */
162 cc.ToggleVisibility.create = function () {
163     return new cc.ToggleVisibility();
164 };
165 
166 cc.RemoveSelf = cc.ActionInstant.extend({
167      _isNeedCleanUp: true,
168 
169 	/**
170 	 * Create a RemoveSelf object with a flag indicate whether the target should be cleaned up while removing.
171 	 *
172 	 * Constructor of cc.RemoveSelf
173 	 * @param {Boolean} [isNeedCleanUp=true]
174 	 *
175 	 * @example
176 	 * // example
177 	 * var removeSelfAction = new cc.RemoveSelf(false);
178 	 */
179     ctor:function(isNeedCleanUp){
180         cc.FiniteTimeAction.prototype.ctor.call(this);
181 
182 	    isNeedCleanUp !== undefined && this.init(isNeedCleanUp);
183     },
184 
185     update:function(time){
186         this.target.removeFromParent(this._isNeedCleanUp);
187     },
188 
189     init:function(isNeedCleanUp){
190         this._isNeedCleanUp = isNeedCleanUp;
191         return true;
192     },
193 
194     reverse:function(){
195         return new cc.RemoveSelf(this._isNeedCleanUp);
196     },
197 
198     clone:function(){
199         return new cc.RemoveSelf(this._isNeedCleanUp);
200     }
201 });
202 
203 /**
204  * Create a RemoveSelf object with a flag indicate whether the target should be cleaned up while removing.
205  *
206  * @param {Boolean} [isNeedCleanUp=true]
207  * @return {cc.RemoveSelf}
208  *
209  * @example
210  * // example
211  * var removeSelfAction = cc.RemoveSelf.create();
212  */
213 cc.RemoveSelf.create = function(isNeedCleanUp){
214     return new cc.RemoveSelf(isNeedCleanUp);
215 };
216 
217 /**
218  * Flips the sprite horizontally
219  * @class
220  * @extends cc.ActionInstant
221  */
222 cc.FlipX = cc.ActionInstant.extend(/** @lends cc.FlipX# */{
223     _flippedX:false,
224 
225 	/**
226 	 * Create a FlipX action to flip or unflip the target
227 	 *
228 	 * Constructor of cc.FlipX
229 	 * @param {Boolean} flip Indicate whether the target should be flipped or not
230 	 *
231 	 * @example
232 	 * var flipXAction = new cc.FlipX(true);
233 	 */
234     ctor:function(flip){
235         cc.FiniteTimeAction.prototype.ctor.call(this);
236         this._flippedX = false;
237 		flip !== undefined && this.initWithFlipX(flip);
238     },
239 
240     /**
241      * @param {Boolean} flip
242      * @return {Boolean}
243      */
244     initWithFlipX:function (flip) {
245         this._flippedX = flip;
246         return true;
247     },
248 
249     /**
250      * @param {Number} time
251      */
252     update:function (time) {
253         this.target.flippedX = this._flippedX;
254     },
255 
256     /**
257      * @return {cc.FiniteTimeAction}
258      */
259     reverse:function () {
260         return cc.FlipX.create(!this._flippedX);
261     },
262 
263     clone:function(){
264         var action = new cc.FlipX();
265         action.initWithFlipX(this._flippedX);
266         return action;
267     }
268 });
269 
270 /**
271  * Create a FlipX action to flip or unflip the target
272  *
273  * @param {Boolean} flip Indicate whether the target should be flipped or not
274  * @return {cc.FlipX}
275  * @example
276  * var flipXAction = cc.FlipX.create(true);
277  */
278 cc.FlipX.create = function (flip) {
279     return new cc.FlipX(flip);
280 };
281 
282 /**
283  * Flips the sprite vertically
284  * @class
285  * @extends cc.ActionInstant
286  */
287 cc.FlipY = cc.ActionInstant.extend(/** @lends cc.FlipY# */{
288     _flippedY:false,
289 
290 	/**
291 	 * Create a FlipY action to flip or unflip the target
292 	 *
293 	 * Constructor of cc.FlipY
294 	 * @param {Boolean} flip
295 	 * @example
296 	 * var flipYAction = new cc.FlipY(true);
297 	 */
298     ctor: function(flip){
299         cc.FiniteTimeAction.prototype.ctor.call(this);
300         this._flippedY = false;
301 
302 		flip !== undefined && this.initWithFlipY(flip);
303     },
304     /**
305      * @param {Boolean} flip
306      * @return {Boolean}
307      */
308     initWithFlipY:function (flip) {
309         this._flippedY = flip;
310         return true;
311     },
312 
313     /**
314      * @param {Number} time
315      */
316     update:function (time) {
317         //this._super();
318         this.target.flippedY = this._flippedY;
319     },
320 
321     /**
322      * @return {cc.FiniteTimeAction}
323      */
324     reverse:function () {
325         return cc.FlipY.create(!this._flippedY);
326     },
327 
328     clone:function(){
329         var action = new cc.FlipY();
330         action.initWithFlipY(this._flippedY);
331         return action;
332     }
333 });
334 /**
335  * Create a FlipY action to flip or unflip the target
336  *
337  * @param {Boolean} flip
338  * @return {cc.FlipY}
339  * @example
340  * var flipYAction = cc.FlipY.create(true);
341  */
342 cc.FlipY.create = function (flip) {
343     return new cc.FlipY(flip);
344 };
345 
346 
347 /** Places the node in a certain position
348  * @class
349  * @extends cc.ActionInstant
350  */
351 cc.Place = cc.ActionInstant.extend(/** @lends cc.Place# */{
352     _x: 0,
353 	_y: 0,
354 
355 	/**
356 	 * Creates a Place action with a position
357 	 *
358 	 * Constructor of cc.Place
359 	 * @param {cc.Point|Number} pos
360 	 * @param {Number} [y]
361 	 * @example
362 	 * var placeAction = new cc.Place.create(cc.p(200, 200));
363 	 * var placeAction = new cc.Place.create(200, 200);
364 	 */
365     ctor:function(pos, y){
366         cc.FiniteTimeAction.prototype.ctor.call(this);
367         this._x = 0;
368 	    this._y = 0;
369 
370 		if (pos !== undefined) {
371 			if (pos.x !== undefined) {
372 				y = pos.y;
373 				pos = pos.x;
374 			}
375 			this.initWithPosition(pos, y);
376 		}
377     },
378 
379     /** Initializes a Place action with a position
380      * @param {cc.Point} pos
381      * @return {Boolean}
382      */
383     initWithPosition: function (x, y) {
384         this._x = x;
385         this._y = y;
386         return true;
387     },
388 
389     /**
390      * @param {Number} time
391      */
392     update:function (time) {
393         this.target.setPosition(this._x, this._y);
394     },
395 
396     clone:function(){
397         var action = new cc.Place();
398         action.initWithPosition(this._x, this._y);
399         return action;
400     }
401 });
402 /**
403  * Creates a Place action with a position
404  * @param {cc.Point|Number} pos
405  * @param {Number} [y]
406  * @return {cc.Place}
407  * @example
408  * // example
409  * var placeAction = cc.Place.create(cc.p(200, 200));
410  * var placeAction = cc.Place.create(200, 200);
411  */
412 cc.Place.create = function (pos, y) {
413     return new cc.Place(pos, y);
414 };
415 
416 
417 /** Calls a 'callback'
418  * @class
419  * @extends cc.ActionInstant
420  */
421 cc.CallFunc = cc.ActionInstant.extend(/** @lends cc.CallFunc# */{
422     _selectorTarget:null,
423     _callFunc:null,
424     _function:null,
425     _data:null,
426 
427 	/**
428 	 * Creates a CallFunc action with the callback
429 	 *
430 	 * Constructor of cc.CallFunc
431 	 * @param {function} selector
432 	 * @param {object|null} [selectorTarget]
433 	 * @param {*|null} [data] data for function, it accepts all data types.
434 	 * @example
435 	 * // example
436 	 * // CallFunc without data
437 	 * var finish = new cc.CallFunc(this.removeSprite, this);
438 	 *
439 	 * // CallFunc with data
440 	 * var finish = new cc.CallFunc(this.removeFromParentAndCleanup, this,  true);
441 	 */
442     ctor:function(selector, selectorTarget, data){
443         cc.FiniteTimeAction.prototype.ctor.call(this);
444 
445 		if(selector !== undefined){
446 			if(selectorTarget === undefined)
447 				this.initWithFunction(selector);
448 			else this.initWithFunction(selector, selectorTarget, data);
449 		}
450     },
451 
452     /**
453      * Initializes the action with a function or function and its target
454      * @param {function} selector
455      * @param {object|Null} selectorTarget
456      * @param {*|Null} [data] data for function, it accepts all data types.
457      * @return {Boolean}
458      */
459     initWithFunction:function (selector, selectorTarget, data) {
460 	    if (selectorTarget) {
461             this._data = data;
462             this._callFunc = selector;
463             this._selectorTarget = selectorTarget;
464 	    }
465 	    else if (selector)
466 		    this._function = selector;
467         return true;
468     },
469 
470     /**
471      * execute the function.
472      */
473     execute:function () {
474         if (this._callFunc != null)         //CallFunc, N, ND
475             this._callFunc.call(this._selectorTarget, this.target, this._data);
476         else if(this._function)
477             this._function.call(null, this.target);
478     },
479 
480     /**
481      * @param {Number} time
482      */
483     update:function (time) {
484         //this._super(target);
485         this.execute();
486     },
487 
488     /**
489      * @return {object}
490      */
491     getTargetCallback:function () {
492         return this._selectorTarget;
493     },
494 
495     /**
496      * @param {object} sel
497      */
498     setTargetCallback:function (sel) {
499         if (sel != this._selectorTarget) {
500             if (this._selectorTarget)
501                 this._selectorTarget = null;
502             this._selectorTarget = sel;
503         }
504     },
505 
506     clone:function(){
507        var action = new cc.CallFunc();
508         if(this._selectorTarget){
509              action.initWithFunction(this._callFunc,  this._selectorTarget, this._data)
510         }else if(this._function){
511              action.initWithFunction(this._function);
512         }
513         return action;
514     }
515 });
516 /**
517  * Creates the action with the callback
518  * @param {function} selector
519  * @param {object|null} [selectorTarget]
520  * @param {*|null} [data] data for function, it accepts all data types.
521  * @return {cc.CallFunc}
522  * @example
523  * // example
524  * // CallFunc without data
525  * var finish = cc.CallFunc.create(this.removeSprite, this);
526  *
527  * // CallFunc with data
528  * var finish = cc.CallFunc.create(this.removeFromParentAndCleanup, this._grossini,  true);
529  */
530 cc.CallFunc.create = function (selector, selectorTarget, data) {
531     return new cc.CallFunc(selector, selectorTarget, data);
532 };
533