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  * @ignore
 29  */
 30 ccs.FRAME_TYPE_MOVE = 0;
 31 ccs.FRAME_TYPE_SCALE = 1;
 32 ccs.FRAME_TYPE_ROTATE = 2;
 33 ccs.FRAME_TYPE_TINT = 3;
 34 ccs.FRAME_TYPE_FADE = 4;
 35 ccs.FRAME_TYPE_MAX = 5;
 36 
 37 ccs.FrameEaseType = {
 38     Custom : -1,
 39 
 40     Linear : 0,
 41 
 42     Sine_EaseIn : 1,
 43     Sine_EaseOut : 2,
 44     Sine_EaseInOut : 3,
 45 
 46     Quad_EaseIn : 4,
 47     Quad_EaseOut : 5,
 48     Quad_EaseInOut : 6,
 49 
 50     Cubic_EaseIn : 7,
 51     Cubic_EaseOut : 8,
 52     Cubic_EaseInOut : 9,
 53 
 54     Quart_EaseIn : 10,
 55     Quart_EaseOut : 11,
 56     Quart_EaseInOut : 12,
 57 
 58     Quint_EaseIn : 13,
 59     Quint_EaseOut : 14,
 60     Quint_EaseInOut : 15,
 61 
 62     Expo_EaseIn : 16,
 63     Expo_EaseOut : 17,
 64     Expo_EaseInOut : 18,
 65 
 66     Circ_EaseIn : 19,
 67     Circ_EaseOut : 20,
 68     Circ_EaseInOut : 21,
 69 
 70     Elastic_EaesIn : 22,
 71     Elastic_EaesOut : 23,
 72     Elastic_EaesInOut : 24,
 73 
 74     Back_EaseIn : 25,
 75     Back_EaseOut : 26,
 76     Back_EaseInOut : 27,
 77 
 78     Bounce_EaseIn : 28,
 79     Bounce_EaseOut : 29,
 80     Bounce_EaseInOut : 30
 81 };
 82 
 83 
 84 /**
 85  * Base class for ccs.ActionFrame
 86  * @class
 87  * @extends ccs.Class
 88  */
 89 ccs.ActionFrame = ccs.Class.extend(/** @lends ccs.ActionFrame# */{
 90     frameType: 0,
 91     easingType: 0,
 92     frameIndex: 0,
 93     frameTweenParameter: null,
 94     time: 0,
 95     ctor: function () {
 96         this.frameType = 0;
 97         this.easingType = 0;
 98         this.frameIndex = 0;
 99         this.time = 0;
100     },
101 
102     /**
103      * Gets the action of ActionFrame.
104      * @param {number} duration
105      * @returns {null}
106      */
107     getAction: function (duration) {
108         return null;
109     },
110 
111     _getEasingAction : function (action) {
112         if (action === null) {
113             console.error("Action cannot be null!");
114             return null;
115         }
116 
117         var resultAction;
118         switch (this.easingType) {
119             case ccs.FrameEaseType.Custom:
120                 break;
121             case ccs.FrameEaseType.Linear:
122                 resultAction = action;
123                 break;
124             case ccs.FrameEaseType.Sine_EaseIn:
125                 resultAction = action.easing(cc.easeSineIn());
126                 break;
127             case ccs.FrameEaseType.Sine_EaseOut:
128                 resultAction = action.easing(cc.easeSineOut());
129                 break;
130             case ccs.FrameEaseType.Sine_EaseInOut:
131                 resultAction = action.easing(cc.easeSineInOut());
132                 break;
133             case ccs.FrameEaseType.Quad_EaseIn:
134                 resultAction = action.easing(cc.easeQuadraticActionIn());
135                 break;
136             case ccs.FrameEaseType.Quad_EaseOut:
137                 resultAction = action.easing(cc.easeQuadraticActionOut());
138                 break;
139             case ccs.FrameEaseType.Quad_EaseInOut:
140                 resultAction = action.easing(cc.easeQuadraticActionInOut());
141                 break;
142             case ccs.FrameEaseType.Cubic_EaseIn:
143                 resultAction = action.easing(cc.easeCubicActionIn());
144                 break;
145             case ccs.FrameEaseType.Cubic_EaseOut:
146                 resultAction = action.easing(cc.easeCubicActionOut());
147                 break;
148             case ccs.FrameEaseType.Cubic_EaseInOut:
149                 resultAction = action.easing(cc.easeCubicActionInOut());
150                 break;
151             case ccs.FrameEaseType.Quart_EaseIn:
152                 resultAction = action.easing(cc.easeQuarticActionIn());
153                 break;
154             case ccs.FrameEaseType.Quart_EaseOut:
155                 resultAction = action.easing(cc.easeQuarticActionOut());
156                 break;
157             case ccs.FrameEaseType.Quart_EaseInOut:
158                 resultAction = action.easing(cc.easeQuarticActionInOut());
159                 break;
160             case ccs.FrameEaseType.Quint_EaseIn:
161                 resultAction = action.easing(cc.easeQuinticActionIn());
162                 break;
163             case ccs.FrameEaseType.Quint_EaseOut:
164                 resultAction = action.easing(cc.easeQuinticActionOut());
165                 break;
166             case ccs.FrameEaseType.Quint_EaseInOut:
167                 resultAction = action.easing(cc.easeQuinticActionInOut());
168                 break;
169             case ccs.FrameEaseType.Expo_EaseIn:
170                 resultAction = action.easing(cc.easeExponentialIn());
171                 break;
172             case ccs.FrameEaseType.Expo_EaseOut:
173                 resultAction = action.easing(cc.easeExponentialOut());
174                 break;
175             case ccs.FrameEaseType.Expo_EaseInOut:
176                 resultAction = action.easing(cc.easeExponentialInOut());
177                 break;
178             case ccs.FrameEaseType.Circ_EaseIn:
179                 resultAction = action.easing(cc.easeCircleActionIn());
180                 break;
181             case ccs.FrameEaseType.Circ_EaseOut:
182                 resultAction = action.easing(cc.easeCircleActionOut());
183                 break;
184             case ccs.FrameEaseType.Circ_EaseInOut:
185                 resultAction = action.easing(cc.easeCircleActionInOut());
186                 break;
187             case ccs.FrameEaseType.Elastic_EaesIn:
188                 resultAction = action.easing(cc.easeElasticIn());
189                 break;
190             case ccs.FrameEaseType.Elastic_EaesOut:
191                 resultAction = action.easing(cc.easeElasticOut());
192                 break;
193             case ccs.FrameEaseType.Elastic_EaesInOut:
194                 resultAction = action.easing(cc.easeElasticInOut());
195                 break;
196             case ccs.FrameEaseType.Back_EaseIn:
197                 resultAction = action.easing(cc.easeBackIn());
198                 break;
199             case ccs.FrameEaseType.Back_EaseOut:
200                 resultAction = action.easing(cc.easeBackOut());
201                 break;
202             case ccs.FrameEaseType.Back_EaseInOut:
203                 resultAction = action.easing(cc.easeBackInOut());
204                 break;
205             case ccs.FrameEaseType.Bounce_EaseIn:
206                 resultAction = action.easing(cc.easeBounceIn());
207                 break;
208             case ccs.FrameEaseType.Bounce_EaseOut:
209                 resultAction = action.easing(cc.easeBounceOut());
210                 break;
211             case ccs.FrameEaseType.Bounce_EaseInOut:
212                 resultAction = action.easing(cc.easeBounceInOut());
213                 break;
214         }
215 
216         return resultAction;
217     },
218     setEasingParameter: function(parameter){
219         this._Parameter = [];
220         for(var i=0;i<parameter.length;i++){
221             this._Parameter.push(parameter[i]);
222         }
223     },
224     setEasingType: function(easingType){
225         this._easingType = easingType;
226     }
227 });
228 
229 /**
230  * Base class for ccs.ActionMoveFrame
231  * @class
232  * @extends ccs.ActionFrame
233  */
234 ccs.ActionMoveFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionMoveFrame# */{
235     _position: null,
236     ctor: function () {
237         ccs.ActionFrame.prototype.ctor.call(this);
238         this._position = cc.p(0, 0);
239         this.frameType = ccs.FRAME_TYPE_MOVE;
240     },
241 
242     /**
243      * Changes the move action position.
244      * @param {cc.Point|Number} pos
245      * @param {Number} y
246      */
247     setPosition: function (pos, y) {
248         if (y === undefined) {
249             this._position.x = pos.x;
250             this._position.y = pos.y;
251         } else {
252             this._position.x = pos;
253             this._position.y = y;
254         }
255     },
256 
257     /**
258      * Gets the move action position.
259      * @returns {cc.Point}
260      */
261     getPosition: function () {
262         return this._position;
263     },
264 
265     /**
266      * Gets the CCAction of ActionFrame.
267      * @param {number} duration
268      * @returns {cc.MoveTo}
269      */
270     getAction: function (duration) {
271         var action = cc.MoveTo.create(duration, this._position);
272         action.easingType = this.easingType || ccs.FrameEaseType.Linear;
273         return this._getEasingAction(action);
274     }
275 });
276 
277 /**
278  * Base class for ccs.ActionScaleFrame
279  * @class
280  * @extends ccs.ActionFrame
281  */
282 ccs.ActionScaleFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionScaleFrame# */{
283     _scaleX: 1,
284     _scaleY: 1,
285     ctor: function () {
286         ccs.ActionFrame.prototype.ctor.call(this);
287         this._scaleX = 1;
288         this._scaleY = 1;
289         this.frameType = ccs.FRAME_TYPE_SCALE;
290     },
291 
292     /**
293      * Changes the scale action scaleX.
294      * @param {number} scaleX
295      */
296     setScaleX: function (scaleX) {
297         this._scaleX = scaleX;
298     },
299 
300     /**
301      * Gets the scale action scaleX.
302      * @returns {number} {number}
303      */
304     getScaleX: function () {
305         return this._scaleX;
306     },
307 
308     /**
309      * Changes the scale action scaleY.
310      * @param {number} scaleY
311      */
312     setScaleY: function (scaleY) {
313         this._scaleY = scaleY;
314     },
315 
316     /**
317      * Gets the scale action scaleY.
318      * @returns {number}
319      */
320     getScaleY: function () {
321         return this._scaleY;
322     },
323 
324     /**
325      * Gets the action of ActionFrame.
326      * @param duration
327      * @returns {cc.ScaleTo}
328      */
329     getAction: function (duration) {
330         var action = cc.ScaleTo.create(duration, this._scaleX, this._scaleY);
331         action.easingType = this.easingType || ccs.FrameEaseType.Linear;
332         return this._getEasingAction(action);
333     }
334 });
335 
336 /**
337  * Base class for ccs.ActionRotationFrame
338  * @class
339  * @extends ccs.ActionFrame
340  */
341 ccs.ActionRotationFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionRotationFrame# */{
342     _rotation: 0,
343     ctor: function () {
344         ccs.ActionFrame.prototype.ctor.call(this);
345         this._rotation = 0;
346         this.frameType = ccs.FRAME_TYPE_ROTATE;
347     },
348 
349     /**
350      * Changes rotate action rotation.
351      * @param {number} rotation
352      */
353     setRotation: function (rotation) {
354         this._rotation = rotation;
355     },
356 
357     /**
358      * Gets the rotate action rotation.
359      * @returns {number}
360      */
361     getRotation: function () {
362         return this._rotation;
363     },
364 
365     /**
366      * Gets the CCAction of ActionFrame.
367      * @param {number} duration
368      * @returns {cc.RotateTo}
369      */
370     getAction: function (duration) {
371         var action = cc.RotateTo.create(duration, this._rotation);
372         action.easingType = this.easingType || ccs.FrameEaseType.Linear;
373         return this._getEasingAction(action);
374     }
375 });
376 
377 /**
378  * Base class for ccs.ActionFadeFrame
379  * @class
380  * @extends ccs.ActionFrame
381  */
382 ccs.ActionFadeFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionFadeFrame# */{
383     _opacity: 255,
384     ctor: function () {
385         ccs.ActionFrame.prototype.ctor.call(this);
386         this._opacity = 255;
387         this.frameType = ccs.FRAME_TYPE_FADE;
388     },
389 
390     /**
391      * Changes the fade action opacity.
392      * @param {number} opacity
393      */
394     setOpacity: function (opacity) {
395         this._opacity = opacity;
396     },
397 
398     /**
399      * Gets the fade action opacity.
400      * @returns {number}
401      */
402     getOpacity: function () {
403         return this._opacity;
404     },
405 
406     /**
407      * Gets the CCAction of ActionFrame.
408      * @param duration
409      * @returns {cc.FadeTo}
410      */
411     getAction: function (duration) {
412         var action = cc.FadeTo.create(duration, this._opacity);
413         action.easingType = this.easingType || ccs.FrameEaseType.Linear;
414         return this._getEasingAction(action);
415     }
416 });
417 
418 /**
419  * Base class for ccs.ActionTintFrame
420  * @class
421  * @extends ccs.ActionFrame
422  */
423 ccs.ActionTintFrame = ccs.ActionFrame.extend(/** @lends ccs.ActionTintFrame# */{
424     _color: null,
425     ctor: function () {
426         ccs.ActionFrame.prototype.ctor.call(this);
427         this._color = cc.color(255, 255, 255, 255);
428         this.frameType = ccs.FRAME_TYPE_TINT;
429     },
430 
431     /**
432      * Changes the tint action color.
433      * @param {cc.Color} color
434      */
435     setColor: function (color) {
436         var locColor = this._color;
437         locColor.r = color.r;
438         locColor.g = color.g;
439         locColor.b = color.b;
440     },
441 
442     /**
443      * Gets the tint action color.
444      * @returns {cc.Color}
445      */
446     getColor: function () {
447         var locColor = this._color;
448         return cc.color(locColor.r, locColor.g, locColor.b, locColor.a);
449     },
450 
451     /**
452      * Gets the action of ActionFrame.
453      * @param duration
454      * @returns {cc.TintTo}
455      */
456     getAction: function (duration) {
457         var action = cc.TintTo.create(duration, this._color.r, this._color.g, this._color.b);
458         action.easingType = this.easingType || ccs.FrameEaseType.Linear;
459         return this._getEasingAction(action);
460     }
461 });