UIHelper.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /****************************************************************************
  2. Copyright (c) 2011-2012 cocos2d-x.org
  3. Copyright (c) 2013-2014 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. /**
  22. * ccui.helper is the singleton object which is the Helper object contains some functions for seek widget
  23. * @class
  24. * @name ccui.helper
  25. */
  26. ccui.helper = {
  27. /**
  28. * Finds a widget whose tag equals to param tag from root widget.
  29. * @param {ccui.Widget} root
  30. * @param {number} tag
  31. * @returns {ccui.Widget}
  32. */
  33. seekWidgetByTag: function (root, tag) {
  34. if (!root)
  35. return null;
  36. if (root.getTag() == tag)
  37. return root;
  38. var arrayRootChildren = root.getChildren();
  39. var length = arrayRootChildren.length;
  40. for (var i = 0; i < length; i++) {
  41. var child = arrayRootChildren[i];
  42. var res = ccui.helper.seekWidgetByTag(child, tag);
  43. if (res != null)
  44. return res;
  45. }
  46. return null;
  47. },
  48. /**
  49. * Finds a widget whose name equals to param name from root widget.
  50. * @param {ccui.Widget} root
  51. * @param {String} name
  52. * @returns {ccui.Widget}
  53. */
  54. seekWidgetByName: function (root, name) {
  55. if (!root)
  56. return null;
  57. if (root.getName() == name)
  58. return root;
  59. var arrayRootChildren = root.getChildren();
  60. var length = arrayRootChildren.length;
  61. for (var i = 0; i < length; i++) {
  62. var child = arrayRootChildren[i];
  63. var res = ccui.helper.seekWidgetByName(child, name);
  64. if (res != null)
  65. return res;
  66. }
  67. return null;
  68. },
  69. /**
  70. * Finds a widget whose name equals to param name from root widget.
  71. * RelativeLayout will call this method to find the widget witch is needed.
  72. * @param {ccui.Widget} root
  73. * @param {String} name
  74. * @returns {ccui.Widget}
  75. */
  76. seekWidgetByRelativeName: function (root, name) {
  77. if (!root)
  78. return null;
  79. var arrayRootChildren = root.getChildren();
  80. var length = arrayRootChildren.length;
  81. for (var i = 0; i < length; i++) {
  82. var child = arrayRootChildren[i];
  83. var layoutParameter = child.getLayoutParameter(ccui.LayoutParameter.RELATIVE);
  84. if (layoutParameter && layoutParameter.getRelativeName() == name)
  85. return child;
  86. }
  87. return null;
  88. },
  89. /**
  90. * Finds a widget whose action tag equals to param name from root widget.
  91. * @param {ccui.Widget} root
  92. * @param {Number} tag
  93. * @returns {ccui.Widget}
  94. */
  95. seekActionWidgetByActionTag: function (root, tag) {
  96. if (!root)
  97. return null;
  98. if (root.getActionTag() == tag)
  99. return root;
  100. var arrayRootChildren = root.getChildren();
  101. for (var i = 0; i < arrayRootChildren.length; i++) {
  102. var child = arrayRootChildren[i];
  103. var res = ccui.helper.seekActionWidgetByActionTag(child, tag);
  104. if (res != null)
  105. return res;
  106. }
  107. return null;
  108. }
  109. };