axmol/tests/cpp-tests/Source/UITest/CocoStudioGUITest/UIImageViewTest/UIImageViewTest.cpp

314 lines
12 KiB
C++
Raw Normal View History

/****************************************************************************
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
2021-12-28 16:06:23 +08:00
2022-10-01 16:24:52 +08:00
https://axmolengine.github.io/
2021-12-28 16:06:23 +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:
2021-12-28 16:06:23 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-28 16:06:23 +08:00
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.
****************************************************************************/
2013-09-16 20:54:13 +08:00
#include "UIImageViewTest.h"
USING_NS_AX;
using namespace ax::ui;
2015-04-09 12:23:47 +08:00
UIImageViewTests::UIImageViewTests()
{
ADD_TEST_CASE(UIImageViewTest);
ADD_TEST_CASE(UIImageViewTest_Scale9);
ADD_TEST_CASE(UIImageViewTest_Scale9_State_Change);
ADD_TEST_CASE(UIImageViewTest_ContentSize);
ADD_TEST_CASE(UIImageViewFlipTest);
2015-12-09 17:15:09 +08:00
ADD_TEST_CASE(UIImageViewIssue12249Test);
}
2013-09-16 20:54:13 +08:00
// UIImageViewTest
bool UIImageViewTest::init()
{
if (UIScene::init())
{
2014-06-20 11:18:53 +08:00
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2014-04-03 10:42:30 +08:00
Text* alert = Text::create("ImageView", "fonts/Marker Felt.ttf", 30);
2013-09-16 20:54:13 +08:00
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
_uiLayer->addChild(alert);
2013-09-16 20:54:13 +08:00
// Create the imageview
ImageView* imageView = ImageView::create("cocosui/ccicon.png");
2021-12-28 16:06:23 +08:00
imageView->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
2013-12-23 15:35:35 +08:00
_uiLayer->addChild(imageView);
2021-12-28 16:06:23 +08:00
2015-11-30 16:17:13 +08:00
_image = imageView;
TTFConfig ttfConfig("fonts/arial.ttf", 15);
auto label1 = Label::createWithTTF(ttfConfig, "Print Resources");
2022-07-15 19:17:01 +08:00
auto item1 = MenuItemLabel::create(label1, AX_CALLBACK_1(UIImageViewTest::printWidgetResources, this));
2021-12-28 16:06:23 +08:00
item1->setPosition(
Vec2(VisibleRect::left().x + 60, VisibleRect::bottom().y + item1->getContentSize().height * 3));
2015-11-30 16:17:13 +08:00
auto pMenu1 = Menu::create(item1, nullptr);
pMenu1->setPosition(Vec2(0.0f, 0.0f));
2015-11-30 16:17:13 +08:00
this->addChild(pMenu1, 10);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
return true;
}
return false;
}
2022-10-18 19:17:36 +08:00
void UIImageViewTest::printWidgetResources(ax::Ref* sender)
2015-11-30 16:17:13 +08:00
{
2022-10-18 19:17:36 +08:00
ax::ResourceData textureFile = _image->getRenderFile();
2022-07-16 10:43:05 +08:00
AXLOG("textureFile Name : %s, Type: %d", textureFile.file.c_str(), textureFile.type);
2015-11-30 16:17:13 +08:00
}
2013-09-16 20:54:13 +08:00
// UIImageViewTest_Scale9
bool UIImageViewTest_Scale9::init()
{
if (UIScene::init())
{
2014-06-20 11:18:53 +08:00
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2014-04-03 10:42:30 +08:00
Text* alert = Text::create("ImageView scale9 render", "fonts/Marker Felt.ttf", 26);
2013-09-16 20:54:13 +08:00
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.125f));
_uiLayer->addChild(alert);
2013-09-16 20:54:13 +08:00
// Create the imageview
2014-04-03 10:42:30 +08:00
ImageView* imageView = ImageView::create("cocosui/buttonHighlighted.png");
2013-09-16 20:54:13 +08:00
imageView->setScale9Enabled(true);
2015-06-05 14:19:05 +08:00
imageView->setContentSize(Size(200, 115));
2021-12-28 16:06:23 +08:00
imageView->setPosition(Vec2(widgetSize.width / 2.0f - 100, widgetSize.height / 2.0f));
2015-06-05 14:19:05 +08:00
_uiLayer->addChild(imageView);
auto imageCopy = imageView->clone();
2021-12-28 16:06:23 +08:00
imageCopy->setPosition(Vec2(widgetSize.width / 2.0f + 100, widgetSize.height / 2.0f));
2015-06-05 14:19:05 +08:00
_uiLayer->addChild(imageCopy);
2021-12-28 16:06:23 +08:00
2013-09-16 20:54:13 +08:00
return true;
}
return false;
2014-03-11 17:13:54 +08:00
}
// UIImageViewTest_Scale9_State_Change
bool UIImageViewTest_Scale9_State_Change::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
Text* alert = Text::create("Click The Image", "fonts/Marker Felt.ttf", 26);
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.125f));
_uiLayer->addChild(alert);
// Create the imageview
ImageView* imageView = ImageView::create("cocosui/ccicon.png");
imageView->ignoreContentAdaptWithSize(false);
imageView->setScale9Enabled(true);
imageView->setContentSize(Size(100.0f, 100.0f));
2021-12-28 16:06:23 +08:00
imageView->setCapInsets(Rect(20.0f, 20.0f, 20.0f, 20.0f));
imageView->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
imageView->setTouchEnabled(true);
2021-12-28 16:06:23 +08:00
imageView->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type) {
if (type == Widget::TouchEventType::ENDED)
{
if (imageView->isScale9Enabled())
{
imageView->setScale9Enabled(false);
}
else
imageView->setScale9Enabled(true);
}
});
_uiLayer->addChild(imageView);
return true;
}
return false;
}
// UIImageViewTest_ContentSize
bool UIImageViewTest_ContentSize::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
Text* alert = Text::create("ImageView ContentSize Change", "fonts/Marker Felt.ttf", 26);
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.125f));
_uiLayer->addChild(alert);
2021-12-28 16:06:23 +08:00
Text* status = Text::create("child ImageView position percent", "fonts/Marker Felt.ttf", 16);
status->setColor(Color3B::RED);
2021-12-28 16:06:23 +08:00
status->setPosition(Vec2(widgetSize.width / 2, widgetSize.height / 2 + 80));
_uiLayer->addChild(status, 20);
// Create the imageview
ImageView* imageView = ImageView::create("cocosui/buttonHighlighted.png");
imageView->setScale9Enabled(true);
imageView->setContentSize(Size(200.0f, 80.0f));
2021-12-28 16:06:23 +08:00
imageView->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
ImageView* imageViewChild = ImageView::create("cocosui/buttonHighlighted.png");
imageViewChild->setScale9Enabled(true);
imageViewChild->setSizeType(Widget::SizeType::PERCENT);
imageViewChild->setPositionType(Widget::PositionType::PERCENT);
imageViewChild->setSizePercent(Vec2::ANCHOR_MIDDLE);
imageViewChild->setPositionPercent(Vec2::ANCHOR_MIDDLE);
2021-12-28 16:06:23 +08:00
imageViewChild->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
ImageView* imageViewChild2 = ImageView::create("cocosui/buttonHighlighted.png");
imageViewChild2->setScale9Enabled(true);
imageViewChild2->setSizeType(Widget::SizeType::PERCENT);
imageViewChild2->setPositionType(Widget::PositionType::PERCENT);
imageViewChild2->setSizePercent(Vec2::ANCHOR_MIDDLE);
imageViewChild2->setPositionPercent(Vec2::ANCHOR_MIDDLE);
imageViewChild->addChild(imageViewChild2);
2021-12-28 16:06:23 +08:00
imageView->addChild(imageViewChild);
2021-12-28 16:06:23 +08:00
imageView->setTouchEnabled(true);
2021-12-28 16:06:23 +08:00
imageView->addTouchEventListener([=](Ref* sender, Widget::TouchEventType type) {
if (type == Widget::TouchEventType::ENDED)
{
2022-07-16 10:43:05 +08:00
float width = AXRANDOM_0_1() * 200 + 50;
float height = AXRANDOM_0_1() * 80 + 30;
imageView->setContentSize(Size(width, height));
2021-12-28 16:06:23 +08:00
2022-07-16 10:43:05 +08:00
imageViewChild->setPositionPercent(Vec2(AXRANDOM_0_1(), AXRANDOM_0_1()));
status->setString(StringUtils::format("child ImageView position percent: %f, %f",
2021-12-28 16:06:23 +08:00
imageViewChild->getPositionPercent().x,
imageViewChild->getPositionPercent().y));
}
});
2021-12-28 16:06:23 +08:00
_uiLayer->addChild(imageView);
2021-12-28 16:06:23 +08:00
return true;
}
return false;
}
2014-11-06 09:23:58 +08:00
// UIImageViewFlipTest
bool UIImageViewFlipTest::init()
{
if (UIScene::init())
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/blocks9ss.plist");
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2014-11-06 09:23:58 +08:00
Text* alert = Text::create("ImageView flip test", "fonts/Marker Felt.ttf", 26);
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.125f));
2014-11-06 09:23:58 +08:00
_uiLayer->addChild(alert);
2021-12-28 16:06:23 +08:00
2014-11-06 09:23:58 +08:00
// Create the imageview
ImageView* imageView = ImageView::create("blocks9ss/blocks9r.png", Widget::TextureResType::PLIST);
2014-11-06 09:23:58 +08:00
imageView->setScale9Enabled(true);
imageView->setContentSize(Size(250, 115));
imageView->setFlippedX(true);
imageView->setScale(0.5);
imageView->ignoreContentAdaptWithSize(false);
2021-12-28 16:06:23 +08:00
imageView->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
2014-11-06 09:23:58 +08:00
_uiLayer->addChild(imageView);
2021-12-28 16:06:23 +08:00
2014-11-06 09:23:58 +08:00
auto toggleButton = Button::create();
toggleButton->setTitleText("Toggle FlipX");
2021-12-28 16:06:23 +08:00
toggleButton->setPosition(imageView->getPosition() +
Vec2(-50.0f, -imageView->getContentSize().height / 2 - 20));
2014-11-06 09:23:58 +08:00
this->addChild(toggleButton);
2021-12-28 16:06:23 +08:00
toggleButton->addClickEventListener([=](Ref*) { imageView->setFlippedX(!imageView->isFlippedX()); });
2014-11-06 09:23:58 +08:00
auto toggleScale9 = Button::create();
toggleScale9->setTitleText("Toggle Scale9");
2021-12-28 16:06:23 +08:00
toggleScale9->setPosition(imageView->getPosition() +
Vec2(+50.0f, -imageView->getContentSize().height / 2 - 20));
2014-11-06 09:23:58 +08:00
this->addChild(toggleScale9);
2021-12-28 16:06:23 +08:00
toggleScale9->addClickEventListener([=](Ref*) {
2014-11-06 09:23:58 +08:00
imageView->setScale9Enabled(!imageView->isScale9Enabled());
2021-12-28 16:06:23 +08:00
// after switching scale9, you must call setContentSize to keep the size not change
imageView->setContentSize(Size(250, 115));
2014-11-06 09:23:58 +08:00
});
2021-12-28 16:06:23 +08:00
2014-11-06 09:23:58 +08:00
return true;
}
return false;
}
2015-12-09 17:15:09 +08:00
// UIImageViewIssue12249Test
bool UIImageViewIssue12249Test::init()
{
if (UIScene::init())
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/blocks9ss.plist");
Size widgetSize = _widget->getContentSize();
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
Text* alert = Text::create("UIImageViewIssue12249Test", "fonts/Marker Felt.ttf", 26);
alert->setColor(Color3B(159, 168, 176));
2021-12-28 16:06:23 +08:00
alert->setPosition(
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 2.125f));
2015-12-09 17:15:09 +08:00
_uiLayer->addChild(alert);
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
// Create the imageview
ImageView* imageView = ImageView::create("blocks9ss/blocks9r.png", Widget::TextureResType::PLIST);
2015-12-09 17:15:09 +08:00
imageView->setScale9Enabled(true);
imageView->setContentSize(Size(250, imageView->getContentSize().height * 2));
imageView->setFlippedX(true);
imageView->setScale(0.5);
2021-12-28 16:06:23 +08:00
imageView->setPosition(Vec2(widgetSize.width / 2.0f - 80, widgetSize.height / 2.0f));
2015-12-09 17:15:09 +08:00
_uiLayer->addChild(imageView);
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
ImageView* imageView2 = ImageView::create();
imageView2->setScale9Enabled(true);
imageView2->loadTexture("blocks9ss/blocks9r.png", Widget::TextureResType::PLIST);
2015-12-09 17:15:09 +08:00
imageView2->setContentSize(Size(250, imageView2->getContentSize().height * 2));
imageView2->setFlippedX(true);
imageView2->setScale(0.5);
2021-12-28 16:06:23 +08:00
imageView2->setPosition(Vec2(widgetSize.width / 2.0f + 80, widgetSize.height / 2.0f));
2015-12-09 17:15:09 +08:00
_uiLayer->addChild(imageView2);
2021-12-28 16:06:23 +08:00
2015-12-09 17:15:09 +08:00
return true;
}
return false;
}