1 /****************************************************************************
  2  Copyright (c) 2011-2012 cocos2d-x.org
  3  Copyright (c) 2013-2014 Chukong Technologies Inc.
  4 
  5  http://www.cocos2d-x.org
  6 
  7  Permission is hereby granted, free of charge, to any person obtaining a copy
  8  of this software and associated documentation files (the "Software"), to deal
  9  in the Software without restriction, including without limitation the rights
 10  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 11  copies of the Software, and to permit persons to whom the Software is
 12  furnished to do so, subject to the following conditions:
 13 
 14  The above copyright notice and this permission notice shall be included in
 15  all copies or substantial portions of the Software.
 16 
 17  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 18  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 19  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 20  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 21  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 22  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 23  THE SOFTWARE.
 24  ****************************************************************************/
 25 
 26 //Action frame type
 27 /**
 28  * The flag move action type of Cocostudio frame.
 29  * @constant
 30  * @type {number}
 31  */
 32 ccs.FRAME_TYPE_MOVE = 0;
 33 /**
 34  * The flag scale action type of Cocostudio frame.
 35  * @constant
 36  * @type {number}
 37  */
 38 ccs.FRAME_TYPE_SCALE = 1;
 39 /**
 40  * The flag rotate action type of Cocostudio frame.
 41  * @constant
 42  * @type {number}
 43  */
 44 ccs.FRAME_TYPE_ROTATE = 2;
 45 /**
 46  * The flag tint action type of Cocostudio frame.
 47  * @constant
 48  * @type {number}
 49  */
 50 ccs.FRAME_TYPE_TINT = 3;
 51 /**
 52  * The flag fade action type of Cocostudio frame.
 53  * @constant
 54  * @type {number}
 55  */
 56 ccs.FRAME_TYPE_FADE = 4;
 57 /**
 58  * The max flag of Cocostudio frame.
 59  * @constant
 60  * @type {number}
 61  */
 62 ccs.FRAME_TYPE_MAX = 5;
 63 
 64 /**
 65  * The ease type of Cocostudio frame.
 66  * @constant
 67  * @type {Object}
 68  */
 69 ccs.FrameEaseType = {
 70     Custom : -1,
 71 
 72     Linear : 0,
 73 
 74     Sine_EaseIn : 1,
 75     Sine_EaseOut : 2,
 76     Sine_EaseInOut : 3,
 77 
 78     Quad_EaseIn : 4,
 79     Quad_EaseOut : 5,
 80     Quad_EaseInOut : 6,
 81 
 82     Cubic_EaseIn : 7,
 83     Cubic_EaseOut : 8,
 84     Cubic_EaseInOut : 9,
 85 
 86     Quart_EaseIn : 10,
 87     Quart_EaseOut : 11,
 88     Quart_EaseInOut : 12,
 89 
 90     Quint_EaseIn : 13,
 91     Quint_EaseOut : 14,
 92     Quint_EaseInOut : 15,
 93 
 94     Expo_EaseIn : 16,
 95     Expo_EaseOut : 17,
 96     Expo_EaseInOut : 18,
 97 
 98     Circ_EaseIn : 19,
 99     Circ_EaseOut : 20,
100     Circ_EaseInOut : 21,
101 
102     Elastic_EaesIn : 22,
103     Elastic_EaesOut : 23,
104     Elastic_EaesInOut : 24,
105 
106     Back_EaseIn : 25,
107     Back_EaseOut : 26,
108     Back_EaseInOut : 27,
109 
110     Bounce_EaseIn : 28,
111     Bounce_EaseOut : 29,
112     Bounce_EaseInOut : 30
113 };
114 
115 
116 /**
117  * The action frame of Cocostudio. It's the base class of ccs.ActionMoveFrame, ccs.ActionScaleFrame etc.
118  * @class
119  * @extends ccs.Class
120  *
121  * @property {Number}               frameType               - frame type of ccs.ActionFrame
122  * @property {Number}               easingType              - easing type of ccs.ActionFrame
123  * @property {Number}               frameIndex              - frame index of ccs.ActionFrame
124  * @property {Number}               time                    - time of ccs.ActionFrame
125  */
126 ccs.ActionFrame = ccs.Class.extend(/** @lends ccs.ActionFrame# */{
127     frameType: 0,
128     easingType: 0,
129     frameIndex: 0,
130     _Parameter: null,
131     time: 0,
132 
133     /**
134      * The constructor of cc.ActionFrame.
135      */
136     ctor: function () {
137         this.frameType = 0;
138         this.easingType = ccs.FrameEaseType.Linear;
139         this.frameIndex = 0;
140         this.time = 0;
141     },
142 
143     /**
144      * Returns the action of ActionFrame. its subClass need override it.
145      * @param {number} duration the duration time of ActionFrame
146      * @param {ccs.ActionFrame} srcFrame source frame.
147      * @returns {null}
148      */
149     getAction: function (duration, srcFrame) {
150         cc.log("Need a definition of <getAction> for ActionFrame");
151         return null;
152     },
153 
154     _getEasingAction : function (action) {
155         if (action === null) {
156             console.error("Action cannot be null!");
157             return null;
158         }
159 
160         var resultAction;
161         switch (this.easingType) {
162             case ccs.FrameEaseType.Custom:
163                 break;
164             case ccs.FrameEaseType.Linear:
165                 resultAction = action;
166                 break;
167             case ccs.FrameEaseType.Sine_EaseIn:
168                 resultAction = action.easing(cc.easeSineIn());
169                 break;
170             case ccs.FrameEaseType.Sine_EaseOut:
171                 resultAction = action.easing(cc.easeSineOut());
172                 break;
173             case ccs.FrameEaseType.Sine_EaseInOut:
174                 resultAction = action.easing(cc.easeSineInOut());
175                 break;
176             case ccs.FrameEaseType.Quad_EaseIn:
177                 resultAction = action.easing(cc.easeQuadraticActionIn());
178                 break;
179             case ccs.FrameEaseType.Quad_EaseOut:
180                 resultAction = action.easing(cc.easeQuadraticActionOut());
181                 break;
182             case ccs.FrameEaseType.Quad_EaseInOut:
183                 resultAction = action.easing(cc.easeQuadraticActionInOut());
184                 break;
185             case ccs.FrameEaseType.Cubic_EaseIn:
186                 resultAction = action.easing(cc.easeCubicActionIn());
187                 break;
188             case ccs.FrameEaseType.Cubic_EaseOut:
189                 resultAction = action.easing(cc.easeCubicActionOut());
190                 break;
191             case ccs.FrameEaseType.Cubic_EaseInOut:
192                 resultAction = action.easing(cc.easeCubicActionInOut());
193                 break;
194             case ccs.FrameEaseType.Quart_EaseIn:
195                 resultAction = action.easing(cc.easeQuarticActionIn());
196                 break;
197             case ccs.FrameEaseType.Quart_EaseOut:
198                 resultAction = action.easing(cc.easeQuarticActionOut());
199                 break;
200             case ccs.FrameEaseType.Quart_EaseInOut:
201                 resultAction = action.easing(cc.easeQuarticActionInOut());
202                 break;
203             case ccs.FrameEaseType.Quint_EaseIn:
204                 resultAction = action.easing(cc.easeQuinticActionIn());
205                 break;
206             case ccs.FrameEaseType.Quint_EaseOut:
207                 resultAction = action.easing(cc.easeQuinticActionOut());
208                 break;
209             case ccs.FrameEaseType.Quint_EaseInOut:
210                 resultAction = action.easing(cc.easeQuinticActionInOut());
211                 break;
212             case ccs.FrameEaseType.Expo_EaseIn:
213                 resultAction = action.easing(cc.easeExponentialIn());
214                 break;
215             case ccs.FrameEaseType.Expo_EaseOut:
216                 resultAction = action.easing(cc.easeExponentialOut());
217                 break;
218             case ccs.FrameEaseType.Expo_EaseInOut:
219                 resultAction = action.easing(cc.easeExponentialInOut());
220                 break;
221             case ccs.FrameEaseType.Circ_EaseIn:
222                 resultAction = action.easing(cc.easeCircleActionIn());
223                 break;
224             case ccs.FrameEaseType.Circ_EaseOut:
225                 resultAction = action.easing(cc.easeCircleActionOut());
226                 break;
227             case ccs.FrameEaseType.Circ_EaseInOut:
228                 resultAction = action.easing(cc.easeCircleActionInOut());
229                 break;
230             case ccs.FrameEaseType.Elastic_EaesIn:
231                 resultAction = action.easing(cc.easeElasticIn());
232                 break;
233             case ccs.FrameEaseType.Elastic_EaesOut:
234                 resultAction = action.easing(cc.easeElasticOut());
235                 break;
236             case ccs.FrameEaseType.Elastic_EaesInOut:
237                 resultAction = action.easing(cc.easeElasticInOut());
238                 break;
239             case ccs.FrameEaseType.Back_EaseIn:
240                 resultAction = action.easing(cc.easeBackIn());
241                 break;
242             case ccs.FrameEaseType.Back_EaseOut:
243                 resultAction = action.easing(cc.easeBackOut());
244                 break;
245             case ccs.FrameEaseType.Back_EaseInOut:
246                 resultAction = action.easing(cc.easeBackInOut());
247                 break;
248             case ccs.FrameEaseType.Bounce_EaseIn:
249                 resultAction = action.easing(cc.easeBounceIn());
250                 break;
251             case ccs.FrameEaseType.Bounce_EaseOut:
252                 resultAction = action.easing(cc.easeBounceOut());
253                 break;
254             case ccs.FrameEaseType.Bounce_EaseInOut:
255                 resultAction = action.easing(cc.easeBounceInOut());
256                 break;
257         }
258 
259         return resultAction;
260     },
261 
262     /**
263      *
264      * @param {Array} parameter
265      */
266     setEasingParameter: function(parameter){
267         this._Parameter = [];
268         for(var i=0;i<parameter.length;i++)
269             this._Parameter.push(parameter[i]);
270     },
271 
272     setEasingType: function(easingType){
273         this.easingType = easingType;
274     }
275 });
276 
277 /**
278  * Base class for ccs.ActionMoveFrame
279  * @class
280  * @extends ccs.ActionFrame
281  */
282 ccs.ActionMoveFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionMoveFrame# */{
283     _position: null,
284     ctor: function () {
285         ccs.ActionFrame.prototype.ctor.call(this);
286         this._position = cc.p(0, 0);
287         this.frameType = ccs.FRAME_TYPE_MOVE;
288     },
289 
290     /**
291      * Changes the move action position.
292      * @param {cc.Point|Number} pos
293      * @param {Number} y
294      */
295     setPosition: function (pos, y) {
296         if (y === undefined) {
297             this._position.x = pos.x;
298             this._position.y = pos.y;
299         } else {
300             this._position.x = pos;
301             this._position.y = y;
302         }
303     },
304 
305     /**
306      * Gets the move action position.
307      * @returns {cc.Point}
308      */
309     getPosition: function () {
310         return this._position;
311     },
312 
313     /**
314      * Gets the CCAction of ActionFrame.
315      * @param {number} duration
316      * @returns {cc.MoveTo}
317      */
318     getAction: function (duration) {
319         return this._getEasingAction(cc.moveTo(duration, this._position));
320     }
321 });
322 
323 /**
324  * Base class for ccs.ActionScaleFrame
325  * @class
326  * @extends ccs.ActionFrame
327  */
328 ccs.ActionScaleFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionScaleFrame# */{
329     _scaleX: 1,
330     _scaleY: 1,
331     ctor: function () {
332         ccs.ActionFrame.prototype.ctor.call(this);
333         this._scaleX = 1;
334         this._scaleY = 1;
335         this.frameType = ccs.FRAME_TYPE_SCALE;
336     },
337 
338     /**
339      * Changes the scale action scaleX.
340      * @param {number} scaleX
341      */
342     setScaleX: function (scaleX) {
343         this._scaleX = scaleX;
344     },
345 
346     /**
347      * Gets the scale action scaleX.
348      * @returns {number} {number}
349      */
350     getScaleX: function () {
351         return this._scaleX;
352     },
353 
354     /**
355      * Changes the scale action scaleY.
356      * @param {number} scaleY
357      */
358     setScaleY: function (scaleY) {
359         this._scaleY = scaleY;
360     },
361 
362     /**
363      * Gets the scale action scaleY.
364      * @returns {number}
365      */
366     getScaleY: function () {
367         return this._scaleY;
368     },
369 
370     /**
371      * Gets the action of ActionFrame.
372      * @param duration
373      * @returns {cc.ScaleTo}
374      */
375     getAction: function (duration) {
376         return this._getEasingAction(cc.scaleTo(duration, this._scaleX, this._scaleY));
377     }
378 });
379 
380 /**
381  * Base class for ccs.ActionRotationFrame
382  * @class
383  * @extends ccs.ActionFrame
384  */
385 ccs.ActionRotationFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionRotationFrame# */{
386     _rotation: 0,
387     ctor: function () {
388         ccs.ActionFrame.prototype.ctor.call(this);
389         this._rotation = 0;
390         this.frameType = ccs.FRAME_TYPE_ROTATE;
391     },
392 
393     /**
394      * Changes rotate action rotation.
395      * @param {number} rotation
396      */
397     setRotation: function (rotation) {
398         this._rotation = rotation;
399     },
400 
401     /**
402      * Gets the rotate action rotation.
403      * @returns {number}
404      */
405     getRotation: function () {
406         return this._rotation;
407     },
408 
409     /**
410      * Gets the CCAction of ActionFrame.
411      * @param {number} duration
412      * @param {cc.ActionFrame} [srcFrame]
413      * @returns {cc.RotateTo}
414      */
415     getAction: function (duration, srcFrame) {
416         if(srcFrame === undefined)
417             return this._getEasingAction(cc.rotateTo(duration, this._rotation));
418         else {
419             if (!(srcFrame instanceof cc.ActionRotationFrame))
420                 return this.getAction(duration);
421             else{
422                 var diffRotation = this._rotation - srcFrame._rotation;
423                 return this._getEasingAction(cc.rotateBy(duration,diffRotation));
424             }
425         }
426     }
427 });
428 
429 /**
430  * Base class for ccs.ActionFadeFrame
431  * @class
432  * @extends ccs.ActionFrame
433  */
434 ccs.ActionFadeFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionFadeFrame# */{
435     _opacity: 255,
436     ctor: function () {
437         ccs.ActionFrame.prototype.ctor.call(this);
438         this._opacity = 255;
439         this.frameType = ccs.FRAME_TYPE_FADE;
440     },
441 
442     /**
443      * Changes the fade action opacity.
444      * @param {number} opacity
445      */
446     setOpacity: function (opacity) {
447         this._opacity = opacity;
448     },
449 
450     /**
451      * Gets the fade action opacity.
452      * @returns {number}
453      */
454     getOpacity: function () {
455         return this._opacity;
456     },
457 
458     /**
459      * Gets the CCAction of ActionFrame.
460      * @param duration
461      * @returns {cc.FadeTo}
462      */
463     getAction: function (duration) {
464         return this._getEasingAction(cc.fadeTo(duration, this._opacity));
465     }
466 });
467 
468 /**
469  * Base class for ccs.ActionTintFrame
470  * @class
471  * @extends ccs.ActionFrame
472  */
473 ccs.ActionTintFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionTintFrame# */{
474     _color: null,
475     ctor: function () {
476         ccs.ActionFrame.prototype.ctor.call(this);
477         this._color = cc.color(255, 255, 255, 255);
478         this.frameType = ccs.FRAME_TYPE_TINT;
479     },
480 
481     /**
482      * Changes the tint action color.
483      * @param {cc.Color} color
484      */
485     setColor: function (color) {
486         var locColor = this._color;
487         locColor.r = color.r;
488         locColor.g = color.g;
489         locColor.b = color.b;
490     },
491 
492     /**
493      * Gets the tint action color.
494      * @returns {cc.Color}
495      */
496     getColor: function () {
497         var locColor = this._color;
498         return cc.color(locColor.r, locColor.g, locColor.b, locColor.a);
499     },
500 
501     /**
502      * Gets the action of ActionFrame.
503      * @param duration
504      * @returns {cc.TintTo}
505      */
506     getAction: function (duration) {
507         return this._getEasingAction(cc.tintTo(duration, this._color.r, this._color.g, this._color.b));
508     }
509 });