UIHelper.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  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. #ifndef __UIHELPER_H__
  22. #define __UIHELPER_H__
  23. #include <string>
  24. #include "platform/CCPlatformMacros.h"
  25. #include "ui/GUIExport.h"
  26. #include "2d/CCNode.h"
  27. NS_CC_BEGIN
  28. /**
  29. * @addtogroup ui
  30. * @{
  31. */
  32. namespace ui {
  33. class Widget;
  34. /**
  35. * Helper class for traversing children in widget tree.
  36. * It also provides some helper functions for layout.
  37. */
  38. class CC_GUI_DLL Helper
  39. {
  40. public:
  41. /**
  42. * Find a widget with a specific tag from root widget.
  43. * This search will be recursive through all child widgets.
  44. * @param root The be searched root widget.
  45. * @param tag The widget tag.
  46. * @return Widget instance pointer.
  47. */
  48. static Widget* seekWidgetByTag(Widget* root, int tag);
  49. /**
  50. * Find a widget with a specific name from root widget.
  51. * This search will be recursive through all child widgets.
  52. *
  53. * @param root The be searched root widget.
  54. * @param name The widget name.
  55. * @return Widget instance pointer.
  56. */
  57. static Widget* seekWidgetByName(Widget* root, const std::string& name);
  58. /**
  59. * Find a widget with a specific action tag from root widget
  60. * This search will be recursive through all child widgets.
  61. *@param root The be searched root widget.
  62. *@param tag The widget action's tag.
  63. *@return Widget instance pointer.
  64. */
  65. static Widget* seekActionWidgetByActionTag(Widget* root, int tag);
  66. /**
  67. * @brief Get a UTF8 substring from a std::string with a given start position and length
  68. * Sample: std::string str = "中国中国中国"; substr = getSubStringOfUTF8String(str,0,2) will = "中国"
  69. *
  70. * @param str The source string.
  71. * @param start The start position of the substring.
  72. * @param length The length of the substring in UTF8 count
  73. * @return a UTF8 substring
  74. * @js NA
  75. */
  76. static std::string getSubStringOfUTF8String(const std::string& str,
  77. std::string::size_type start,
  78. std::string::size_type length);
  79. /**
  80. * Refresh object and it's children layout state
  81. *
  82. *@param rootNode A Node* or Node* descendant instance pointer.
  83. *
  84. */
  85. static void doLayout(Node *rootNode);
  86. /**
  87. * Change the active property of Layout's @see `LayoutComponent`
  88. *@param active A boolean value.
  89. */
  90. static void changeLayoutSystemActiveState(bool active);
  91. /**
  92. *@brief restrict capInsetSize, when the capInsets's width is larger than the textureSize, it will restrict to 0,
  93. * the height goes the same way as width.
  94. *@param capInsets A user defined capInsets.
  95. *@param textureSize The size of a scale9enabled texture
  96. *@return a restricted capInset.
  97. */
  98. static Rect restrictCapInsetRect(const Rect& capInsets, const Size& textureSize);
  99. /**
  100. *@brief Convert a node's boundingBox rect into screen coordinates.
  101. *
  102. * @param node Any node pointer.
  103. *
  104. * @return A Rect in screen coordinates.
  105. */
  106. static Rect convertBoundingBoxToScreen(Node* node);
  107. };
  108. }
  109. // end of ui group
  110. /// @}
  111. NS_CC_END
  112. #endif /* defined(__CocoGUI__UISystem__) */