2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ui/UIHelper.h"
|
|
|
|
#include "ui/UIWidget.h"
|
|
|
|
#include "ui/UILayoutComponent.h"
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
static bool _activeLayout = true;
|
|
|
|
|
|
|
|
Widget* Helper::seekWidgetByTag(Widget* root, int tag)
|
|
|
|
{
|
|
|
|
if (!root)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (root->getTag() == tag)
|
|
|
|
{
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
const auto& arrayRootChildren = root->getChildren();
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t length = arrayRootChildren.size();
|
|
|
|
for (ssize_t i = 0; i < length; i++)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Widget* child = dynamic_cast<Widget*>(arrayRootChildren.at(i));
|
|
|
|
if (child)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* res = seekWidgetByTag(child, tag);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (res != nullptr)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
Widget* Helper::seekWidgetByName(Widget* root, std::string_view name)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (!root)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (root->getName() == name)
|
|
|
|
{
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
const auto& arrayRootChildren = root->getChildren();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& subWidget : arrayRootChildren)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
Widget* child = dynamic_cast<Widget*>(subWidget);
|
|
|
|
if (child)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* res = seekWidgetByName(child, name);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (res != nullptr)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*temp action*/
|
|
|
|
Widget* Helper::seekActionWidgetByActionTag(Widget* root, int tag)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!root)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (root->getActionTag() == tag)
|
|
|
|
{
|
|
|
|
return root;
|
|
|
|
}
|
2019-11-23 20:27:39 +08:00
|
|
|
const auto& arrayRootChildren = root->getChildren();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& subWidget : arrayRootChildren)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
Widget* child = dynamic_cast<Widget*>(subWidget);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (child)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* res = seekActionWidgetByActionTag(child, tag);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (res != nullptr)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string Helper::getSubStringOfUTF8String(std::string_view str,
|
2021-12-25 10:04:45 +08:00
|
|
|
std::string::size_type start,
|
|
|
|
std::string::size_type length)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::u32string utf32;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!StringUtils::UTF8ToUTF32(str, utf32))
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGERROR("Can't convert string to UTF-32: %s", str.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
return "";
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (utf32.size() < start)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGERROR("'start' is out of range: %d, %s", static_cast<int32_t>(start), str.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
std::string result;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!StringUtils::UTF32ToUTF8(utf32.substr(start, length), result))
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOGERROR("Can't convert internal UTF-32 string to UTF-8: %s", str.data());
|
2019-11-23 20:27:39 +08:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Helper::changeLayoutSystemActiveState(bool bActive)
|
|
|
|
{
|
|
|
|
_activeLayout = bActive;
|
|
|
|
}
|
2022-08-08 18:02:17 +08:00
|
|
|
void Helper::doLayout(ax::Node* rootNode)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_activeLayout)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& node : rootNode->getChildren())
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto com = node->getComponent(__LAYOUT_COMPONENT_NAME);
|
|
|
|
Node* parent = node->getParent();
|
|
|
|
if (nullptr != com && nullptr != parent)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
LayoutComponent* layoutComponent = (LayoutComponent*)com;
|
|
|
|
|
|
|
|
layoutComponent->refreshLayout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
Rect Helper::restrictCapInsetRect(const ax::Rect& capInsets, const Vec2& textureSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float x = capInsets.origin.x;
|
|
|
|
float y = capInsets.origin.y;
|
|
|
|
float width = capInsets.size.width;
|
2019-11-23 20:27:39 +08:00
|
|
|
float height = capInsets.size.height;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
if (textureSize.width < width)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
x = textureSize.width / 2.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
width = textureSize.width > 0 ? 1.0f : 0.0f;
|
|
|
|
}
|
|
|
|
if (textureSize.height < height)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
y = textureSize.height / 2.0f;
|
2019-11-23 20:27:39 +08:00
|
|
|
height = textureSize.height > 0 ? 1.0f : 0.0f;
|
|
|
|
}
|
|
|
|
return Rect(x, y, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect Helper::convertBoundingBoxToScreen(Node* node)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
auto glView = director->getOpenGLView();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto frameSize = glView->getFrameSize();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto winSize = director->getWinSize();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto leftBottom = node->convertToWorldSpace(Point::ZERO);
|
|
|
|
|
|
|
|
auto contentSize = node->getContentSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto rightTop = node->convertToWorldSpace(Point(contentSize.width, contentSize.height));
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto uiLeft = frameSize.width / 2 + (leftBottom.x - winSize.width / 2) * glView->getScaleX();
|
|
|
|
auto uiTop = frameSize.height / 2 - (rightTop.y - winSize.height / 2) * glView->getScaleY();
|
|
|
|
auto uiWidth = (rightTop.x - leftBottom.x) * glView->getScaleX();
|
2019-11-23 20:27:39 +08:00
|
|
|
auto uiHeight = (rightTop.y - leftBottom.y) * glView->getScaleY();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return Rect(uiLeft, uiTop, uiWidth, uiHeight);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace ui
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|