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 _inputManager: null, 33 init: function () { 34 if (cc.Layer.prototype.init.call(this)) { 35 this._rootWidget = ccs.UIRootWidget.create(); 36 this._rootWidget.onEnter(); 37 this.addChild(this._rootWidget.getRenderer()); 38 this._inputManager = new ccs.UIInputManager(); 39 this._inputManager.setRootWidget(this._rootWidget); 40 return true; 41 } 42 return false; 43 }, 44 45 onEnter: function () { 46 this.setTouchMode(cc.TOUCH_ONE_BY_ONE); 47 this.setTouchEnabled(true); 48 cc.Layer.prototype.onEnter.call(this); 49 50 }, 51 52 onExit: function () { 53 this.setTouchEnabled(false); 54 cc.Layer.prototype.onExit.call(this); 55 }, 56 57 onEnterTransitionDidFinish: function () { 58 cc.Layer.prototype.onEnterTransitionDidFinish.call(this); 59 }, 60 61 /** 62 * add widget 63 * @param {ccs.Widget} widget 64 */ 65 addWidget: function (widget) { 66 this._rootWidget.addChild(widget); 67 }, 68 69 /** 70 * remove widget 71 * @param {ccs.Widget} widget 72 */ 73 removeWidget: function (widget) { 74 this._rootWidget.removeChild(widget); 75 }, 76 77 /** 78 * @param {Boolean} visible 79 */ 80 setVisible: function (visible) { 81 cc.Layer.prototype.setVisible.call(this,visible); 82 this._rootWidget.setVisible(visible); 83 }, 84 85 /** 86 * get widget by tag 87 * @param {Number} tag 88 * @returns {ccs.Widget} 89 */ 90 getWidgetByTag: function (tag) { 91 if (!this._rootWidget) { 92 return null; 93 } 94 return ccs.UIHelper.seekWidgetByTag(this._rootWidget, tag); 95 }, 96 97 /** 98 * get widget by name 99 * @param name 100 * @returns {ccs.Widget} 101 */ 102 getWidgetByName: function (name) { 103 if (!this._rootWidget) { 104 return null; 105 } 106 return ccs.UIHelper.seekWidgetByName(this._rootWidget, name); 107 }, 108 109 /** 110 * get root widget 111 * @returns {ccs.Widget} 112 */ 113 getRootWidget: function () { 114 return this._rootWidget; 115 }, 116 117 /** 118 * get inputManager 119 * @returns {ccs.UIInputManager} 120 */ 121 getInputManager: function () { 122 return this._inputManager; 123 }, 124 125 /** 126 * remove all children 127 */ 128 clear: function () { 129 this._rootWidget.removeAllChildren(); 130 }, 131 132 onTouchBegan: function (touch, event) { 133 if (this._inputManager && this._inputManager.onTouchBegan(touch)) { 134 return true; 135 } 136 return false; 137 }, 138 139 onTouchMoved: function (touch, event) { 140 this._inputManager.onTouchMoved(touch); 141 }, 142 143 onTouchEnded: function (touch, event) { 144 this._inputManager.onTouchEnded(touch); 145 }, 146 147 onTouchCancelled: function (touch, event) { 148 this._inputManager.onTouchCancelled(touch); 149 }, 150 151 /** 152 * remove all children 153 */ 154 dispose: function () { 155 this.removeFromParent(true); 156 }, 157 158 /** 159 * remove Widget 160 * @param {ccs.Widget} widget 161 * @param {Boolean} cleanup 162 */ 163 removeWidgetAndCleanUp: function (widget, cleanup) { 164 this.removeWidget(widget); 165 } 166 }); 167 /** 168 * allocates and initializes a UILayer. 169 * @constructs 170 * @return {ccs.UILayer} 171 * @example 172 * // example 173 * var uiLayer = ccs.UILayer.create(); 174 */ 175 ccs.UILayer.create = function () { 176 var uiLayer = new ccs.UILayer(); 177 if (uiLayer && uiLayer.init()) { 178 return uiLayer; 179 } 180 return null; 181 };