UIHelper.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. #include "ui/UIHelper.h"
  22. #include "ui/UIWidget.h"
  23. #include "ui/UILayoutComponent.h"
  24. #include "base/CCDirector.h"
  25. #include "base/ccUTF8.h"
  26. NS_CC_BEGIN
  27. namespace ui {
  28. static bool _activeLayout = true;
  29. Widget* Helper::seekWidgetByTag(Widget* root, int tag)
  30. {
  31. if (!root)
  32. {
  33. return nullptr;
  34. }
  35. if (root->getTag() == tag)
  36. {
  37. return root;
  38. }
  39. const auto& arrayRootChildren = root->getChildren();
  40. ssize_t length = arrayRootChildren.size();
  41. for (ssize_t i=0;i<length;i++)
  42. {
  43. Widget* child = dynamic_cast<Widget*>(arrayRootChildren.at(i));
  44. if (child)
  45. {
  46. Widget* res = seekWidgetByTag(child,tag);
  47. if (res != nullptr)
  48. {
  49. return res;
  50. }
  51. }
  52. }
  53. return nullptr;
  54. }
  55. Widget* Helper::seekWidgetByName(Widget* root, const std::string& name)
  56. {
  57. if (!root)
  58. {
  59. return nullptr;
  60. }
  61. if (root->getName() == name)
  62. {
  63. return root;
  64. }
  65. const auto& arrayRootChildren = root->getChildren();
  66. for (auto& subWidget : arrayRootChildren)
  67. {
  68. Widget* child = dynamic_cast<Widget*>(subWidget);
  69. if (child)
  70. {
  71. Widget* res = seekWidgetByName(child,name);
  72. if (res != nullptr)
  73. {
  74. return res;
  75. }
  76. }
  77. }
  78. return nullptr;
  79. }
  80. /*temp action*/
  81. Widget* Helper::seekActionWidgetByActionTag(Widget* root, int tag)
  82. {
  83. if (!root)
  84. {
  85. return nullptr;
  86. }
  87. if (root->getActionTag() == tag)
  88. {
  89. return root;
  90. }
  91. const auto& arrayRootChildren = root->getChildren();
  92. for (auto& subWidget : arrayRootChildren)
  93. {
  94. Widget* child = dynamic_cast<Widget*>(subWidget);
  95. if (child)
  96. {
  97. Widget* res = seekActionWidgetByActionTag(child,tag);
  98. if (res != nullptr)
  99. {
  100. return res;
  101. }
  102. }
  103. }
  104. return nullptr;
  105. }
  106. std::string Helper::getSubStringOfUTF8String(const std::string& str, std::string::size_type start, std::string::size_type length)
  107. {
  108. std::u32string utf32;
  109. if (!StringUtils::UTF8ToUTF32(str, utf32)) {
  110. CCLOGERROR("Can't convert string to UTF-32: %s", str.c_str());
  111. return "";
  112. }
  113. if (utf32.size() < start) {
  114. CCLOGERROR("'start' is out of range: %ld, %s", static_cast<long>(start), str.c_str());
  115. return "";
  116. }
  117. std::string result;
  118. if (!StringUtils::UTF32ToUTF8(utf32.substr(start, length), result)) {
  119. CCLOGERROR("Can't convert internal UTF-32 string to UTF-8: %s", str.c_str());
  120. return "";
  121. }
  122. return result;
  123. }
  124. void Helper::changeLayoutSystemActiveState(bool bActive)
  125. {
  126. _activeLayout = bActive;
  127. }
  128. void Helper::doLayout(cocos2d::Node *rootNode)
  129. {
  130. if(!_activeLayout)
  131. {
  132. return;
  133. }
  134. for(auto& node : rootNode->getChildren())
  135. {
  136. auto com = node->getComponent(__LAYOUT_COMPONENT_NAME);
  137. Node *parent = node->getParent();
  138. if (nullptr != com && nullptr != parent) {
  139. LayoutComponent* layoutComponent = (LayoutComponent*)com;
  140. layoutComponent->refreshLayout();
  141. }
  142. }
  143. }
  144. Rect Helper::restrictCapInsetRect(const cocos2d::Rect &capInsets, const Size& textureSize )
  145. {
  146. float x = capInsets.origin.x;
  147. float y = capInsets.origin.y;
  148. float width = capInsets.size.width;
  149. float height = capInsets.size.height;
  150. if (textureSize.width < width)
  151. {
  152. x = textureSize.width / 2.0f;
  153. width = textureSize.width > 0 ? 1.0f : 0.0f;
  154. }
  155. if (textureSize.height < height)
  156. {
  157. y = textureSize.height / 2.0f;
  158. height = textureSize.height > 0 ? 1.0f : 0.0f;
  159. }
  160. return Rect(x, y, width, height);
  161. }
  162. Rect Helper::convertBoundingBoxToScreen(Node* node)
  163. {
  164. auto director = Director::getInstance();
  165. auto glView = director->getOpenGLView();
  166. auto frameSize = glView->getFrameSize();
  167. auto winSize = director->getWinSize();
  168. auto leftBottom = node->convertToWorldSpace(Point::ZERO);
  169. auto contentSize = node->getContentSize();
  170. auto rightTop = node->convertToWorldSpace(Point(contentSize.width, contentSize.height));
  171. auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2 ) * glView->getScaleX();
  172. auto uiTop = frameSize.height /2 - (rightTop.y - winSize.height / 2) * glView->getScaleY();
  173. auto uiWidth = (rightTop.x - leftBottom.x) * glView->getScaleX();
  174. auto uiHeight = (rightTop.y - leftBottom.y) * glView->getScaleY();
  175. return Rect(uiLeft, uiTop, uiWidth, uiHeight);
  176. }
  177. }
  178. NS_CC_END