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