1 /**************************************************************************** 2 Copyright (c) 2011-2012 cocos2d-x.org 3 Copyright (c) 2013-2014 Chukong Technologies Inc. 4 5 http://www.cocos2d-x.org 6 7 Permission is hereby granted, free of charge, to any person obtaining a copy 8 of this software and associated documentation files (the "Software"), to deal 9 in the Software without restriction, including without limitation the rights 10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 copies of the Software, and to permit persons to whom the Software is 12 furnished to do so, subject to the following conditions: 13 14 The above copyright notice and this permission notice shall be included in 15 all copies or substantial portions of the Software. 16 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 THE SOFTWARE. 24 ****************************************************************************/ 25 26 //todo maybe need change here 27 28 29 /** 30 * ccui.helper is the singleton object which is the Helper object contains some functions for seek widget 31 * @class 32 * @name ccui.helper 33 */ 34 ccui.helper = { 35 /** 36 * Finds a widget whose tag equals to param tag from root widget. 37 * @param {ccui.Widget} root 38 * @param {number} tag 39 * @returns {ccui.Widget} 40 */ 41 seekWidgetByTag: function (root, tag) { 42 if (!root) 43 return null; 44 if (root.getTag() === tag) 45 return root; 46 47 var arrayRootChildren = root.getChildren(); 48 var length = arrayRootChildren.length; 49 for (var i = 0; i < length; i++) { 50 var child = arrayRootChildren[i]; 51 var res = ccui.helper.seekWidgetByTag(child, tag); 52 if (res !== null) 53 return res; 54 } 55 return null; 56 }, 57 58 /** 59 * Finds a widget whose name equals to param name from root widget. 60 * @param {ccui.Widget} root 61 * @param {String} name 62 * @returns {ccui.Widget} 63 */ 64 seekWidgetByName: function (root, name) { 65 if (!root) 66 return null; 67 if (root.getName() === name) 68 return root; 69 var arrayRootChildren = root.getChildren(); 70 var length = arrayRootChildren.length; 71 for (var i = 0; i < length; i++) { 72 var child = arrayRootChildren[i]; 73 var res = ccui.helper.seekWidgetByName(child, name); 74 if (res !== null) 75 return res; 76 } 77 return null; 78 }, 79 80 /** 81 * Finds a widget whose name equals to param name from root widget. 82 * RelativeLayout will call this method to find the widget witch is needed. 83 * @param {ccui.Widget} root 84 * @param {String} name 85 * @returns {ccui.Widget} 86 */ 87 seekWidgetByRelativeName: function (root, name) { 88 if (!root) 89 return null; 90 var arrayRootChildren = root.getChildren(); 91 var length = arrayRootChildren.length; 92 for (var i = 0; i < length; i++) { 93 var child = arrayRootChildren[i]; 94 var layoutParameter = child.getLayoutParameter(ccui.LayoutParameter.RELATIVE); 95 if (layoutParameter && layoutParameter.getRelativeName() === name) 96 return child; 97 } 98 return null; 99 }, 100 101 /** 102 * Finds a widget whose action tag equals to param name from root widget. 103 * @param {ccui.Widget} root 104 * @param {Number} tag 105 * @returns {ccui.Widget} 106 */ 107 seekActionWidgetByActionTag: function (root, tag) { 108 if (!root) 109 return null; 110 if (root.getActionTag() === tag) 111 return root; 112 var arrayRootChildren = root.getChildren(); 113 for (var i = 0; i < arrayRootChildren.length; i++) { 114 var child = arrayRootChildren[i]; 115 var res = ccui.helper.seekActionWidgetByActionTag(child, tag); 116 if (res !== null) 117 return res; 118 } 119 return null; 120 } , 121 122 _activeLayout: true, 123 /** 124 * Refresh object and it's children layout state 125 * @param {cc.Node} rootNode 126 */ 127 doLayout: function(rootNode){ 128 if(!this._activeLayout) 129 return; 130 var children = rootNode.getChildren(), node; 131 for(var i = 0, len = children.length;i < len; i++) { 132 node = children[i]; 133 var com = node.getComponent(ccui.LayoutComponent.NAME); 134 var parent = node.getParent(); 135 if (null != com && null !== parent && com.refreshLayout) 136 com.refreshLayout(); 137 } 138 }, 139 140 changeLayoutSystemActiveState: function(active){ 141 this._activeLayout = active; 142 }, 143 144 /** 145 * restrict capInsetSize, when the capInsets' width is larger than the textureSize, it will restrict to 0, <br/> 146 * the height goes the same way as width. 147 * @param {cc.Rect} capInsets 148 * @param {cc.Size} textureSize 149 */ 150 restrictCapInsetRect: function (capInsets, textureSize) { 151 var x = capInsets.x, y = capInsets.y; 152 var width = capInsets.width, height = capInsets.height; 153 154 if (textureSize.width < width) { 155 x = 0.0; 156 width = 0.0; 157 } 158 if (textureSize.height < height) { 159 y = 0.0; 160 height = 0.0; 161 } 162 return cc.rect(x, y, width, height); 163 }, 164 165 _createSpriteFromBase64: function(base64String, key) { 166 var texture2D = cc.textureCache.getTextureForKey(key); 167 168 if(!texture2D) { 169 var image = new Image(); 170 image.src = base64String; 171 cc.textureCache.cacheImage(key, image); 172 texture2D = cc.textureCache.getTextureForKey(key); 173 } 174 175 var sprite = new cc.Sprite(texture2D); 176 177 return sprite; 178 } 179 }; 180