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 * UI Helper 27 * @type {Object} 28 */ 29 ccui.helper = { 30 /** 31 * Finds a widget whose tag equals to param tag from root widget. 32 * @param {ccui.Widget} root 33 * @param {number} tag 34 * @returns {ccui.Widget} 35 */ 36 seekWidgetByTag: function (root, tag) { 37 if (!root) { 38 return null; 39 } 40 if (root.getTag() == tag) { 41 return root; 42 } 43 var arrayRootChildren = root.getChildren(); 44 var length = arrayRootChildren.length; 45 for (var i = 0; i < length; i++) { 46 var child = arrayRootChildren[i]; 47 var res = this.seekWidgetByTag(child, tag); 48 if (res != null) { 49 return res; 50 } 51 } 52 return null; 53 }, 54 55 /** 56 * Finds a widget whose name equals to param name from root widget. 57 * @param {ccui.Widget} root 58 * @param {String} name 59 * @returns {ccui.Widget} 60 */ 61 seekWidgetByName: function (root, name) { 62 if (!root) { 63 return null; 64 } 65 if (root.getName() == name) { 66 return root; 67 } 68 var arrayRootChildren = root.getChildren(); 69 var length = arrayRootChildren.length; 70 for (var i = 0; i < length; i++) { 71 var child = arrayRootChildren[i]; 72 var res = this.seekWidgetByName(child, name); 73 if (res != null) { 74 return res; 75 } 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 } 91 var arrayRootChildren = root.getChildren(); 92 var length = arrayRootChildren.length; 93 for (var i = 0; i < length; i++) { 94 var child = arrayRootChildren[i]; 95 var layoutParameter = child.getLayoutParameter(ccui.LayoutParameter.RELATIVE); 96 if (layoutParameter && layoutParameter.getRelativeName() == name) { 97 return child; 98 } 99 } 100 return null; 101 }, 102 103 /*temp action*/ 104 seekActionWidgetByActionTag: function (root, tag) { 105 if (!root) { 106 return null; 107 } 108 if (root.getActionTag() == tag) { 109 return root; 110 } 111 var arrayRootChildren = root.getChildren(); 112 for (var i = 0; i < arrayRootChildren.length; i++) { 113 var child = arrayRootChildren[i]; 114 var res = this.seekActionWidgetByActionTag(child, tag); 115 if (res != null) { 116 return res; 117 } 118 } 119 return null; 120 } 121 122 }; 123 ccui.helper; 124