1 /****************************************************************************
  2  Copyright (c) 2008-2010 Ricardo Quesada
  3  Copyright (c) 2011-2012 cocos2d-x.org
  4  Copyright (c) 2013-2014 Chukong Technologies 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  * Base class for Grid actions
 29  * @class
 30  * @extends cc.ActionInterval
 31  * @param {Number} duration
 32  * @param {cc.Size} gridSize
 33  */
 34 cc.GridAction = cc.ActionInterval.extend(/** @lends cc.GridAction# */{
 35     _gridSize:null,
 36 
 37 	/**
 38 	 * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
 39 	 * @param {Number} duration
 40 	 * @param {cc.Size} gridSize
 41 	 */
 42     ctor:function(duration, gridSize){
 43         cc._checkWebGLRenderMode();
 44         cc.ActionInterval.prototype.ctor.call(this);
 45         this._gridSize = cc.size(0,0);
 46 
 47 		gridSize && this.initWithDuration(duration, gridSize);
 48     },
 49 
 50     /**
 51      * to copy object with deep copy.
 52      * returns a clone of action.
 53      *
 54      * @return {cc.Action}
 55      */
 56     clone:function(){
 57         var action = new cc.GridAction();
 58         var locGridSize = this._gridSize;
 59         action.initWithDuration(this._duration, cc.size(locGridSize.width, locGridSize.height));
 60         return action;
 61     },
 62 
 63     /**
 64      * called before the action start. It will also set the target.
 65      *
 66      * @param {cc.Node} target
 67      */
 68     startWithTarget:function (target) {
 69         cc.ActionInterval.prototype.startWithTarget.call(this, target);
 70         var newGrid = this.getGrid();
 71         var t = this.target;
 72         var targetGrid = t.grid;
 73         if (targetGrid && targetGrid.getReuseGrid() > 0) {
 74             var locGridSize = targetGrid.getGridSize();
 75             if (targetGrid.isActive() && (locGridSize.width == this._gridSize.width) && (locGridSize.height == this._gridSize.height))
 76                 targetGrid.reuse();
 77         } else {
 78             if (targetGrid && targetGrid.isActive())
 79                 targetGrid.setActive(false);
 80             t.grid = newGrid;
 81             t.grid.setActive(true);
 82         }
 83     },
 84 
 85     /**
 86      * Create a cc.ReverseTime action. Opposite with the original motion trajectory.
 87      * @return {cc.ReverseTime}
 88      */
 89     reverse:function () {
 90         return new cc.ReverseTime(this);
 91     },
 92 
 93     /**
 94      * Initializes the action with size and duration.
 95      * @param {Number} duration
 96      * @param {cc.Size} gridSize
 97      * @return {Boolean}
 98      */
 99     initWithDuration:function (duration, gridSize) {
100         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
101             this._gridSize.width = gridSize.width;
102             this._gridSize.height = gridSize.height;
103             return true;
104         }
105         return false;
106     },
107 
108     /**
109      * Returns the grid.
110      * @return {cc.GridBase}
111      */
112     getGrid:function () {
113         // Abstract class needs implementation
114         cc.log("cc.GridAction.getGrid(): it should be overridden in subclass.");
115     }
116 });
117 
118 /**
119  * creates the action with size and duration
120  * @function
121  * @param {Number} duration
122  * @param {cc.Size} gridSize
123  * @return {cc.GridAction}
124  */
125 cc.gridAction = function (duration, gridSize) {
126     return new cc.GridAction(duration, gridSize);
127 };
128 
129 /**
130  * Please use cc.gridAction instead. <br />
131  * Creates the action with size and duration.
132  * @param {Number} duration
133  * @param {cc.Size} gridSize
134  * @return {cc.GridAction}
135  * @static
136  * @deprecated since v3.0 <br /> Please use cc.gridAction instead.
137  */
138 cc.GridAction.create = cc.gridAction;
139 
140 /**
141  * Base class for cc.Grid3D actions. <br/>
142  * Grid3D actions can modify a non-tiled grid.
143  * @class
144  * @extends cc.GridAction
145  */
146 cc.Grid3DAction = cc.GridAction.extend(/** @lends cc.Grid3DAction# */{
147 
148     /**
149      * returns the grid
150      * @return {cc.Grid3D}
151      */
152     getGrid:function () {
153         return new cc.Grid3D(this._gridSize);
154     },
155 
156     /**
157      * returns the vertex than belongs to certain position in the grid
158      * @param {cc.Point} position
159      * @return {cc.Vertex3F}
160      */
161     vertex:function (position) {
162         return this.target.grid.vertex(position);
163     },
164 
165     /**
166      * returns the non-transformed vertex than belongs to certain position in the grid
167      * @param {cc.Point} position
168      * @return {cc.Vertex3F}
169      */
170     originalVertex:function (position) {
171         return this.target.grid.originalVertex(position);
172     },
173 
174     /**
175      * sets a new vertex to a certain position of the grid
176      * @param {cc.Point} position
177      * @param {cc.Vertex3F} vertex
178      */
179     setVertex:function (position, vertex) {
180         this.target.grid.setVertex(position, vertex);
181     }
182 });
183 
184 /**
185  * creates the action with size and duration
186  * @function
187  * @param {Number} duration
188  * @param {cc.Size} gridSize
189  * @return {cc.Grid3DAction}
190  */
191 cc.grid3DAction = function (duration, gridSize) {
192     return new cc.Grid3DAction(duration, gridSize);
193 };
194 /**
195  * Please use cc.grid3DAction instead. <br />
196  * creates the action with size and duration. <br />
197  * @param {Number} duration
198  * @param {cc.Size} gridSize
199  * @return {cc.Grid3DAction}
200  * @static
201  * @deprecated since v3.0 <br /> Please use cc.grid3DAction instead.
202  */
203 cc.Grid3DAction.create = cc.grid3DAction;
204 
205 /**
206  * Base class for cc.TiledGrid3D actions.
207  * @class
208  * @extends cc.GridAction
209  */
210 cc.TiledGrid3DAction = cc.GridAction.extend(/** @lends cc.TiledGrid3DAction# */{
211 
212     /**
213      * returns the tile that belongs to a certain position of the grid
214      * @param {cc.Point} position
215      * @return {cc.Quad3}
216      */
217     tile:function (position) {
218         return this.target.grid.tile(position);
219     },
220 
221     /**
222      * returns the non-transformed tile that belongs to a certain position of the grid
223      * @param {cc.Point} position
224      * @return {cc.Quad3}
225      */
226     originalTile:function (position) {
227         return this.target.grid.originalTile(position);
228     },
229 
230     /**
231      * sets a new tile to a certain position of the grid
232      * @param {cc.Point} position
233      * @param {cc.Quad3} coords
234      */
235     setTile:function (position, coords) {
236         this.target.grid.setTile(position, coords);
237     },
238 
239     /**
240      * returns the grid
241      * @return {cc.TiledGrid3D}
242      */
243     getGrid:function () {
244         return new cc.TiledGrid3D(this._gridSize);
245     }
246 });
247 
248 /**
249  * Creates the action with duration and grid size
250  * @function
251  * @param {Number} duration
252  * @param {cc.Size} gridSize
253  * @return {cc.TiledGrid3DAction}
254  */
255 cc.tiledGrid3DAction = function (duration, gridSize) {
256     return new cc.TiledGrid3DAction(duration, gridSize);
257 };
258 
259 /**
260  * Please use cc.tiledGrid3DAction instead
261  * Creates the action with duration and grid size
262  * @param {Number} duration
263  * @param {cc.Size} gridSize
264  * @return {cc.TiledGrid3DAction}
265  * @static
266  * @deprecated since v3.0 <br /> Please use cc.tiledGrid3DAction instead.
267  */
268 cc.TiledGrid3DAction.create = cc.tiledGrid3DAction;
269 
270 /**
271  * <p>
272  * cc.StopGrid action.                                               <br/>
273  * @warning Don't call this action if another grid action is active.                 <br/>
274  * Call if you want to remove the the grid effect. Example:                          <br/>
275  * cc.sequence(Lens.action(...), cc.stopGrid(...), null);              <br/>
276  * </p>
277  * @class
278  * @extends cc.ActionInstant
279  */
280 cc.StopGrid = cc.ActionInstant.extend(/** @lends cc.StopGrid# */{
281 
282     /**
283      * called before the action start. It will also set the target.
284      *
285      * @param {cc.Node} target
286      */
287     startWithTarget:function (target) {
288         cc.ActionInstant.prototype.startWithTarget.call(this, target);
289         var grid = this.target.grid;
290         if (grid && grid.isActive())
291             grid.setActive(false);
292     }
293 });
294 
295 /**
296  * Allocates and initializes the action
297  * @function
298  * @return {cc.StopGrid}
299  */
300 cc.stopGrid = function () {
301     return new cc.StopGrid();
302 };
303 /**
304  * Please use cc.stopGrid instead
305  * Allocates and initializes the action
306  * @return {cc.StopGrid}
307  * @static
308  * @deprecated since v3.0 <br /> Please use cc.stopGrid instead.
309  */
310 cc.StopGrid.create = cc.stopGrid;
311 
312 /**
313  * cc.ReuseGrid action
314  * @class
315  * @extends cc.ActionInstant
316  * @param {Number} times
317  */
318 cc.ReuseGrid = cc.ActionInstant.extend(/** @lends cc.ReuseGrid# */{
319     _times:null,
320 
321 	/**
322 	 * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function.
323 	 * @param {Number} times
324 	 */
325 	ctor: function(times) {
326 		cc.ActionInstant.prototype.ctor.call(this);
327 		times !== undefined && this.initWithTimes(times);
328 	},
329 
330     /**
331      * initializes an action with the number of times that the current grid will be reused
332      * @param {Number} times
333      * @return {Boolean}
334      */
335     initWithTimes:function (times) {
336         this._times = times;
337         return true;
338     },
339 
340     /**
341      * called before the action start. It will also set the target.
342      *
343      * @param {cc.Node} target
344      */
345     startWithTarget:function (target) {
346         cc.ActionInstant.prototype.startWithTarget.call(this, target);
347         if (this.target.grid && this.target.grid.isActive())
348             this.target.grid.setReuseGrid(this.target.grid.getReuseGrid() + this._times);
349     }
350 });
351 
352 /**
353  * creates an action with the number of times that the current grid will be reused
354  * @function
355  * @param {Number} times
356  * @return {cc.ReuseGrid}
357  */
358 cc.reuseGrid = function (times) {
359     return new cc.ReuseGrid(times);
360 };
361 /**
362  * Please use cc.reuseGrid instead
363  * creates an action with the number of times that the current grid will be reused
364  * @param {Number} times
365  * @return {cc.ReuseGrid}
366  * @static
367  * @deprecated since v3.0 <br /> Please use cc.reuseGrid instead.
368  */
369 cc.ReuseGrid.create = cc.reuseGrid;
370