Skip to content

Commit bb515dc

Browse files
committed
fix issue 12249
1 parent e37e190 commit bb515dc

File tree

9 files changed

+243
-1
lines changed

9 files changed

+243
-1
lines changed

cocos/ui/UIImageView.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ void ImageView::loadTexture(const std::string& fileName, TextureResType texType)
136136
default:
137137
break;
138138
}
139-
139+
//FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
140+
if (!_ignoreSize) {
141+
_customSize = _imageRenderer->getContentSize();
142+
}
140143
this->setupTexture();
141144
}
142145

cocos/ui/UILoadingBar.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ void LoadingBar::loadTexture(const std::string& texture,TextureResType texType)
161161
default:
162162
break;
163163
}
164+
165+
//FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
166+
if (!_ignoreSize) {
167+
_customSize = _barRenderer->getContentSize();
168+
}
164169
this->setupTexture();
165170
}
166171

@@ -203,6 +208,7 @@ void LoadingBar::setupTexture()
203208
this->updateChildrenDisplayedRGBA();
204209

205210
barRendererScaleChangedWithSize();
211+
206212
updateContentSizeWithTextureSize(_barRendererTextureSize);
207213

208214
this->updateProgressBar();

cocos/ui/UISlider.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,10 @@ void Slider::loadBarTexture(const std::string& fileName, TextureResType texType)
171171
break;
172172
}
173173
}
174+
//FIXME: https://github.com/cocos2d/cocos2d-x/issues/12249
175+
if (!_ignoreSize) {
176+
_customSize = _barRenderer->getContentSize();
177+
}
174178
this->setupBarTexture();
175179
}
176180
void Slider::loadBarTexture(SpriteFrame* spriteframe)

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

+47
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ UIImageViewTests::UIImageViewTests()
1010
ADD_TEST_CASE(UIImageViewTest_Scale9_State_Change);
1111
ADD_TEST_CASE(UIImageViewTest_ContentSize);
1212
ADD_TEST_CASE(UIImageViewFlipTest);
13+
ADD_TEST_CASE(UIImageViewIssue12249Test);
1314
}
1415

1516
// UIImageViewTest
@@ -239,3 +240,49 @@ bool UIImageViewFlipTest::init()
239240
}
240241
return false;
241242
}
243+
244+
245+
// UIImageViewIssue12249Test
246+
247+
bool UIImageViewIssue12249Test::init()
248+
{
249+
if (UIScene::init())
250+
{
251+
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/blocks9ss.plist");
252+
Size widgetSize = _widget->getContentSize();
253+
254+
Text* alert = Text::create("UIImageViewIssue12249Test", "fonts/Marker Felt.ttf", 26);
255+
alert->setColor(Color3B(159, 168, 176));
256+
alert->setPosition(Vec2(widgetSize.width / 2.0f,
257+
widgetSize.height / 2.0f - alert->getContentSize().height * 2.125f));
258+
259+
_uiLayer->addChild(alert);
260+
261+
// Create the imageview
262+
ImageView* imageView = ImageView::create("blocks9r.png", Widget::TextureResType::PLIST);
263+
imageView->setScale9Enabled(true);
264+
imageView->setContentSize(Size(250, imageView->getContentSize().height * 2));
265+
imageView->setFlippedX(true);
266+
imageView->setScale(0.5);
267+
imageView->setPosition(Vec2(widgetSize.width / 2.0f - 80,
268+
widgetSize.height / 2.0f));
269+
270+
_uiLayer->addChild(imageView);
271+
272+
ImageView* imageView2 = ImageView::create();
273+
imageView2->setScale9Enabled(true);
274+
imageView2->loadTexture("blocks9r.png", Widget::TextureResType::PLIST);
275+
imageView2->setContentSize(Size(250, imageView2->getContentSize().height * 2));
276+
imageView2->setFlippedX(true);
277+
imageView2->setScale(0.5);
278+
imageView2->setPosition(Vec2(widgetSize.width / 2.0f + 80,
279+
widgetSize.height / 2.0f));
280+
281+
_uiLayer->addChild(imageView2);
282+
283+
284+
return true;
285+
}
286+
return false;
287+
}
288+

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

+8
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,12 @@ class UIImageViewFlipTest : public UIScene
6969
virtual bool init() override;
7070
};
7171

72+
class UIImageViewIssue12249Test : public UIScene
73+
{
74+
public:
75+
CREATE_FUNC(UIImageViewIssue12249Test);
76+
77+
virtual bool init() override;
78+
};
79+
7280
#endif /* defined(__TestCpp__UIImageViewTest__) */

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILoadingBarTest/UILoadingBarTest.cpp

+91
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ UILoadingBarTests::UILoadingBarTests()
1111
ADD_TEST_CASE(UILoadingBarTest_Right_Scale9);
1212
ADD_TEST_CASE(UILoadingBarTest_Scale9_State_Change);
1313
ADD_TEST_CASE(UILoadingBarReloadTexture);
14+
ADD_TEST_CASE(UILoadingBarIssue12249);
1415
}
1516

1617
// UILoadingBarTest_Left
@@ -403,3 +404,93 @@ void UILoadingBarReloadTexture::update(float delta)
403404
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
404405
loadingBar->setPercent(_count);
405406
}
407+
408+
409+
// UILoadingBarIssue12249
410+
411+
UILoadingBarIssue12249::UILoadingBarIssue12249()
412+
: _count(0)
413+
{
414+
415+
}
416+
417+
UILoadingBarIssue12249::~UILoadingBarIssue12249()
418+
{
419+
unscheduleUpdate();
420+
}
421+
422+
bool UILoadingBarIssue12249::init()
423+
{
424+
if (UIScene::init())
425+
{
426+
scheduleUpdate();
427+
428+
Size widgetSize = _widget->getContentSize();
429+
430+
// Add the alert
431+
Text* alert = Text::create("Test LoadingBar Change Direction",
432+
"fonts/Marker Felt.ttf", 30);
433+
alert->setColor(Color3B(159, 168, 176));
434+
alert->setPosition(Vec2(widgetSize.width / 2.0f,
435+
widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
436+
_uiLayer->addChild(alert);
437+
438+
// Create the loading bar
439+
LoadingBar* loadingBar = LoadingBar::create("cocosui/sliderProgress.png");
440+
loadingBar->setScale9Enabled(true);
441+
loadingBar->setContentSize(Size(200, loadingBar->getContentSize().height * 1.5));
442+
loadingBar->setTag(0);
443+
loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
444+
widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
445+
446+
LoadingBar* loadingBarCopy = LoadingBar::create();
447+
loadingBarCopy->setScale9Enabled(true);
448+
loadingBarCopy->loadTexture("cocosui/sliderProgress.png");
449+
loadingBarCopy->setContentSize(Size(200, loadingBarCopy->getContentSize().height * 1.5));
450+
loadingBarCopy->setTag(1);
451+
loadingBarCopy->setPosition(loadingBar->getPosition()
452+
+ Vec2(0, -40));
453+
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
454+
455+
Button* button = Button::create("cocosui/animationbuttonnormal.png",
456+
"cocosui/animationbuttonpressed.png");
457+
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
458+
button->setTitleText("Click to change direction!");
459+
460+
button->addTouchEventListener([=](Ref*, Widget::TouchEventType type)
461+
{
462+
if (type == Widget::TouchEventType::ENDED)
463+
{
464+
if (loadingBar->getDirection() == LoadingBar::Direction::LEFT)
465+
{
466+
loadingBar->setDirection(LoadingBar::Direction::RIGHT);
467+
loadingBarCopy->setDirection(LoadingBar::Direction::LEFT);
468+
}
469+
else
470+
{
471+
loadingBar->setDirection(LoadingBar::Direction::LEFT);
472+
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
473+
}
474+
}
475+
});
476+
_uiLayer->addChild(loadingBar,1);
477+
_uiLayer->addChild(loadingBarCopy,2);
478+
_uiLayer->addChild(button);
479+
480+
return true;
481+
}
482+
return false;
483+
}
484+
485+
void UILoadingBarIssue12249::update(float delta)
486+
{
487+
_count++;
488+
if (_count > 100)
489+
{
490+
_count = 0;
491+
}
492+
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
493+
LoadingBar* loadingBarCopy = static_cast<LoadingBar*>(_uiLayer->getChildByTag(1));
494+
loadingBar->setPercent(_count);
495+
loadingBarCopy->setPercent(_count);
496+
}

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UILoadingBarTest/UILoadingBarTest.h

+14
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,18 @@ class UILoadingBarReloadTexture : public UIScene
113113

114114
};
115115

116+
class UILoadingBarIssue12249 : public UIScene
117+
{
118+
public:
119+
CREATE_FUNC(UILoadingBarIssue12249);
120+
121+
UILoadingBarIssue12249();
122+
~UILoadingBarIssue12249();
123+
virtual bool init() override;
124+
void update(float delta)override;
125+
126+
protected:
127+
int _count;
128+
};
129+
116130
#endif /* defined(__TestCpp__UILoadingBarTest__) */

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ UISliderTests::UISliderTests()
1111
ADD_TEST_CASE(UISliderNormalDefaultTest);
1212
ADD_TEST_CASE(UISliderDisabledDefaultTest);
1313
ADD_TEST_CASE(UISliderNewEventCallbackTest);
14+
ADD_TEST_CASE(UISliderIssue12249Test);
1415
}
1516

1617
// UISliderTest
@@ -370,3 +371,57 @@ bool UISliderNewEventCallbackTest::init()
370371
}
371372
return false;
372373
}
374+
375+
376+
// UISliderIssue12249Test
377+
378+
UISliderIssue12249Test::UISliderIssue12249Test()
379+
: _displayValueLabel(nullptr)
380+
{
381+
382+
}
383+
384+
UISliderIssue12249Test::~UISliderIssue12249Test()
385+
{
386+
}
387+
388+
bool UISliderIssue12249Test::init()
389+
{
390+
if (UIScene::init())
391+
{
392+
Size widgetSize = _widget->getContentSize();
393+
394+
// Add a label in which the slider alert will be displayed
395+
_displayValueLabel = TextBMFont::create("Move the slider thumb", "ccb/markerfelt24shadow.fnt");
396+
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1));
397+
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
398+
_uiLayer->addChild(_displayValueLabel);
399+
400+
// Create the slider
401+
Slider* slider = Slider::create();
402+
slider->setScale9Enabled(true);
403+
slider->loadBarTexture("cocosui/sliderTrack.png");
404+
slider->loadSlidBallTextures("cocosui/sliderThumb.png", "cocosui/sliderThumb.png", "");
405+
slider->loadProgressBarTexture("cocosui/sliderProgress.png");
406+
slider->setContentSize(Size(300, slider->getContentSize().height * 1.5));
407+
slider->setMaxPercent(10000);
408+
slider->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f/* + slider->getSize().height * 2.0f*/));
409+
slider->addEventListener(CC_CALLBACK_2(UISliderIssue12249Test::sliderEvent, this));
410+
_uiLayer->addChild(slider);
411+
412+
413+
return true;
414+
}
415+
return false;
416+
}
417+
418+
void UISliderIssue12249Test::sliderEvent(Ref *pSender, Slider::EventType type)
419+
{
420+
if (type == Slider::EventType::ON_PERCENTAGE_CHANGED)
421+
{
422+
Slider* slider = dynamic_cast<Slider*>(pSender);
423+
int percent = slider->getPercent();
424+
int maxPercent = slider->getMaxPercent();
425+
_displayValueLabel->setString(StringUtils::format("Percent %f", 10000.0 * percent / maxPercent));
426+
}
427+
}

tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest.h

+14
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,18 @@ class UISliderNewEventCallbackTest : public UIScene
110110
protected:
111111
cocos2d::ui::Text* _displayValueLabel;
112112
};
113+
114+
class UISliderIssue12249Test : public UIScene
115+
{
116+
public:
117+
CREATE_FUNC(UISliderIssue12249Test);
118+
119+
UISliderIssue12249Test();
120+
~UISliderIssue12249Test();
121+
virtual bool init() override;
122+
void sliderEvent(cocos2d::Ref* sender, cocos2d::ui::Slider::EventType type);
123+
124+
protected:
125+
cocos2d::ui::TextBMFont* _displayValueLabel;
126+
};
113127
#endif /* defined(__TestCpp__UISliderTest__) */

0 commit comments

Comments
 (0)