axmol/cocos/ui/LayoutHelper.cpp

1295 lines
42 KiB
C++
Raw Normal View History

2021-09-03 10:59:08 +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
2021-10-23 23:27:14 +08:00
Vec2 LayoutHelper::s_designSize;
2020-08-26 12:46:41 +08:00
float LayoutHelper::s_adjustedScale = 1.0f;
2019-11-24 23:15:56 +08:00
2021-10-23 23:27:14 +08:00
void LayoutHelper::setDesignSizeFixedEdge(const Vec2& 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
2021-09-03 10:59:08 +08:00
// Set the design resolution//分辨率的大小
2021-12-25 10:04:45 +08:00
GLView* pEGLView = Director::getInstance()->getOpenGLView();
2021-10-23 23:27:14 +08:00
const Vec2& frameSize = pEGLView->getFrameSize();
2019-11-24 23:15:56 +08:00
2021-10-23 23:27:14 +08:00
// Vec2 lsSize = lsaSize;
2019-11-24 23:15:56 +08:00
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);
2021-12-25 10:04:45 +08:00
if (scaleX < scaleY)
{
pEGLView->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::FIXED_WIDTH);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
else
{
pEGLView->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::FIXED_HEIGHT);
2019-11-24 23:15:56 +08:00
}
}
2021-10-23 23:27:14 +08:00
void LayoutHelper::setDesignSizeNoBorder(const Vec2& 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
2021-09-03 10:59:08 +08:00
// Set the design resolution//分辨率的大小
2021-12-25 10:04:45 +08:00
GLView* pEGLView = Director::getInstance()->getOpenGLView();
2021-10-23 23:27:14 +08:00
const Vec2& frameSize = pEGLView->getFrameSize();
2019-11-24 23:15:56 +08:00
2021-10-23 23:27:14 +08:00
// Vec2 lsSize = lsaSize;
2019-11-24 23:15:56 +08:00
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
2021-12-25 10:04:45 +08:00
LayoutHelper::s_adjustedScale = 0.0f; // MAX(scaleX, scaleY);
if (scaleX > scaleY)
{
2020-08-26 12:46:41 +08:00
LayoutHelper::s_adjustedScale = scaleX / (frameSize.height / LayoutHelper::s_designSize.height);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +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,
2021-12-25 10:04:45 +08:00
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,
2021-12-25 10:04:45 +08:00
(adjustedDesignSize.height - LayoutHelper::s_designSize.height) * .5f);
2019-11-24 23:15:56 +08:00
}
2021-10-23 23:27:14 +08:00
Vec2 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
2021-10-23 23:27:14 +08:00
Vec2 LayoutHelper::getNodeGroupSize(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.empty())
{
2021-10-23 23:27:14 +08:00
return Vec2::ZERO;
2019-11-24 23:15:56 +08:00
}
// group nodes locators
2021-12-25 10:04:45 +08:00
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()*/;
2019-11-24 23:15:56 +08:00
float x = 0.0f, y = 0.0f;
// float width = 0.0f, height = 0.f;
2021-12-25 10:04:45 +08:00
for (size_t index = 1; index < nodes.size(); ++index)
{
2019-11-24 23:15:56 +08:00
Node* child = nodes[index];
2021-12-25 10:04:45 +08:00
if (minX > (x = getNodeLeft(child)))
2019-11-24 23:15:56 +08:00
{
minX = x;
}
2021-12-25 10:04:45 +08:00
if (maxX < x + child->getContentSize().width /* * child->getScaleX()*/)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
maxX = x + child->getContentSize().width /* * child->getScaleX()*/;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
if (minY > (y = getNodeTop(child)))
2019-11-24 23:15:56 +08:00
{
minY = y;
}
2021-12-25 10:04:45 +08:00
if (maxY < y + child->getContentSize().height /* + child->getScaleY()*/)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
maxY = y + child->getContentSize().height /* + child->getScaleY()*/;
2019-11-24 23:15:56 +08:00
}
}
2021-12-25 10:04:45 +08:00
float groupWidth = maxX - minX;
2019-11-24 23:15:56 +08:00
float groupHeight = maxY - minY;
2021-10-23 23:27:14 +08:00
return Vec2(groupWidth, groupHeight);
2019-11-24 23:15:56 +08:00
}
/// Set nodes group size
2021-12-25 10:04:45 +08:00
void LayoutHelper::setNodeGroupSize(const std::vector<Node*>& nodes, const Vec2& newSize)
2019-11-24 23:15:56 +08:00
{
2021-10-23 23:27:14 +08:00
Vec2 groupSize = getNodeGroupScaledSize(nodes);
2019-11-24 23:15:56 +08:00
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);
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-24 23:15:56 +08:00
(*iter)->setContentSize(
2021-12-25 10:04:45 +08:00
Vec2((*iter)->getContentSize().width * scaleX, (*iter)->getContentSize().height * scaleY));
2019-11-24 23:15:56 +08:00
}
}
}
2021-10-23 23:27:14 +08:00
Vec2 LayoutHelper::getNodeGroupScaledSize(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
if (nodes.empty())
{
2021-10-23 23:27:14 +08:00
return Vec2::ZERO;
2019-11-24 23:15:56 +08:00
}
auto scale = getScale2D(nodes[0]);
// group nodes locators
2021-12-25 10:04:45 +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;
2021-12-25 10:04:45 +08:00
for (size_t index = 1; index < nodes.size(); ++index)
{
2019-11-24 23:15:56 +08:00
Node* child = nodes[index];
scale = getScale2D(child);
2021-12-25 10:04:45 +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;
}
2021-12-25 10:04:45 +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;
}
}
2021-12-25 10:04:45 +08:00
float groupWidth = maxX - minX;
2019-11-24 23:15:56 +08:00
float groupHeight = maxY - minY;
2021-10-23 23:27:14 +08:00
return Vec2(groupWidth, groupHeight);
2019-11-24 23:15:56 +08:00
}
/// Get Node group left
2021-12-25 10:04:45 +08:00
float LayoutHelper::getNodeGroupLeft(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
size_t index = 0;
2019-11-24 23:15:56 +08:00
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
2021-12-25 10:04:45 +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
2021-12-25 10:04:45 +08:00
float LayoutHelper::getNodeGroupRight(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
size_t index = 0;
2019-11-24 23:15:56 +08:00
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
2021-12-25 10:04:45 +08:00
float LayoutHelper::getNodeGroupBottom(const std::vector<Node*>& nodes)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
size_t index = 0;
2019-11-24 23:15:56 +08:00
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>
2021-09-03 10:59:08 +08:00
/// 水平居中
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
float x = 0.0f;
2019-11-24 23:15:56 +08:00
float width = 0.0f;
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin() + 1, nodes.end(), [&](Node* child) -> void {
2019-11-24 23:15:56 +08:00
if (minX > (x = getNodeLeft(child)))
{
minX = x;
}
if (maxX < x + child->getContentSize().width)
{
maxX = x + child->getContentSize().width;
}
});
#else
2021-12-25 10:04:45 +08:00
for (size_t index = 1; index < nodes.size(); ++index)
{
2019-11-24 23:15:56 +08:00
Node* child = nodes[index];
2021-12-25 10:04:45 +08:00
if (minX > (x = getNodeLeft(child)))
2019-11-24 23:15:56 +08:00
{
minX = x;
}
if (maxX < x + child->getContentSize().width)
{
maxX = x + child->getContentSize().width;
}
}
#endif
float groupWidth = maxX - minX;
2021-12-25 10:04:45 +08:00
float dist = ((nodes[0]->getParent()->getContentSize().width - groupWidth) / 2.0f) - minX;
2019-11-24 23:15:56 +08:00
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin(), nodes.end(),
[dist](Node* child) -> void { setNodeLeft(child, getNodeLeft(child) + dist); });
2019-11-24 23:15:56 +08:00
#else
2021-12-25 10:04:45 +08:00
for (size_t index = 0; index < nodes.size(); ++index)
{
2019-11-24 23:15:56 +08:00
Node* child = nodes[index];
setNodeLeft(child, getNodeLeft(child) + dist, 0);
}
#endif
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 垂直居中
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
float y = 0.0f;
2019-11-24 23:15:56 +08:00
float height = 0.0f;
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin() + 1, nodes.end(), [&](Node* child) -> void {
2019-11-24 23:15:56 +08:00
if (minY > (y = getNodeTop(child)))
{
minY = y;
}
if (maxY < y + child->getContentSize().height)
{
maxY = y + child->getContentSize().height;
}
});
#else
#endif
float groupHeight = maxY - minY;
2021-12-25 10:04:45 +08:00
float dist = ((nodes[0]->getParent()->getContentSize().height - groupHeight) / 2.0f) - minY;
2019-11-24 23:15:56 +08:00
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin(), nodes.end(),
[dist](Node* child) -> void { setNodeTop(child, getNodeTop(child) + dist); });
2019-11-24 23:15:56 +08:00
#else
#endif
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 居中
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
float x = 0.0f;
2019-11-24 23:15:56 +08:00
float width = 0.0f;
float minY = getNodeTop(nodes[0]), maxY = getNodeTop(nodes[0]) + nodes[0]->getContentSize().height;
2021-12-25 10:04:45 +08:00
float y = 0.0f;
2019-11-24 23:15:56 +08:00
float height = 0.0f;
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin() + 1, nodes.end(), [&](Node* child) -> void {
2019-11-24 23:15:56 +08:00
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;
}
});
2021-12-25 10:04:45 +08:00
float groupWidth = maxX - minX;
2019-11-24 23:15:56 +08:00
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;
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin(), nodes.end(), [distX, distY](Node* child) -> void {
2019-11-24 23:15:56 +08:00
setNodeLT(child, cocos2d::Point(getNodeLeft(child) + distX, getNodeTop(child) + distY));
});
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 左对齐
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
size_t index = 0;
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin(), nodes.end(), [minLeft](Node* child) -> void { setNodeLeft(child, minLeft); });
2019-11-24 23:15:56 +08:00
#else
#endif
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 右对齐
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
size_t index = 0;
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin(), nodes.end(),
[maxRight](Node* child) -> void { setNodeLeft(child, maxRight - child->getContentSize().width); });
2019-11-24 23:15:56 +08:00
#else
#endif
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 顶端对齐
2019-11-24 23:15:56 +08:00
/// </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)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin(), nodes.end(), [minTop](Node* child) -> void { setNodeTop(child, minTop); });
2019-11-24 23:15:56 +08:00
#else
#endif
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 底端对齐
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
size_t index = 0;
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin(), nodes.end(),
[maxBottom](Node* child) -> void { setNodeTop(child, maxBottom - child->getContentSize().height); });
2019-11-24 23:15:56 +08:00
#else
#endif
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 水平对齐
2019-11-24 23:15:56 +08:00
/// </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];
2021-12-25 10:04:45 +08:00
minCenterY = maxCenterY = getNodeBottom(child, 0.5f); // child.GetX(0.5f);
2019-11-24 23:15:56 +08:00
for (index = 1; index < nodes.size(); ++index)
{
2021-12-25 10:04:45 +08:00
child = nodes[index];
centerY = getNodeBottom(child, 0.5f); // child.GetX(0.5f);
2019-11-24 23:15:56 +08:00
if (minCenterY > centerY)
{
minCenterY = centerY;
}
if (maxCenterY < centerY)
{
maxCenterY = centerY;
}
}
2021-12-25 10:04:45 +08:00
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);
2019-11-24 23:15:56 +08:00
});
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 垂直对齐
2019-11-24 23:15:56 +08:00
/// </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];
2021-12-25 10:04:45 +08:00
minCenterX = maxCenterX = getNodeLeft(child, 0.5f); // child.GetX(0.5f);
2019-11-24 23:15:56 +08:00
for (index = 1; index < nodes.size(); ++index)
{
2021-12-25 10:04:45 +08:00
child = nodes[index];
centerX = getNodeLeft(child, 0.5f); // child.GetX(0.5f);
2019-11-24 23:15:56 +08:00
if (minCenterX > centerX)
{
minCenterX = centerX;
}
if (maxCenterX < centerX)
{
maxCenterX = centerX;
}
}
2021-12-25 10:04:45 +08:00
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);
2019-11-24 23:15:56 +08:00
});
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 中心原点对齐
2019-11-24 23:15:56 +08:00
/// </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>
2021-09-03 10:59:08 +08:00
/// 使宽度相等
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
size_t index = 0;
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin(), nodes.end(), [maxWidth](Node* child) -> void {
2021-10-23 23:27:14 +08:00
child->setContentSize(Vec2(maxWidth, child->getContentSize().height));
2019-11-24 23:15:56 +08:00
});
#else
#endif
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 使高度相等
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
size_t index = 0;
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
std::for_each(nodes.begin(), nodes.end(), [minHeight](Node* child) -> void {
2021-10-23 23:27:14 +08:00
child->setContentSize(Vec2(child->getContentSize().width, minHeight));
2019-11-24 23:15:56 +08:00
});
#else
#endif
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 使得大小相等
2019-11-24 23:15:56 +08:00
/// </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>
2021-09-03 10:59:08 +08:00
/// 使水平间距相等
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
Node *child, *childNext;
if (nodes.size() < 3)
return;
2019-11-24 23:15:56 +08:00
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
// sort by x firstly
2021-12-25 10:04:45 +08:00
std::sort(nodes.begin(), nodes.end(),
[](Node* const left, Node* const right) { return getNodeLeft(left) < getNodeLeft(right); });
2019-11-24 23:15:56 +08:00
#else
#endif
for (size_t index = 0; index < nodes.size() - 1; ++index)
{
2021-12-25 10:04:45 +08:00
child = nodes[index];
2019-11-24 23:15:56 +08:00
childNext = nodes[index + 1];
avgHSpacing += (getNodeLeft(childNext) - getNodeLeft(child) - child->getContentSize().width);
}
avgHSpacing /= (nodes.size() - 1);
2021-12-25 10:04:45 +08:00
if (avgHSpacing < 0.0f)
avgHSpacing = 0.0f;
2019-11-24 23:15:56 +08:00
2020-08-26 12:46:41 +08:00
LayoutHelper::makeHorizontalSpacingEqual(nodes, avgHSpacing);
2019-11-24 23:15:56 +08:00
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 使水平间距相等
2019-11-24 23:15:56 +08:00
/// </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
{
2021-12-25 10:04:45 +08:00
Node *child, *childNext;
2019-11-24 23:15:56 +08:00
for (size_t index = 0; index < nodes.size() - 1; ++index)
{
2021-12-25 10:04:45 +08:00
child = nodes[index];
2019-11-24 23:15:56 +08:00
childNext = nodes[index + 1];
setNodeLeft(childNext, theSpacing + getNodeLeft(child) + child->getContentSize().width);
}
}
/// <summary>
2021-09-03 10:59:08 +08:00
/// 使垂直间距相等
2019-11-24 23:15:56 +08:00
/// </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;
2021-12-25 10:04:45 +08:00
Node *child, *childNext;
if (nodes.size() < 3)
return;
2019-11-24 23:15:56 +08:00
#if (defined(_WIN32) && _MSC_VER >= 1700) || !defined(_WIN32)
// sort by y firstly
2021-12-25 10:04:45 +08:00
std::sort(nodes.begin(), nodes.end(),
[](Node* const left, Node* const right) -> bool { return getNodeTop(left) < getNodeTop(right); });
2019-11-24 23:15:56 +08:00
#else
#endif
for (size_t index = 0; index < nodes.size() - 1; ++index)
{
2021-12-25 10:04:45 +08:00
child = nodes[index];
2019-11-24 23:15:56 +08:00
childNext = nodes[index + 1];
avgVSpacing += (getNodeTop(childNext) - getNodeTop(child) - child->getContentSize().height);
}
avgVSpacing /= (nodes.size() - 1);
2021-12-25 10:04:45 +08:00
if (avgVSpacing < 0.0f)
avgVSpacing = 0.0f;
2019-11-24 23:15:56 +08:00
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;
2021-12-25 10:04:45 +08:00
std::sort(nodes.begin(), nodes.end(),
[](Node* const left, Node* const right) -> bool { return getNodeLeft(left) < getNodeLeft(right); });
2019-11-24 23:15:56 +08:00
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;
2021-12-25 10:04:45 +08:00
std::sort(nodes.begin(), nodes.end(),
[](Node* const left, Node* const right) -> bool { return getNodeTop(left) < getNodeTop(right); });
2019-11-24 23:15:56 +08:00
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
}
2021-12-25 10:04:45 +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>
2021-09-03 10:59:08 +08:00
/// 使垂直间距相等
2019-11-24 23:15:56 +08:00
/// </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
{
2021-12-25 10:04:45 +08:00
Node *child, *childNext;
2019-11-24 23:15:56 +08:00
for (size_t index = 0; index < nodes.size() - 1; ++index)
{
2021-12-25 10:04:45 +08:00
child = nodes[index];
2019-11-24 23:15:56 +08:00
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)
{
2021-12-25 10:04:45 +08:00
auto director = Director::getInstance();
2020-08-26 12:46:41 +08:00
s_ScreenVisibleRect.origin = Director::getInstance()->getVisibleOrigin();
2021-12-25 10:04:45 +08:00
s_ScreenVisibleRect.size = Director::getInstance()->getVisibleSize();
2020-08-26 12:46:41 +08:00
}
void LayoutHelper::VisibleRect::lazyInit()
{
if (s_ScreenVisibleRect.size.width == 0.0f && s_ScreenVisibleRect.size.height == 0.0f)
{
auto director = Director::getInstance();
2021-12-25 10:04:45 +08:00
auto glview = director->getOpenGLView();
2020-08-26 12:46:41 +08:00
2021-12-25 10:04:45 +08:00
if (glview->getResolutionPolicy() == ResolutionPolicy::NO_BORDER)
{
2020-08-26 12:46:41 +08:00
s_ScreenVisibleRect.origin = director->getVisibleOrigin();
2021-12-25 10:04:45 +08:00
s_ScreenVisibleRect.size = director->getVisibleSize();
2020-08-26 12:46:41 +08:00
}
2021-12-25 10:04:45 +08:00
else
{
2020-08-26 12:46:41 +08:00
s_ScreenVisibleRect.origin = LayoutHelper::getVisibleOrigin();
2021-12-25 10:04:45 +08:00
s_ScreenVisibleRect.size = LayoutHelper::getVisibleSize();
2020-08-26 12:46:41 +08:00
}
}
}
cocos2d::Rect LayoutHelper::VisibleRect::getScreenVisibleRect()
{
lazyInit();
2021-12-25 10:04:45 +08:00
return cocos2d::Rect(s_ScreenVisibleRect.origin.x, s_ScreenVisibleRect.origin.y, s_ScreenVisibleRect.size.width,
s_ScreenVisibleRect.size.height);
2020-08-26 12:46:41 +08:00
}
2021-10-23 23:27:14 +08:00
Vec2 LayoutHelper::VisibleRect::size()
2020-08-26 12:46:41 +08:00
{
lazyInit();
return s_ScreenVisibleRect.size;
}
Point LayoutHelper::VisibleRect::left()
{
lazyInit();
2021-12-25 10:04:45 +08:00
return cocos2d::Point(s_ScreenVisibleRect.origin.x,
s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height / 2);
2020-08-26 12:46:41 +08:00
}
Point LayoutHelper::VisibleRect::right()
{
lazyInit();
2021-12-25 10:04:45 +08:00
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width,
s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height / 2);
2020-08-26 12:46:41 +08:00
}
Point LayoutHelper::VisibleRect::top()
{
lazyInit();
2021-12-25 10:04:45 +08:00
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width / 2,
s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height);
2020-08-26 12:46:41 +08:00
}
Point LayoutHelper::VisibleRect::bottom()
{
lazyInit();
2021-12-25 10:04:45 +08:00
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width / 2,
s_ScreenVisibleRect.origin.y);
2020-08-26 12:46:41 +08:00
}
Point LayoutHelper::VisibleRect::center()
{
lazyInit();
2021-12-25 10:04:45 +08:00
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width / 2,
s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height / 2);
2020-08-26 12:46:41 +08:00
}
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();
2021-12-25 10:04:45 +08:00
return cocos2d::Point(s_ScreenVisibleRect.origin.x + s_ScreenVisibleRect.size.width,
s_ScreenVisibleRect.origin.y + s_ScreenVisibleRect.size.height);
2020-08-26 12:46:41 +08:00
}
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
{
2021-12-25 10:04:45 +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
{
2021-12-25 10:04:45 +08:00
cocos2d::Point ptLocal(
0, LayoutHelper::getNodeBottom(pNode) + pNode->getContentSize().height /* * pNode->getScaleY()*/);
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);
2021-12-25 10:04:45 +08:00
Vec2 scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
cocos2d::Point delta = cocos2d::Vec2(0, scrSize.height) - LayoutHelper::VisibleRect::leftTop();
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
2021-12-25 10:04:45 +08:00
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, left) /* - delta.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::setNodeTop(Node* pNode, float top)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
cocos2d::Point delta = cocos2d::Vec2(0, scrSize.height) - LayoutHelper::VisibleRect::leftTop();
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
2021-12-25 10:04:45 +08:00
cocos2d::Point ptWorld(0, adjust_coord_neg(scrSize.height, size.height, achorPoint.y, top) - delta.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::setNodeRight(Node* pNode, float right)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, 0) - LayoutHelper::VisibleRect::rightBottom();
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
2021-12-25 10:04:45 +08:00
cocos2d::Point ptWorld(adjust_coord_neg(scrSize.width, size.width, achorPoint.x, right) /* - delta.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::setNodeBottom(Node* pNode, float bottom)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, 0) - LayoutHelper::VisibleRect::rightBottom();
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
2021-12-25 10:04:45 +08:00
cocos2d::Point ptWorld(0, adjust_coord(size.height, achorPoint.y, bottom) - delta.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::setNodeLT(Node* pNode, const cocos2d::Point& p)
2019-11-24 23:15:56 +08:00
{
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
cocos2d::Point delta = cocos2d::Vec2(0, scrSize.height) - LayoutHelper::VisibleRect::leftTop();
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, p.x),
2021-12-25 10:04:45 +08:00
adjust_coord_neg(scrSize.height, size.height, achorPoint.y, p.y) - delta.y);
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
{ // right top
2019-11-24 23:15:56 +08:00
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, scrSize.height) - LayoutHelper::VisibleRect::rightTop();
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
2021-12-25 10:04:45 +08:00
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*/);
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
{ // left bottom
2019-11-24 23:15:56 +08:00
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
cocos2d::Point delta = cocos2d::Vec2(0, 0) - LayoutHelper::VisibleRect::leftBottom();
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, p.x) - delta.x,
2021-12-25 10:04:45 +08:00
adjust_coord(size.height, achorPoint.y, p.y) - delta.y);
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
{ // right bottom
2019-11-24 23:15:56 +08:00
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 scrSize = Director::getInstance()->getOpenGLView()->getDesignResolutionSize();
cocos2d::Point delta = cocos2d::Vec2(scrSize.width, 0) - LayoutHelper::VisibleRect::rightBottom();
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
2021-12-25 10:04:45 +08:00
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);
2019-11-24 23:15:56 +08:00
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);
2021-12-25 10:04:45 +08:00
Vec2 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
2021-12-25 10:04:45 +08:00
Vec2 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);
2021-12-25 10:04:45 +08:00
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, p.x) - delta.x,
2021-12-25 10:04:45 +08:00
adjust_coord_neg(scrSize.height, size.height, achorPoint.y, p.y) - delta.y);
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
{ // right top
2019-11-24 23:15:56 +08:00
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 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
2021-12-25 10:04:45 +08:00
Vec2 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);
2021-12-25 10:04:45 +08:00
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
2021-12-25 10:04:45 +08:00
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);
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
{ // left bottom
2019-11-24 23:15:56 +08:00
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 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
2021-12-25 10:04:45 +08:00
Vec2 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);
2021-12-25 10:04:45 +08:00
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
2021-12-25 10:04:45 +08:00
cocos2d::Point ptWorld(adjust_coord(size.width, achorPoint.x, p.x) - delta.x,
adjust_coord(size.height, achorPoint.y, p.y) - delta.y);
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
{ // right bottom
2019-11-24 23:15:56 +08:00
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 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
2021-12-25 10:04:45 +08:00
Vec2 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);
2021-12-25 10:04:45 +08:00
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
cocos2d::Point achorPoint = Vec2::ZERO;
if (!pNode->isIgnoreAnchorPointForPosition())
{
achorPoint = pNode->getAnchorPoint();
}
2021-12-25 10:04:45 +08:00
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);
2019-11-24 23:15:56 +08:00
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)
2021-12-25 10:04:45 +08:00
{ // right top
2019-11-24 23:15:56 +08:00
CC_ASSERT(pNode);
2021-12-25 10:04:45 +08:00
Vec2 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
2021-10-23 23:27:14 +08:00
Vec2 vscrSize = LayoutHelper::VisibleRect::size();
2021-12-25 10:04:45 +08:00
float top = vscrSize.width * ratioTop;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
Vec2 size = pNode->getContentSize() * getScale2D(pNode);
2019-11-24 23:15:56 +08:00
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();
2021-12-25 10:04:45 +08:00
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);
}