UILayoutComponent.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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/UIPageView.h"
  22. #include "ui/UILayoutComponent.h"
  23. #include "2d/CCNode.h"
  24. #include "ui/GUIDefine.h"
  25. #include "ui/UIHelper.h"
  26. NS_CC_BEGIN
  27. namespace ui {
  28. LayoutComponent::LayoutComponent()
  29. :_horizontalEdge(HorizontalEdge::None)
  30. , _verticalEdge(VerticalEdge::None)
  31. , _leftMargin(0)
  32. , _rightMargin(0)
  33. , _bottomMargin(0)
  34. , _topMargin(0)
  35. , _usingPositionPercentX(false)
  36. , _positionPercentX(0)
  37. , _usingPositionPercentY(false)
  38. , _positionPercentY(0)
  39. , _usingStretchWidth(false)
  40. , _usingStretchHeight(false)
  41. , _percentWidth(0)
  42. , _usingPercentWidth(false)
  43. , _percentHeight(0)
  44. , _usingPercentHeight(false)
  45. , _actived(true)
  46. , _isPercentOnly(false)
  47. {
  48. _name = __LAYOUT_COMPONENT_NAME;
  49. }
  50. LayoutComponent::~LayoutComponent()
  51. {
  52. }
  53. LayoutComponent* LayoutComponent::bindLayoutComponent(Node* node)
  54. {
  55. LayoutComponent * layout = (LayoutComponent*)node->getComponent(__LAYOUT_COMPONENT_NAME);
  56. if (layout != nullptr)
  57. return layout;
  58. layout = new (std::nothrow) LayoutComponent();
  59. if (layout && layout->init())
  60. {
  61. layout->autorelease();
  62. node->addComponent(layout);
  63. return layout;
  64. }
  65. CC_SAFE_DELETE(layout);
  66. return nullptr;
  67. }
  68. bool LayoutComponent::init()
  69. {
  70. bool ret = true;
  71. do
  72. {
  73. if (!Component::init())
  74. {
  75. ret = false;
  76. break;
  77. }
  78. //put layout component initialized code here
  79. } while (0);
  80. return ret;
  81. }
  82. Node* LayoutComponent::getOwnerParent()
  83. {
  84. Node* parent = _owner->getParent();
  85. return parent;
  86. }
  87. void LayoutComponent::refreshHorizontalMargin()
  88. {
  89. Node* parent = this->getOwnerParent();
  90. if (parent == nullptr)
  91. return;
  92. const Point& ownerPoint = _owner->getPosition();
  93. const Point& ownerAnchor = _owner->getAnchorPoint();
  94. const Size& ownerSize = _owner->getContentSize();
  95. const Size& parentSize = parent->getContentSize();
  96. _leftMargin = ownerPoint.x - ownerAnchor.x * ownerSize.width;
  97. _rightMargin = parentSize.width - (ownerPoint.x + (1 - ownerAnchor.x) * ownerSize.width);
  98. }
  99. void LayoutComponent::refreshVerticalMargin()
  100. {
  101. Node* parent = this->getOwnerParent();
  102. if (parent == nullptr)
  103. return;
  104. const Point& ownerPoint = _owner->getPosition();
  105. const Point& ownerAnchor = _owner->getAnchorPoint();
  106. const Size& ownerSize = _owner->getContentSize();
  107. const Size& parentSize = parent->getContentSize();
  108. _bottomMargin = ownerPoint.y - ownerAnchor.y * ownerSize.height;
  109. _topMargin = parentSize.height - (ownerPoint.y + (1 - ownerAnchor.y) * ownerSize.height);
  110. }
  111. //OldVersion
  112. void LayoutComponent::setUsingPercentContentSize(bool isUsed)
  113. {
  114. _usingPercentWidth = _usingPercentHeight = isUsed;
  115. }
  116. bool LayoutComponent::getUsingPercentContentSize()const
  117. {
  118. return _usingPercentWidth && _usingPercentHeight;
  119. }
  120. void LayoutComponent::setPercentContentSize(const Vec2 &percent)
  121. {
  122. this->setPercentWidth(percent.x);
  123. this->setPercentHeight(percent.y);
  124. }
  125. Vec2 LayoutComponent::getPercentContentSize()const
  126. {
  127. Vec2 vec2=Vec2(_percentWidth,_percentHeight);
  128. return vec2;
  129. }
  130. //Position & Margin
  131. const Point& LayoutComponent::getAnchorPosition()const
  132. {
  133. return _owner->getAnchorPoint();
  134. }
  135. void LayoutComponent::setAnchorPosition(const Point& point)
  136. {
  137. Rect oldRect = _owner->getBoundingBox();
  138. _owner->setAnchorPoint(point);
  139. Rect newRect = _owner->getBoundingBox();
  140. float offSetX = oldRect.origin.x - newRect.origin.x;
  141. float offSetY = oldRect.origin.y - newRect.origin.y;
  142. Point ownerPosition = _owner->getPosition();
  143. ownerPosition.x += offSetX;
  144. ownerPosition.y += offSetY;
  145. this->setPosition(ownerPosition);
  146. }
  147. const Point& LayoutComponent::getPosition()const
  148. {
  149. return _owner->getPosition();
  150. }
  151. void LayoutComponent::setPosition(const Point& position)
  152. {
  153. Node* parent = this->getOwnerParent();
  154. if (parent != nullptr)
  155. {
  156. Point ownerPoint = position;
  157. const Size& parentSize = parent->getContentSize();
  158. if (parentSize.width != 0)
  159. _positionPercentX = ownerPoint.x / parentSize.width;
  160. else
  161. {
  162. _positionPercentX = 0;
  163. if (_usingPositionPercentX || _horizontalEdge == HorizontalEdge::Center)
  164. ownerPoint.x = 0;
  165. }
  166. if (parentSize.height != 0)
  167. _positionPercentY = ownerPoint.y / parentSize.height;
  168. else
  169. {
  170. _positionPercentY = 0;
  171. if (_usingPositionPercentY || _verticalEdge == VerticalEdge::Center)
  172. ownerPoint.y = 0;
  173. }
  174. _owner->setPosition(ownerPoint);
  175. this->refreshHorizontalMargin();
  176. this->refreshVerticalMargin();
  177. }
  178. else
  179. _owner->setPosition(position);
  180. }
  181. bool LayoutComponent::isPositionPercentXEnabled()const
  182. {
  183. return _usingPositionPercentX;
  184. }
  185. void LayoutComponent::setPositionPercentXEnabled(bool isUsed)
  186. {
  187. _usingPositionPercentX = isUsed;
  188. if (_usingPositionPercentX)
  189. {
  190. _horizontalEdge = HorizontalEdge::None;
  191. }
  192. }
  193. float LayoutComponent::getPositionPercentX()const
  194. {
  195. return _positionPercentX;
  196. }
  197. void LayoutComponent::setPositionPercentX(float percentMargin)
  198. {
  199. _positionPercentX = percentMargin;
  200. if (_usingPositionPercentX || _horizontalEdge == HorizontalEdge::Center)
  201. {
  202. Node* parent = this->getOwnerParent();
  203. if (parent != nullptr)
  204. {
  205. _owner->setPositionX(parent->getContentSize().width * _positionPercentX);
  206. this->refreshHorizontalMargin();
  207. }
  208. }
  209. }
  210. bool LayoutComponent::isPositionPercentYEnabled()const
  211. {
  212. return _usingPositionPercentY;
  213. }
  214. void LayoutComponent::setPositionPercentYEnabled(bool isUsed)
  215. {
  216. _usingPositionPercentY = isUsed;
  217. if (_usingPositionPercentY)
  218. {
  219. _verticalEdge = VerticalEdge::None;
  220. }
  221. }
  222. float LayoutComponent::getPositionPercentY()const
  223. {
  224. return _positionPercentY;
  225. }
  226. void LayoutComponent::setPositionPercentY(float percentMargin)
  227. {
  228. _positionPercentY = percentMargin;
  229. if (_usingPositionPercentY || _verticalEdge == VerticalEdge::Center)
  230. {
  231. Node* parent = this->getOwnerParent();
  232. if (parent != nullptr)
  233. {
  234. _owner->setPositionY(parent->getContentSize().height * _positionPercentY);
  235. this->refreshVerticalMargin();
  236. }
  237. }
  238. }
  239. LayoutComponent::HorizontalEdge LayoutComponent::getHorizontalEdge()const
  240. {
  241. return _horizontalEdge;
  242. }
  243. void LayoutComponent::setHorizontalEdge(HorizontalEdge hEage)
  244. {
  245. _horizontalEdge = hEage;
  246. if (_horizontalEdge != HorizontalEdge::None)
  247. {
  248. _usingPositionPercentX = false;
  249. }
  250. }
  251. LayoutComponent::VerticalEdge LayoutComponent::getVerticalEdge()const
  252. {
  253. return _verticalEdge;
  254. }
  255. void LayoutComponent::setVerticalEdge(VerticalEdge vEage)
  256. {
  257. _verticalEdge = vEage;
  258. if (_verticalEdge != VerticalEdge::None)
  259. {
  260. _usingPositionPercentY = false;
  261. }
  262. }
  263. float LayoutComponent::getLeftMargin()const
  264. {
  265. return _leftMargin;
  266. }
  267. void LayoutComponent::setLeftMargin(float margin)
  268. {
  269. _leftMargin = margin;
  270. }
  271. float LayoutComponent::getRightMargin()const
  272. {
  273. return _rightMargin;
  274. }
  275. void LayoutComponent::setRightMargin(float margin)
  276. {
  277. _rightMargin = margin;
  278. }
  279. float LayoutComponent::getTopMargin()const
  280. {
  281. return _topMargin;
  282. }
  283. void LayoutComponent::setTopMargin(float margin)
  284. {
  285. _topMargin = margin;
  286. }
  287. float LayoutComponent::getBottomMargin()const
  288. {
  289. return _bottomMargin;
  290. }
  291. void LayoutComponent::setBottomMargin(float margin)
  292. {
  293. _bottomMargin = margin;
  294. }
  295. //Size & Percent
  296. const Size& LayoutComponent::getSize()const
  297. {
  298. return this->getOwner()->getContentSize();
  299. }
  300. void LayoutComponent::setSize(const Size& size)
  301. {
  302. Node* parent = this->getOwnerParent();
  303. if (parent != nullptr)
  304. {
  305. Size ownerSize = size;
  306. const Size& parentSize = parent->getContentSize();
  307. if (parentSize.width != 0)
  308. _percentWidth = ownerSize.width / parentSize.width;
  309. else
  310. {
  311. _percentWidth = 0;
  312. if (_usingPercentWidth || (this->_horizontalEdge != HorizontalEdge::Center && this->_usingStretchWidth))
  313. ownerSize.width = 0;
  314. }
  315. if (parentSize.height != 0)
  316. _percentHeight = ownerSize.height / parentSize.height;
  317. else
  318. {
  319. _percentHeight = 0;
  320. if (_usingPercentHeight || (this->_verticalEdge != VerticalEdge::Center && this->_usingStretchHeight))
  321. ownerSize.height = 0;
  322. }
  323. _owner->setContentSize(ownerSize);
  324. this->refreshHorizontalMargin();
  325. this->refreshVerticalMargin();
  326. }
  327. else
  328. _owner->setContentSize(size);
  329. }
  330. bool LayoutComponent::isPercentWidthEnabled()const
  331. {
  332. return _usingPercentWidth;
  333. }
  334. void LayoutComponent::setPercentWidthEnabled(bool isUsed)
  335. {
  336. _usingPercentWidth = isUsed;
  337. if (_usingPercentWidth)
  338. {
  339. _usingStretchWidth = false;
  340. }
  341. }
  342. float LayoutComponent::getSizeWidth()const
  343. {
  344. return _owner->getContentSize().width;
  345. }
  346. void LayoutComponent::setSizeWidth(float width)
  347. {
  348. Size ownerSize = _owner->getContentSize();
  349. ownerSize.width = width;
  350. Node* parent = this->getOwnerParent();
  351. if (parent != nullptr)
  352. {
  353. const Size& parentSize = parent->getContentSize();
  354. if (parentSize.width != 0)
  355. _percentWidth = ownerSize.width / parentSize.width;
  356. else
  357. {
  358. _percentWidth = 0;
  359. if (_usingPercentWidth)
  360. ownerSize.width = 0;
  361. }
  362. _owner->setContentSize(ownerSize);
  363. this->refreshHorizontalMargin();
  364. }
  365. else
  366. _owner->setContentSize(ownerSize);
  367. }
  368. float LayoutComponent::getPercentWidth()const
  369. {
  370. return _percentWidth;
  371. }
  372. void LayoutComponent::setPercentWidth(float percentWidth)
  373. {
  374. _percentWidth = percentWidth;
  375. if (_usingPercentWidth)
  376. {
  377. Node* parent = this->getOwnerParent();
  378. if (parent != nullptr)
  379. {
  380. Size ownerSize = _owner->getContentSize();
  381. ownerSize.width = parent->getContentSize().width * _percentWidth;
  382. _owner->setContentSize(ownerSize);
  383. this->refreshHorizontalMargin();
  384. }
  385. }
  386. }
  387. bool LayoutComponent::isPercentHeightEnabled()const
  388. {
  389. return _usingPercentHeight;
  390. }
  391. void LayoutComponent::setPercentHeightEnabled(bool isUsed)
  392. {
  393. _usingPercentHeight = isUsed;
  394. if (_usingPercentHeight)
  395. {
  396. _usingStretchHeight = false;
  397. }
  398. }
  399. float LayoutComponent::getSizeHeight()const
  400. {
  401. return _owner->getContentSize().height;
  402. }
  403. void LayoutComponent::setSizeHeight(float height)
  404. {
  405. Size ownerSize = _owner->getContentSize();
  406. ownerSize.height = height;
  407. Node* parent = this->getOwnerParent();
  408. if (parent != nullptr)
  409. {
  410. const Size& parentSize = parent->getContentSize();
  411. if (parentSize.height != 0)
  412. _percentHeight = ownerSize.height / parentSize.height;
  413. else
  414. {
  415. _percentHeight = 0;
  416. if (_usingPercentHeight)
  417. ownerSize.height = 0;
  418. }
  419. _owner->setContentSize(ownerSize);
  420. this->refreshVerticalMargin();
  421. }
  422. else
  423. _owner->setContentSize(ownerSize);
  424. }
  425. float LayoutComponent::getPercentHeight()const
  426. {
  427. return _percentHeight;
  428. }
  429. void LayoutComponent::setPercentHeight(float percentHeight)
  430. {
  431. _percentHeight = percentHeight;
  432. if (_usingPercentHeight)
  433. {
  434. Node* parent = this->getOwnerParent();
  435. if (parent != nullptr)
  436. {
  437. Size ownerSize = _owner->getContentSize();
  438. ownerSize.height = parent->getContentSize().height * _percentHeight;
  439. _owner->setContentSize(ownerSize);
  440. this->refreshVerticalMargin();
  441. }
  442. }
  443. }
  444. bool LayoutComponent::isStretchWidthEnabled()const
  445. {
  446. return _usingStretchWidth;
  447. }
  448. void LayoutComponent::setStretchWidthEnabled(bool isUsed)
  449. {
  450. _usingStretchWidth = isUsed;
  451. if (_usingStretchWidth)
  452. {
  453. _usingPercentWidth = false;
  454. }
  455. }
  456. bool LayoutComponent::isStretchHeightEnabled()const
  457. {
  458. return _usingStretchHeight;
  459. }
  460. void LayoutComponent::setStretchHeightEnabled(bool isUsed)
  461. {
  462. _usingStretchHeight = isUsed;
  463. if (_usingStretchHeight)
  464. {
  465. _usingPercentHeight = false;
  466. }
  467. }
  468. void LayoutComponent::refreshLayout()
  469. {
  470. if (!_actived)
  471. return;
  472. Node* parent = this->getOwnerParent();
  473. if (parent == nullptr)
  474. return;
  475. const Size& parentSize = parent->getContentSize();
  476. const Point& ownerAnchor = _owner->getAnchorPoint();
  477. Size ownerSize = _owner->getContentSize();
  478. Point ownerPosition = _owner->getPosition();
  479. switch (this->_horizontalEdge)
  480. {
  481. case HorizontalEdge::None:
  482. if (_usingStretchWidth && !_isPercentOnly)
  483. {
  484. ownerSize.width = parentSize.width * _percentWidth;
  485. ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
  486. }
  487. else
  488. {
  489. if (_usingPositionPercentX)
  490. ownerPosition.x = parentSize.width * _positionPercentX;
  491. if (_usingPercentWidth)
  492. ownerSize.width = parentSize.width * _percentWidth;
  493. }
  494. break;
  495. case HorizontalEdge::Left:
  496. if (_isPercentOnly)
  497. break;
  498. if (_usingPercentWidth || _usingStretchWidth)
  499. ownerSize.width = parentSize.width * _percentWidth;
  500. ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
  501. break;
  502. case HorizontalEdge::Right:
  503. if (_isPercentOnly)
  504. break;
  505. if (_usingPercentWidth || _usingStretchWidth)
  506. ownerSize.width = parentSize.width * _percentWidth;
  507. ownerPosition.x = parentSize.width - (_rightMargin + (1 - ownerAnchor.x) * ownerSize.width);
  508. break;
  509. case HorizontalEdge::Center:
  510. if (_isPercentOnly)
  511. break;
  512. if (_usingStretchWidth)
  513. {
  514. ownerSize.width = parentSize.width - _leftMargin - _rightMargin;
  515. if (ownerSize.width < 0)
  516. ownerSize.width = 0;
  517. ownerPosition.x = _leftMargin + ownerAnchor.x * ownerSize.width;
  518. }
  519. else
  520. {
  521. if (_usingPercentWidth)
  522. ownerSize.width = parentSize.width * _percentWidth;
  523. ownerPosition.x = parentSize.width * _positionPercentX;
  524. }
  525. break;
  526. default:
  527. break;
  528. }
  529. switch (this->_verticalEdge)
  530. {
  531. case VerticalEdge::None:
  532. if (_usingStretchHeight && !_isPercentOnly)
  533. {
  534. ownerSize.height = parentSize.height * _percentHeight;
  535. ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
  536. }
  537. else
  538. {
  539. if (_usingPositionPercentY)
  540. ownerPosition.y = parentSize.height * _positionPercentY;
  541. if (_usingPercentHeight)
  542. ownerSize.height = parentSize.height * _percentHeight;
  543. }
  544. break;
  545. case VerticalEdge::Bottom:
  546. if (_isPercentOnly)
  547. break;
  548. if (_usingPercentHeight || _usingStretchHeight)
  549. ownerSize.height = parentSize.height * _percentHeight;
  550. ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
  551. break;
  552. case VerticalEdge::Top:
  553. if (_isPercentOnly)
  554. break;
  555. if (_usingPercentHeight || _usingStretchHeight)
  556. ownerSize.height = parentSize.height * _percentHeight;
  557. ownerPosition.y = parentSize.height - (_topMargin + (1 - ownerAnchor.y) * ownerSize.height);
  558. break;
  559. case VerticalEdge::Center:
  560. if (_isPercentOnly)
  561. break;
  562. if (_usingStretchHeight)
  563. {
  564. ownerSize.height = parentSize.height - _topMargin - _bottomMargin;
  565. if (ownerSize.height < 0)
  566. ownerSize.height = 0;
  567. ownerPosition.y = _bottomMargin + ownerAnchor.y * ownerSize.height;
  568. }
  569. else
  570. {
  571. if (_usingPercentHeight)
  572. ownerSize.height = parentSize.height * _percentHeight;
  573. ownerPosition.y = parentSize.height* _positionPercentY;
  574. }
  575. break;
  576. default:
  577. break;
  578. }
  579. _owner->setPosition(ownerPosition);
  580. _owner->setContentSize(ownerSize);
  581. if (typeid(*_owner) == typeid(PageView))
  582. {
  583. PageView* page = static_cast<PageView*>(_owner);
  584. page->forceDoLayout();
  585. Vector<Widget*> _widgetVector = page->getItems();
  586. for(auto& item : _widgetVector)
  587. {
  588. ui::Helper::doLayout(item);
  589. }
  590. }
  591. else
  592. {
  593. ui::Helper::doLayout(_owner);
  594. }
  595. }
  596. void LayoutComponent::setActiveEnabled(bool enable)
  597. {
  598. _actived = enable;
  599. }
  600. void LayoutComponent::setPercentOnlyEnabled(bool enable)
  601. {
  602. _isPercentOnly = enable;
  603. }
  604. }
  605. NS_CC_END