PuzzleSDK
UIScale9SpriteTest.cpp
浏览该文件的文档.
1/****************************************************************************
2 Copyright (c) 2013-2016 Chukong Technologies Inc.
3 Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
4
5 http://www.cocos2d-x.org
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 ****************************************************************************/
25
26#include "UIScale9SpriteTest.h"
27#include "testResource.h"
28
30using namespace cocos2d::ui;
31
32UIScale9SpriteTests::UIScale9SpriteTests()
33{
64}
65
66// UIScale9SpriteTest
68{
69
70}
71
73{
74}
75
77{
78 if (UIScene::init())
79 {
80 Size widgetSize = _widget->getContentSize();
81
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);
87
88
89 Sprite *normalSprite1 = Sprite::create("cocosui/animationbuttonnormal.png");
90 normalSprite1->setPosition(100, 270);
91 normalSprite1->setFlippedY(true);
92
93
94 this->addChild(normalSprite1);
95 normalSprite1->runAction((FiniteTimeAction*)action->clone());
96
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);
105
106 auto action2 = action->clone();
107 ui::Scale9Sprite *sp1 = ui::Scale9Sprite::create("cocosui/animationbuttonnormal.png");
108 sp1->setPosition(100, 160);
109 sp1->setScale(1.2f);
110 sp1->setContentSize(Size(100.0f,100.0f));
111 sp1->setColor(Color3B::GREEN);
112 this->addChild(sp1);
113 sp1->runAction((FiniteTimeAction*)action2);
114
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));
120
121 this->addChild(sp2);
122 auto action3 = action->clone();
123 sp2->runAction((FiniteTimeAction*)action3);
124
125 return true;
126 }
127 return false;
128}
129
130
132{
133
134}
135
137{
138}
139
141{
142 if (UIScene::init())
143 {
144 Size widgetSize = _widget->getContentSize();
145
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);
152
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);
158
159 this->addChild(normalSprite1);
160 normalSprite1->runAction((FiniteTimeAction*)action->clone());
161
162 ui::Scale9Sprite *normalSprite2 = ui::Scale9Sprite::create("cocosui/animationbuttonnormal.png");
163 normalSprite2->setScale9Enabled(false);
164 normalSprite2->setOpacity(100);
165 normalSprite1->addChild(normalSprite2);
166
167 auto action2 = action->clone();
168 ui::Scale9Sprite *sp1 = ui::Scale9Sprite::create("cocosui/animationbuttonnormal.png");
169 sp1->setPosition(200.0f, 160.0f);
170 sp1->setScale(1.2f);
171 sp1->setContentSize(Size(100.0f,100.0f));
172 sp1->setColor(Color3B::GREEN);
173 this->addChild(sp1);
174 sp1->runAction((FiniteTimeAction*)action2);
175
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));
181
182 sp1->addChild(sp2);
183
184 return true;
185 }
186 return false;
187}
188
190{
191
192}
193
195{
196}
197
199{
200 if (UIScene::init())
201 {
202
203 Vec2 origin = Director::getInstance()->getVisibleOrigin();
204 Size size = Director::getInstance()->getVisibleSize();
205
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);
211
212 auto sprite2 = ui::Scale9Sprite::create("Images/MagentaSquare.png");
213 sprite2->setPosition(origin+Vec2(size.width/2, size.height/2));
214
215
216 addChild(sprite2, 20);
217
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);
222
223
224 // Make sprite1 touchable
225 auto listener1 = EventListenerTouchOneByOne::create();
226 listener1->setSwallowTouches(true);
227
228 listener1->onTouchBegan = [](Touch* touch, Event* event){
229 auto target = static_cast<Sprite*>(event->getCurrentTarget());
230
231 Vec2 locationInNode = target->convertToNodeSpace(touch->getLocation());
232 Size s = target->getContentSize();
233 Rect rect = Rect(0, 0, s.width, s.height);
234
235 if (rect.containsPoint(locationInNode))
236 {
237 log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y);
238 target->setOpacity(180);
239 return true;
240 }
241 return false;
242 };
243
244 listener1->onTouchMoved = [](Touch* touch, Event* event){
245 auto target = static_cast<Sprite*>(event->getCurrentTarget());
246 target->setPosition(target->getPosition() + touch->getDelta());
247 };
248
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)
254 {
255 containerForSprite1->setLocalZOrder(100);
256 }
257 else if(target == sprite1)
258 {
259 containerForSprite1->setLocalZOrder(0);
260 }
261 };
262
263 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite1);
264 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite2);
265 _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1->clone(), sprite3);
266
267 return true;
268 }
269 return false;
270}
271
273{
274 if (UIScene::init()) {
275 auto winSize = Director::getInstance()->getWinSize();
276 float x = winSize.width / 2;
277 float y = 0 + (winSize.height / 2);
278
279
280 auto sprite = Sprite::create("Images/blocks9.png");
281
282 auto blocks = ui::Scale9Sprite::create();
283
284 blocks->updateWithSprite(sprite, Rect(0, 0, 96, 96), false, Rect(0, 0, 96, 96));
285
286 blocks->setPosition(Vec2(x, y));
287
288 this->addChild(blocks);
289
290
291 return true;
292 }
293
294
295 return false;
296}
297
299{
300 if (UIScene::init()) {
301 auto winSize = Director::getInstance()->getWinSize();
302 float x = winSize.width / 2;
303 float y = 0 + (winSize.height / 2);
304
305 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
306
307
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));
315
316 this->addChild(blocks);
317
318
319 return true;
320 }
321
322 return false;
323}
324
325
327{
328 if (UIScene::init()) {
329 auto winSize = Director::getInstance()->getWinSize();
330 float x = winSize.width / 2;
331 float y = 0 + (winSize.height / 2);
332
333 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
334
335 auto blocks = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
336
337 blocks->setPreferredSize(Size(400.0f,80.0f));
338
339 blocks->setPosition(Vec2(x, y));
340
341 this->addChild(blocks);
342
343 return true;
344 }
345
346 return false;
347}
348
350{
351 if (UIScene::init()) {
352 auto winSize = Director::getInstance()->getWinSize();
353 float x = winSize.width / 2;
354 float y = 0 + (winSize.height / 2);
355
356 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
357
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);
362
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);
367
368 return true;
369 }
370
371 return false;
372}
373
375{
376 if (UIScene::init()) {
377 auto winSize = Director::getInstance()->getWinSize();
378 float x = winSize.width / 2;
379 float y = 0 + (winSize.height / 2);
380
381 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
382
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);
387
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);
392
393 return true;
394 }
395
396 return false;
397}
398
400{
401 if (UIScene::init()) {
402 auto winSize = Director::getInstance()->getWinSize();
403 float x = winSize.width / 2;
404 float y = 0 + (winSize.height / 2);
405
406 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
407
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);
413
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);
418
419 return true;
420 }
421
422 return false;
423}
424
426{
427 if (UIScene::init()) {
428 auto winSize = Director::getInstance()->getWinSize();
429 float x = winSize.width / 2;
430 float y = 0 + (winSize.height / 2);
431
432 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
433
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);
439
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);
444
445 return true;
446 }
447
448 return false;
449}
450
451
453{
454 if (UIScene::init()) {
455 auto winSize = Director::getInstance()->getWinSize();
456 float x = winSize.width / 2;
457 float y = 0 + (winSize.height / 2);
458
459 // scaled without insets
460 auto sprite_scaled = Sprite::create("Images/blocks9.png");
461
462 auto blocks_scaled = ui::Scale9Sprite::create();
463 blocks_scaled->updateWithSprite(sprite_scaled, Rect(0, 0, 96, 96), false, Rect(0, 0, 96, 96));
464
465 blocks_scaled->setPosition(Vec2(x, y));
466
467 blocks_scaled->setContentSize(Size(96 * 4, 96*2));
468
469 this->addChild(blocks_scaled);
470 return true;
471 }
472 return false;
473}
474
476{
477 if (UIScene::init()) {
478 auto winSize = Director::getInstance()->getWinSize();
479 float x = winSize.width / 2;
480 float y = 0 + (winSize.height / 2);
481 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
482
483 auto blocks_scaled = ui::Scale9Sprite::createWithSpriteFrameName("blocks9.png");
484
485 blocks_scaled->setPosition(Vec2(x, y));
486
487 blocks_scaled->setContentSize(Size(96 * 4, 96*2));
488
489 this->addChild(blocks_scaled);
490 return true;
491 }
492 return false;
493}
494
496{
497 if (UIScene::init()) {
498 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
499
500 auto winSize = Director::getInstance()->getWinSize();
501 float x = winSize.width / 2;
502 float y = 0 + (winSize.height / 2);
503
504
505 auto blocks_scaled = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
506
507 blocks_scaled->setPosition(Vec2(x, y));
508
509 blocks_scaled->setContentSize(Size(96 * 4, 96*2));
510
511 this->addChild(blocks_scaled);
512
513 return true;
514 }
515 return false;
516}
517
518
520{
521 if (UIScene::init()) {
522 auto winSize = Director::getInstance()->getWinSize();
523 float x = winSize.width / 2;
524 float y = 0 + (winSize.height / 2);
525
526
527 auto sprite_scaled_with_insets = Sprite::create("Images/blocks9.png");
528
529 auto blocks_scaled_with_insets = ui::Scale9Sprite::create();
530
531 blocks_scaled_with_insets->updateWithSprite(sprite_scaled_with_insets, Rect(0, 0, 96, 96), false, Rect(32, 32, 32, 32));
532
533 blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5));
534
535 blocks_scaled_with_insets->setPosition(Vec2(x, y));
536
537 this->addChild(blocks_scaled_with_insets);
538 return true;
539 }
540 return false;
541}
542
544{
545 if (UIScene::init()) {
546 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
547
548
549 auto winSize = Director::getInstance()->getWinSize();
550 float x = winSize.width / 2;
551 float y = 0 + (winSize.height / 2);
552
553
554 auto blocks_with_insets = ui::Scale9Sprite::createWithSpriteFrameName("blocks9.png", Rect(32, 32, 32, 32));
555
556 blocks_with_insets->setPosition(Vec2(x, y));
557
558 this->addChild(blocks_with_insets);
559 return true;
560 }
561 return false;
562}
563
565{
566 if (UIScene::init()) {
567 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
568
569 auto winSize = Director::getInstance()->getWinSize();
570 float x = winSize.width / 2;
571 float y = 0 + (winSize.height / 2);
572
573 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName("blocks9.png", Rect(32, 32, 32, 32));
574
575 blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5));
576
577 blocks_scaled_with_insets->setPosition(Vec2(x, y));
578
579 this->addChild(blocks_scaled_with_insets);
580 return true;
581 }
582 return false;
583}
584
586{
587 if (UIScene::init()) {
588 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
589 auto winSize = Director::getInstance()->getWinSize();
590 float x = winSize.width / 2;
591 float y = 0 + (winSize.height / 2);
592
593 auto blocks_with_insets = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png", Rect(32, 32, 32, 32));
594
595 blocks_with_insets->setPosition(Vec2(x, y));
596
597 this->addChild(blocks_with_insets);
598 return true;
599 }
600 return false;
601}
602
604{
605 if (UIScene::init()) {
606 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
607
608 auto winSize = Director::getInstance()->getWinSize();
609 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_ui_plist);
610
611 float x = winSize.width / 4;
612 float y = 0 + (winSize.height / 2);
613
614 auto s = ui::Scale9Sprite::createWithSpriteFrameName("button_normal.png");
615
616 s->setPosition(Vec2(x, y));
617
618 s->setContentSize(Size(14 * 16, 10 * 16));
619
620 this->addChild(s);
621
622 x = winSize.width * 3/4;
623
624 auto s2 = ui::Scale9Sprite::createWithSpriteFrameName("button_actived.png");
625
626 s2->setPosition(Vec2(x, y));
627
628 s2->setContentSize(Size(14 * 16, 10 * 16));
629
630 this->addChild(s2);
631 return true;
632 }
633 return false;
634}
635
637{
638 if (UIScene::init()) {
639 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
640
641 auto winSize = Director::getInstance()->getWinSize();
642 float x = winSize.width / 2;
643 float y = 0 + (winSize.height / 2);
644
645 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName("blocks9.png", Rect(32, 32, 32, 32));
646
647 blocks_scaled_with_insets->setContentSize(Size(96 * 4.5, 96 * 2.5));
648
649 blocks_scaled_with_insets->setPosition(Vec2(x, y));
650
651 this->addChild(blocks_scaled_with_insets);
652 return true;
653 }
654 return false;
655}
656
658{
659 if (UIScene::init()) {
660 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
661
662 auto winSize = Director::getInstance()->getWinSize();
663 float x = winSize.width / 2;
664 float y = 0 + (winSize.height / 2);
665
666 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
667
668 blocks_scaled_with_insets->setInsetLeft(32);
669 blocks_scaled_with_insets->setInsetRight(32);
670
671 blocks_scaled_with_insets->setPreferredSize(Size(32*5.5f, 32*4));
672 blocks_scaled_with_insets->setPosition(Vec2(x, y));
673
674 this->addChild(blocks_scaled_with_insets);
675 return true;
676 }
677 return false;
678}
679
681{
682 if (UIScene::init()) {
683 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
684
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);
692
693
694 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
695
696 blocks_scaled_with_insets->setPosition(Vec2(x, y));
697
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);
705 }),
706 nullptr);
707 auto repeat = RepeatForever::create(actions);
708 rgba->runAction(repeat);
709 return true;
710 }
711 return false;
712}
713
715{
716 if (UIScene::init()) {
717 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
718
719 auto winSize = Director::getInstance()->getWinSize();
720 float x = winSize.width / 2;
721 float y = 0 + (winSize.height / 2);
722
723 auto blocks_scaled_with_insets = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
724
725 blocks_scaled_with_insets->setPosition(Vec2(x, y));
726 this->addChild(blocks_scaled_with_insets);
727
728 Sprite *normalSprite = Sprite::createWithSpriteFrameName("blocks9r.png");
729 normalSprite->setColor(Color3B::RED);
730 blocks_scaled_with_insets->addChild(normalSprite);
731
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);
735
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);
740
741 return true;
742 }
743 return false;
744}
745
747{
748 if (UIScene::init()) {
749 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
750
751 auto winSize = Director::getInstance()->getWinSize();
752 float x = winSize.width / 2;
753 float y = 0 + (winSize.height / 2);
754
755 auto statusLabel = Label::createWithSystemFont("Scale9Enabled", "Arial", 10);
756 statusLabel->setPosition(Vec2(x, winSize.height - statusLabel->getContentSize().height - 40));
757 this->addChild(statusLabel);
758
759 auto normalSprite = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
760
761 normalSprite->setPosition(Vec2(x, y ));
762 this->addChild(normalSprite);
763
764
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);
768
769
770
771 auto flipXSprite = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
772
773 flipXSprite->setPosition(Vec2(x - 120, y ));
774 flipXSprite->setScale(1.2f);
775 this->addChild(flipXSprite);
776 flipXSprite->setFlippedX(false);
777
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);
781
782
783 auto flipYSprite = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
784
785 flipYSprite->setPosition(Vec2(x + 120, y));
786 this->addChild(flipYSprite);
787
788 flipYSprite->setScale(0.8f);
789 flipYSprite->setFlippedY(true);
790
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);
794
795
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!");
803 }
804 else{
805 flipXLabel->setString("sprite is not flipped!");
806 }
807 });
808 this->addChild(toggleFlipXButton);
809
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!");
817 }
818 else{
819 flipYLabel->setString("sprite is not flipped!");
820 }
821 });
822 this->addChild(toggleFlipYButton);
823
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");
832 }else{
833 statusLabel->setString("Scale9Disabled");
834 }
835
836 CCLOG("scaleX = %f", flipXSprite->getScaleX());
837 CCLOG("scaleY = %f", flipYSprite->getScale());
838 if (flipXSprite->isFlippedX()) {
839 CCLOG("xxxxxxx");
840 }
841 if (flipYSprite->isFlippedY()) {
842 CCLOG("YYYYYY");
843 }
844
845 if (flipXSprite->isFlippedX()) {
846 flipXLabel->setString("sprite is flipped!");
847 }
848 else{
849 flipXLabel->setString("sprite is not flipped!");
850 }
851
852 if (flipYSprite->isFlippedY()) {
853 flipYLabel->setString("sprite is flipped!");
854 }
855 else{
856 flipYLabel->setString("sprite is not flipped!");
857 }
858
859 });
860 this->addChild(toggleScale9Button);
861
862 return true;
863 }
864 return false;
865}
866
868{
869 if (UIScene::init()) {
870 SpriteFrameCache::getInstance()->addSpriteFramesWithFile(s_s9s_blocks9_plist);
871
872 auto winSize = Director::getInstance()->getWinSize();
873 float x = winSize.width / 2;
874 float y = 0 + (winSize.height / 2 + 50);
875
876
877 auto normalSprite = ui::Scale9Sprite::createWithSpriteFrameName("blocks9r.png");
878 normalSprite->setPosition(Vec2(x, y ));
879// normalSprite->setScale9Enabled(false);
880// normalSprite->setAnchorPoint(Vec2::ANCHOR_TOP_RIGHT);
881
882 this->addChild(normalSprite);
883
884
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)
890 {
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);
901 }
902 });
903 this->addChild(button1);
904
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)
910 {
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);
921
922 }
923 });
924 this->addChild(button2);
925
926
927 return true;
928 }
929 return false;
930}
931
933{
934 if (UIScene::init()) {
935 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("cocosui/android9patch.plist");
936
937 auto winSize = Director::getInstance()->getWinSize();
938 float x = winSize.width / 2;
939 float y = 0 + (winSize.height / 2 + 50);
940
941 auto preferedSize = Size(99,99);
942
943 //9-patch sprite with filename
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);
951
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);
958
959
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);
967
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);
973
974 return true;
975 }
976 return false;
977}
978
980{
981 if (UIScene::init()) {
982 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/blocks9ss.plist");
983
984 auto winSize = Director::getInstance()->getVisibleSize();
985
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);
989
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);
1001 });
1002 this->addChild(addSpriteButton);
1003
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));
1012 if (random == 0) {
1013 sprite->setRenderingType(Scale9Sprite::RenderingType::SIMPLE);
1014 }else{
1015 sprite->setRenderingType(Scale9Sprite::RenderingType::SLICE);
1016 }
1017 sprite->setPreferredSize(preferedSize);
1018 this->addChild(sprite);
1019 });
1020 this->addChild(addSliceSpriteButton);
1021
1022
1023 return true;
1024 }
1025 return false;
1026}
1027
1029{
1030 if (UIScene::init()) {
1031
1032 auto winSize = Director::getInstance()->getWinSize();
1033 float x = winSize.width / 2;
1034 float y = 0 + (winSize.height / 2 - 20);
1035
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);
1039
1040 auto blocks = ui::Scale9Sprite::create("Images/blocks9.png");
1041
1042 blocks->setPosition(Vec2(x, y));
1043 blocks->setPreferredSize(Size(96*2, 96));
1044 this->addChild(blocks);
1045
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");
1053 }else{
1054 blocks->setRenderingType(Scale9Sprite::RenderingType::SLICE);
1055 addSliceSpriteButton->setTitleText("Slice Rendering");
1056 blocks->setCapInsets(Rect(96/3,96/3,96/3,96/3));
1057 }
1058 });
1059 this->addChild(addSliceSpriteButton);
1060
1061 return true;
1062 }
1063 return false;
1064}
1065
1066
1068{
1069 if (UIScene::init()) {
1070
1071 auto winSize = Director::getInstance()->getWinSize();
1072 float x = winSize.width / 2;
1073 float y = 0 + (winSize.height / 2 - 20);
1074
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);
1078
1079 auto blocks = ui::Scale9Sprite::create("Images/blocks9.png");
1080
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);
1086
1087
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);
1094
1095
1096 return true;
1097 }
1098 return false;
1099}
1100
1101
1103{
1104 if (UIScene::init()) {
1105
1106 auto winSize = Director::getInstance()->getWinSize();
1107 float x = winSize.width / 2 + 50;
1108 float y = 0 + (winSize.height / 2 + 10);
1109
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);
1113
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);
1121
1122
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);
1130
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);
1138
1139
1140 return true;
1141 }
1142 return false;
1143}
1144
1146{
1147 if (UIScene::init()) {
1148
1149 auto winSize = Director::getInstance()->getWinSize();
1150 float x = winSize.width / 2;
1151 float y = 0 + (winSize.height / 2 + 10);
1152
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);
1156
1157 auto blocks = ui::Scale9Sprite::create("Images/blocks9.png");
1158
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);
1166
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));
1175 slider->addEventListener(CC_CALLBACK_2(UIS9GrayStateOpacityTest::sliderEvent, this));
1176 _uiLayer->addChild(slider);
1177
1178 return true;
1179 }
1180 return false;
1181}
1182
1183void UIS9GrayStateOpacityTest::sliderEvent(cocos2d::Ref *sender, cocos2d::ui::Slider::EventType type)
1184{
1185 if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
1186 {
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);
1192 }
1193}
#define ADD_TEST_CASE(__className__)
Definition: BaseTest.h:211
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
virtual bool init() override
virtual bool init() override
cocos2d::Layer * _uiLayer
Definition: UIScene.h:44
virtual bool init() override
Definition: UIScene.cpp:46
cocos2d::ui::Layout * _widget
Definition: UIScene.h:45
static const char s_s9s_ui_plist[]
Definition: testResource.h:70
static const char s_s9s_blocks9_plist[]
Definition: testResource.h:68