123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578 |
- /****************************************************************************
- Copyright (c) 2013-2016 Chukong Technologies Inc.
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
-
- http://www.cocos2d-x.org
-
- Permission is hereby granted, free of charge, to any person obtaining a copy
- of this software and associated documentation files (the "Software"), to deal
- in the Software without restriction, including without limitation the rights
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- copies of the Software, and to permit persons to whom the Software is
- furnished to do so, subject to the following conditions:
-
- The above copyright notice and this permission notice shall be included in
- all copies or substantial portions of the Software.
-
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- THE SOFTWARE.
- ****************************************************************************/
- #include "ui/UILayoutManager.h"
- #include "ui/UILayout.h"
- NS_CC_BEGIN
- namespace ui {
- LinearHorizontalLayoutManager* LinearHorizontalLayoutManager::create()
- {
- LinearHorizontalLayoutManager* exe = new (std::nothrow) LinearHorizontalLayoutManager();
- if (exe)
- {
- exe->autorelease();
- return exe;
- }
- CC_SAFE_DELETE(exe);
- return nullptr;
- }
-
-
- void LinearHorizontalLayoutManager::doLayout(LayoutProtocol* layout)
- {
- Size layoutSize = layout->getLayoutContentSize();
- Vector<Node*> container = layout->getLayoutElements();
- float leftBoundary = 0.0f;
- for (auto& subWidget : container)
- {
- Widget* child = dynamic_cast<Widget*>(subWidget);
- if (child)
- {
- LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
- if (layoutParameter)
- {
- LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
- Vec2 ap = child->getAnchorPoint();
- Size cs = child->getBoundingBox().size;
- float finalPosX = leftBoundary + (ap.x * cs.width);
- float finalPosY = layoutSize.height - (1.0f - ap.y) * cs.height;
- switch (childGravity)
- {
- case LinearLayoutParameter::LinearGravity::NONE:
- case LinearLayoutParameter::LinearGravity::TOP:
- break;
- case LinearLayoutParameter::LinearGravity::BOTTOM:
- finalPosY = ap.y * cs.height;
- break;
- case LinearLayoutParameter::LinearGravity::CENTER_VERTICAL:
- finalPosY = layoutSize.height / 2.0f - cs.height * (0.5f - ap.y);
- break;
- default:
- break;
- }
- Margin mg = layoutParameter->getMargin();
- finalPosX += mg.left;
- finalPosY -= mg.top;
- child->setPosition(Vec2(finalPosX, finalPosY));
- leftBoundary = child->getRightBoundary() + mg.right;
- }
- }
- }
- }
-
-
- //LinearVerticalLayoutManager
- LinearVerticalLayoutManager* LinearVerticalLayoutManager::create()
- {
- LinearVerticalLayoutManager* exe = new (std::nothrow) LinearVerticalLayoutManager();
- if (exe)
- {
- exe->autorelease();
- return exe;
- }
- CC_SAFE_DELETE(exe);
- return nullptr;
- }
-
- void LinearVerticalLayoutManager::doLayout(LayoutProtocol* layout)
- {
- Size layoutSize = layout->getLayoutContentSize();
- Vector<Node*> container = layout->getLayoutElements();
- float topBoundary = layoutSize.height;
-
- for (auto& subWidget : container)
- {
- LayoutParameterProtocol* child = dynamic_cast<LayoutParameterProtocol*>(subWidget);
- if (child)
- {
- LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
-
- if (layoutParameter)
- {
- LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
- Vec2 ap = subWidget->getAnchorPoint();
- Size cs = subWidget->getBoundingBox().size;
- float finalPosX = ap.x * cs.width;
- float finalPosY = topBoundary - ((1.0f-ap.y) * cs.height);
- switch (childGravity)
- {
- case LinearLayoutParameter::LinearGravity::NONE:
- case LinearLayoutParameter::LinearGravity::LEFT:
- break;
- case LinearLayoutParameter::LinearGravity::RIGHT:
- finalPosX = layoutSize.width - ((1.0f - ap.x) * cs.width);
- break;
- case LinearLayoutParameter::LinearGravity::CENTER_HORIZONTAL:
- finalPosX = layoutSize.width / 2.0f - cs.width * (0.5f-ap.x);
- break;
- default:
- break;
- }
- Margin mg = layoutParameter->getMargin();
- finalPosX += mg.left;
- finalPosY -= mg.top;
- subWidget->setPosition(finalPosX, finalPosY);
- topBoundary = subWidget->getPosition().y - subWidget->getAnchorPoint().y * subWidget->getBoundingBox().size.height - mg.bottom;
- }
- }
- }
- }
-
- //RelativeLayoutManager
- RelativeLayoutManager* RelativeLayoutManager::create()
- {
- RelativeLayoutManager* exe = new (std::nothrow) RelativeLayoutManager();
- if (exe)
- {
- exe->autorelease();
- return exe;
- }
- CC_SAFE_DELETE(exe);
- return nullptr;
- }
- Vector<Widget*> RelativeLayoutManager::getAllWidgets(cocos2d::ui::LayoutProtocol *layout)
- {
- Vector<Node*> container = layout->getLayoutElements();
- Vector<Widget*> widgetChildren;
- for (auto& subWidget : container)
- {
- Widget* child = dynamic_cast<Widget*>(subWidget);
- if (child)
- {
- RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
- layoutParameter->_put = false;
- _unlayoutChildCount++;
- widgetChildren.pushBack(child);
- }
- }
- return widgetChildren;
- }
-
- Widget* RelativeLayoutManager::getRelativeWidget(Widget* widget)
- {
- Widget* relativeWidget = nullptr;
- RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(widget->getLayoutParameter());
- const std::string relativeName = layoutParameter->getRelativeToWidgetName();
-
- if (!relativeName.empty())
- {
- for (auto& sWidget : _widgetChildren)
- {
- if (sWidget)
- {
- RelativeLayoutParameter* rlayoutParameter = dynamic_cast<RelativeLayoutParameter*>(sWidget->getLayoutParameter());
- if (rlayoutParameter && rlayoutParameter->getRelativeName() == relativeName)
- {
- relativeWidget = sWidget;
- _relativeWidgetLP = rlayoutParameter;
- break;
- }
- }
- }
- }
- return relativeWidget;
- }
-
- bool RelativeLayoutManager::calculateFinalPositionWithRelativeWidget(LayoutProtocol *layout)
- {
- Vec2 ap = _widget->getAnchorPoint();
- Size cs = _widget->getBoundingBox().size;
-
- _finalPositionX = 0.0f;
- _finalPositionY = 0.0f;
-
- Widget* relativeWidget = this->getRelativeWidget(_widget);
-
- RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(_widget->getLayoutParameter());
- RelativeLayoutParameter::RelativeAlign align = layoutParameter->getAlign();
- Size layoutSize = layout->getLayoutContentSize();
-
- switch (align)
- {
- case RelativeLayoutParameter::RelativeAlign::NONE:
- case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT:
- _finalPositionX = ap.x * cs.width;
- _finalPositionY = layoutSize.height - ((1.0f - ap.y) * cs.height);
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL:
- _finalPositionX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
- _finalPositionY = layoutSize.height - ((1.0f - ap.y) * cs.height);
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT:
- _finalPositionX = layoutSize.width - ((1.0f - ap.x) * cs.width);
- _finalPositionY = layoutSize.height - ((1.0f - ap.y) * cs.height);
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL:
- _finalPositionX = ap.x * cs.width;
- _finalPositionY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
- break;
- case RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT:
- _finalPositionX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
- _finalPositionY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL:
- _finalPositionX = layoutSize.width - ((1.0f - ap.x) * cs.width);
- _finalPositionY = layoutSize.height * 0.5f - cs.height * (0.5f - ap.y);
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM:
- _finalPositionX = ap.x * cs.width;
- _finalPositionY = ap.y * cs.height;
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL:
- _finalPositionX = layoutSize.width * 0.5f - cs.width * (0.5f - ap.x);
- _finalPositionY = ap.y * cs.height;
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM:
- _finalPositionX = layoutSize.width - ((1.0f - ap.x) * cs.width);
- _finalPositionY = ap.y * cs.height;
- break;
-
- case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_LEFTALIGN:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- float locationTop = relativeWidget->getTopBoundary();
- float locationLeft = relativeWidget->getLeftBoundary();
- _finalPositionY = locationTop + ap.y * cs.height;
- _finalPositionX = locationLeft + ap.x * cs.width;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- Size rbs = relativeWidget->getBoundingBox().size;
- float locationTop = relativeWidget->getTopBoundary();
-
- _finalPositionY = locationTop + ap.y * cs.height;
- _finalPositionX = relativeWidget->getLeftBoundary() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_RIGHTALIGN:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- float locationTop = relativeWidget->getTopBoundary();
- float locationRight = relativeWidget->getRightBoundary();
- _finalPositionY = locationTop + ap.y * cs.height;
- _finalPositionX = locationRight - (1.0f - ap.x) * cs.width;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_TOPALIGN:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- float locationTop = relativeWidget->getTopBoundary();
- float locationLeft = relativeWidget->getLeftBoundary();
- _finalPositionY = locationTop - (1.0f - ap.y) * cs.height;
- _finalPositionX = locationLeft - (1.0f - ap.x) * cs.width;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- Size rbs = relativeWidget->getBoundingBox().size;
- float locationLeft = relativeWidget->getLeftBoundary();
- _finalPositionX = locationLeft - (1.0f - ap.x) * cs.width;
-
- _finalPositionY = relativeWidget->getBottomBoundary() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_BOTTOMALIGN:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- float locationBottom = relativeWidget->getBottomBoundary();
- float locationLeft = relativeWidget->getLeftBoundary();
- _finalPositionY = locationBottom + ap.y * cs.height;
- _finalPositionX = locationLeft - (1.0f - ap.x) * cs.width;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_TOPALIGN:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- float locationTop = relativeWidget->getTopBoundary();
- float locationRight = relativeWidget->getRightBoundary();
- _finalPositionY = locationTop - (1.0f - ap.y) * cs.height;
- _finalPositionX = locationRight + ap.x * cs.width;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- Size rbs = relativeWidget->getBoundingBox().size;
- float locationRight = relativeWidget->getRightBoundary();
- _finalPositionX = locationRight + ap.x * cs.width;
-
- _finalPositionY = relativeWidget->getBottomBoundary() + rbs.height * 0.5f + ap.y * cs.height - cs.height * 0.5f;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_BOTTOMALIGN:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- float locationBottom = relativeWidget->getBottomBoundary();
- float locationRight = relativeWidget->getRightBoundary();
- _finalPositionY = locationBottom + ap.y * cs.height;
- _finalPositionX = locationRight + ap.x * cs.width;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_LEFTALIGN:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- float locationBottom = relativeWidget->getBottomBoundary();
- float locationLeft = relativeWidget->getLeftBoundary();
- _finalPositionY = locationBottom - (1.0f - ap.y) * cs.height;
- _finalPositionX = locationLeft + ap.x * cs.width;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- Size rbs = relativeWidget->getBoundingBox().size;
- float locationBottom = relativeWidget->getBottomBoundary();
-
- _finalPositionY = locationBottom - (1.0f - ap.y) * cs.height;
- _finalPositionX = relativeWidget->getLeftBoundary() + rbs.width * 0.5f + ap.x * cs.width - cs.width * 0.5f;
- }
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN:
- if (relativeWidget)
- {
- if (_relativeWidgetLP && !_relativeWidgetLP->_put)
- {
- return false;
- }
- float locationBottom = relativeWidget->getBottomBoundary();
- float locationRight = relativeWidget->getRightBoundary();
- _finalPositionY = locationBottom - (1.0f - ap.y) * cs.height;
- _finalPositionX = locationRight - (1.0f - ap.x) * cs.width;
- }
- break;
- default:
- break;
- }
- return true;
- }
-
- void RelativeLayoutManager::calculateFinalPositionWithRelativeAlign()
- {
- RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(_widget->getLayoutParameter());
-
- Margin mg = layoutParameter->getMargin();
-
-
- RelativeLayoutParameter::RelativeAlign align = layoutParameter->getAlign();
-
- //handle margin
- switch (align)
- {
- case RelativeLayoutParameter::RelativeAlign::NONE:
- case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_LEFT:
- _finalPositionX += mg.left;
- _finalPositionY -= mg.top;
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_CENTER_HORIZONTAL:
- _finalPositionY -= mg.top;
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_TOP_RIGHT:
- _finalPositionX -= mg.right;
- _finalPositionY -= mg.top;
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_CENTER_VERTICAL:
- _finalPositionX += mg.left;
- break;
- case RelativeLayoutParameter::RelativeAlign::CENTER_IN_PARENT:
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_CENTER_VERTICAL:
- _finalPositionX -= mg.right;
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_LEFT_BOTTOM:
- _finalPositionX += mg.left;
- _finalPositionY += mg.bottom;
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_BOTTOM_CENTER_HORIZONTAL:
- _finalPositionY += mg.bottom;
- break;
- case RelativeLayoutParameter::RelativeAlign::PARENT_RIGHT_BOTTOM:
- _finalPositionX -= mg.right;
- _finalPositionY += mg.bottom;
- break;
-
- case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_LEFTALIGN:
- _finalPositionY += mg.bottom;
- _finalPositionX += mg.left;
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_RIGHTALIGN:
- _finalPositionY += mg.bottom;
- _finalPositionX -= mg.right;
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_CENTER:
- _finalPositionY += mg.bottom;
- break;
-
- case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_TOPALIGN:
- _finalPositionX -= mg.right;
- _finalPositionY -= mg.top;
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_BOTTOMALIGN:
- _finalPositionX -= mg.right;
- _finalPositionY += mg.bottom;
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_LEFT_OF_CENTER:
- _finalPositionX -= mg.right;
- break;
-
- case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_TOPALIGN:
- _finalPositionX += mg.left;
- _finalPositionY -= mg.top;
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_BOTTOMALIGN:
- _finalPositionX += mg.left;
- _finalPositionY += mg.bottom;
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_RIGHT_OF_CENTER:
- _finalPositionX += mg.left;
- break;
-
- case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_LEFTALIGN:
- _finalPositionY -= mg.top;
- _finalPositionX += mg.left;
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_RIGHTALIGN:
- _finalPositionY -= mg.top;
- _finalPositionX -= mg.right;
- break;
- case RelativeLayoutParameter::RelativeAlign::LOCATION_BELOW_CENTER:
- _finalPositionY -= mg.top;
- break;
- default:
- break;
- }
- }
- bool RelativeLayoutManager::caculateFinalPositionWithRelativeWidget(LayoutProtocol *layout)
- {
- return calculateFinalPositionWithRelativeWidget(layout);
- }
- void RelativeLayoutManager::caculateFinalPositionWithRelativeAlign()
- {
- calculateFinalPositionWithRelativeAlign();
- }
- void RelativeLayoutManager::doLayout(LayoutProtocol *layout)
- {
-
- _widgetChildren = this->getAllWidgets(layout);
-
- while (_unlayoutChildCount > 0)
- {
- for (auto& subWidget : _widgetChildren)
- {
- _widget = static_cast<Widget*>(subWidget);
-
- RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(_widget->getLayoutParameter());
-
- if (layoutParameter)
- {
- if (layoutParameter->_put)
- {
- continue;
- }
-
-
- bool ret = this->calculateFinalPositionWithRelativeWidget(layout);
- if (!ret) {
- continue;
- }
-
- this->calculateFinalPositionWithRelativeAlign();
-
-
- _widget->setPosition(Vec2(_finalPositionX, _finalPositionY));
-
- layoutParameter->_put = true;
- }
- }
- _unlayoutChildCount--;
- }
- _widgetChildren.clear();
- }
- }
- NS_CC_END
|