UILayoutManager.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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/UILayoutManager.h"
  22. #include "ui/UILayout.h"
  23. NS_CC_BEGIN
  24. namespace ui {
  25. LinearHorizontalLayoutManager* LinearHorizontalLayoutManager::create()
  26. {
  27. LinearHorizontalLayoutManager* exe = new (std::nothrow) LinearHorizontalLayoutManager();
  28. if (exe)
  29. {
  30. exe->autorelease();
  31. return exe;
  32. }
  33. CC_SAFE_DELETE(exe);
  34. return nullptr;
  35. }
  36. void LinearHorizontalLayoutManager::doLayout(LayoutProtocol* layout)
  37. {
  38. Size layoutSize = layout->getLayoutContentSize();
  39. Vector<Node*> container = layout->getLayoutElements();
  40. float leftBoundary = 0.0f;
  41. for (auto& subWidget : container)
  42. {
  43. Widget* child = dynamic_cast<Widget*>(subWidget);
  44. if (child)
  45. {
  46. LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
  47. if (layoutParameter)
  48. {
  49. LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
  50. Vec2 ap = child->getAnchorPoint();
  51. Size cs = child->getBoundingBox().size;
  52. float finalPosX = leftBoundary + (ap.x * cs.width);
  53. float finalPosY = layoutSize.height - (1.0f - ap.y) * cs.height;
  54. switch (childGravity)
  55. {
  56. case LinearLayoutParameter::LinearGravity::NONE:
  57. case LinearLayoutParameter::LinearGravity::TOP:
  58. break;
  59. case LinearLayoutParameter::LinearGravity::BOTTOM:
  60. finalPosY = ap.y * cs.height;
  61. break;
  62. case LinearLayoutParameter::LinearGravity::CENTER_VERTICAL:
  63. finalPosY = layoutSize.height / 2.0f - cs.height * (0.5f - ap.y);
  64. break;
  65. default:
  66. break;
  67. }
  68. Margin mg = layoutParameter->getMargin();
  69. finalPosX += mg.left;
  70. finalPosY -= mg.top;
  71. child->setPosition(Vec2(finalPosX, finalPosY));
  72. leftBoundary = child->getRightBoundary() + mg.right;
  73. }
  74. }
  75. }
  76. }
  77. //LinearVerticalLayoutManager
  78. LinearVerticalLayoutManager* LinearVerticalLayoutManager::create()
  79. {
  80. LinearVerticalLayoutManager* exe = new (std::nothrow) LinearVerticalLayoutManager();
  81. if (exe)
  82. {
  83. exe->autorelease();
  84. return exe;
  85. }
  86. CC_SAFE_DELETE(exe);
  87. return nullptr;
  88. }
  89. void LinearVerticalLayoutManager::doLayout(LayoutProtocol* layout)
  90. {
  91. Size layoutSize = layout->getLayoutContentSize();
  92. Vector<Node*> container = layout->getLayoutElements();
  93. float topBoundary = layoutSize.height;
  94. for (auto& subWidget : container)
  95. {
  96. LayoutParameterProtocol* child = dynamic_cast<LayoutParameterProtocol*>(subWidget);
  97. if (child)
  98. {
  99. LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
  100. if (layoutParameter)
  101. {
  102. LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
  103. Vec2 ap = subWidget->getAnchorPoint();
  104. Size cs = subWidget->getBoundingBox().size;
  105. float finalPosX = ap.x * cs.width;
  106. float finalPosY = topBoundary - ((1.0f-ap.y) * cs.height);
  107. switch (childGravity)
  108. {
  109. case LinearLayoutParameter::LinearGravity::NONE:
  110. case LinearLayoutParameter::LinearGravity::LEFT:
  111. break;
  112. case LinearLayoutParameter::LinearGravity::RIGHT:
  113. finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
  114. break;
  115. case LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL:
  116. finalPosX = layoutSize.width / 2.0f - cs.width * (0.5f-ap.x);
  117. break;
  118. default:
  119. break;
  120. }
  121. Margin mg = layoutParameter->getMargin();
  122. finalPosX += mg.left;
  123. finalPosY -= mg.top;
  124. subWidget->setPosition(finalPosX, finalPosY);
  125. topBoundary = subWidget->getPosition().y - subWidget->getAnchorPoint().y * subWidget->getBoundingBox().size.height - mg.bottom;
  126. }
  127. }
  128. }
  129. }
  130. //RelativeLayoutManager
  131. RelativeLayoutManager* RelativeLayoutManager::create()
  132. {
  133. RelativeLayoutManager* exe = new (std::nothrow) RelativeLayoutManager();
  134. if (exe)
  135. {
  136. exe->autorelease();
  137. return exe;
  138. }
  139. CC_SAFE_DELETE(exe);
  140. return nullptr;
  141. }
  142. Vector<Widget*> RelativeLayoutManager::getAllWidgets(cocos2d::ui::LayoutProtocol *layout)
  143. {
  144. Vector<Node*> container = layout->getLayoutElements();
  145. Vector<Widget*> widgetChildren;
  146. for (auto& subWidget : container)
  147. {
  148. Widget* child = dynamic_cast<Widget*>(subWidget);
  149. if (child)
  150. {
  151. RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
  152. layoutParameter->_put = false;
  153. _unlayoutChildCount++;
  154. widgetChildren.pushBack(child);
  155. }
  156. }
  157. return widgetChildren;
  158. }
  159. Widget* RelativeLayoutManager::getRelativeWidget(Widget* widget)
  160. {
  161. Widget* relativeWidget = nullptr;
  162. RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(widget->getLayoutParameter());
  163. const std::string relativeName = layoutParameter->getRelativeToWidgetName();
  164. if (!relativeName.empty())
  165. {
  166. for (auto& sWidget : _widgetChildren)
  167. {
  168. if (sWidget)
  169. {
  170. RelativeLayoutParameter* rlayoutParameter = dynamic_cast<RelativeLayoutParameter*>(sWidget->getLayoutParameter());
  171. if (rlayoutParameter && rlayoutParameter->getRelativeName() == relativeName)
  172. {
  173. relativeWidget = sWidget;
  174. _relativeWidgetLP = rlayoutParameter;
  175. break;
  176. }
  177. }
  178. }
  179. }
  180. return relativeWidget;
  181. }
  182. bool RelativeLayoutManager::calculateFinalPositionWithRelativeWidget(LayoutProtocol *layout)
  183. {
  184. Vec2 ap = _widget->getAnchorPoint();
  185. Size cs = _widget->getBoundingBox().size;
  186. _finalPositionX = 0.0f;
  187. _finalPositionY = 0.0f;
  188. Widget* relativeWidget = this->getRelativeWidget(_widget);
  189. RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(_widget->getLayoutParameter());
  190. RelativeLayoutParameter::RelativeAlign align = layoutParameter->getAlign();
  191. Size layoutSize = layout->getLayoutContentSize();
  192. switch (align)
  193. {
  194. case RelativeLayoutParameter::RelativeAlign::NONE:
  195. case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT:
  196. _finalPositionX = ap.x * cs.width;
  197. _finalPositionY = layoutSize.height - ((1.0f - ap.y) * cs.height);
  198. break;
  199. case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL:
  200. _finalPositionX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
  201. _finalPositionY = layoutSize.height - ((1.0f - ap.y) * cs.height);
  202. break;
  203. case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT:
  204. _finalPositionX = layoutSize.width - ((1.0f - ap.x) * cs.width);
  205. _finalPositionY = layoutSize.height - ((1.0f - ap.y) * cs.height);
  206. break;
  207. case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL:
  208. _finalPositionX = ap.x * cs.width;
  209. _finalPositionY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
  210. break;
  211. case RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT:
  212. _finalPositionX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
  213. _finalPositionY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
  214. break;
  215. case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL:
  216. _finalPositionX = layoutSize.width - ((1.0f - ap.x) * cs.width);
  217. _finalPositionY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
  218. break;
  219. case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM:
  220. _finalPositionX = ap.x * cs.width;
  221. _finalPositionY = ap.y * cs.height;
  222. break;
  223. case RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL:
  224. _finalPositionX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
  225. _finalPositionY = ap.y * cs.height;
  226. break;
  227. case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM:
  228. _finalPositionX = layoutSize.width - ((1.0f - ap.x) * cs.width);
  229. _finalPositionY = ap.y * cs.height;
  230. break;
  231. case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_LEFTALIGN:
  232. if (relativeWidget)
  233. {
  234. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  235. {
  236. return false;
  237. }
  238. float locationTop = relativeWidget->getTopBoundary();
  239. float locationLeft = relativeWidget->getLeftBoundary();
  240. _finalPositionY = locationTop + ap.y * cs.height;
  241. _finalPositionX = locationLeft + ap.x * cs.width;
  242. }
  243. break;
  244. case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER:
  245. if (relativeWidget)
  246. {
  247. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  248. {
  249. return false;
  250. }
  251. Size rbs = relativeWidget->getBoundingBox().size;
  252. float locationTop = relativeWidget->getTopBoundary();
  253. _finalPositionY = locationTop + ap.y * cs.height;
  254. _finalPositionX = relativeWidget->getLeftBoundary() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f;
  255. }
  256. break;
  257. case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_RIGHTALIGN:
  258. if (relativeWidget)
  259. {
  260. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  261. {
  262. return false;
  263. }
  264. float locationTop = relativeWidget->getTopBoundary();
  265. float locationRight = relativeWidget->getRightBoundary();
  266. _finalPositionY = locationTop + ap.y * cs.height;
  267. _finalPositionX = locationRight - (1.0f - ap.x) * cs.width;
  268. }
  269. break;
  270. case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_TOPALIGN:
  271. if (relativeWidget)
  272. {
  273. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  274. {
  275. return false;
  276. }
  277. float locationTop = relativeWidget->getTopBoundary();
  278. float locationLeft = relativeWidget->getLeftBoundary();
  279. _finalPositionY = locationTop - (1.0f - ap.y) * cs.height;
  280. _finalPositionX = locationLeft - (1.0f - ap.x) * cs.width;
  281. }
  282. break;
  283. case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER:
  284. if (relativeWidget)
  285. {
  286. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  287. {
  288. return false;
  289. }
  290. Size rbs = relativeWidget->getBoundingBox().size;
  291. float locationLeft = relativeWidget->getLeftBoundary();
  292. _finalPositionX = locationLeft - (1.0f - ap.x) * cs.width;
  293. _finalPositionY = relativeWidget->getBottomBoundary() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f;
  294. }
  295. break;
  296. case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_BOTTOMALIGN:
  297. if (relativeWidget)
  298. {
  299. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  300. {
  301. return false;
  302. }
  303. float locationBottom = relativeWidget->getBottomBoundary();
  304. float locationLeft = relativeWidget->getLeftBoundary();
  305. _finalPositionY = locationBottom + ap.y * cs.height;
  306. _finalPositionX = locationLeft - (1.0f - ap.x) * cs.width;
  307. }
  308. break;
  309. case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_TOPALIGN:
  310. if (relativeWidget)
  311. {
  312. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  313. {
  314. return false;
  315. }
  316. float locationTop = relativeWidget->getTopBoundary();
  317. float locationRight = relativeWidget->getRightBoundary();
  318. _finalPositionY = locationTop - (1.0f - ap.y) * cs.height;
  319. _finalPositionX = locationRight + ap.x * cs.width;
  320. }
  321. break;
  322. case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER:
  323. if (relativeWidget)
  324. {
  325. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  326. {
  327. return false;
  328. }
  329. Size rbs = relativeWidget->getBoundingBox().size;
  330. float locationRight = relativeWidget->getRightBoundary();
  331. _finalPositionX = locationRight + ap.x * cs.width;
  332. _finalPositionY = relativeWidget->getBottomBoundary() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f;
  333. }
  334. break;
  335. case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_BOTTOMALIGN:
  336. if (relativeWidget)
  337. {
  338. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  339. {
  340. return false;
  341. }
  342. float locationBottom = relativeWidget->getBottomBoundary();
  343. float locationRight = relativeWidget->getRightBoundary();
  344. _finalPositionY = locationBottom + ap.y * cs.height;
  345. _finalPositionX = locationRight + ap.x * cs.width;
  346. }
  347. break;
  348. case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_LEFTALIGN:
  349. if (relativeWidget)
  350. {
  351. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  352. {
  353. return false;
  354. }
  355. float locationBottom = relativeWidget->getBottomBoundary();
  356. float locationLeft = relativeWidget->getLeftBoundary();
  357. _finalPositionY = locationBottom - (1.0f - ap.y) * cs.height;
  358. _finalPositionX = locationLeft + ap.x * cs.width;
  359. }
  360. break;
  361. case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER:
  362. if (relativeWidget)
  363. {
  364. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  365. {
  366. return false;
  367. }
  368. Size rbs = relativeWidget->getBoundingBox().size;
  369. float locationBottom = relativeWidget->getBottomBoundary();
  370. _finalPositionY = locationBottom - (1.0f - ap.y) * cs.height;
  371. _finalPositionX = relativeWidget->getLeftBoundary() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f;
  372. }
  373. break;
  374. case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN:
  375. if (relativeWidget)
  376. {
  377. if (_relativeWidgetLP && !_relativeWidgetLP->_put)
  378. {
  379. return false;
  380. }
  381. float locationBottom = relativeWidget->getBottomBoundary();
  382. float locationRight = relativeWidget->getRightBoundary();
  383. _finalPositionY = locationBottom - (1.0f - ap.y) * cs.height;
  384. _finalPositionX = locationRight - (1.0f - ap.x) * cs.width;
  385. }
  386. break;
  387. default:
  388. break;
  389. }
  390. return true;
  391. }
  392. void RelativeLayoutManager::calculateFinalPositionWithRelativeAlign()
  393. {
  394. RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(_widget->getLayoutParameter());
  395. Margin mg = layoutParameter->getMargin();
  396. RelativeLayoutParameter::RelativeAlign align = layoutParameter->getAlign();
  397. //handle margin
  398. switch (align)
  399. {
  400. case RelativeLayoutParameter::RelativeAlign::NONE:
  401. case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT:
  402. _finalPositionX += mg.left;
  403. _finalPositionY -= mg.top;
  404. break;
  405. case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL:
  406. _finalPositionY -= mg.top;
  407. break;
  408. case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT:
  409. _finalPositionX -= mg.right;
  410. _finalPositionY -= mg.top;
  411. break;
  412. case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL:
  413. _finalPositionX += mg.left;
  414. break;
  415. case RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT:
  416. break;
  417. case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL:
  418. _finalPositionX -= mg.right;
  419. break;
  420. case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM:
  421. _finalPositionX += mg.left;
  422. _finalPositionY += mg.bottom;
  423. break;
  424. case RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL:
  425. _finalPositionY += mg.bottom;
  426. break;
  427. case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM:
  428. _finalPositionX -= mg.right;
  429. _finalPositionY += mg.bottom;
  430. break;
  431. case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_LEFTALIGN:
  432. _finalPositionY += mg.bottom;
  433. _finalPositionX += mg.left;
  434. break;
  435. case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_RIGHTALIGN:
  436. _finalPositionY += mg.bottom;
  437. _finalPositionX -= mg.right;
  438. break;
  439. case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER:
  440. _finalPositionY += mg.bottom;
  441. break;
  442. case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_TOPALIGN:
  443. _finalPositionX -= mg.right;
  444. _finalPositionY -= mg.top;
  445. break;
  446. case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_BOTTOMALIGN:
  447. _finalPositionX -= mg.right;
  448. _finalPositionY += mg.bottom;
  449. break;
  450. case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER:
  451. _finalPositionX -= mg.right;
  452. break;
  453. case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_TOPALIGN:
  454. _finalPositionX += mg.left;
  455. _finalPositionY -= mg.top;
  456. break;
  457. case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_BOTTOMALIGN:
  458. _finalPositionX += mg.left;
  459. _finalPositionY += mg.bottom;
  460. break;
  461. case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER:
  462. _finalPositionX += mg.left;
  463. break;
  464. case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_LEFTALIGN:
  465. _finalPositionY -= mg.top;
  466. _finalPositionX += mg.left;
  467. break;
  468. case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN:
  469. _finalPositionY -= mg.top;
  470. _finalPositionX -= mg.right;
  471. break;
  472. case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER:
  473. _finalPositionY -= mg.top;
  474. break;
  475. default:
  476. break;
  477. }
  478. }
  479. bool RelativeLayoutManager::caculateFinalPositionWithRelativeWidget(LayoutProtocol *layout)
  480. {
  481. return calculateFinalPositionWithRelativeWidget(layout);
  482. }
  483. void RelativeLayoutManager::caculateFinalPositionWithRelativeAlign()
  484. {
  485. calculateFinalPositionWithRelativeAlign();
  486. }
  487. void RelativeLayoutManager::doLayout(LayoutProtocol *layout)
  488. {
  489. _widgetChildren = this->getAllWidgets(layout);
  490. while (_unlayoutChildCount > 0)
  491. {
  492. for (auto& subWidget : _widgetChildren)
  493. {
  494. _widget = static_cast<Widget*>(subWidget);
  495. RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(_widget->getLayoutParameter());
  496. if (layoutParameter)
  497. {
  498. if (layoutParameter->_put)
  499. {
  500. continue;
  501. }
  502. bool ret = this->calculateFinalPositionWithRelativeWidget(layout);
  503. if (!ret) {
  504. continue;
  505. }
  506. this->calculateFinalPositionWithRelativeAlign();
  507. _widget->setPosition(Vec2(_finalPositionX, _finalPositionY));
  508. layoutParameter->_put = true;
  509. }
  510. }
  511. _unlayoutChildCount--;
  512. }
  513. _widgetChildren.clear();
  514. }
  515. }
  516. NS_CC_END