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 ccs.UIHelper = ccs.UIHelper || ccs.Class.extend({});
 27 
 28 /**
 29  * Finds a widget whose tag equals to param tag from root widget.
 30  * @param {ccs.Widget} root
 31  * @param {number} tag
 32  * @returns {ccs.Widget}
 33  */
 34 ccs.UIHelper.seekWidgetByTag = function (root, tag) {
 35     if (!root) {
 36         return null;
 37     }
 38     if (root.getTag() == tag) {
 39         return root;
 40     }
 41     var arrayRootChildren = root.getChildren();
 42     var length = arrayRootChildren.length;
 43     for (var i = 0; i < length; i++) {
 44         var child = arrayRootChildren[i];
 45         var res = this.seekWidgetByTag(child, tag);
 46         if (res != null) {
 47             return res;
 48         }
 49     }
 50     return null;
 51 };
 52 
 53 /**
 54  * Finds a widget whose name equals to param name from root widget.
 55  * @param {ccs.Widget} root
 56  * @param {String} name
 57  * @returns {ccs.Widget}
 58  */
 59 ccs.UIHelper.seekWidgetByName = function (root, name) {
 60     if (!root) {
 61         return null;
 62     }
 63     if (root.getName() == name) {
 64         return root;
 65     }
 66     var arrayRootChildren = root.getChildren();
 67     var length = arrayRootChildren.length;
 68     for (var i = 0; i < length; i++) {
 69         var child = arrayRootChildren[i];
 70         var res = this.seekWidgetByName(child, name);
 71         if (res != null) {
 72             return res;
 73         }
 74     }
 75     return null;
 76 };
 77 
 78 /**
 79  * Finds a widget whose name equals to param name from root widget.
 80  * RelativeLayout will call this method to find the widget witch is needed.
 81  * @param {ccs.Widget} root
 82  * @param {String} name
 83  * @returns {ccs.Widget}
 84  */
 85 ccs.UIHelper.seekWidgetByRelativeName = function (root, name) {
 86     if (!root) {
 87         return null;
 88     }
 89     var arrayRootChildren = root.getChildren();
 90     var length = arrayRootChildren.length;
 91     for (var i = 0; i < length; i++) {
 92         var child = arrayRootChildren[i];
 93         var layoutParameter = child.getLayoutParameter(ccs.LayoutParameterType.relative);
 94         if (layoutParameter && layoutParameter.getRelativeName() == name) {
 95             return child;
 96         }
 97     }
 98     return null;
 99 };
100 
101 /*temp action*/
102 ccs.UIHelper.seekActionWidgetByActionTag = function (root, tag) {
103     if (!root) {
104         return null;
105     }
106     if (root.getActionTag() == tag) {
107         return root;
108     }
109     var arrayRootChildren = root.getChildren();
110     for (var i = 0; i < arrayRootChildren.length; i++) {
111         var child = arrayRootChildren[i];
112         var res = this.seekActionWidgetByActionTag(child, tag);
113         if (res != null) {
114             return res;
115         }
116     }
117     return null;
118 };
119