2014-05-14 18:03:44 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-28 10:17:34 +08:00
|
|
|
#include "UILayoutManager.h"
|
2014-05-15 10:07:42 +08:00
|
|
|
#include "UILayout.h"
|
2014-05-14 18:03:44 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2014-05-28 10:11:49 +08:00
|
|
|
LinearHorizontalLayoutManager* LinearHorizontalLayoutManager::create()
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-28 10:11:49 +08:00
|
|
|
LinearHorizontalLayoutManager* exe = new LinearHorizontalLayoutManager();
|
2014-05-14 18:03:44 +08:00
|
|
|
if (exe)
|
|
|
|
{
|
|
|
|
exe->autorelease();
|
|
|
|
return exe;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(exe);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-28 10:11:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
void LinearHorizontalLayoutManager::doLayout(LayoutProtocol* layout)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-28 10:11:49 +08:00
|
|
|
Size layoutSize = layout->getLayoutContentSize();
|
|
|
|
Vector<Node*> container = layout->getLayoutElements();
|
|
|
|
float leftBoundary = 0.0f;
|
|
|
|
for (auto& subWidget : container)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-28 10:11:49 +08:00
|
|
|
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->getSize();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
|
|
|
}
|
2014-05-28 10:11:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
//LinearVerticalLayoutManager
|
|
|
|
LinearVerticalLayoutManager* LinearVerticalLayoutManager::create()
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-28 10:11:49 +08:00
|
|
|
LinearVerticalLayoutManager* exe = new LinearVerticalLayoutManager();
|
2014-05-14 18:03:44 +08:00
|
|
|
if (exe)
|
|
|
|
{
|
|
|
|
exe->autorelease();
|
|
|
|
return exe;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(exe);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-28 10:11:49 +08:00
|
|
|
|
|
|
|
void LinearVerticalLayoutManager::doLayout(LayoutProtocol* layout)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 10:16:19 +08:00
|
|
|
Size layoutSize = layout->getLayoutContentSize();
|
|
|
|
Vector<Node*> container = layout->getLayoutElements();
|
2014-05-14 18:03:44 +08:00
|
|
|
float topBoundary = layoutSize.height;
|
|
|
|
|
|
|
|
for (auto& subWidget : container)
|
|
|
|
{
|
2014-05-15 10:44:19 +08:00
|
|
|
LayoutParameterProtocol* child = dynamic_cast<LayoutParameterProtocol*>(subWidget);
|
2014-05-14 18:03:44 +08:00
|
|
|
if (child)
|
|
|
|
{
|
2014-05-16 10:33:28 +08:00
|
|
|
LinearLayoutParameter* layoutParameter = dynamic_cast<LinearLayoutParameter*>(child->getLayoutParameter());
|
2014-05-14 18:03:44 +08:00
|
|
|
|
|
|
|
if (layoutParameter)
|
|
|
|
{
|
|
|
|
LinearLayoutParameter::LinearGravity childGravity = layoutParameter->getGravity();
|
2014-05-16 10:22:53 +08:00
|
|
|
Vec2 ap = subWidget->getAnchorPoint();
|
2014-05-15 10:44:19 +08:00
|
|
|
Size cs = subWidget->getContentSize();
|
2014-05-14 18:03:44 +08:00
|
|
|
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;
|
2014-05-16 10:22:53 +08:00
|
|
|
subWidget->setPosition(Vec2(finalPosX, finalPosY));
|
2014-05-15 10:44:19 +08:00
|
|
|
topBoundary = subWidget->getPosition().y - subWidget->getAnchorPoint().y * subWidget->getContentSize().height - mg.bottom;
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-05-28 10:11:49 +08:00
|
|
|
|
|
|
|
//RelativeLayoutManager
|
2014-05-14 18:03:44 +08:00
|
|
|
|
2014-05-28 10:11:49 +08:00
|
|
|
RelativeLayoutManager* RelativeLayoutManager::create()
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-28 10:11:49 +08:00
|
|
|
RelativeLayoutManager* exe = new RelativeLayoutManager();
|
|
|
|
if (exe)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-28 10:11:49 +08:00
|
|
|
exe->autorelease();
|
|
|
|
return exe;
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
2014-05-28 10:11:49 +08:00
|
|
|
CC_SAFE_DELETE(exe);
|
|
|
|
return nullptr;
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
2014-05-28 10:11:49 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Vector<Widget*> RelativeLayoutManager::getAllWidgets(cocos2d::ui::LayoutProtocol *layout)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 10:16:19 +08:00
|
|
|
Vector<Node*> container = layout->getLayoutElements();
|
2014-05-14 18:03:44 +08:00
|
|
|
Vector<Widget*> widgetChildren;
|
|
|
|
for (auto& subWidget : container)
|
|
|
|
{
|
|
|
|
Widget* child = dynamic_cast<Widget*>(subWidget);
|
|
|
|
if (child)
|
|
|
|
{
|
2014-05-16 10:33:28 +08:00
|
|
|
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(child->getLayoutParameter());
|
2014-05-14 18:03:44 +08:00
|
|
|
layoutParameter->_put = false;
|
2014-05-20 16:09:30 +08:00
|
|
|
_unlayoutChildCount++;
|
2014-05-14 18:03:44 +08:00
|
|
|
widgetChildren.pushBack(child);
|
|
|
|
}
|
|
|
|
}
|
2014-05-20 16:09:30 +08:00
|
|
|
return widgetChildren;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-28 10:11:49 +08:00
|
|
|
Widget* RelativeLayoutManager::getRelativeWidget(Widget* widget)
|
2014-05-20 16:09:30 +08:00
|
|
|
{
|
|
|
|
Widget* relativeWidget = nullptr;
|
|
|
|
RelativeLayoutParameter* layoutParameter = dynamic_cast<RelativeLayoutParameter*>(widget->getLayoutParameter());
|
|
|
|
const std::string relativeName = layoutParameter->getRelativeToWidgetName();
|
|
|
|
|
|
|
|
if (!relativeName.empty())
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 16:09:30 +08:00
|
|
|
for (auto& sWidget : _widgetChildren)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 16:09:30 +08:00
|
|
|
if (sWidget)
|
|
|
|
{
|
|
|
|
RelativeLayoutParameter* rlayoutParameter = dynamic_cast<RelativeLayoutParameter*>(sWidget->getLayoutParameter());
|
|
|
|
if (rlayoutParameter && rlayoutParameter->getRelativeName() == relativeName)
|
|
|
|
{
|
|
|
|
relativeWidget = sWidget;
|
|
|
|
_relativeWidgetLP = rlayoutParameter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return relativeWidget;
|
|
|
|
}
|
|
|
|
|
2014-05-28 10:11:49 +08:00
|
|
|
bool RelativeLayoutManager::caculateFinalPositionWithRelativeWidget(LayoutProtocol *layout)
|
2014-05-20 16:09:30 +08:00
|
|
|
{
|
|
|
|
Vec2 ap = _widget->getAnchorPoint();
|
|
|
|
Size cs = _widget->getSize();
|
|
|
|
|
|
|
|
_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;
|
2014-05-14 18:03:44 +08:00
|
|
|
|
2014-05-20 16:09:30 +08:00
|
|
|
case RelativeLayoutParameter::RelativeAlign::LOCATION_ABOVE_LEFTALIGN:
|
|
|
|
if (relativeWidget)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 16:09:30 +08:00
|
|
|
if (_relativeWidgetLP && !_relativeWidgetLP->_put)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 16:09:30 +08:00
|
|
|
return false;
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
2014-05-20 16:09:30 +08:00
|
|
|
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)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 16:09:30 +08:00
|
|
|
return false;
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
2014-05-20 16:09:30 +08:00
|
|
|
Size rbs = relativeWidget->getSize();
|
|
|
|
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)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 16:09:30 +08:00
|
|
|
return false;
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
2014-05-20 16:09:30 +08:00
|
|
|
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)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 16:09:30 +08:00
|
|
|
return false;
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
2014-05-20 16:09:30 +08:00
|
|
|
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)
|
2014-05-14 18:03:44 +08:00
|
|
|
{
|
2014-05-20 16:09:30 +08:00
|
|
|
return false;
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
2014-05-20 16:09:30 +08:00
|
|
|
Size rbs = relativeWidget->getSize();
|
|
|
|
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->getSize();
|
|
|
|
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->getSize();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-05-28 10:11:49 +08:00
|
|
|
void RelativeLayoutManager::caculateFinalPositionWithRelativeAlign()
|
2014-05-20 16:09:30 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-28 10:11:49 +08:00
|
|
|
void RelativeLayoutManager::doLayout(LayoutProtocol *layout)
|
2014-05-20 16:09:30 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
_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->caculateFinalPositionWithRelativeWidget(layout);
|
|
|
|
if (!ret) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
this->caculateFinalPositionWithRelativeAlign();
|
|
|
|
|
|
|
|
|
|
|
|
_widget->setPosition(Vec2(_finalPositionX, _finalPositionY));
|
|
|
|
|
2014-05-14 18:03:44 +08:00
|
|
|
layoutParameter->_put = true;
|
|
|
|
}
|
|
|
|
}
|
2014-05-20 16:09:30 +08:00
|
|
|
_unlayoutChildCount--;
|
|
|
|
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
2014-05-20 16:09:30 +08:00
|
|
|
_widgetChildren.clear();
|
2014-05-14 18:03:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|