1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3 
  4  http://www.cocos2d-x.org
  5 
  6  Permission is hereby granted, free of charge, to any person obtaining a copy
  7  of this software and associated documentation files (the "Software"), to deal
  8  in the Software without restriction, including without limitation the rights
  9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  copies of the Software, and to permit persons to whom the Software is
 11  furnished to do so, subject to the following conditions:
 12 
 13  The above copyright notice and this permission notice shall be included in
 14  all copies or substantial portions of the Software.
 15 
 16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22  THE SOFTWARE.
 23  ****************************************************************************/
 24 
 25 /**
 26  * Base class for ccs.UILayer
 27  * @class
 28  * @extends cc.Layer
 29  */
 30 ccs.UILayer = cc.Layer.extend(/** @lends ccs.UILayer# */{
 31     _rootWidget: null,
 32     _selectedWidgets: null,
 33     _touchBeganedPoint: null,
 34     _touchMovedPoint: null,
 35     _touchEndedPoint: null,
 36     _touchCanceledPoint: null,
 37     ctor: function () {
 38         cc.Layer.prototype.ctor.call(this);
 39         this._selectedWidgets = [];
 40         this._touchBeganedPoint = cc.p(0, 0);
 41         this._touchMovedPoint = cc.p(0, 0);
 42         this._touchEndedPoint = cc.p(0, 0);
 43         this._touchCanceledPoint = cc.p(0, 0);
 44     },
 45     init: function () {
 46         if (cc.Layer.prototype.init.call(this)) {
 47             this._rootWidget = ccs.Widget.create();
 48             this.addChild(this._rootWidget);
 49             return true;
 50         }
 51         return false;
 52     },
 53 
 54     onEnter: function () {
 55         this.setTouchMode(cc.TOUCH_ONE_BY_ONE);
 56         this.setTouchEnabled(true);
 57         cc.Layer.prototype.onEnter.call(this);
 58 
 59     },
 60 
 61     onExit: function () {
 62         this.setTouchEnabled(false);
 63         cc.Layer.prototype.onExit.call(this);
 64     },
 65 
 66     /**
 67      * Check touch event
 68      * @param {ccs.Widget} root
 69      * @param {cc.Touch} touch
 70      * @param {event} event
 71      * @returns {boolean}
 72      */
 73     checkTouchEvent: function (root, touch,event) {
 74         var arrayRootChildren = root.getChildren();
 75         var length = arrayRootChildren.length;
 76         for (var i = length - 1; i >= 0; i--) {
 77             var widget = arrayRootChildren[i];
 78             if (this.checkTouchEvent(widget, touch,event)) {
 79                 return true;
 80             }
 81         }
 82         var pass = root.onTouchBegan(touch,event);
 83         if (root._hitted)
 84         {
 85             this._selectedWidgets.push(root);
 86             return true;
 87         }
 88         return pass;
 89     },
 90 
 91     /**
 92      * Finds a widget which is selected and call it's "onTouchBegan" method.
 93      * @param {cc.Touch} touch
 94      * @param {event} event
 95      * @returns {boolean}
 96      */
 97     checkEventWidget: function (touch,event) {
 98         this.checkTouchEvent(this._rootWidget, touch,event);
 99         return (this._selectedWidgets.length > 0);
100     },
101 
102     /**
103      * add widget
104      * @param {ccs.Widget} widget
105      */
106     addWidget: function (widget) {
107         this._rootWidget.addChild(widget);
108     },
109 
110     /**
111      * remove widget
112      * @param {ccs.Widget} widget
113      */
114     removeWidget: function (widget) {
115         this._rootWidget.removeChild(widget);
116     },
117 
118     /**
119      * get widget by tag
120      * @param {Number} tag
121      * @returns {ccs.Widget}
122      */
123     getWidgetByTag: function (tag) {
124         if (!this._rootWidget) {
125             return null;
126         }
127         return ccs.UIHelper.seekWidgetByTag(this._rootWidget, tag);
128     },
129 
130     /**
131      * get widget by name
132      * @param name
133      * @returns {ccs.Widget}
134      */
135     getWidgetByName: function (name) {
136         if (!this._rootWidget) {
137             return null;
138         }
139         return ccs.UIHelper.seekWidgetByName(this._rootWidget, name);
140     },
141 
142     /**
143      * get root widget
144      * @returns {ccs.Widget}
145      */
146     getRootWidget: function () {
147         return this._rootWidget;
148     },
149 
150     /**
151      * get inputManager
152      * @returns {ccs.UIInputManager}
153      */
154     getInputManager: function () {
155         return this._inputManager;
156     },
157 
158     /**
159      * remove all children
160      */
161     clear: function () {
162         this._rootWidget.removeAllChildren();
163     },
164 
165     onTouchBegan: function (touch, event) {
166         return this.checkEventWidget(touch,event);
167     },
168 
169     onTouchMoved: function (touch, event) {
170         var selectedWidgetArray = this._selectedWidgets;
171         for (var i = 0; i < selectedWidgetArray.length; ++i) {
172             var hitWidget = selectedWidgetArray[i];
173             hitWidget.onTouchMoved(touch,event);
174         }
175     },
176 
177     onTouchEnded: function (touch, event) {
178         var selectedWidgetArray = this._selectedWidgets;
179         for (var i = 0; i < selectedWidgetArray.length; ++i) {
180             var hitWidget = selectedWidgetArray[i];
181             hitWidget.onTouchEnded(touch,event);
182         }
183         this._selectedWidgets = [];
184     },
185 
186     onTouchCancelled: function (touch, event) {
187         var selectedWidgetArray = this._selectedWidgets;
188         for (var i = 0; i < selectedWidgetArray.length; ++i) {
189             var hitWidget = selectedWidgetArray[i];
190             hitWidget.onTouchCancelled(touch,event);
191         }
192         this._selectedWidgets = [];
193     }
194 });
195 /**
196  * allocates and initializes a UILayer.
197  * @constructs
198  * @return {ccs.UILayer}
199  * @example
200  * // example
201  * var uiLayer = ccs.UILayer.create();
202  */
203 ccs.UILayer.create = function () {
204     var uiLayer = new ccs.UILayer();
205     if (uiLayer && uiLayer.init()) {
206         return uiLayer;
207     }
208     return null;
209 };