1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga 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.setVisible(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.setVisible(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.setVisible(!this._target.isVisible());
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     ctor:function(){
169         cc.FiniteTimeAction.prototype.ctor.call(this);
170         this._isNeedCleanUp = true;
171     },
172 
173     update:function(time){
174         this._target.removeFromParent(this._isNeedCleanUp);
175     },
176 
177     init:function(isNeedCleanUp){
178         this._isNeedCleanUp = isNeedCleanUp;
179         return true;
180     },
181 
182     reverse:function(){
183         return new cc.RemoveSelf(this._isNeedCleanUp);
184     },
185 
186     clone:function(){
187         return new cc.RemoveSelf(this._isNeedCleanUp);
188     }
189 });
190 
191 cc.RemoveSelf.create = function(isNeedCleanUp){
192     if(isNeedCleanUp == null)
193         isNeedCleanUp = true;
194     var removeSelf = new cc.RemoveSelf();
195     if(removeSelf)
196         removeSelf.init(isNeedCleanUp);
197     return removeSelf;
198 };
199 
200 /**
201  * Flips the sprite horizontally
202  * @class
203  * @extends cc.ActionInstant
204  */
205 cc.FlipX = cc.ActionInstant.extend(/** @lends cc.FlipX# */{
206     _flippedX:false,
207     ctor:function(){
208         cc.FiniteTimeAction.prototype.ctor.call(this);
209         this._flippedX = false;
210     },
211     /**
212      * @param {Boolean} x
213      * @return {Boolean}
214      */
215     initWithFlipX:function (x) {
216         this._flippedX = x;
217         return true;
218     },
219 
220     /**
221      * @param {Number} time
222      */
223     update:function (time) {
224         this._target.setFlippedX(this._flippedX);
225     },
226 
227     /**
228      * @return {cc.FiniteTimeAction}
229      */
230     reverse:function () {
231         return cc.FlipX.create(!this._flippedX);
232     },
233 
234     clone:function(){
235         var action = new cc.FlipX();
236         action.initWithFlipX(this._flippedX);
237         return action;
238     }
239 });
240 
241 /**
242  * @param {Boolean} x
243  * @return {cc.FlipX}
244  * var flipXAction = cc.FlipX.create(true);
245  */
246 cc.FlipX.create = function (x) {
247     var ret = new cc.FlipX();
248     if (ret.initWithFlipX(x))
249         return ret;
250     return null;
251 };
252 
253 /**
254  * Flips the sprite vertically
255  * @class
256  * @extends cc.ActionInstant
257  */
258 cc.FlipY = cc.ActionInstant.extend(/** @lends cc.FlipY# */{
259     _flippedY:false,
260     ctor:function(){
261         cc.FiniteTimeAction.prototype.ctor.call(this);
262         this._flippedY = false;
263     },
264     /**
265      * @param {Boolean} Y
266      * @return {Boolean}
267      */
268     initWithFlipY:function (Y) {
269         this._flippedY = Y;
270         return true;
271     },
272 
273     /**
274      * @param {Number} time
275      */
276     update:function (time) {
277         //this._super();
278         this._target.setFlippedY(this._flippedY);
279     },
280 
281     /**
282      * @return {cc.FiniteTimeAction}
283      */
284     reverse:function () {
285         return cc.FlipY.create(!this._flippedY);
286     },
287 
288     clone:function(){
289         var action = new cc.FlipY();
290         action.initWithFlipY(this._flippedY);
291         return action;
292     }
293 });
294 /**
295  * @param {Boolean} y
296  * @return {cc.FlipY}
297  * @example
298  * // example
299  * var flipYAction = cc.FlipY.create();
300  */
301 cc.FlipY.create = function (y) {
302     var ret = new cc.FlipY();
303     if (ret.initWithFlipY(y))
304         return ret;
305     return null;
306 };
307 
308 
309 /** Places the node in a certain position
310  * @class
311  * @extends cc.ActionInstant
312  */
313 cc.Place = cc.ActionInstant.extend(/** @lends cc.Place# */{
314     _position:null,
315     ctor:function(){
316         cc.FiniteTimeAction.prototype.ctor.call(this);
317         this._position = cc.p(0, 0);
318     },
319 
320     /** Initializes a Place action with a position
321      * @param {cc.Point} pos
322      * @return {Boolean}
323      */
324     initWithPosition:function (pos) {
325         this._position.x = pos.x;
326         this._position.y = pos.y;
327         return true;
328     },
329 
330     /**
331      * @param {Number} time
332      */
333     update:function (time) {
334         this._target.setPosition(this._position);
335     },
336 
337     clone:function(){
338         var action = new cc.Place();
339         action.initWithPosition(this._position);
340         return action;
341     }
342 });
343 /** creates a Place action with a position
344  * @param {cc.Point} pos
345  * @return {cc.Place}
346  * @example
347  * // example
348  * var placeAction = cc.Place.create(cc.p(200, 200));
349  */
350 cc.Place.create = function (pos) {
351     var ret = new cc.Place();
352     ret.initWithPosition(pos);
353     return ret;
354 };
355 
356 
357 /** Calls a 'callback'
358  * @class
359  * @extends cc.ActionInstant
360  */
361 cc.CallFunc = cc.ActionInstant.extend(/** @lends cc.CallFunc# */{
362     _selectorTarget:null,
363     _callFunc:null,
364     _function:null,
365     _data:null,
366 
367     ctor:function(){
368         cc.FiniteTimeAction.prototype.ctor.call(this);
369         this._selectorTarget = null;
370         this._callFunc = null;
371         this._function = null;
372         this._data = null;
373     },
374 
375     /**
376      * @param {function|Null} selector
377      * @param {object} selectorTarget
378      * @param {*|Null} data data for function, it accepts all data types.
379      * @return {Boolean}
380      */
381     initWithTarget:function (selector, selectorTarget, data) {
382         this._data = data;
383         this._callFunc = selector;
384         this._selectorTarget = selectorTarget;
385         return true;
386     },
387 
388     /**
389      * initializes the action with the std::function<void()>
390      * @param {function} func
391      * @returns {boolean}
392      */
393     initWithFunction:function(func){
394         this._function = func;
395         return true;
396     },
397 
398     /**
399      * execute the function.
400      */
401     execute:function () {
402         if (this._callFunc != null)
403             this._callFunc.call(this._selectorTarget, this._target, this._data);
404         else if(this._function)
405             this._function.call(null, this._target);
406     },
407 
408     /**
409      * @param {Number} time
410      */
411     update:function (time) {
412         //this._super(target);
413         this.execute();
414     },
415 
416     /**
417      * @return {object}
418      */
419     getTargetCallback:function () {
420         return this._selectorTarget;
421     },
422 
423     /**
424      * @param {object} sel
425      */
426     setTargetCallback:function (sel) {
427         if (sel != this._selectorTarget) {
428             if (this._selectorTarget)
429                 this._selectorTarget = null;
430             this._selectorTarget = sel;
431         }
432     },
433 
434     copy:function() {
435         var n = new cc.CallFunc();
436         if(this._selectorTarget){
437             n.initWithTarget(this._callFunc,  this._selectorTarget, this._data)
438         }else if(this._function){
439             n.initWithFunction(this._function);
440         }
441         return n;
442     },
443 
444     clone:function(){
445        var action = new cc.CallFunc();
446         if(this._selectorTarget){
447              action.initWithTarget(this._callFunc,  this._selectorTarget, this._data)
448         }else if(this._function){
449              action.initWithFunction(this._function);
450         }
451         return action;
452     }
453 });
454 /** creates the action with the callback
455  * @param {function} selector
456  * @param {object|null} [selectorTarget=]
457  * @param {*|Null} [data=] data for function, it accepts all data types.
458  * @return {cc.CallFunc}
459  * @example
460  * // example
461  * // CallFunc without data
462  * var finish = cc.CallFunc.create(this.removeSprite, this);
463  *
464  * // CallFunc with data
465  * var finish = cc.CallFunc.create(this.removeFromParentAndCleanup, this._grossini,  true),
466  */
467 cc.CallFunc.create = function (selector, selectorTarget, data) {
468     var ret = new cc.CallFunc();
469     if(selectorTarget === undefined){
470         if (ret && ret.initWithFunction(selector)) {
471             return ret;
472         }
473     }else{
474         if (ret && ret.initWithTarget(selector, selectorTarget, data)) {
475             ret._callFunc = selector;
476             return ret;
477         }
478     }
479     return null;
480 };
481