30using namespace cocos2d::ui;
32UIScale9SpriteTests::UIScale9SpriteTests()
80 Size widgetSize =
_widget->getContentSize();
82 auto moveTo = MoveBy::create(1.0f, Vec2(30.0f,0.0f));
83 auto moveBack = moveTo->reverse();
84 auto rotateBy = RotateBy::create(1.0f, 180.0f);
85 auto scaleBy = ScaleTo::create(1.0f, -2.0f);
86 auto action = Sequence::create(moveTo,moveBack, rotateBy,scaleBy, NULL);
89 Sprite *normalSprite1 = Sprite::create(
"cocosui/animationbuttonnormal.png");
90 normalSprite1->setPosition(100, 270);
91 normalSprite1->setFlippedY(
true);
94 this->addChild(normalSprite1);
95 normalSprite1->runAction((FiniteTimeAction*)action->clone());
97 ui::Scale9Sprite *normalSprite2 = ui::Scale9Sprite::create(
"cocosui/animationbuttonnormal.png");
98 normalSprite2->setPosition(120, 270);
99 normalSprite2->setScale9Enabled(
false);
100 normalSprite2->setOpacity(100);
101 normalSprite2->setContentSize(normalSprite2->getContentSize() * 2);
102 this->addChild(normalSprite2);
103 normalSprite2->setColor(Color3B::GREEN);
104 normalSprite2->runAction(action);
106 auto action2 = action->clone();
107 ui::Scale9Sprite *sp1 = ui::Scale9Sprite::create(
"cocosui/animationbuttonnormal.png");
108 sp1->setPosition(100, 160);
110 sp1->setContentSize(Size(100.0f,100.0f));
111 sp1->setColor(Color3B::GREEN);
113 sp1->runAction((FiniteTimeAction*)action2);
115 cocos2d::ui::Scale9Sprite *sp2 = ui::Scale9Sprite::create(
"cocosui/animationbuttonnormal.png");
116 sp2->setPosition(350.0f, 160.0f);
117 sp2->setPreferredSize(sp1->getContentSize() * 1.2f);
118 sp2->setColor(Color3B::GREEN);
119 sp2->setContentSize(Size(100.0f,100.0f));
122 auto action3 = action->clone();
123 sp2->runAction((FiniteTimeAction*)action3);
144 Size widgetSize =
_widget->getContentSize();
146 auto moveTo = MoveBy::create(1.0f, Vec2(30.0f,0.0f));
147 auto moveBack = moveTo->reverse();
148 auto rotateBy = RotateBy::create(1.0f, 180.0f);
149 auto fadeOut = FadeOut::create(2.0f);
150 auto scaleTo = ScaleTo::create(1.0f, 2.0f);
151 auto action = Sequence::create(moveTo,moveBack, rotateBy,fadeOut,scaleTo, NULL);
153 Sprite *normalSprite1 = Sprite::create(
"cocosui/animationbuttonnormal.png");
154 normalSprite1->setPosition(100, 270);
155 normalSprite1->setCascadeColorEnabled(
true);
156 normalSprite1->setCascadeOpacityEnabled(
true);
157 normalSprite1->setColor(Color3B::GREEN);
159 this->addChild(normalSprite1);
160 normalSprite1->runAction((FiniteTimeAction*)action->clone());
162 ui::Scale9Sprite *normalSprite2 = ui::Scale9Sprite::create(
"cocosui/animationbuttonnormal.png");
163 normalSprite2->setScale9Enabled(
false);
164 normalSprite2->setOpacity(100);
165 normalSprite1->addChild(normalSprite2);
167 auto action2 = action->clone();
168 ui::Scale9Sprite *sp1 = ui::Scale9Sprite::create(
"cocosui/animationbuttonnormal.png");
169 sp1->setPosition(200.0f, 160.0f);
171 sp1->setContentSize(Size(100.0f,100.0f));
172 sp1->setColor(Color3B::GREEN);
174 sp1->runAction((FiniteTimeAction*)action2);
176 cocos2d::ui::Scale9Sprite *sp2 = ui::Scale9Sprite::create(
"cocosui/animationbuttonnormal.png");
177 sp2->setPreferredSize(sp1->getContentSize() * 1.2f);
178 sp2->setColor(Color3B::GREEN);
179 sp2->setFlippedX(
true);
180 sp2->setContentSize(Size(100.0f,100.0f));
203 Vec2 origin = Director::getInstance()->getVisibleOrigin();
204 Size size = Director::getInstance()->getVisibleSize();
206 auto containerForSprite1 = Node::create();
207 auto sprite1 = cocos2d::ui::Scale9Sprite::create(
"Images/CyanSquare.png");
208 sprite1->setPosition(origin+Vec2(size.width/2, size.height/2) + Vec2(-80.0f, 80.0f));
209 containerForSprite1->addChild(sprite1);
210 addChild(containerForSprite1, 10);
212 auto sprite2 = ui::Scale9Sprite::create(
"Images/MagentaSquare.png");
213 sprite2->setPosition(origin+Vec2(size.width/2, size.height/2));
216 addChild(sprite2, 20);
218 auto sprite3 = ui::Scale9Sprite::create(
"Images/YellowSquare.png");
219 sprite3->setPosition(Vec2(0.0f, 0.0f));
220 sprite3->setCascadeOpacityEnabled(
false);
221 sprite2->addChild(sprite3, 1);
225 auto listener1 = EventListenerTouchOneByOne::create();
226 listener1->setSwallowTouches(
true);
228 listener1->onTouchBegan = [](Touch* touch, Event* event){
229 auto target =
static_cast<Sprite*
>(
event->getCurrentTarget());
231 Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
232 Size s = target->getContentSize();
233 Rect rect = Rect(0, 0, s.width, s.height);
235 if (rect.containsPoint(locationInNode))
237 log(
"sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
238 target->setOpacity(180);
244 listener1->onTouchMoved = [](Touch* touch, Event* event){
245 auto target =
static_cast<Sprite*
>(
event->getCurrentTarget());
246 target->setPosition(target->getPosition() + touch->getDelta());
249 listener1->onTouchEnded = [=](Touch* touch, Event* event){
250 auto target =
static_cast<ui::Scale9Sprite*
>(
event->getCurrentTarget());
251 log(
"sprite onTouchesEnded.. ");
252 target->setOpacity(255);
253 if (target == sprite2)
255 containerForSprite1->setLocalZOrder(100);
257 else if(target == sprite1)
259 containerForSprite1->setLocalZOrder(0);
263 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
264 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
265 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3);
275 auto winSize = Director::getInstance()->getWinSize();
276 float x = winSize.width / 2;
277 float y = 0 + (winSize.height / 2);
280 auto sprite = Sprite::create(
"Images/blocks9.png");
282 auto blocks = ui::Scale9Sprite::create();
284 blocks->updateWithSprite(sprite, Rect(0, 0, 96, 96),
false, Rect(0, 0, 96, 96));
286 blocks->setPosition(Vec2(x, y));
288 this->addChild(blocks);
301 auto winSize = Director::getInstance()->getWinSize();
302 float x = winSize.width / 2;
303 float y = 0 + (winSize.height / 2);
308 auto blocks = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9.png");
309 blocks->setInsetLeft(0);
310 blocks->setInsetRight(0);
311 blocks->setInsetTop(0);
312 blocks->setInsetBottom(0);
313 blocks->setPreferredSize(Size(400.0f,80.0f));
314 blocks->setPosition(Vec2(x, y));
316 this->addChild(blocks);
329 auto winSize = Director::getInstance()->getWinSize();
330 float x = winSize.width / 2;
331 float y = 0 + (winSize.height / 2);
335 auto blocks = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png");
337 blocks->setPreferredSize(Size(400.0f,80.0f));
339 blocks->setPosition(Vec2(x, y));
341 this->addChild(blocks);
352 auto winSize = Director::getInstance()->getWinSize();
353 float x = winSize.width / 2;
354 float y = 0 + (winSize.height / 2);
358 auto blocks = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9c.png");
359 blocks->setPreferredSize(Size(400.0f,80.0f));
360 blocks->setPosition(Vec2(x, y + 45));
361 this->addChild(blocks);
363 auto blocks2 = ui::Scale9Sprite::create(
"Images/blocks9c.png");
364 blocks2->setPreferredSize(Size(400.0f,80.0f));
365 blocks2->setPosition(Vec2(x, y - 45));
366 this->addChild(blocks2);
377 auto winSize = Director::getInstance()->getWinSize();
378 float x = winSize.width / 2;
379 float y = 0 + (winSize.height / 2);
383 auto blocks = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9cr.png");
384 blocks->setPreferredSize(Size(400.0f,80.0f));
385 blocks->setPosition(Vec2(x, y + 45));
386 this->addChild(blocks);
388 auto blocks2 = ui::Scale9Sprite::create(
"Images/blocks9cr.png");
389 blocks2->setPreferredSize(Size(400.0f,80.0f));
390 blocks2->setPosition(Vec2(x, y - 45));
391 this->addChild(blocks2);
402 auto winSize = Director::getInstance()->getWinSize();
403 float x = winSize.width / 2;
404 float y = 0 + (winSize.height / 2);
408 auto blocks = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9c.png");
409 blocks->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
410 blocks->setPreferredSize(Size(400.0f,80.0f));
411 blocks->setPosition(Vec2(x, y + 45));
412 this->addChild(blocks);
414 auto blocksSprite = Sprite::createWithSpriteFrameName(
"blocks9c.png");
415 blocksSprite->setScale(400/blocksSprite->getContentSize().width, 80/blocksSprite->getContentSize().height);
416 blocksSprite->setPosition(Vec2(x, y - 45));
417 this->addChild(blocksSprite);
428 auto winSize = Director::getInstance()->getWinSize();
429 float x = winSize.width / 2;
430 float y = 0 + (winSize.height / 2);
434 auto blocks = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9cr.png");
435 blocks->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
436 blocks->setPreferredSize(Size(400.0f,80.0f));
437 blocks->setPosition(Vec2(x, y + 45));
438 this->addChild(blocks);
440 auto blocksSprite = Sprite::createWithSpriteFrameName(
"blocks9cr.png");
441 blocksSprite->setScale(400/blocksSprite->getContentSize().width, 80/blocksSprite->getContentSize().height);
442 blocksSprite->setPosition(Vec2(x, y - 45));
443 this->addChild(blocksSprite);
455 auto winSize = Director::getInstance()->getWinSize();
456 float x = winSize.width / 2;
457 float y = 0 + (winSize.height / 2);
460 auto sprite_scaled = Sprite::create(
"Images/blocks9.png");
462 auto blocks_scaled = ui::Scale9Sprite::create();
463 blocks_scaled->updateWithSprite(sprite_scaled, Rect(0, 0, 96, 96),
false, Rect(0, 0, 96, 96));
465 blocks_scaled->setPosition(Vec2(x, y));
467 blocks_scaled->setContentSize(Size(96 * 4, 96*2));
469 this->addChild(blocks_scaled);
478 auto winSize = Director::getInstance()->getWinSize();
479 float x = winSize.width / 2;
480 float y = 0 + (winSize.height / 2);
483 auto blocks_scaled = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9.png");
485 blocks_scaled->setPosition(Vec2(x, y));
487 blocks_scaled->setContentSize(Size(96 * 4, 96*2));
489 this->addChild(blocks_scaled);
500 auto winSize = Director::getInstance()->getWinSize();
501 float x = winSize.width / 2;
502 float y = 0 + (winSize.height / 2);
505 auto blocks_scaled = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png");
507 blocks_scaled->setPosition(Vec2(x, y));
509 blocks_scaled->setContentSize(Size(96 * 4, 96*2));
511 this->addChild(blocks_scaled);
522 auto winSize = Director::getInstance()->getWinSize();
523 float x = winSize.width / 2;
524 float y = 0 + (winSize.height / 2);
527 auto sprite_scaled_with_insets = Sprite::create(
"Images/blocks9.png");
529 auto blocks_scaled_with_insets = ui::Scale9Sprite::create();
531 blocks_scaled_with_insets->updateWithSprite(sprite_scaled_with_insets, Rect(0, 0, 96, 96),
false, Rect(32, 32, 32, 32));
533 blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5));
535 blocks_scaled_with_insets->setPosition(Vec2(x, y));
537 this->addChild(blocks_scaled_with_insets);
549 auto winSize = Director::getInstance()->getWinSize();
550 float x = winSize.width / 2;
551 float y = 0 + (winSize.height / 2);
554 auto blocks_with_insets = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9.png", Rect(32, 32, 32, 32));
556 blocks_with_insets->setPosition(Vec2(x, y));
558 this->addChild(blocks_with_insets);
569 auto winSize = Director::getInstance()->getWinSize();
570 float x = winSize.width / 2;
571 float y = 0 + (winSize.height / 2);
573 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9.png", Rect(32, 32, 32, 32));
575 blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5));
577 blocks_scaled_with_insets->setPosition(Vec2(x, y));
579 this->addChild(blocks_scaled_with_insets);
589 auto winSize = Director::getInstance()->getWinSize();
590 float x = winSize.width / 2;
591 float y = 0 + (winSize.height / 2);
593 auto blocks_with_insets = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png", Rect(32, 32, 32, 32));
595 blocks_with_insets->setPosition(Vec2(x, y));
597 this->addChild(blocks_with_insets);
608 auto winSize = Director::getInstance()->getWinSize();
609 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(
s_s9s_ui_plist);
611 float x = winSize.width / 4;
612 float y = 0 + (winSize.height / 2);
614 auto s = ui::Scale9Sprite::createWithSpriteFrameName(
"button_normal.png");
616 s->setPosition(Vec2(x, y));
618 s->setContentSize(Size(14 * 16, 10 * 16));
622 x = winSize.width * 3/4;
624 auto s2 = ui::Scale9Sprite::createWithSpriteFrameName(
"button_actived.png");
626 s2->setPosition(Vec2(x, y));
628 s2->setContentSize(Size(14 * 16, 10 * 16));
641 auto winSize = Director::getInstance()->getWinSize();
642 float x = winSize.width / 2;
643 float y = 0 + (winSize.height / 2);
645 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9.png", Rect(32, 32, 32, 32));
647 blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5));
649 blocks_scaled_with_insets->setPosition(Vec2(x, y));
651 this->addChild(blocks_scaled_with_insets);
662 auto winSize = Director::getInstance()->getWinSize();
663 float x = winSize.width / 2;
664 float y = 0 + (winSize.height / 2);
666 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png");
668 blocks_scaled_with_insets->setInsetLeft(32);
669 blocks_scaled_with_insets->setInsetRight(32);
671 blocks_scaled_with_insets->setPreferredSize(Size(32*5.5f, 32*4));
672 blocks_scaled_with_insets->setPosition(Vec2(x, y));
674 this->addChild(blocks_scaled_with_insets);
685 auto winSize = Director::getInstance()->getWinSize();
686 float x = winSize.width / 2;
687 float y = 0 + (winSize.height / 2);
688 auto rgba = Layer::create();
689 rgba->setCascadeColorEnabled(
true);
690 rgba->setCascadeOpacityEnabled(
true);
691 this->addChild(rgba);
694 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png");
696 blocks_scaled_with_insets->setPosition(Vec2(x, y));
698 rgba->addChild(blocks_scaled_with_insets);
699 auto actions = Sequence::create(FadeIn::create(1),
700 TintTo::create(1, 0, 255, 0),
701 TintTo::create(1, 255, 0, 255),
702 CallFunc::create([=](){
703 rgba->setCascadeColorEnabled(
false);
704 rgba->setCascadeOpacityEnabled(
false);
707 auto repeat = RepeatForever::create(actions);
708 rgba->runAction(repeat);
719 auto winSize = Director::getInstance()->getWinSize();
720 float x = winSize.width / 2;
721 float y = 0 + (winSize.height / 2);
723 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png");
725 blocks_scaled_with_insets->setPosition(Vec2(x, y));
726 this->addChild(blocks_scaled_with_insets);
728 Sprite *normalSprite = Sprite::createWithSpriteFrameName(
"blocks9r.png");
729 normalSprite->setColor(Color3B::RED);
730 blocks_scaled_with_insets->addChild(normalSprite);
732 auto topLabel = Label::createWithSystemFont(
"I Must be On the Top",
"Arial", 15);
733 topLabel->setPosition(Vec2(20.0f,20.0f));
734 blocks_scaled_with_insets->addChild(topLabel);
736 auto bottomLabel = Label::createWithSystemFont(
"I Must be On the Bottom",
"Arial", 15);
737 bottomLabel->setPosition(Vec2(80.0f,80.0f));
738 bottomLabel->setColor(Color3B::BLUE);
739 blocks_scaled_with_insets->addChild(bottomLabel,-1);
751 auto winSize = Director::getInstance()->getWinSize();
752 float x = winSize.width / 2;
753 float y = 0 + (winSize.height / 2);
755 auto statusLabel = Label::createWithSystemFont(
"Scale9Enabled",
"Arial", 10);
756 statusLabel->setPosition(Vec2(x, winSize.height - statusLabel->getContentSize().height - 40));
757 this->addChild(statusLabel);
759 auto normalSprite = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png");
761 normalSprite->setPosition(Vec2(x, y ));
762 this->addChild(normalSprite);
765 auto normalLabel = Label::createWithSystemFont(
"Normal Sprite",
"Arial",10);
766 normalLabel->setPosition(normalSprite->getPosition() + Vec2(0.0f, normalSprite->getContentSize().height/2 + 10));
767 this->addChild(normalLabel);
771 auto flipXSprite = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png");
773 flipXSprite->setPosition(Vec2(x - 120, y ));
774 flipXSprite->setScale(1.2f);
775 this->addChild(flipXSprite);
776 flipXSprite->setFlippedX(
false);
778 auto flipXLabel = Label::createWithSystemFont(
"sprite is not flipped!",
"Arial",10);
779 flipXLabel->setPosition(flipXSprite->getPosition() + Vec2(0.0f, flipXSprite->getContentSize().height/2 + 10));
780 this->addChild(flipXLabel);
783 auto flipYSprite = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png");
785 flipYSprite->setPosition(Vec2(x + 120, y));
786 this->addChild(flipYSprite);
788 flipYSprite->setScale(0.8f);
789 flipYSprite->setFlippedY(
true);
791 auto flipYLabel = Label::createWithSystemFont(
"sprite is flipped!",
"Arial",10);
792 flipYLabel->setPosition(flipYSprite->getPosition() + Vec2(0.0f, flipYSprite->getContentSize().height/2 + 10));
793 this->addChild(flipYLabel);
796 auto toggleFlipXButton = Button::create();
797 toggleFlipXButton->setTitleText(
"Toggle FlipX");
798 toggleFlipXButton->setPosition(flipXSprite->getPosition() + Vec2(0.0f, - 20 - flipXSprite->getContentSize().height/2));
799 toggleFlipXButton->addClickEventListener([=](Ref*){
800 flipXSprite->setFlippedX(! flipXSprite->isFlippedX());
801 if (flipXSprite->isFlippedX()) {
802 flipXLabel->setString(
"sprite is flipped!");
805 flipXLabel->setString(
"sprite is not flipped!");
808 this->addChild(toggleFlipXButton);
810 auto toggleFlipYButton = Button::create();
811 toggleFlipYButton->setTitleText(
"Toggle FlipY");
812 toggleFlipYButton->setPosition(flipYSprite->getPosition() + Vec2(0.0f, -20 - flipYSprite->getContentSize().height/2));
813 toggleFlipYButton->addClickEventListener([=](Ref*){
814 flipYSprite->setFlippedY(!flipYSprite->isFlippedY());
815 if (flipYSprite->isFlippedY()) {
816 flipYLabel->setString(
"sprite is flipped!");
819 flipYLabel->setString(
"sprite is not flipped!");
822 this->addChild(toggleFlipYButton);
824 auto toggleScale9Button = Button::create();
825 toggleScale9Button->setTitleText(
"Toggle Scale9");
826 toggleScale9Button->setPosition(normalSprite->getPosition() + Vec2(0.0f, -20 - normalSprite->getContentSize().height/2));
827 toggleScale9Button->addClickEventListener([=](Ref*){
828 flipXSprite->setScale9Enabled(!flipXSprite->isScale9Enabled());
829 flipYSprite->setScale9Enabled(!flipYSprite->isScale9Enabled());
830 if (flipXSprite->isScale9Enabled()) {
831 statusLabel->setString(
"Scale9Enabled");
833 statusLabel->setString(
"Scale9Disabled");
836 CCLOG(
"scaleX = %f", flipXSprite->getScaleX());
837 CCLOG(
"scaleY = %f", flipYSprite->getScale());
838 if (flipXSprite->isFlippedX()) {
841 if (flipYSprite->isFlippedY()) {
845 if (flipXSprite->isFlippedX()) {
846 flipXLabel->setString(
"sprite is flipped!");
849 flipXLabel->setString(
"sprite is not flipped!");
852 if (flipYSprite->isFlippedY()) {
853 flipYLabel->setString(
"sprite is flipped!");
856 flipYLabel->setString(
"sprite is not flipped!");
860 this->addChild(toggleScale9Button);
872 auto winSize = Director::getInstance()->getWinSize();
873 float x = winSize.width / 2;
874 float y = 0 + (winSize.height / 2 + 50);
877 auto normalSprite = ui::Scale9Sprite::createWithSpriteFrameName(
"blocks9r.png");
878 normalSprite->setPosition(Vec2(x, y ));
882 this->addChild(normalSprite);
885 Button* button1 = Button::create(
"cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
886 button1->setPosition(Vec2(winSize.width/2 - 100,winSize.height/2 - 50));
887 button1->setName(
"button2");
888 button1->setTitleText(
"Vec(0,0)");
889 button1->addTouchEventListener([=](Ref*, Widget::TouchEventType type)
891 if (type == Widget::TouchEventType::ENDED) {
892 normalSprite->setAnchorPoint(Vec2::ZERO);
893 normalSprite->setScale9Enabled(
true);
894 CCLOG(
"position = %f, %f, anchor point = %f, %f", normalSprite->getPosition().x,
895 normalSprite->getPosition().y,
896 normalSprite->getAnchorPoint().x,
897 normalSprite->getAnchorPoint().y);
898 CCLOG(
"tests:content size : width = %f, height = %f",
899 normalSprite->getContentSize().width,
900 normalSprite->getContentSize().height);
903 this->addChild(button1);
905 Button* button2 = Button::create(
"cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
906 button2->setPosition(Vec2(winSize.width/2 + 100,winSize.height/2 - 50));
907 button2->setName(
"button2");
908 button2->setTitleText(
"Vec(1,1)");
909 button2->addTouchEventListener([=](Ref*, Widget::TouchEventType type)
911 if (type == Widget::TouchEventType::ENDED) {
912 normalSprite->setAnchorPoint(Vec2::ANCHOR_TOP_RIGHT);
913 normalSprite->setScale9Enabled(
false);
914 CCLOG(
"position = %f, %f, anchor point = %f, %f", normalSprite->getPosition().x,
915 normalSprite->getPosition().y,
916 normalSprite->getAnchorPoint().x,
917 normalSprite->getAnchorPoint().y);
918 CCLOG(
"tests:content size : width = %f, height = %f",
919 normalSprite->getContentSize().width,
920 normalSprite->getContentSize().height);
924 this->addChild(button2);
935 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(
"cocosui/android9patch.plist");
937 auto winSize = Director::getInstance()->getWinSize();
938 float x = winSize.width / 2;
939 float y = 0 + (winSize.height / 2 + 50);
941 auto preferedSize = Size(99,99);
944 auto playerSprite = ui::Scale9Sprite::create(
"cocosui/player.9.png");
945 playerSprite->setPosition(x, y);
946 playerSprite->setContentSize(preferedSize);
947 auto capInsets = playerSprite->getCapInsets();
948 CCLOG(
"player sprite capInset = %g, %g %g, %g", capInsets.origin.x,
949 capInsets.origin.y, capInsets.size.width, capInsets.size.height);
950 this->addChild(playerSprite);
952 auto animationBtnSprite = ui::Scale9Sprite::createWithSpriteFrameName(
"animationbuttonpressed.png");
953 animationBtnSprite->setPosition(x-100, y-100);
954 capInsets = animationBtnSprite->getCapInsets();
955 CCLOG(
"animationBtnSprite capInset = %g, %g %g, %g", capInsets.origin.x,
956 capInsets.origin.y, capInsets.size.width, capInsets.size.height);
957 this->addChild(animationBtnSprite);
960 auto monsterSprite = ui::Scale9Sprite::createWithSpriteFrameName(
"monster.9.png");
961 monsterSprite->setPosition(x+100, y-100);
962 capInsets = monsterSprite->getCapInsets();
963 monsterSprite->setContentSize(preferedSize);
964 CCLOG(
"monsterSprite capInset = %g, %g %g, %g", capInsets.origin.x,
965 capInsets.origin.y, capInsets.size.width, capInsets.size.height);
966 this->addChild(monsterSprite);
968 auto spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(
"buttonnormal.9.png");
969 auto buttonScale9Sprite = Scale9Sprite::createWithSpriteFrame(spriteFrame);
970 buttonScale9Sprite->setContentSize(Size(150.0f,80.0f));
971 buttonScale9Sprite->setPosition(Vec2(100.0f,200.0f));
972 this->addChild(buttonScale9Sprite);
982 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(
"Images/blocks9ss.plist");
984 auto winSize = Director::getInstance()->getVisibleSize();
986 auto label = Label::createWithSystemFont(
"Click Button to Add Sprite and Slice Sprite\nThe draw call should always be 19 after adding sprites",
"Arial", 15);
987 label->setPosition(Vec2(winSize.width/2, winSize.height - 60));
988 this->addChild(label);
990 auto preferedSize = Size(150.0f,99.0f);
991 std::vector<std::string> spriteFrameNameArray = {
"blocks9.png",
"blocks9r.png"};
992 auto addSpriteButton = ui::Button::create(
"cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
993 addSpriteButton->setPosition(Vec2(winSize.width/2 - 50,winSize.height - 100));
994 addSpriteButton->setTitleText(
"Add Normal Sprite");
995 std::srand((
unsigned)time(
nullptr));
996 addSpriteButton->addClickEventListener([=](Ref*){
997 auto spriteFrameName = spriteFrameNameArray[rand()%2];
998 auto sprite = Sprite::createWithSpriteFrameName(spriteFrameName);
999 sprite->setPosition(Vec2(rand() % (
int)winSize.width + 50.0f, winSize.height/2));
1000 this->addChild(sprite);
1002 this->addChild(addSpriteButton);
1004 auto addSliceSpriteButton = ui::Button::create(
"cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
1005 addSliceSpriteButton->setPosition(Vec2(winSize.width/2 + 50,winSize.height - 100));
1006 addSliceSpriteButton->setTitleText(
"Add Slice Sprite");
1007 addSliceSpriteButton->addClickEventListener([=](Ref*){
1008 int random = rand()%2;
1009 auto spriteFrameName = spriteFrameNameArray[random];
1010 auto sprite = ui::Scale9Sprite::createWithSpriteFrameName(spriteFrameName);
1011 sprite->setPosition(Vec2(rand() % (
int)winSize.width + 50.0f, winSize.height/3));
1013 sprite->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
1015 sprite->setRenderingType(Scale9Sprite::RenderingType::SLICE);
1017 sprite->setPreferredSize(preferedSize);
1018 this->addChild(sprite);
1020 this->addChild(addSliceSpriteButton);
1032 auto winSize = Director::getInstance()->getWinSize();
1033 float x = winSize.width / 2;
1034 float y = 0 + (winSize.height / 2 - 20);
1036 auto label = Label::createWithSystemFont(
"Click Button to toggle rendering type",
"Arial", 15);
1037 label->setPosition(Vec2(winSize.width/2, winSize.height - 60));
1038 this->addChild(label);
1040 auto blocks = ui::Scale9Sprite::create(
"Images/blocks9.png");
1042 blocks->setPosition(Vec2(x, y));
1043 blocks->setPreferredSize(Size(96*2, 96));
1044 this->addChild(blocks);
1046 auto addSliceSpriteButton = ui::Button::create(
"cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
1047 addSliceSpriteButton->setPosition(Vec2(winSize.width/2,winSize.height - 100));
1048 addSliceSpriteButton->setTitleText(
"Slice Rendering");
1049 addSliceSpriteButton->addClickEventListener([=](Ref*){
1050 if (blocks->getRenderingType() == Scale9Sprite::RenderingType::SLICE) {
1051 blocks->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
1052 addSliceSpriteButton->setTitleText(
"Simple Rendering");
1054 blocks->setRenderingType(Scale9Sprite::RenderingType::SLICE);
1055 addSliceSpriteButton->setTitleText(
"Slice Rendering");
1056 blocks->setCapInsets(Rect(96/3,96/3,96/3,96/3));
1059 this->addChild(addSliceSpriteButton);
1071 auto winSize = Director::getInstance()->getWinSize();
1072 float x = winSize.width / 2;
1073 float y = 0 + (winSize.height / 2 - 20);
1075 auto label = Label::createWithSystemFont(
"The green scale9sprite is in the back.",
"Arial", 15);
1076 label->setPosition(Vec2(winSize.width/2, winSize.height - 60));
1077 this->addChild(label);
1079 auto blocks = ui::Scale9Sprite::create(
"Images/blocks9.png");
1081 blocks->setPosition(Vec2(x, y));
1082 blocks->setPreferredSize(Size(96*2, 96*1.5));
1083 blocks->setColor(Color3B::RED);
1084 blocks->setGlobalZOrder(1);
1085 this->addChild(blocks);
1088 auto blocks2 = ui::Scale9Sprite::create(
"Images/blocks9.png");
1089 blocks2->setPosition(Vec2(x, y));
1090 blocks2->setPreferredSize(Size(96*3, 96));
1091 blocks2->setGlobalZOrder(0);
1092 blocks2->setColor(Color3B::GREEN);
1093 this->addChild(blocks2);
1106 auto winSize = Director::getInstance()->getWinSize();
1107 float x = winSize.width / 2 + 50;
1108 float y = 0 + (winSize.height / 2 + 10);
1110 auto label = Label::createWithSystemFont(
"Only the yellow block intersect with the green one.",
"Arial", 15);
1111 label->setPosition(Vec2(winSize.width/2, winSize.height - 60));
1112 this->addChild(label);
1114 auto blocks = ui::Scale9Sprite::create(
"Images/blocks9.png");
1115 blocks->setScale9Enabled(
false);
1116 blocks->setPosition(Vec2(x, y));
1117 blocks->setPreferredSize(Size(96*2.0f, 96.0f));
1118 blocks->setColor(Color3B::RED);
1119 blocks->setGlobalZOrder(1);
1120 this->addChild(blocks);
1123 auto blocks2 = ui::Scale9Sprite::create(
"Images/blocks9.png");
1124 blocks2->setScale9Enabled(
false);
1125 blocks2->setPosition(Vec2(0.0f, 0.0f));
1126 blocks2->setPreferredSize(Size(96*1.5f, 96.0f));
1127 blocks2->setGlobalZOrder(0);
1128 blocks2->setColor(Color3B::GREEN);
1129 blocks->addChild(blocks2);
1131 auto blocks3 = ui::Scale9Sprite::create(
"Images/blocks9.png");
1132 blocks3->setScale9Enabled(
false);
1133 blocks3->setPosition(Vec2(0.0f, 0.0f));
1134 blocks3->setPreferredSize(Size(96.0f, 96.0f));
1135 blocks3->setGlobalZOrder(2);
1136 blocks3->setColor(Color3B::YELLOW);
1137 blocks2->addChild(blocks3);
1149 auto winSize = Director::getInstance()->getWinSize();
1150 float x = winSize.width / 2;
1151 float y = 0 + (winSize.height / 2 + 10);
1153 auto label = Label::createWithSystemFont(
"Drap slider to change opacity of the scale9Sprite",
"Arial", 15);
1154 label->setPosition(Vec2(winSize.width/2, winSize.height - 60));
1155 this->addChild(label);
1157 auto blocks = ui::Scale9Sprite::create(
"Images/blocks9.png");
1159 blocks->setPosition(Vec2(x, y));
1160 blocks->setPreferredSize(Size(96*2, 96*1.5));
1161 blocks->setOpacity(100);
1162 blocks->setState(Scale9Sprite::State::GRAY);
1163 blocks->setGlobalZOrder(1);
1164 blocks->setName(
"GrayScale9");
1165 this->addChild(blocks);
1167 Slider* slider = Slider::create();
1168 slider->loadBarTexture(
"cocosui/sliderTrack.png");
1169 slider->loadSlidBallTextures(
"cocosui/sliderThumb.png",
"cocosui/sliderThumb.png",
"");
1170 slider->loadProgressBarTexture(
"cocosui/sliderProgress.png");
1171 slider->setContentSize(Size(300, slider->getContentSize().height * 1.5f));
1172 slider->setMaxPercent(100);
1173 slider->setPercent(100 * 100.0f / 255.0);
1174 slider->setPosition(Vec2(winSize.width / 2.0f, winSize.height / 2.0f - 100));
1185 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
1187 Slider* slider =
dynamic_cast<Slider*
>(sender);
1188 int percent = slider->getPercent();
1189 int maxPercent = slider->getMaxPercent();
1190 auto scale9Sprite = (Scale9Sprite*)this->getChildByName(
"GrayScale9");
1191 scale9Sprite->setOpacity(1.0 * percent / maxPercent * 255.0);
#define ADD_TEST_CASE(__className__)
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
virtual bool init() override
void sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
virtual bool init() override
virtual bool init() override
virtual bool init() override
UIScale9SpriteHierarchialTest()
~UIScale9SpriteHierarchialTest()
virtual bool init() override
virtual bool init() override
virtual bool init() override
UIScale9SpriteTouchTest()
~UIScale9SpriteTouchTest()
cocos2d::Layer * _uiLayer
virtual bool init() override
cocos2d::ui::Layout * _widget
static const char s_s9s_ui_plist[]
static const char s_s9s_blocks9_plist[]