1 /****************************************************************************
  2  Copyright (c) 2010-2012 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  * @class
 29  * @extends cc.Class
 30  */
 31 cc.PointObject = cc.Class.extend(/** @lends cc.PointObject# */{
 32     _ratio:null,
 33     _offset:null,
 34     _child:null,
 35 
 36     /**
 37      * @return  {cc.Point}
 38      */
 39     getRatio:function () {
 40         return this._ratio;
 41     },
 42 
 43     /**
 44      * @param  {cc.Point} value
 45      */
 46     setRatio:function (value) {
 47         this._ratio = value;
 48     },
 49 
 50     /**
 51      * @return  {cc.Point}
 52      */
 53     getOffset:function () {
 54         return this._offset;
 55     },
 56 
 57     /**
 58      * @param {cc.Point} value
 59      */
 60     setOffset:function (value) {
 61         this._offset = value;
 62     },
 63 
 64     /**
 65      * @return {cc.Node}
 66      */
 67     getChild:function () {
 68         return this._child;
 69     },
 70 
 71     /**
 72      * @param  {cc.Node} value
 73      */
 74     setChild:function (value) {
 75         this._child = value;
 76     },
 77 
 78     /**
 79      * @param  {cc.Point} ratio
 80      * @param  {cc.Point} offset
 81      * @return {Boolean}
 82      */
 83     initWithCCPoint:function (ratio, offset) {
 84         this._ratio = ratio;
 85         this._offset = offset;
 86         this._child = null;
 87         return true;
 88     }
 89 });
 90 
 91 /**
 92  * @param {cc.Point} ratio
 93  * @param {cc.Point} offset
 94  * @return {cc.PointObject}
 95  */
 96 cc.PointObject.create = function (ratio, offset) {
 97     var ret = new cc.PointObject();
 98     ret.initWithCCPoint(ratio, offset);
 99     return ret;
100 };
101 
102 /**
103  * <p>cc.ParallaxNode: A node that simulates a parallax scroller<br />
104  * The children will be moved faster / slower than the parent according the the parallax ratio. </p>
105  * @class
106  * @extends cc.Node
107  */
108 cc.ParallaxNode = cc.Node.extend(/** @lends cc.ParallaxNode# */{
109     _lastPosition:null,
110     _parallaxArray:null,
111 
112     /**
113      * @return {Array}
114      */
115 
116     getParallaxArray:function () {
117         return this._parallaxArray;
118     },
119 
120     /**
121      * @param {Array} value
122      */
123     setParallaxArray:function (value) {
124         this._parallaxArray = value;
125     },
126 
127     /**
128      * Constructor
129      */
130     ctor:function () {
131         cc.Node.prototype.ctor.call(this);
132         this._parallaxArray = [];
133         this._lastPosition = cc.p(-100, -100);
134     },
135 
136     /**
137      * Adds a child to the container with a z-order, a parallax ratio and a position offset
138      * It returns self, so you can chain several addChilds.
139      * @param {cc.Node} child
140      * @param {Number} z
141      * @param {cc.Point} ratio
142      * @param {cc.Point} offset
143      * @example
144      * //example
145      * voidNode.addChild(background, -1, cc.p(0.4, 0.5), cc.PointZero());
146      */
147     addChild:function (child, z, ratio, offset) {
148         if (arguments.length === 3) {
149             cc.Assert(0, "ParallaxNode: use addChild:z:parallaxRatio:positionOffset instead");
150             return;
151         }
152         cc.Assert(child != null, "Argument must be non-nil");
153         var obj = cc.PointObject.create(ratio, offset);
154         obj.setChild(child);
155         this._parallaxArray.push(obj);
156 
157         var pos = cc.p(this._position.x, this._position.y);
158         pos.x = pos.x * ratio.x + offset.x;
159         pos.y = pos.y * ratio.y + offset.y;
160         child.setPosition(pos);
161 
162         cc.Node.prototype.addChild.call(this, child, z, child.getTag());
163     },
164 
165     /**
166      *  Remove Child
167      * @param {cc.Node} child
168      * @param {Boolean} cleanup
169      * @example
170      * //example
171      * voidNode.removeChild(background,true);
172      */
173     removeChild:function (child, cleanup) {
174         var locParallaxArray = this._parallaxArray;
175         for (var i = 0; i < locParallaxArray.length; i++) {
176             var point = locParallaxArray[i];
177             if (point.getChild() == child) {
178                 locParallaxArray.splice(i, 1);
179                 break;
180             }
181         }
182         cc.Node.prototype.removeChild.call(this, child, cleanup);
183     },
184 
185     /**
186      *  Remove all children with cleanup
187      * @param {Boolean} cleanup
188      */
189     removeAllChildren:function (cleanup) {
190         this._parallaxArray = [];
191         cc.Node.prototype.removeAllChildren.call(this, cleanup);
192     },
193 
194     /**
195      * Visit
196      */
197     visit:function () {
198         var pos = this._absolutePosition();
199         if (!cc.pointEqualToPoint(pos, this._lastPosition)) {
200             var locParallaxArray = this._parallaxArray;
201             for (var i = 0, len = locParallaxArray.length; i < len; i++) {
202                 var point = locParallaxArray[i];
203                 var x = -pos.x + pos.x * point.getRatio().x + point.getOffset().x;
204                 var y = -pos.y + pos.y * point.getRatio().y + point.getOffset().y;
205                 point.getChild().setPosition(x, y);
206             }
207             this._lastPosition = pos;
208         }
209         cc.Node.prototype.visit.call(this);
210     },
211 
212     _absolutePosition:function () {
213         var ret = this._position;
214         var cn = this;
215         while (cn.getParent() != null) {
216             cn = cn.getParent();
217             ret = cc.pAdd(ret, cn.getPosition());
218         }
219         return ret;
220     }
221 });
222 
223 /**
224  * @return {cc.ParallaxNode}
225  * @example
226  * //example
227  * var voidNode = cc.ParallaxNode.create();
228  */
229 cc.ParallaxNode.create = function () {
230     return new cc.ParallaxNode();
231 };
232