2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2014-03-11 17:13:54 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-05-22 10:49:28 +08:00
|
|
|
#include "ui/UIHelper.h"
|
|
|
|
#include "ui/UIWidget.h"
|
2014-10-09 18:28:09 +08:00
|
|
|
#include "ui/UILayoutComponent.h"
|
2015-08-07 17:27:15 +08:00
|
|
|
#include "base/CCDirector.h"
|
2016-06-30 01:24:23 +08:00
|
|
|
#include "base/ccUTF8.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-10-17 19:13:37 +08:00
|
|
|
static bool _activeLayout = true;
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
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++)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
Widget* child = dynamic_cast<Widget*>(arrayRootChildren.at(i));
|
|
|
|
if (child)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* res = seekWidgetByTag(child, tag);
|
2014-03-24 15:25:44 +08:00
|
|
|
if (res != nullptr)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
Widget* Helper::seekWidgetByName(Widget* root, std::string_view name)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (!root)
|
|
|
|
{
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-12 14:59:00 +08:00
|
|
|
if (root->getName() == name)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return root;
|
|
|
|
}
|
|
|
|
const auto& arrayRootChildren = root->getChildren();
|
|
|
|
for (auto& subWidget : arrayRootChildren)
|
|
|
|
{
|
2014-03-24 15:25:44 +08:00
|
|
|
Widget* child = dynamic_cast<Widget*>(subWidget);
|
|
|
|
if (child)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* res = seekWidgetByName(child, name);
|
2014-03-24 15:25:44 +08:00
|
|
|
if (res != nullptr)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
const auto& arrayRootChildren = root->getChildren();
|
|
|
|
for (auto& subWidget : arrayRootChildren)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
Widget* child = dynamic_cast<Widget*>(subWidget);
|
2014-03-24 15:25:44 +08:00
|
|
|
if (child)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Widget* res = seekActionWidgetByActionTag(child, tag);
|
2014-03-24 15:25:44 +08:00
|
|
|
if (res != nullptr)
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
return nullptr;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-26 23:26:34 +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)
|
2014-07-28 17:24:37 +08:00
|
|
|
{
|
2016-06-30 01:24:23 +08:00
|
|
|
std::u32string utf32;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!StringUtils::UTF8ToUTF32(str, utf32))
|
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
CCLOGERROR("Can't convert string to UTF-32: %s", str.data());
|
2014-07-28 17:24:37 +08:00
|
|
|
return "";
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (utf32.size() < start)
|
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
CCLOGERROR("'start' is out of range: %d, %s", static_cast<int32_t>(start), str.data());
|
2016-06-30 01:24:23 +08:00
|
|
|
return "";
|
2014-07-28 17:24:37 +08:00
|
|
|
}
|
2016-06-30 01:24:23 +08:00
|
|
|
std::string result;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!StringUtils::UTF32ToUTF8(utf32.substr(start, length), result))
|
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
CCLOGERROR("Can't convert internal UTF-32 string to UTF-8: %s", str.data());
|
2014-07-28 17:24:37 +08:00
|
|
|
return "";
|
|
|
|
}
|
2016-06-30 01:24:23 +08:00
|
|
|
return result;
|
2014-07-28 17:24:37 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-10-18 00:25:55 +08:00
|
|
|
void Helper::changeLayoutSystemActiveState(bool bActive)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
|
|
|
_activeLayout = bActive;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
void Helper::doLayout(cocos2d::Node* rootNode)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_activeLayout)
|
2014-10-17 19:13:37 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (auto& node : rootNode->getChildren())
|
2014-10-17 19:13:37 +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)
|
|
|
|
{
|
2014-10-17 19:13:37 +08:00
|
|
|
LayoutComponent* layoutComponent = (LayoutComponent*)com;
|
|
|
|
|
2014-12-22 11:20:03 +08:00
|
|
|
layoutComponent->refreshLayout();
|
2014-10-09 18:28:09 +08:00
|
|
|
}
|
2014-10-17 19:13:37 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
Rect Helper::restrictCapInsetRect(const cocos2d::Rect& capInsets, const Vec2& textureSize)
|
2014-12-16 16:44:04 +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;
|
2014-12-16 16:44:04 +08:00
|
|
|
float height = capInsets.size.height;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-12-16 16:44:04 +08:00
|
|
|
if (textureSize.width < width)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
x = textureSize.width / 2.0f;
|
2015-10-14 13:58:58 +08:00
|
|
|
width = textureSize.width > 0 ? 1.0f : 0.0f;
|
2014-12-16 16:44:04 +08:00
|
|
|
}
|
|
|
|
if (textureSize.height < height)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
y = textureSize.height / 2.0f;
|
2015-10-14 13:58:58 +08:00
|
|
|
height = textureSize.height > 0 ? 1.0f : 0.0f;
|
2014-12-16 16:44:04 +08:00
|
|
|
}
|
|
|
|
return Rect(x, y, width, height);
|
|
|
|
}
|
2015-08-07 17:27:15 +08:00
|
|
|
|
2015-09-02 11:04:50 +08:00
|
|
|
Rect Helper::convertBoundingBoxToScreen(Node* node)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
auto director = Director::getInstance();
|
|
|
|
auto glView = director->getOpenGLView();
|
2015-09-02 11:04:50 +08:00
|
|
|
auto frameSize = glView->getFrameSize();
|
2015-08-07 17:27:15 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
auto winSize = director->getWinSize();
|
2015-09-02 11:04:50 +08:00
|
|
|
auto leftBottom = node->convertToWorldSpace(Point::ZERO);
|
2015-08-07 17:27:15 +08:00
|
|
|
|
2015-09-02 11:04:50 +08:00
|
|
|
auto contentSize = node->getContentSize();
|
2021-12-25 10:04:45 +08:00
|
|
|
auto rightTop = node->convertToWorldSpace(Point(contentSize.width, contentSize.height));
|
2015-08-07 17:27:15 +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();
|
2015-09-02 11:04:50 +08:00
|
|
|
auto uiHeight = (rightTop.y - leftBottom.y) * glView->getScaleY();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-02 11:04:50 +08:00
|
|
|
return Rect(uiLeft, uiTop, uiWidth, uiHeight);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
} // namespace ui
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_END
|