1 /****************************************************************************
  2  Copyright (c) 2010-2013 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga 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
 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  * @param {Number} duration
105  * @param {cc.Size} gridSize
106  * @return {cc.GridAction}
107  */
108 cc.GridAction.create = function (duration, gridSize) {
109     return new cc.GridAction(duration, gridSize);
110 };
111 
112 /**
113  * Base class for cc.Grid3D actions.                     <br/>
114  * Grid3D actions can modify a non-tiled grid.
115  * @class
116  * @extends cc.GridAction
117  */
118 cc.Grid3DAction = cc.GridAction.extend(/** @lends cc.Grid3DAction# */{
119 
120     /**
121      * returns the grid
122      * @return {cc.GridBase}
123      */
124     getGrid:function () {
125         return cc.Grid3D.create(this._gridSize);
126     },
127 
128     /**
129      * returns the vertex than belongs to certain position in the grid
130      * @param {cc.Point} position
131      * @return {cc.Vertex3F}
132      */
133     vertex:function (position) {
134         return this.target.grid.vertex(position);
135     },
136 
137     /**
138      * returns the non-transformed vertex than belongs to certain position in the grid
139      * @param {cc.Point} position
140      * @return {*}
141      */
142     originalVertex:function (position) {
143         return this.target.grid.originalVertex(position);
144     },
145 
146     /**
147      * sets a new vertex to a certain position of the grid
148      * @param {cc.Point} position
149      * @param {cc.Vertex3F} vertex
150      */
151     setVertex:function (position, vertex) {
152         this.target.grid.setVertex(position, vertex);
153     }
154 });
155 
156 /**
157  * creates the action with size and duration
158  * @param {Number} duration
159  * @param {cc.Size} gridSize
160  * @return {cc.Grid3DAction}
161  */
162 cc.Grid3DAction.create = function (duration, gridSize) {
163     return new cc.Grid3DAction(duration, gridSize);
164 };
165 
166 /**
167  * Base class for cc.TiledGrid3D actions
168  * @class
169  * @extends cc.GridAction
170  */
171 cc.TiledGrid3DAction = cc.GridAction.extend(/** @lends cc.TiledGrid3DAction# */{
172 
173     /**
174      * returns the tile that belongs to a certain position of the grid
175      * @param {cc.Point} position
176      * @return {cc.Quad3}
177      */
178     tile:function (position) {
179         return this.target.grid.tile(position);
180     },
181 
182     /**
183      * returns the non-transformed tile that belongs to a certain position of the grid
184      * @param {cc.Point} position
185      * @return {cc.Quad3}
186      */
187     originalTile:function (position) {
188         return this.target.grid.originalTile(position);
189     },
190 
191     /**
192      * sets a new tile to a certain position of the grid
193      * @param {cc.Point} position
194      * @param {cc.Quad3} coords
195      */
196     setTile:function (position, coords) {
197         this.target.grid.setTile(position, coords);
198     },
199 
200     /**
201      * returns the grid
202      * @return {cc.GridBase}
203      */
204     getGrid:function () {
205         return cc.TiledGrid3D.create(this._gridSize);
206     }
207 });
208 
209 /**
210  * Creates the action with duration and grid size
211  * @param {Number} duration
212  * @param {cc.Size} gridSize
213  * @return {cc.TiledGrid3DAction}
214  */
215 cc.TiledGrid3DAction.create = function (duration, gridSize) {
216     return new cc.TiledGrid3DAction(duration, gridSize);
217 };
218 
219 /**
220  * <p>
221  * cc.StopGrid action.                                               <br/>
222  * @warning Don't call this action if another grid action is active.                 <br/>
223  * Call if you want to remove the the grid effect. Example:                          <br/>
224  * cc.Sequence.create(Lens.action(...), cc.StopGrid.create(...), null);              <br/>
225  * </p>
226  * @class
227  * @extends cc.ActionInstant
228  */
229 cc.StopGrid = cc.ActionInstant.extend(/** @lends cc.StopGrid# */{
230     startWithTarget:function (target) {
231         cc.ActionInstant.prototype.startWithTarget.call(this, target);
232         var grid = this.target.grid;
233         if (grid && grid.isActive())
234             grid.setActive(false);
235     }
236 });
237 
238 /**
239  * Allocates and initializes the action
240  * @return {cc.StopGrid}
241  */
242 cc.StopGrid.create = function () {
243     return new cc.StopGrid();
244 };
245 
246 /**
247  * cc.ReuseGrid action
248  * @class
249  * @extends cc.ActionInstant
250  */
251 cc.ReuseGrid = cc.ActionInstant.extend(/** @lends cc.ReuseGrid# */{
252     _times:null,
253 
254 	/**
255 	 * creates an action with the number of times that the current grid will be reused
256 	 * @constructor
257 	 * @param {Number} times
258 	 */
259 	ctor: function(times) {
260 		cc.ActionInstant.prototype.ctor.call(this);
261 		times !== undefined && this.initWithTimes(times);
262 	},
263 
264     /**
265      * initializes an action with the number of times that the current grid will be reused
266      * @param {Number} times
267      * @return {Boolean}
268      */
269     initWithTimes:function (times) {
270         this._times = times;
271         return true;
272     },
273 
274     startWithTarget:function (target) {
275         cc.ActionInstant.prototype.startWithTarget.call(this, target);
276         if (this.target.grid && this.target.grid.isActive())
277             this.target.grid.setReuseGrid(this.target.grid.getReuseGrid() + this._times);
278     }
279 });
280 
281 /**
282  * creates an action with the number of times that the current grid will be reused
283  * @param {Number} times
284  * @return {cc.ReuseGrid}
285  */
286 cc.ReuseGrid.create = function (times) {
287     return new cc.ReuseGrid(times);
288 };
289