axmol/cocos/ui/LayoutHelper.cpp

1286 lines
41 KiB
C++
Raw Normal View History

2019-11-24 23:15:56 +08:00
//
2020-08-04 00:14:35 +08:00
// Copyright (c) 2014-2020 Simdsoft Limited - All Rights Reserved
2019-11-24 23:15:56 +08:00
//
2020-08-26 12:46:41 +08:00
#include "ui/LayoutHelper.h"
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
USING_NS_CC;
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
Size LayoutHelper::s_designSize;
float LayoutHelper::s_adjustedScale = 1.0f;
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
void LayoutHelper::setDesignSizeFixedEdge(const Size& designSize)
2019-11-24 23:15:56 +08:00
{
2020-08-26 12:46:41 +08:00
LayoutHelper::s_designSize = designSize;
2019-11-24 23:15:56 +08:00
// Set the design resolution//<2F>ֱ<EFBFBD><D6B1>ʵĴ<CAB5>С
GLView* pEGLView = Director::getInstance()->getOpenGLView();
2020-08-26 12:46:41 +08:00
const Size& frameSize = pEGLView->getFrameSize();
2019-11-24 23:15:56 +08:00
// Size lsSize = lsaSize;
2020-08-26 12:46:41 +08:00
float scaleX = (float)frameSize.width / designSize.width;
float scaleY = (float)frameSize.height / designSize.height;
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
// adjustedScale = 0.0f; // MAX(scaleX, scaleY);
if (scaleX < scaleY) {
pEGLView->setDesignResolutionSize(designSize.width,
designSize.height, ResolutionPolicy::FIXED_WIDTH);
2019-11-24 23:15:56 +08:00
}
else {
2020-08-26 12:46:41 +08:00
pEGLView->setDesignResolutionSize(designSize.width,
designSize.height, ResolutionPolicy::FIXED_HEIGHT);
2019-11-24 23:15:56 +08:00
}
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::setDesignSizeNoBorder(const cocos2d::Size& designSize)
2019-11-24 23:15:56 +08:00
{
2020-08-26 12:46:41 +08:00
// save smart size
LayoutHelper::s_designSize = designSize;
2019-11-24 23:15:56 +08:00
// Set the design resolution//<2F>ֱ<EFBFBD><D6B1>ʵĴ<CAB5>С
GLView* pEGLView = Director::getInstance()->getOpenGLView();
2020-08-26 12:46:41 +08:00
const cocos2d::Size& frameSize = pEGLView->getFrameSize();
2019-11-24 23:15:56 +08:00
// Size lsSize = lsaSize;
2020-08-26 12:46:41 +08:00
float scaleX = (float)frameSize.width / LayoutHelper::s_designSize.width;
float scaleY = (float)frameSize.height / LayoutHelper::s_designSize.height;
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
LayoutHelper::s_adjustedScale = 0.0f; // MAX(scaleX, scaleY);
if (scaleX > scaleY) {
LayoutHelper::s_adjustedScale = scaleX / (frameSize.height / LayoutHelper::s_designSize.height);
2019-11-24 23:15:56 +08:00
}
else {
2020-08-26 12:46:41 +08:00
LayoutHelper::s_adjustedScale = scaleY / (frameSize.width / LayoutHelper::s_designSize.width);
2019-11-24 23:15:56 +08:00
}
2020-08-26 12:46:41 +08:00
cocos2d::log("x: %f; y: %f; scale: %f", scaleX, scaleY, s_adjustedScale);
pEGLView->setDesignResolutionSize(LayoutHelper::s_designSize.width * s_adjustedScale,
LayoutHelper::s_designSize.height * s_adjustedScale, ResolutionPolicy::NO_BORDER);
2019-11-24 23:15:56 +08:00
}
2020-08-26 12:46:41 +08:00
cocos2d::Vec2 LayoutHelper::getVisibleOrigin(void)
2019-11-24 23:15:56 +08:00
{
const auto& adjustedDesignSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
return cocos2d::Vec2((adjustedDesignSize.width - LayoutHelper::s_designSize.width) * .5f,
(adjustedDesignSize.height - LayoutHelper::s_designSize.height) * .5f);
2019-11-24 23:15:56 +08:00
}
2020-08-26 12:46:41 +08:00
cocos2d::Size LayoutHelper::getVisibleSize(void)
2019-11-24 23:15:56 +08:00
{
2020-08-26 12:46:41 +08:00
return LayoutHelper::s_designSize;
2019-11-24 23:15:56 +08:00
}
/// Get node group size
2020-08-26 12:46:41 +08:00
Size LayoutHelper::getNodeGroupSize(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.empty())
{
return Size::ZERO;
}
// group nodes locators
float minX = getNodeLeft(nodes[0]), maxX = getNodeLeft(nodes[0]) + nodes[0]->getContentSize().width/* * nodes[0]->getScaleX()*/;
float minY = getNodeTop(nodes[0]), maxY = getNodeTop(nodes[0]) + nodes[0]->getContentSize().height/* * nodes[0]->getScaleY()*/;
float x = 0.0f, y = 0.0f;
// float width = 0.0f, height = 0.f;
for (size_t index = 1; index < nodes.size(); ++index) {
Node* child = nodes[index];
if (minX >(x = getNodeLeft(child)))
{
minX = x;
}
if (maxX < x + child->getContentSize().width/* * child->getScaleX()*/)
{
maxX = x + child->getContentSize().width/* * child->getScaleX()*/;
}
if (minY >(y = getNodeTop(child)))
{
minY = y;
}
if (maxY < y + child->getContentSize().height/* + child->getScaleY()*/)
{
maxY = y + child->getContentSize().height/* + child->getScaleY()*/;
}
}
float groupWidth = maxX - minX;
float groupHeight = maxY - minY;
return Size(groupWidth, groupHeight);
}
/// Set nodes group size
2020-08-26 12:46:41 +08:00
void LayoutHelper::setNodeGroupSize(const std::vector<Node*>& nodes, const cocos2d::Size& newSize)
2019-11-24 23:15:56 +08:00
{
cocos2d::Size groupSize = getNodeGroupScaledSize(nodes);
if (groupSize.height == 0 || groupSize.width == 0 || newSize.width == 0 || newSize.height == 0)
{
return;
}
float scaleX = newSize.width / groupSize.width;
float scaleY = newSize.height / groupSize.height;
for (auto iter = nodes.begin(); iter != nodes.end(); ++iter)
{
auto detail = dynamic_cast<cocos2d::Sprite*>(*iter);
if (detail != nullptr)
{
/*auto scaledWidth = (detail->getContentWidth() * detail->getScaleX());
auto scaledHeight = (detail->getContentHeight() * detail->getScaleY());
scaledWidth *= scaleX;
scaledHeight *= scaleY;*/
detail->setScale(detail->getScaleX() * scaleX, detail->getScaleY() * scaleY);
}
else {
(*iter)->setContentSize(
Size(
(*iter)->getContentSize().width * scaleX,
(*iter)->getContentSize().height * scaleY));
}
}
}
2020-08-26 12:46:41 +08:00
Size LayoutHelper::getNodeGroupScaledSize(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.empty())
{
return Size::ZERO;
}
auto scale = getScale2D(nodes[0]);
// group nodes locators
2020-08-26 12:46:41 +08:00
float minX = LayoutHelper::getNodeLeft(nodes[0]), maxX = LayoutHelper::getNodeLeft(nodes[0]) + nodes[0]->getContentSize().width * scale.x;
float minY = LayoutHelper::getNodeTop(nodes[0]), maxY = LayoutHelper::getNodeTop(nodes[0]) + nodes[0]->getContentSize().height * scale.y;
2019-11-24 23:15:56 +08:00
float x = 0.0f, y = 0.0f;
// float width = 0.0f, height = 0.f;
for (size_t index = 1; index < nodes.size(); ++index) {
Node* child = nodes[index];
scale = getScale2D(child);
2020-08-26 12:46:41 +08:00
if (minX >(x = LayoutHelper::getNodeLeft(child)))
2019-11-24 23:15:56 +08:00
{
minX = x;
}
if (maxX < x + child->getContentSize().width * scale.x)
{
maxX = x + child->getContentSize().width * scale.x;
}
2020-08-26 12:46:41 +08:00
if (minY >(y = LayoutHelper::getNodeTop(child)))
2019-11-24 23:15:56 +08:00
{
minY = y;
}
if (maxY < y + child->getContentSize().height * scale.y)
{
maxY = y + child->getContentSize().height * scale.y;
}
}
float groupWidth = maxX - minX;
float groupHeight = maxY - minY;
return cocos2d::Size(groupWidth, groupHeight);
}
/// Get Node group left
2020-08-26 12:46:41 +08:00
float LayoutHelper::getNodeGroupLeft(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
size_t index = 0;
float minLeft = getNodeLeft(nodes[index]);
for (index = 1; index < nodes.size(); ++index)
{
if (minLeft > getNodeLeft(nodes[index]))
{
minLeft = getNodeLeft(nodes[index]);
}
}
return minLeft;
}
/// Get node group top
2020-08-26 12:46:41 +08:00
float LayoutHelper::getNodeGroupTop(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
size_t index = 0;
float minTop = getNodeTop(nodes[index]);
for (index = 1; index < nodes.size(); ++index)
{
if (minTop > getNodeTop(nodes[index]))
{
minTop = getNodeTop(nodes[index]);
}
}
return minTop;
}
/// Get node group right
2020-08-26 12:46:41 +08:00
float LayoutHelper::getNodeGroupRight(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
size_t index = 0;
float minRight = getNodeRight(nodes[index]);
for (index = 1; index < nodes.size(); ++index)
{
if (minRight > getNodeRight(nodes[index]))
{
minRight = getNodeRight(nodes[index]);
}
}
return minRight;
}
/// Get node group bottom
2020-08-26 12:46:41 +08:00
float LayoutHelper::getNodeGroupBottom(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
size_t index = 0;
float minBottom = getNodeBottom(nodes[index]);
for (index = 1; index < nodes.size(); ++index)
{
if (minBottom > getNodeBottom(nodes[index]))
{
minBottom = getNodeBottom(nodes[index]);
}
}
return minBottom;
}
/*
** setNodeGroupLeft
**
*/
2020-08-26 12:46:41 +08:00
void LayoutHelper::setNodeGroupLeft(const std::vector<Node*>& nodes, float left)
2019-11-24 23:15:56 +08:00
{
float delta = left - getNodeGroupLeft(nodes);
for (auto iter = nodes.begin(); iter != nodes.end(); ++iter)
{
setNodeLeft((*iter), getNodeLeft((*iter)) + delta);
}
}
/*
** setNodeGroupLeft
**
*/
2020-08-26 12:46:41 +08:00
void LayoutHelper::setNodeGroupTop(const std::vector<Node*>& nodes, float top)
2019-11-24 23:15:56 +08:00
{
float delta = top - getNodeGroupTop(nodes);
for (auto iter = nodes.begin(); iter != nodes.end(); ++iter)
{
setNodeTop((*iter), getNodeTop((*iter)) + delta);
}
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::setNodeGroupLT(const std::vector<Node*>& nodes, const cocos2d::Vec2& p)
2019-11-24 23:15:56 +08:00
{
float deltaL = p.x - getNodeGroupLeft(nodes);
float deltaT = p.y - getNodeGroupTop(nodes);
for (auto iter = nodes.begin(); iter != nodes.end(); ++iter)
{
setNodeLT((*iter), cocos2d::Vec2(getNodeLeft((*iter)) + deltaL, getNodeTop((*iter)) + deltaT));
}
}
/*
** setNodeGroupRight
**
*/
2020-08-26 12:46:41 +08:00
void LayoutHelper::setNodeGroupRight(const std::vector<Node*>& nodes, float right)
2019-11-24 23:15:56 +08:00
{
float delta = right - getNodeGroupRight(nodes);
for (auto iter = nodes.begin(); iter != nodes.end(); ++iter)
{
setNodeRight((*iter), getNodeRight((*iter)) + delta);
}
}
/*
** setNodeGroupRight
**
*/
2020-08-26 12:46:41 +08:00
void LayoutHelper::setNodeGroupBottom(const std::vector<Node*>& nodes, float bottom)
2019-11-24 23:15:56 +08:00
{
float delta = bottom - getNodeGroupBottom(nodes);
for (auto iter = nodes.begin(); iter != nodes.end(); ++iter)
{
setNodeBottom((*iter), getNodeBottom((*iter)) + delta);
}
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::moveNodeGroupHorizontally(const std::vector<Node*>& nodes, float delta)
2019-11-24 23:15:56 +08:00
{
for (auto node : nodes)
{
node->setPositionX(node->getPositionX() + delta);
}
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::moveNodeGroupVertically(const std::vector<Node*>& nodes, float delta)
2019-11-24 23:15:56 +08:00
{
for (auto node : nodes)
{
node->setPositionY(node->getPositionY() - delta);
}
}
/* @brief: group layout and alignment
** @remark:
*/
/// <summary>
/// ˮƽ<CBAE><C6BD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::centerHorizontally(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.empty())
{
return;
}
else if (nodes.size() == 1)
{
centerNodeX(nodes[0]);
return;
}
// group nodes locators
float minX = getNodeLeft(nodes[0]), maxX = getNodeLeft(nodes[0]) + nodes[0]->getContentSize().width;
float x = 0.0f;
float width = 0.0f;
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin() + 1, nodes.end(), [&](Node* child)->void{
if (minX > (x = getNodeLeft(child)))
{
minX = x;
}
if (maxX < x + child->getContentSize().width)
{
maxX = x + child->getContentSize().width;
}
});
#else
for (size_t index = 1; index < nodes.size(); ++index) {
Node* child = nodes[index];
if (minX >(x = getNodeLeft(child)))
{
minX = x;
}
if (maxX < x + child->getContentSize().width)
{
maxX = x + child->getContentSize().width;
}
}
#endif
float groupWidth = maxX - minX;
float dist = ((nodes[0]->getParent()->getContentSize().width - groupWidth) / 2.0f) - minX;
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin(), nodes.end(), [dist](Node* child)->void{
setNodeLeft(child, getNodeLeft(child) + dist);
});
#else
for (size_t index = 0; index < nodes.size(); ++index) {
Node* child = nodes[index];
setNodeLeft(child, getNodeLeft(child) + dist, 0);
}
#endif
}
/// <summary>
/// <20><>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::centerVertically(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.empty())
{
return;
}
else if (nodes.size() == 1)
{
centerNodeY(nodes[0]);
return;
}
// group nodes locators
float minY = getNodeTop(nodes[0]), maxY = getNodeTop(nodes[0]) + nodes[0]->getContentSize().height;
float y = 0.0f;
float height = 0.0f;
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin() + 1, nodes.end(), [&](Node* child)->void{
if (minY > (y = getNodeTop(child)))
{
minY = y;
}
if (maxY < y + child->getContentSize().height)
{
maxY = y + child->getContentSize().height;
}
});
#else
#endif
float groupHeight = maxY - minY;
float dist = ((nodes[0]->getParent()->getContentSize().height - groupHeight) / 2.0f) - minY;
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin(), nodes.end(), [dist](Node* child)->void{
setNodeTop(child, getNodeTop(child) + dist);
});
#else
#endif
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::centerToParent(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.empty())
{
return;
}
else if (nodes.size() == 1)
{
2020-08-26 12:46:41 +08:00
LayoutHelper::centerNode(nodes[0]);
2019-11-24 23:15:56 +08:00
return;
}
// group nodes locators
float minX = getNodeLeft(nodes[0]), maxX = getNodeLeft(nodes[0]) + nodes[0]->getContentSize().width;
float x = 0.0f;
float width = 0.0f;
float minY = getNodeTop(nodes[0]), maxY = getNodeTop(nodes[0]) + nodes[0]->getContentSize().height;
float y = 0.0f;
float height = 0.0f;
std::for_each(nodes.begin() + 1, nodes.end(), [&](Node* child)->void{
if (minX > (x = getNodeLeft(child)))
{
minX = x;
}
if (maxX < x + child->getContentSize().width)
{
maxX = x + child->getContentSize().width;
}
if (minY > (y = getNodeTop(child)))
{
minY = y;
}
if (maxY < y + child->getContentSize().height)
{
maxY = y + child->getContentSize().height;
}
});
float groupWidth = maxX - minX;
float groupHeight = maxY - minY;
float distX = ((nodes[0]->getParent()->getContentSize().width - groupWidth) / 2.0f) - minX;
float distY = ((nodes[0]->getParent()->getContentSize().height - groupHeight) / 2.0f) - minY;
std::for_each(nodes.begin(), nodes.end(), [distX, distY](Node* child)->void{
setNodeLT(child, cocos2d::Point(getNodeLeft(child) + distX, getNodeTop(child) + distY));
});
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::alignLefts(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
size_t index = 0;
float minLeft = getNodeLeft(nodes[index]);
for (index = 1; index < nodes.size(); ++index)
{
if (minLeft > getNodeLeft(nodes[index]))
{
minLeft = getNodeLeft(nodes[index]);
}
}
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin(), nodes.end(), [minLeft](Node* child)->void{
setNodeLeft(child, minLeft);
});
#else
#endif
}
/// <summary>
/// <20>Ҷ<EFBFBD><D2B6><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::alignRights(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
size_t index = 0;
float maxRight = getNodeLeft(nodes[index]) + nodes[index]->getContentSize().width;
for (index = 1; index < nodes.size(); ++index)
{
if (maxRight < (getNodeLeft(nodes[index]) + nodes[index]->getContentSize().width))
{
maxRight = getNodeLeft(nodes[index]) + nodes[index]->getContentSize().width;
}
}
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin(), nodes.end(), [maxRight](Node* child)->void{
setNodeLeft(child, maxRight - child->getContentSize().width);
});
#else
#endif
}
/// <summary>
/// <20><><EFBFBD>˶<EFBFBD><CBB6><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::alignTops(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
size_t index = 0;
float minTop = getNodeTop(nodes[index]);
for (index = 1; index < nodes.size(); ++index)
{
if (minTop > getNodeTop(nodes[index]))
{
minTop = getNodeTop(nodes[index]);
}
}
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin(), nodes.end(), [minTop](Node* child)->void{
setNodeTop(child, minTop);
});
#else
#endif
}
/// <summary>
/// <20>׶˶<D7B6><CBB6><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::alignBottoms(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
size_t index = 0;
float maxBottom = getNodeTop(nodes[index]) + nodes[index]->getContentSize().height;
for (index = 1; index < nodes.size(); ++index)
{
if (maxBottom < (getNodeTop(nodes[index]) + nodes[index]->getContentSize().height))
{
maxBottom = getNodeTop(nodes[index]) + nodes[index]->getContentSize().height;
}
}
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin(), nodes.end(), [maxBottom](Node* child)->void{
setNodeTop(child, maxBottom - child->getContentSize().height);
});
#else
#endif
}
/// <summary>
/// ˮƽ<CBAE><C6BD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::alignHorizontals(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
size_t index = 0;
float minCenterY, maxCenterY, centerY;
Node* child = nodes[index];
minCenterY = maxCenterY = getNodeBottom(child, 0.5f); // child.GetX(0.5f);
for (index = 1; index < nodes.size(); ++index)
{
child = nodes[index];
centerY = getNodeBottom(child, 0.5f); // child.GetX(0.5f);
if (minCenterY > centerY)
{
minCenterY = centerY;
}
if (maxCenterY < centerY)
{
maxCenterY = centerY;
}
}
std::for_each(nodes.begin(), nodes.end(), [minCenterY, maxCenterY](Node* child)->void{
setNodeBottom(child, (minCenterY + maxCenterY) * 0.5f, 0.5f); // child.SetX((minCenterX + maxCenterX) * 0.5f, 0.5f);
});
}
/// <summary>
/// <20><>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::alignVerticals(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
size_t index = 0;
float minCenterX, maxCenterX, centerX;
Node* child = nodes[index];
minCenterX = maxCenterX = getNodeLeft(child, 0.5f); // child.GetX(0.5f);
for (index = 1; index < nodes.size(); ++index)
{
child = nodes[index];
centerX = getNodeLeft(child, 0.5f); // child.GetX(0.5f);
if (minCenterX > centerX)
{
minCenterX = centerX;
}
if (maxCenterX < centerX)
{
maxCenterX = centerX;
}
}
std::for_each(nodes.begin(), nodes.end(), [minCenterX, maxCenterX](Node* child)->void{
setNodeLeft(child, (minCenterX + maxCenterX) * 0.5f, 0.5f); // child.SetX((minCenterX + maxCenterX) * 0.5f, 0.5f);
});
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>ԭ<EFBFBD><D4AD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::alignCenters(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
alignHorizontals(nodes);
alignVerticals(nodes);
}
/// <summary>
/// ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::makeSameWidth(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
size_t index = 0;
float maxWidth = nodes[index]->getContentSize().width;
for (index = 1; index < nodes.size(); ++index)
{
if (maxWidth < nodes[index]->getContentSize().width)
{
maxWidth = nodes[index]->getContentSize().width;
}
}
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin(), nodes.end(), [maxWidth](Node* child)->void{
child->setContentSize(Size(maxWidth, child->getContentSize().height));
});
#else
#endif
}
/// <summary>
/// ʹ<>߶<EFBFBD><DFB6><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::makeSameHeight(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
size_t index = 0;
float minHeight = nodes[index]->getContentSize().height;
for (index = 1; index < nodes.size(); ++index)
{
if (minHeight > nodes[index]->getContentSize().height)
{
minHeight = nodes[index]->getContentSize().height;
}
}
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
std::for_each(nodes.begin(), nodes.end(), [minHeight](Node* child)->void{
child->setContentSize(Size(child->getContentSize().width, minHeight));
});
#else
#endif
}
/// <summary>
/// ʹ<>ô<EFBFBD>С<EFBFBD><D0A1><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::makeSameSize(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
makeSameHeight(nodes);
makeSameWidth(nodes);
}
/// <summary>
/// ʹˮƽ<CBAE><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::makeHorizontalSpacingEqual(std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 3)
return;
float avgHSpacing = 0;
Node* child, *childNext;
if (nodes.size() < 3) return;
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
// sort by x firstly
std::sort(nodes.begin(), nodes.end(), [](Node* const left, Node* const right){
return getNodeLeft(left) < getNodeLeft(right);
});
#else
#endif
for (size_t index = 0; index < nodes.size() - 1; ++index)
{
child = nodes[index];
childNext = nodes[index + 1];
avgHSpacing += (getNodeLeft(childNext) - getNodeLeft(child) - child->getContentSize().width);
}
avgHSpacing /= (nodes.size() - 1);
if (avgHSpacing < 0.0f) avgHSpacing = 0.0f;
2020-08-26 12:46:41 +08:00
LayoutHelper::makeHorizontalSpacingEqual(nodes, avgHSpacing);
2019-11-24 23:15:56 +08:00
}
/// <summary>
/// ʹˮƽ<CBAE><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::makeHorizontalSpacingEqual(const std::vector<Node*>& nodes, float theSpacing)
2019-11-24 23:15:56 +08:00
{
Node* child, *childNext;
for (size_t index = 0; index < nodes.size() - 1; ++index)
{
child = nodes[index];
childNext = nodes[index + 1];
setNodeLeft(childNext, theSpacing + getNodeLeft(child) + child->getContentSize().width);
}
}
/// <summary>
/// ʹ<><CAB9>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::makeVerticalSpacingEqual(std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 3)
return;
float avgVSpacing = 0;
Node* child, *childNext;
if (nodes.size() < 3) return;
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
// sort by y firstly
std::sort(nodes.begin(), nodes.end(), [](Node* const left, Node* const right)->bool{
return getNodeTop(left) < getNodeTop(right);
});
#else
#endif
for (size_t index = 0; index < nodes.size() - 1; ++index)
{
child = nodes[index];
childNext = nodes[index + 1];
avgVSpacing += (getNodeTop(childNext) - getNodeTop(child) - child->getContentSize().height);
}
avgVSpacing /= (nodes.size() - 1);
if (avgVSpacing < 0.0f) avgVSpacing = 0.0f;
2020-08-26 12:46:41 +08:00
LayoutHelper::makeVerticalSpacingEqual(nodes, avgVSpacing);
2019-11-24 23:15:56 +08:00
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::increaseHorizontalSpacing(std::vector<Node*>& nodes, float theSpacing)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
std::sort(nodes.begin(), nodes.end(), [](Node* const left, Node* const right)->bool{
return getNodeLeft(left) < getNodeLeft(right);
});
for (size_t idx = 1; idx < nodes.size(); ++idx)
{
nodes[idx]->setPositionX(nodes[idx]->getPositionX() + theSpacing * idx);
}
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::increaseVerticalSpacing(std::vector<Node*>& nodes, float theSpacing)
2019-11-24 23:15:56 +08:00
{
if (nodes.size() < 2)
return;
std::sort(nodes.begin(), nodes.end(), [](Node* const left, Node* const right)->bool{
return getNodeTop(left) < getNodeTop(right);
});
for (size_t idx = 1; idx < nodes.size(); ++idx)
{
nodes[idx]->setPositionY(nodes[idx]->getPositionY() - theSpacing * idx);
}
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::decreaseHorizontalSpacing(std::vector<Node*>& nodes, float theSpacing)
2019-11-24 23:15:56 +08:00
{
increaseHorizontalSpacing(nodes, -theSpacing);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::decreaseVerticalSpacing(std::vector<Node*>& nodes, float theSpacing)
2019-11-24 23:15:56 +08:00
{
increaseVerticalSpacing(nodes, -theSpacing);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::removeHorizontalSpacing(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
2020-08-26 12:46:41 +08:00
LayoutHelper::makeHorizontalSpacingEqual(nodes, 0);
2019-11-24 23:15:56 +08:00
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::removeVerticalSpacing(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
2020-08-26 12:46:41 +08:00
LayoutHelper::makeVerticalSpacingEqual(nodes, 0);
2019-11-24 23:15:56 +08:00
}
/// <summary>
/// ʹ<><CAB9>ֱ<EFBFBD><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="nodes"></param>
2020-08-26 12:46:41 +08:00
void LayoutHelper::makeVerticalSpacingEqual(const std::vector<Node*>& nodes, float theSpacing)
2019-11-24 23:15:56 +08:00
{
Node* child, *childNext;
for (size_t index = 0; index < nodes.size() - 1; ++index)
{
child = nodes[index];
childNext = nodes[index + 1];
setNodeTop(childNext, theSpacing + getNodeTop(child) + child->getContentSize().height);
}
}
2020-08-26 12:46:41 +08:00
// ----------------- LayoutHelper::VisibleRect --------------------------
cocos2d::Rect LayoutHelper::VisibleRect::s_ScreenVisibleRect;
/// x-studio: when design resolution changed, should call this func.
void LayoutHelper::VisibleRect::refresh(void)
{
auto director = Director::getInstance();
s_ScreenVisibleRect.origin = Director::getInstance()->getVisibleOrigin();
s_ScreenVisibleRect.size = Director::getInstance()->getVisibleSize();
}
void LayoutHelper::VisibleRect::lazyInit()
{
if (s_ScreenVisibleRect.size.width == 0.0f && s_ScreenVisibleRect.size.height == 0.0f)
{
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if (glview->getResolutionPolicy() == ResolutionPolicy::NO_BORDER) {
s_ScreenVisibleRect.origin = director->getVisibleOrigin();
s_ScreenVisibleRect.size = director->getVisibleSize();
}
else {
s_ScreenVisibleRect.origin = LayoutHelper::getVisibleOrigin();
s_ScreenVisibleRect.size = LayoutHelper::getVisibleSize();
}
}
}
cocos2d::Rect LayoutHelper::VisibleRect::getScreenVisibleRect()
{
lazyInit();
return cocos2d::Rect(s_ScreenVisibleRect.origin.x, s_ScreenVisibleRect.origin.y, s_ScreenVisibleRect.size.width, s_ScreenVisibleRect.size.height);
}
cocos2d::Size LayoutHelper::VisibleRect::size()
{
lazyInit();
return s_ScreenVisibleRect.size;
}
Point LayoutHelper::VisibleRect::left()
{
lazyInit();
return cocos2d::Point(s_ScreenVisibleRect.origin.x, s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height / 2);
}
Point LayoutHelper::VisibleRect::right()
{
lazyInit();
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width, s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height / 2);
}
Point LayoutHelper::VisibleRect::top()
{
lazyInit();
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width / 2, s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height);
}
Point LayoutHelper::VisibleRect::bottom()
{
lazyInit();
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width / 2, s_ScreenVisibleRect.origin.y);
}
Point LayoutHelper::VisibleRect::center()
{
lazyInit();
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width / 2, s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height / 2);
}
Point LayoutHelper::VisibleRect::leftTop()
{
lazyInit();
return cocos2d::Point(s_ScreenVisibleRect.origin.x, s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height);
}
Point LayoutHelper::VisibleRect::rightTop()
{
lazyInit();
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width, s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height);
}
Point LayoutHelper::VisibleRect::leftBottom()
{
lazyInit();
return s_ScreenVisibleRect.origin;
}
Point LayoutHelper::VisibleRect::rightBottom()
{
lazyInit();
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width, s_ScreenVisibleRect.origin.y);
}
2019-11-24 23:15:56 +08:00
/// visual screen
2020-08-26 12:46:41 +08:00
float LayoutHelper::VisibleRect::getNodeLeft(Node* pNode)
2019-11-24 23:15:56 +08:00
{
2020-08-26 12:46:41 +08:00
cocos2d::Point ptLocal(LayoutHelper::getNodeLeft(pNode), 0);
2019-11-24 23:15:56 +08:00
auto ptWorld = pNode->getParent()->convertToWorldSpace(ptLocal);
return ptWorld.x;
}
2020-08-26 12:46:41 +08:00
float LayoutHelper::VisibleRect::getNodeBottom(Node* pNode)
2019-11-24 23:15:56 +08:00
{
2020-08-26 12:46:41 +08:00
cocos2d::Point ptLocal(0, LayoutHelper::getNodeBottom(pNode));
2019-11-24 23:15:56 +08:00
auto ptWorld = pNode->getParent()->convertToWorldSpace(ptLocal);
return ptWorld.y;
}
2020-08-26 12:46:41 +08:00
float LayoutHelper::VisibleRect::getNodeRight(Node* pNode)
2019-11-24 23:15:56 +08:00
{
2020-08-26 12:46:41 +08:00
cocos2d::Point ptLocal(LayoutHelper::getNodeLeft(pNode) + pNode->getContentSize().width/* * pNode->getScaleX()*/, 0);
2019-11-24 23:15:56 +08:00
auto ptWorld = pNode->getParent()->convertToWorldSpace(ptLocal);
2020-08-26 12:46:41 +08:00
auto visibleRect = LayoutHelper::VisibleRect::getScreenVisibleRect();
return visibleRect.size.width - ptWorld.x;
2019-11-24 23:15:56 +08:00
}
2020-08-26 12:46:41 +08:00
float LayoutHelper::VisibleRect::getNodeTop(Node* pNode)
2019-11-24 23:15:56 +08:00
{
2020-08-26 12:46:41 +08:00
cocos2d::Point ptLocal(0, LayoutHelper::getNodeBottom(pNode) + pNode->getContentSize().height/* * pNode->getScaleY()*/);
2019-11-24 23:15:56 +08:00
auto ptWorld = pNode->getParent()->convertToWorldSpace(ptLocal);
2020-08-26 12:46:41 +08:00
auto visibleRect = LayoutHelper::VisibleRect::getScreenVisibleRect();
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
return visibleRect.size.height - ptWorld.y;
2019-11-24 23:15:56 +08:00
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeLeft(Node* pNode, float left)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(0, scrSize.height) - LayoutHelper::VisibleRect::leftTop();
2019-11-24 23:15:56 +08:00
Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, left)/* - delta.x*/, 0);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPositionX(ptLocal.x);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeTop(Node* pNode, float top)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(0, scrSize.height) - LayoutHelper::VisibleRect::leftTop();
2019-11-24 23:15:56 +08:00
Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(0,
adjust_coord_neg(scrSize.height, size.height, achorPoint.y, top) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPositionY(ptLocal.y);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeRight(Node* pNode, float right)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, 0) - LayoutHelper::VisibleRect::rightBottom();
2019-11-24 23:15:56 +08:00
Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord_neg(scrSize.width, size.width, achorPoint.x, right)/* - delta.x*/,
0);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPositionX(ptLocal.x);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeBottom(Node* pNode, float bottom)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, 0) - LayoutHelper::VisibleRect::rightBottom();
2019-11-24 23:15:56 +08:00
Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(0,
adjust_coord(size.height, achorPoint.y, bottom) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPositionY(ptLocal.y);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeLT(Node* pNode, const cocos2d::Point& p)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(0, scrSize.height) - LayoutHelper::VisibleRect::leftTop();
2019-11-24 23:15:56 +08:00
Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, p.x),
adjust_coord_neg(scrSize.height, size.height, achorPoint.y, p.y) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPosition(ptLocal);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeRT(Node* pNode, const cocos2d::Point& p)
2019-11-24 23:15:56 +08:00
{ // right top
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, scrSize.height) - LayoutHelper::VisibleRect::rightTop();
2019-11-24 23:15:56 +08:00
Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord_neg(scrSize.width, size.width, achorPoint.x, p.x)/* - delta.x*/,
adjust_coord_neg(scrSize.height, size.height, achorPoint.y, p.y)/* - delta.y*/);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPosition(ptLocal);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeLB(Node* pNode, const cocos2d::Point& p)
2019-11-24 23:15:56 +08:00
{ // left bottom
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(0, 0) - LayoutHelper::VisibleRect::leftBottom();
2019-11-24 23:15:56 +08:00
Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, p.x) - delta.x,
adjust_coord(size.height, achorPoint.y, p.y) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPosition(ptLocal);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeRB(Node* pNode, const cocos2d::Point& p)
2019-11-24 23:15:56 +08:00
{ // right bottom
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, 0) - LayoutHelper::VisibleRect::rightBottom();
2019-11-24 23:15:56 +08:00
cocos2d::Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord_neg(scrSize.width, size.width, achorPoint.x, p.x)/* - delta.x*/,
adjust_coord(size.height, achorPoint.y, p.y) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPosition(ptLocal);
}
/// ratio position
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeNormalizedLT(Node* pNode, const cocos2d::Point& ratio)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(0, scrSize.height) - LayoutHelper::VisibleRect::leftTop();
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
cocos2d::Size vscrSize = LayoutHelper::VisibleRect::size();
2019-11-24 23:15:56 +08:00
cocos2d::Point p = cocos2d::Vec2(vscrSize.width * ratio.x, vscrSize.height * ratio.y);
cocos2d::Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, p.x) - delta.x,
adjust_coord_neg(scrSize.height, size.height, achorPoint.y, p.y) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPosition(ptLocal);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeNormalizedRT(Node* pNode, const cocos2d::Point& ratio)
2019-11-24 23:15:56 +08:00
{ // right top
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, scrSize.height) - LayoutHelper::VisibleRect::rightTop();
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
cocos2d::Size vscrSize = LayoutHelper::VisibleRect::size();
2019-11-24 23:15:56 +08:00
cocos2d::Point p = cocos2d::Vec2(vscrSize.width * ratio.x, vscrSize.height * ratio.y);
cocos2d::Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord_neg(scrSize.width, size.width, achorPoint.x, p.x) - delta.x, adjust_coord_neg(scrSize.height, size.height, achorPoint.y, p.y) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPosition(ptLocal);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeNormalizedLB(Node* pNode, const cocos2d::Point& ratio)
2019-11-24 23:15:56 +08:00
{ // left bottom
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(0, 0) - LayoutHelper::VisibleRect::leftBottom();
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
cocos2d::Size vscrSize = LayoutHelper::VisibleRect::size();
2019-11-24 23:15:56 +08:00
cocos2d::Point p = cocos2d::Vec2(vscrSize.width * ratio.x, vscrSize.height * ratio.y);
cocos2d::Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, p.x) - delta.x, adjust_coord(size.height, achorPoint.y, p.y) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPosition(ptLocal);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeNormalizedRB(Node* pNode, const cocos2d::Point& ratio)
2019-11-24 23:15:56 +08:00
{ // right bottom
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, 0) - LayoutHelper::VisibleRect::rightBottom();
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
cocos2d::Size vscrSize = LayoutHelper::VisibleRect::size();
2019-11-24 23:15:56 +08:00
cocos2d::Point p = cocos2d::Vec2(vscrSize.width * ratio.x, vscrSize.height * ratio.y);
cocos2d::Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord_neg(scrSize.width, size.width, achorPoint.x, p.x) - delta.x, adjust_coord(size.height, achorPoint.y, p.y) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPosition(ptLocal);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeNormalizedTop(Node* pNode, const float ratioTop)
2019-11-24 23:15:56 +08:00
{ // right top
CC_ASSERT(pNode);
cocos2d::Size scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
2020-08-26 12:46:41 +08:00
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, scrSize.height) - LayoutHelper::VisibleRect::rightTop();
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
cocos2d::Size vscrSize = LayoutHelper::VisibleRect::size();
2019-11-24 23:15:56 +08:00
float top = vscrSize.width * ratioTop;
cocos2d::Size size = pNode->getContentSize() * getScale2D(pNode);
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(0, adjust_coord_neg(scrSize.height, size.height, achorPoint.y, top) - delta.y);
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPositionY(ptWorld.y);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeNormalizedPositionX(cocos2d::Node* pNode, float ratio)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
2020-08-26 12:46:41 +08:00
cocos2d::Rect visibleRect = LayoutHelper::LayoutHelper::VisibleRect::getScreenVisibleRect();
cocos2d::Point ptWorld(visibleRect.size.width * ratio + visibleRect.origin.x, 0);
2019-11-24 23:15:56 +08:00
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPositionX(ptLocal.x);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeNormalizedPositionY(cocos2d::Node* pNode, float ratio)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
2020-08-26 12:46:41 +08:00
cocos2d::Rect visibleRect = LayoutHelper::LayoutHelper::VisibleRect::getScreenVisibleRect();
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
cocos2d::Point ptWorld(0, visibleRect.size.height * ratio + visibleRect.origin.y);
2019-11-24 23:15:56 +08:00
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPositionY(ptLocal.y);
}
2020-08-26 12:46:41 +08:00
void LayoutHelper::VisibleRect::setNodeNormalizedPosition(Node* pNode, const cocos2d::Point& ratio)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
2019-11-25 01:35:26 +08:00
pNode->setIgnoreAnchorPointForPosition(false);
2019-11-24 23:15:56 +08:00
pNode->setAnchorPoint(cocos2d::Vec2(.5f, .5f));
2020-08-26 12:46:41 +08:00
cocos2d::Rect visibleRect = LayoutHelper::LayoutHelper::VisibleRect::getScreenVisibleRect();
cocos2d::Point ptWorld(visibleRect.size.width * ratio.x + visibleRect.origin.x, visibleRect.size.height * ratio.y + visibleRect.origin.y);
2019-11-24 23:15:56 +08:00
auto ptLocal = pNode->getParent()->convertToNodeSpace(ptWorld);
pNode->setPosition(ptLocal);
}