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_EASEIN : 22,
103     ELASTIC_EASEOUT : 23,
104     ELASTIC_EASEINOUT : 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     TWEEN_EASING_MAX: 1000
115 };
116 
117 
118 /**
119  * The action frame of Cocostudio. It's the base class of ccs.ActionMoveFrame, ccs.ActionScaleFrame etc.
120  * @class
121  * @extends ccs.Class
122  *
123  * @property {Number}               frameType               - frame type of ccs.ActionFrame
124  * @property {Number}               easingType              - easing type of ccs.ActionFrame
125  * @property {Number}               frameIndex              - frame index of ccs.ActionFrame
126  * @property {Number}               time                    - time of ccs.ActionFrame
127  */
128 ccs.ActionFrame = ccs.Class.extend(/** @lends ccs.ActionFrame# */{
129     frameType: 0,
130     easingType: 0,
131     frameIndex: 0,
132     _Parameter: null,
133     time: 0,
134 
135     /**
136      * The constructor of cc.ActionFrame.
137      */
138     ctor: function () {
139         this.frameType = 0;
140         this.easingType = ccs.FrameEaseType.LINEAR;
141         this.frameIndex = 0;
142         this.time = 0;
143     },
144 
145     /**
146      * Returns the action of ActionFrame. its subClass need override it.
147      * @param {number} duration the duration time of ActionFrame
148      * @param {ccs.ActionFrame} srcFrame source frame.
149      * @returns {null}
150      */
151     getAction: function (duration, srcFrame) {
152         cc.log("Need a definition of <getAction> for ActionFrame");
153         return null;
154     },
155 
156     _getEasingAction : function (action) {
157         if (action === null) {
158             console.error("Action cannot be null!");
159             return null;
160         }
161 
162         var resultAction;
163         switch (this.easingType) {
164             case ccs.FrameEaseType.CUSTOM:
165                 break;
166             case ccs.FrameEaseType.LINEAR:
167                 resultAction = action;
168                 break;
169             case ccs.FrameEaseType.SINE_EASEIN:
170                 resultAction = action.easing(cc.easeSineIn());
171                 break;
172             case ccs.FrameEaseType.SINE_EASEOUT:
173                 resultAction = action.easing(cc.easeSineOut());
174                 break;
175             case ccs.FrameEaseType.SINE_EASEINOUT:
176                 resultAction = action.easing(cc.easeSineInOut());
177                 break;
178             case ccs.FrameEaseType.QUAD_EASEIN:
179                 resultAction = action.easing(cc.easeQuadraticActionIn());
180                 break;
181             case ccs.FrameEaseType.QUAD_EASEOUT:
182                 resultAction = action.easing(cc.easeQuadraticActionOut());
183                 break;
184             case ccs.FrameEaseType.QUAD_EASEINOUT:
185                 resultAction = action.easing(cc.easeQuadraticActionInOut());
186                 break;
187             case ccs.FrameEaseType.CUBIC_EASEIN:
188                 resultAction = action.easing(cc.easeCubicActionIn());
189                 break;
190             case ccs.FrameEaseType.CUBIC_EASEOUT:
191                 resultAction = action.easing(cc.easeCubicActionOut());
192                 break;
193             case ccs.FrameEaseType.CUBIC_EASEINOUT:
194                 resultAction = action.easing(cc.easeCubicActionInOut());
195                 break;
196             case ccs.FrameEaseType.QUART_EASEIN:
197                 resultAction = action.easing(cc.easeQuarticActionIn());
198                 break;
199             case ccs.FrameEaseType.QUART_EASEOUT:
200                 resultAction = action.easing(cc.easeQuarticActionOut());
201                 break;
202             case ccs.FrameEaseType.QUART_EASEINOUT:
203                 resultAction = action.easing(cc.easeQuarticActionInOut());
204                 break;
205             case ccs.FrameEaseType.QUINT_EASEIN:
206                 resultAction = action.easing(cc.easeQuinticActionIn());
207                 break;
208             case ccs.FrameEaseType.QUINT_EASEOUT:
209                 resultAction = action.easing(cc.easeQuinticActionOut());
210                 break;
211             case ccs.FrameEaseType.QUINT_EASEINOUT:
212                 resultAction = action.easing(cc.easeQuinticActionInOut());
213                 break;
214             case ccs.FrameEaseType.EXPO_EASEIN:
215                 resultAction = action.easing(cc.easeExponentialIn());
216                 break;
217             case ccs.FrameEaseType.EXPO_EASEOUT:
218                 resultAction = action.easing(cc.easeExponentialOut());
219                 break;
220             case ccs.FrameEaseType.EXPO_EASEINOUT:
221                 resultAction = action.easing(cc.easeExponentialInOut());
222                 break;
223             case ccs.FrameEaseType.CIRC_EASEIN:
224                 resultAction = action.easing(cc.easeCircleActionIn());
225                 break;
226             case ccs.FrameEaseType.CIRC_EASEOUT:
227                 resultAction = action.easing(cc.easeCircleActionOut());
228                 break;
229             case ccs.FrameEaseType.CIRC_EASEINOUT:
230                 resultAction = action.easing(cc.easeCircleActionInOut());
231                 break;
232             case ccs.FrameEaseType.ELASTIC_EASEIN:
233                 resultAction = action.easing(cc.easeElasticIn());
234                 break;
235             case ccs.FrameEaseType.ELASTIC_EASEOUT:
236                 resultAction = action.easing(cc.easeElasticOut());
237                 break;
238             case ccs.FrameEaseType.ELASTIC_EASEINOUT:
239                 resultAction = action.easing(cc.easeElasticInOut());
240                 break;
241             case ccs.FrameEaseType.BACK_EASEIN:
242                 resultAction = action.easing(cc.easeBackIn());
243                 break;
244             case ccs.FrameEaseType.BACK_EASEOUT:
245                 resultAction = action.easing(cc.easeBackOut());
246                 break;
247             case ccs.FrameEaseType.BACK_EASEINOUT:
248                 resultAction = action.easing(cc.easeBackInOut());
249                 break;
250             case ccs.FrameEaseType.BOUNCE_EASEIN:
251                 resultAction = action.easing(cc.easeBounceIn());
252                 break;
253             case ccs.FrameEaseType.BOUNCE_EASEOUT:
254                 resultAction = action.easing(cc.easeBounceOut());
255                 break;
256             case ccs.FrameEaseType.BOUNCE_EASEINOUT:
257                 resultAction = action.easing(cc.easeBounceInOut());
258                 break;
259         }
260 
261         return resultAction;
262     },
263 
264     /**
265      * Sets the easing parameter to action frame.
266      * @param {Array} parameter
267      */
268     setEasingParameter: function(parameter){
269         this._Parameter = [];
270         for(var i=0;i<parameter.length;i++)
271             this._Parameter.push(parameter[i]);
272     },
273 
274     /**
275      * Sets the easing type to ccs.ActionFrame
276      * @param {Number} easingType
277      */
278     setEasingType: function(easingType){
279         this.easingType = easingType;
280     }
281 });
282 
283 /**
284  * The Cocostudio's move action frame.
285  * @class
286  * @extends ccs.ActionFrame
287  */
288 ccs.ActionMoveFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionMoveFrame# */{
289     _position: null,
290     /**
291      * Construction of ccs.ActionMoveFrame
292      */
293     ctor: function () {
294         ccs.ActionFrame.prototype.ctor.call(this);
295         this._position = cc.p(0, 0);
296         this.frameType = ccs.FRAME_TYPE_MOVE;
297     },
298 
299     /**
300      * Changes the move action position.
301      * @param {cc.Point|Number} pos
302      * @param {Number} y
303      */
304     setPosition: function (pos, y) {
305         if (y === undefined) {
306             this._position.x = pos.x;
307             this._position.y = pos.y;
308         } else {
309             this._position.x = pos;
310             this._position.y = y;
311         }
312     },
313 
314     /**
315      * Returns the move action position.
316      * @returns {cc.Point}
317      */
318     getPosition: function () {
319         return this._position;
320     },
321 
322     /**
323      * Returns the CCAction of ActionFrame.
324      * @param {number} duration
325      * @returns {cc.MoveTo}
326      */
327     getAction: function (duration) {
328         return this._getEasingAction(cc.moveTo(duration, this._position));
329     }
330 });
331 
332 /**
333  * The Cocostudio's scale action frame
334  * @class
335  * @extends ccs.ActionFrame
336  */
337 ccs.ActionScaleFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionScaleFrame# */{
338     _scaleX: 1,
339     _scaleY: 1,
340     /**
341      * Construction of ccs.ActionScaleFrame
342      */
343     ctor: function () {
344         ccs.ActionFrame.prototype.ctor.call(this);
345         this._scaleX = 1;
346         this._scaleY = 1;
347         this.frameType = ccs.FRAME_TYPE_SCALE;
348     },
349 
350     /**
351      * Changes the scale action scaleX.
352      * @param {number} scaleX
353      */
354     setScaleX: function (scaleX) {
355         this._scaleX = scaleX;
356     },
357 
358     /**
359      * Returns the scale action scaleX.
360      * @returns {number}
361      */
362     getScaleX: function () {
363         return this._scaleX;
364     },
365 
366     /**
367      * Changes the scale action scaleY.
368      * @param {number} scaleY
369      */
370     setScaleY: function (scaleY) {
371         this._scaleY = scaleY;
372     },
373 
374     /**
375      * Returns the scale action scaleY.
376      * @returns {number}
377      */
378     getScaleY: function () {
379         return this._scaleY;
380     },
381 
382     /**
383      * Returns the action of ActionFrame.
384      * @param {number} duration
385      * @returns {cc.ScaleTo}
386      */
387     getAction: function (duration) {
388         return this._getEasingAction(cc.scaleTo(duration, this._scaleX, this._scaleY));
389     }
390 });
391 
392 /**
393  * The Cocostudio's rotation action frame.
394  * @class
395  * @extends ccs.ActionFrame
396  */
397 ccs.ActionRotationFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionRotationFrame# */{
398     _rotation: 0,
399     /**
400      * Construction of ccs.ActionRotationFrame
401      */
402     ctor: function () {
403         ccs.ActionFrame.prototype.ctor.call(this);
404         this._rotation = 0;
405         this.frameType = ccs.FRAME_TYPE_ROTATE;
406     },
407 
408     /**
409      * Changes rotate action rotation.
410      * @param {number} rotation
411      */
412     setRotation: function (rotation) {
413         this._rotation = rotation;
414     },
415 
416     /**
417      * Returns the rotate action rotation.
418      * @returns {number}
419      */
420     getRotation: function () {
421         return this._rotation;
422     },
423 
424     /**
425      * Returns the CCAction of ActionFrame.
426      * @param {number} duration
427      * @param {cc.ActionFrame} [srcFrame]
428      * @returns {cc.RotateTo}
429      */
430     getAction: function (duration, srcFrame) {
431         if(srcFrame === undefined)
432             return this._getEasingAction(cc.rotateTo(duration, this._rotation));
433         else {
434             if (!(srcFrame instanceof cc.ActionRotationFrame))
435                 return this.getAction(duration);
436             else{
437                 var diffRotation = this._rotation - srcFrame._rotation;
438                 return this._getEasingAction(cc.rotateBy(duration,diffRotation));
439             }
440         }
441     }
442 });
443 
444 /**
445  * The Cocostudio's fade action frame.
446  * @class
447  * @extends ccs.ActionFrame
448  */
449 ccs.ActionFadeFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionFadeFrame# */{
450     _opacity: 255,
451     /**
452      * Construction of ccs.ActionFadeFrame
453      */
454     ctor: function () {
455         ccs.ActionFrame.prototype.ctor.call(this);
456         this._opacity = 255;
457         this.frameType = ccs.FRAME_TYPE_FADE;
458     },
459 
460     /**
461      * Changes the fade action opacity.
462      * @param {number} opacity
463      */
464     setOpacity: function (opacity) {
465         this._opacity = opacity;
466     },
467 
468     /**
469      * Returns the fade action opacity.
470      * @returns {number}
471      */
472     getOpacity: function () {
473         return this._opacity;
474     },
475 
476     /**
477      * Returns a fade action with easing.
478      * @param {Number} duration
479      * @returns {cc.FadeTo}
480      */
481     getAction: function (duration) {
482         return this._getEasingAction(cc.fadeTo(duration, this._opacity));
483     }
484 });
485 
486 /**
487  * The Cocostudio's tint action frame.
488  * @class
489  * @extends ccs.ActionFrame
490  */
491 ccs.ActionTintFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionTintFrame# */{
492     _color: null,
493     /**
494      * Construction of ccs.ActionTintFrame
495      */
496     ctor: function () {
497         ccs.ActionFrame.prototype.ctor.call(this);
498         this._color = cc.color(255, 255, 255, 255);
499         this.frameType = ccs.FRAME_TYPE_TINT;
500     },
501 
502     /**
503      * Changes the tint action color.
504      * @param {cc.Color} color
505      */
506     setColor: function (color) {
507         var locColor = this._color;
508         locColor.r = color.r;
509         locColor.g = color.g;
510         locColor.b = color.b;
511     },
512 
513     /**
514      * Returns the color of tint action.
515      * @returns {cc.Color}
516      */
517     getColor: function () {
518         var locColor = this._color;
519         return cc.color(locColor.r, locColor.g, locColor.b, locColor.a);
520     },
521 
522     /**
523      * Returns a tint action with easing.
524      * @param duration
525      * @returns {cc.TintTo}
526      */
527     getAction: function (duration) {
528         return this._getEasingAction(cc.tintTo(duration, this._color.r, this._color.g, this._color.b));
529     }
530 });