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

314 lines
12 KiB
C++
Raw Normal View History

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