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      * Sets the easing parameter to action frame.
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     /**
273      * Sets the easing type to ccs.ActionFrame
274      * @param {Number} easingType
275      */
276     setEasingType: function(easingType){
277         this.easingType = easingType;
278     }
279 });
280 
281 /**
282  * The Cocostudio's move action frame.
283  * @class
284  * @extends ccs.ActionFrame
285  */
286 ccs.ActionMoveFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionMoveFrame# */{
287     _position: null,
288     /**
289      * Construction of ccs.ActionMoveFrame
290      */
291     ctor: function () {
292         ccs.ActionFrame.prototype.ctor.call(this);
293         this._position = cc.p(0, 0);
294         this.frameType = ccs.FRAME_TYPE_MOVE;
295     },
296 
297     /**
298      * Changes the move action position.
299      * @param {cc.Point|Number} pos
300      * @param {Number} y
301      */
302     setPosition: function (pos, y) {
303         if (y === undefined) {
304             this._position.x = pos.x;
305             this._position.y = pos.y;
306         } else {
307             this._position.x = pos;
308             this._position.y = y;
309         }
310     },
311 
312     /**
313      * Returns the move action position.
314      * @returns {cc.Point}
315      */
316     getPosition: function () {
317         return this._position;
318     },
319 
320     /**
321      * Returns the CCAction of ActionFrame.
322      * @param {number} duration
323      * @returns {cc.MoveTo}
324      */
325     getAction: function (duration) {
326         return this._getEasingAction(cc.moveTo(duration, this._position));
327     }
328 });
329 
330 /**
331  * The Cocostudio's scale action frame
332  * @class
333  * @extends ccs.ActionFrame
334  */
335 ccs.ActionScaleFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionScaleFrame# */{
336     _scaleX: 1,
337     _scaleY: 1,
338     /**
339      * Construction of ccs.ActionScaleFrame
340      */
341     ctor: function () {
342         ccs.ActionFrame.prototype.ctor.call(this);
343         this._scaleX = 1;
344         this._scaleY = 1;
345         this.frameType = ccs.FRAME_TYPE_SCALE;
346     },
347 
348     /**
349      * Changes the scale action scaleX.
350      * @param {number} scaleX
351      */
352     setScaleX: function (scaleX) {
353         this._scaleX = scaleX;
354     },
355 
356     /**
357      * Returns the scale action scaleX.
358      * @returns {number}
359      */
360     getScaleX: function () {
361         return this._scaleX;
362     },
363 
364     /**
365      * Changes the scale action scaleY.
366      * @param {number} scaleY
367      */
368     setScaleY: function (scaleY) {
369         this._scaleY = scaleY;
370     },
371 
372     /**
373      * Returns the scale action scaleY.
374      * @returns {number}
375      */
376     getScaleY: function () {
377         return this._scaleY;
378     },
379 
380     /**
381      * Returns the action of ActionFrame.
382      * @param {number} duration
383      * @returns {cc.ScaleTo}
384      */
385     getAction: function (duration) {
386         return this._getEasingAction(cc.scaleTo(duration, this._scaleX, this._scaleY));
387     }
388 });
389 
390 /**
391  * The Cocostudio's rotation action frame.
392  * @class
393  * @extends ccs.ActionFrame
394  */
395 ccs.ActionRotationFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionRotationFrame# */{
396     _rotation: 0,
397     /**
398      * Construction of ccs.ActionRotationFrame
399      */
400     ctor: function () {
401         ccs.ActionFrame.prototype.ctor.call(this);
402         this._rotation = 0;
403         this.frameType = ccs.FRAME_TYPE_ROTATE;
404     },
405 
406     /**
407      * Changes rotate action rotation.
408      * @param {number} rotation
409      */
410     setRotation: function (rotation) {
411         this._rotation = rotation;
412     },
413 
414     /**
415      * Returns the rotate action rotation.
416      * @returns {number}
417      */
418     getRotation: function () {
419         return this._rotation;
420     },
421 
422     /**
423      * Returns the CCAction of ActionFrame.
424      * @param {number} duration
425      * @param {cc.ActionFrame} [srcFrame]
426      * @returns {cc.RotateTo}
427      */
428     getAction: function (duration, srcFrame) {
429         if(srcFrame === undefined)
430             return this._getEasingAction(cc.rotateTo(duration, this._rotation));
431         else {
432             if (!(srcFrame instanceof cc.ActionRotationFrame))
433                 return this.getAction(duration);
434             else{
435                 var diffRotation = this._rotation - srcFrame._rotation;
436                 return this._getEasingAction(cc.rotateBy(duration,diffRotation));
437             }
438         }
439     }
440 });
441 
442 /**
443  * The Cocostudio's fade action frame.
444  * @class
445  * @extends ccs.ActionFrame
446  */
447 ccs.ActionFadeFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionFadeFrame# */{
448     _opacity: 255,
449     /**
450      * Construction of ccs.ActionFadeFrame
451      */
452     ctor: function () {
453         ccs.ActionFrame.prototype.ctor.call(this);
454         this._opacity = 255;
455         this.frameType = ccs.FRAME_TYPE_FADE;
456     },
457 
458     /**
459      * Changes the fade action opacity.
460      * @param {number} opacity
461      */
462     setOpacity: function (opacity) {
463         this._opacity = opacity;
464     },
465 
466     /**
467      * Returns the fade action opacity.
468      * @returns {number}
469      */
470     getOpacity: function () {
471         return this._opacity;
472     },
473 
474     /**
475      * Returns a fade action with easing.
476      * @param {Number} duration
477      * @returns {cc.FadeTo}
478      */
479     getAction: function (duration) {
480         return this._getEasingAction(cc.fadeTo(duration, this._opacity));
481     }
482 });
483 
484 /**
485  * The Cocostudio's tint action frame.
486  * @class
487  * @extends ccs.ActionFrame
488  */
489 ccs.ActionTintFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionTintFrame# */{
490     _color: null,
491     /**
492      * Construction of ccs.ActionTintFrame
493      */
494     ctor: function () {
495         ccs.ActionFrame.prototype.ctor.call(this);
496         this._color = cc.color(255, 255, 255, 255);
497         this.frameType = ccs.FRAME_TYPE_TINT;
498     },
499 
500     /**
501      * Changes the tint action color.
502      * @param {cc.Color} color
503      */
504     setColor: function (color) {
505         var locColor = this._color;
506         locColor.r = color.r;
507         locColor.g = color.g;
508         locColor.b = color.b;
509     },
510 
511     /**
512      * Returns the color of tint action.
513      * @returns {cc.Color}
514      */
515     getColor: function () {
516         var locColor = this._color;
517         return cc.color(locColor.r, locColor.g, locColor.b, locColor.a);
518     },
519 
520     /**
521      * Returns a tint action with easing.
522      * @param duration
523      * @returns {cc.TintTo}
524      */
525     getAction: function (duration) {
526         return this._getEasingAction(cc.tintTo(duration, this._color.r, this._color.g, this._color.b));
527     }
528 });