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  * @property {Array}    parallaxArray   - Parallax nodes array
109  */
110 cc.ParallaxNode = cc.NodeRGBA.extend(/** @lends cc.ParallaxNode# */{
111 	parallaxArray:null,
112 
113     _lastPosition:null,
114     _className:"ParallaxNode",
115 
116     /**
117      * @return {Array}
118      */
119     getParallaxArray:function () {
120         return this.parallaxArray;
121     },
122 
123     /**
124      * @param {Array} value
125      */
126     setParallaxArray:function (value) {
127         this.parallaxArray = value;
128     },
129 
130     /**
131      * Constructor
132      */
133     ctor:function () {
134         cc.NodeRGBA.prototype.ctor.call(this);
135         this.parallaxArray = [];
136         this._lastPosition = cc.p(-100, -100);
137     },
138 
139     /**
140      * Adds a child to the container with a z-order, a parallax ratio and a position offset
141      * It returns self, so you can chain several addChilds.
142      * @param {cc.Node} child
143      * @param {Number} z
144      * @param {cc.Point} ratio
145      * @param {cc.Point} offset
146      * @example
147      * //example
148      * voidNode.addChild(background, -1, cc.p(0.4, 0.5), cc.p(0,0));
149      */
150     addChild:function (child, z, ratio, offset) {
151         if (arguments.length === 3) {
152             cc.log("ParallaxNode: use addChild(child, z, ratio, offset) instead")
153             return;
154         }
155         if(!child)
156             throw "cc.ParallaxNode.addChild(): child should be non-null";
157         var obj = cc.PointObject.create(ratio, offset);
158         obj.setChild(child);
159         this.parallaxArray.push(obj);
160 
161 	    child.setPosition(this._position.x * ratio.x + offset.x, this._position.y * ratio.y + offset.y);
162 
163         cc.NodeRGBA.prototype.addChild.call(this, child, z, child.tag);
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 child = point.getChild();
205 	            child.setPosition(-pos.x + pos.x * point.getRatio().x + point.getOffset().x,
206 	                               -pos.y + pos.y * point.getRatio().y + point.getOffset().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.parent != null) {
217             cn = cn.parent;
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