1
0

UILayoutComponent.cpp 20 KB

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