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.NodeRGBA.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.NodeRGBA.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.log("ParallaxNode: use addChild(child, z, ratio, offset) instead")
150             return;
151         }
152         if(!child)
153             throw "cc.ParallaxNode.addChild(): child should be non-null";
154         var obj = cc.PointObject.create(ratio, offset);
155         obj.setChild(child);
156         this._parallaxArray.push(obj);
157 
158         var pos = cc.p(this._position.x, this._position.y);
159         pos.x = pos.x * ratio.x + offset.x;
160         pos.y = pos.y * ratio.y + offset.y;
161         child.setPosition(pos);
162 
163         cc.NodeRGBA.prototype.addChild.call(this, child, z, child.getTag());
164     },
165 
166     /**
167      *  Remove Child
168      * @param {cc.Node} child
169      * @param {Boolean} cleanup
170      * @example
171      * //example
172      * voidNode.removeChild(background,true);
173      */
174     removeChild:function (child, cleanup) {
175         var locParallaxArray = this._parallaxArray;
176         for (var i = 0; i < locParallaxArray.length; i++) {
177             var point = locParallaxArray[i];
178             if (point.getChild() == child) {
179                 locParallaxArray.splice(i, 1);
180                 break;
181             }
182         }
183         cc.NodeRGBA.prototype.removeChild.call(this, child, cleanup);
184     },
185 
186     /**
187      *  Remove all children with cleanup
188      * @param {Boolean} cleanup
189      */
190     removeAllChildren:function (cleanup) {
191         this._parallaxArray.length = 0;
192         cc.NodeRGBA.prototype.removeAllChildren.call(this, cleanup);
193     },
194 
195     /**
196      * Visit
197      */
198     visit:function () {
199         var pos = this._absolutePosition();
200         if (!cc.pointEqualToPoint(pos, this._lastPosition)) {
201             var locParallaxArray = this._parallaxArray;
202             for (var i = 0, len = locParallaxArray.length; i < len; i++) {
203                 var point = locParallaxArray[i];
204                 var x = -pos.x + pos.x * point.getRatio().x + point.getOffset().x;
205                 var y = -pos.y + pos.y * point.getRatio().y + point.getOffset().y;
206                 point.getChild().setPosition(x, y);
207             }
208             this._lastPosition = pos;
209         }
210         cc.NodeRGBA.prototype.visit.call(this);
211     },
212 
213     _absolutePosition:function () {
214         var ret = this._position;
215         var cn = this;
216         while (cn.getParent() != null) {
217             cn = cn.getParent();
218             ret = cc.pAdd(ret, cn.getPosition());
219         }
220         return ret;
221     }
222 });
223 
224 /**
225  * @return {cc.ParallaxNode}
226  * @example
227  * //example
228  * var voidNode = cc.ParallaxNode.create();
229  */
230 cc.ParallaxNode.create = function () {
231     return new cc.ParallaxNode();
232 };
233