Merge pull request #9028 from andyque/fixWidgetFlipIssue

Fix widget flip issue
This commit is contained in:
minggo 2014-11-24 15:19:50 +08:00
commit b8bb61c7c6
17 changed files with 233 additions and 159 deletions

View File

@ -236,8 +236,7 @@ void Button::loadTextureNormal(const std::string& normal,TextureResType texType)
}
_normalTextureSize = _buttonNormalRenderer->getContentSize();
updateFlippedX();
updateFlippedY();
this->updateChildrenDisplayedRGBA();
if (_unifySize )
@ -277,9 +276,7 @@ void Button::loadTexturePressed(const std::string& selected,TextureResType texTy
}
_pressedTextureSize = _buttonClickedRenderer->getContentSize();
//TODO: mark as dirty
updateFlippedX();
updateFlippedY();
this->updateChildrenDisplayedRGBA();
_pressedTextureLoaded = true;
@ -308,8 +305,7 @@ void Button::loadTextureDisabled(const std::string& disabled,TextureResType texT
}
_disabledTextureSize = _buttonDisableRenderer->getContentSize();
updateFlippedX();
updateFlippedY();
this->updateChildrenDisplayedRGBA();
_disabledTextureLoaded = true;
@ -540,25 +536,6 @@ void Button::onPressStateChangedToDisabled()
_buttonClickedRenderer->setScale(_pressedTextureScaleXInSize, _pressedTextureScaleYInSize);
}
void Button::updateFlippedX()
{
float flip = _flippedX ? -1.0f : 1.0f;
_titleRenderer->setScaleX(flip);
_buttonNormalRenderer->setFlippedX(_flippedX);
_buttonClickedRenderer->setFlippedX(_flippedX);
_buttonDisableRenderer->setFlippedX(_flippedX);
}
void Button::updateFlippedY()
{
float flip = _flippedY ? -1.0f : 1.0f;
_titleRenderer->setScaleY(flip);
_buttonNormalRenderer->setFlippedY(_flippedY);
_buttonClickedRenderer->setFlippedY(_flippedY);
_buttonDisableRenderer->setFlippedY(_flippedY);
}
void Button::updateTitleLocation()
{
_titleRenderer->setPosition(_contentSize.width * 0.5f, _contentSize.height * 0.5f);

View File

@ -220,9 +220,6 @@ protected:
virtual void onPressStateChangedToDisabled() override;
virtual void onSizeChanged() override;
virtual void updateFlippedX() override;
virtual void updateFlippedY() override;
void normalTextureScaleChangedWithSize();
void pressedTextureScaleChangedWithSize();
void disabledTextureScaleChangedWithSize();

View File

@ -182,8 +182,7 @@ void CheckBox::loadTextureBackGround(const std::string& backGround,TextureResTyp
default:
break;
}
updateFlippedX();
updateFlippedY();
this->updateChildrenDisplayedRGBA();
updateContentSizeWithTextureSize(_backGroundBoxRenderer->getContentSize());
@ -209,8 +208,7 @@ void CheckBox::loadTextureBackGroundSelected(const std::string& backGroundSelect
default:
break;
}
updateFlippedX();
updateFlippedY();
this->updateChildrenDisplayedRGBA();
_backGroundSelectedBoxRendererAdaptDirty = true;
@ -235,8 +233,7 @@ void CheckBox::loadTextureFrontCross(const std::string& cross,TextureResType tex
default:
break;
}
updateFlippedX();
updateFlippedY();
this->updateChildrenDisplayedRGBA();
_frontCrossRendererAdaptDirty = true;
@ -261,8 +258,7 @@ void CheckBox::loadTextureBackGroundDisabled(const std::string& backGroundDisabl
default:
break;
}
updateFlippedX();
updateFlippedY();
this->updateChildrenDisplayedRGBA();
_backGroundBoxDisabledRendererAdaptDirty = true;
@ -287,8 +283,7 @@ void CheckBox::loadTextureFrontCrossDisabled(const std::string& frontCrossDisabl
default:
break;
}
updateFlippedX();
updateFlippedY();
this->updateChildrenDisplayedRGBA();
_frontCrossDisabledRendererAdaptDirty = true;
@ -397,24 +392,6 @@ void CheckBox::addEventListener(const ccCheckBoxCallback& callback)
_checkBoxEventCallback = callback;
}
void CheckBox::updateFlippedX()
{
_backGroundBoxRenderer->setFlippedX(_flippedX);
_backGroundSelectedBoxRenderer->setFlippedX(_flippedX);
_frontCrossRenderer->setFlippedX(_flippedX);
_backGroundBoxDisabledRenderer->setFlippedX(_flippedX);
_frontCrossDisabledRenderer->setFlippedX(_flippedX);
}
void CheckBox::updateFlippedY()
{
_backGroundBoxRenderer->setFlippedY(_flippedY);
_backGroundSelectedBoxRenderer->setFlippedY(_flippedY);
_frontCrossRenderer->setFlippedY(_flippedY);
_backGroundBoxDisabledRenderer->setFlippedY(_flippedY);
_frontCrossDisabledRenderer->setFlippedY(_flippedY);
}
void CheckBox::onSizeChanged()
{
Widget::onSizeChanged();

View File

@ -216,9 +216,6 @@ protected:
virtual void onSizeChanged() override;
virtual void updateFlippedX() override;
virtual void updateFlippedY() override;
void backGroundTextureScaleChangedWithSize();
void backGroundSelectedTextureScaleChangedWithSize();
void frontCrossTextureScaleChangedWithSize();

View File

@ -131,8 +131,7 @@ void ImageView::loadTexture(const std::string& fileName, TextureResType texType)
}
_imageTextureSize = _imageRenderer->getContentSize();
updateFlippedX();
updateFlippedY();
this->updateChildrenDisplayedRGBA();
updateContentSizeWithTextureSize(_imageTextureSize);
@ -159,17 +158,6 @@ void ImageView::setTextureRect(const Rect &rect)
}
}
void ImageView::updateFlippedX()
{
_imageRenderer->setFlippedX(_flippedX);
}
void ImageView::updateFlippedY()
{
_imageRenderer->setFlippedY(_flippedY);
}
void ImageView::setScale9Enabled(bool able)
{
if (_scale9Enabled == able)

View File

@ -120,9 +120,6 @@ protected:
virtual void initRenderer() override;
virtual void onSizeChanged() override;
virtual void updateFlippedX() override;
virtual void updateFlippedY() override;
virtual void adaptRenderers() override;
void imageTextureScaleChangedWithSize();

View File

@ -257,30 +257,6 @@ void Text::onPressStateChangedToDisabled()
}
void Text::updateFlippedX()
{
if (_flippedX)
{
_labelRenderer->setScaleX(-1.0f);
}
else
{
_labelRenderer->setScaleX(1.0f);
}
}
void Text::updateFlippedY()
{
if (_flippedY)
{
_labelRenderer->setScaleY(-1.0f);
}
else
{
_labelRenderer->setScaleY(1.0f);
}
}
void Text::onSizeChanged()
{
Widget::onSizeChanged();

View File

@ -197,8 +197,6 @@ protected:
virtual void onPressStateChangedToDisabled() override;
virtual void onSizeChanged() override;
virtual void updateFlippedX() override;
virtual void updateFlippedY() override;
void labelScaleChangedWithSize();
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;

View File

@ -1135,17 +1135,77 @@ void Widget::copyProperties(Widget *widget)
}
}
void Widget::setFlippedX(bool flippedX)
{
_flippedX = flippedX;
updateFlippedX();
}
void Widget::setFlippedX(bool flippedX)
{
void Widget::setFlippedY(bool flippedY)
{
float realScale = this->getScaleX();
_flippedX = flippedX;
this->setScaleX(realScale);
}
void Widget::setFlippedY(bool flippedY)
{
float realScale = this->getScaleY();
_flippedY = flippedY;
updateFlippedY();
}
this->setScaleY(realScale);
}
void Widget::setScaleX(float scaleX)
{
if (_flippedX) {
scaleX = scaleX * -1;
}
Node::setScaleX(scaleX);
}
void Widget::setScaleY(float scaleY)
{
if (_flippedY) {
scaleY = scaleY * -1;
}
Node::setScaleY(scaleY);
}
void Widget::setScale(float scale)
{
this->setScaleX(scale);
this->setScaleY(scale);
this->setScaleZ(scale);
}
void Widget::setScale(float scaleX, float scaleY)
{
this->setScaleX(scaleX);
this->setScaleY(scaleY);
}
float Widget::getScaleX()const
{
float originalScale = Node::getScaleX();
if (_flippedX)
{
originalScale = originalScale * -1.0;
}
return originalScale;
}
float Widget::getScaleY()const
{
float originalScale = Node::getScaleY();
if (_flippedY)
{
originalScale = originalScale * -1.0;
}
return originalScale;
}
float Widget::getScale()const
{
CCASSERT(this->getScaleX() == this->getScaleY(), "");
return this->getScaleX();
}
/*temp action*/

View File

@ -333,6 +333,17 @@ public:
/** @deprecated Use setFlippedY() instead */
CC_DEPRECATED_ATTRIBUTE void setFlipY(bool flipY) { setFlippedY(flipY); };
//override the setScale function of Node
virtual void setScaleX(float scaleX) override;
virtual void setScaleY(float scaleY) override;
virtual void setScale(float scale) override;
virtual void setScale(float scalex, float scaley) override;
using Node::setScaleZ;
virtual float getScaleX() const override;
virtual float getScaleY() const override;
virtual float getScale() const override;
using Node::getScaleZ;
/*
* Checks a point if in parent's area.
*
@ -663,8 +674,7 @@ protected:
virtual void releaseUpEvent();
virtual void cancelUpEvent();
virtual void updateFlippedX(){};
virtual void updateFlippedY(){};
virtual void adaptRenderers(){};
void updateChildrenDisplayedRGBA();

View File

@ -91,7 +91,7 @@ g_guisTests[] =
UISceneManager* sceneManager = UISceneManager::sharedUISceneManager();
sceneManager->setCurrentUISceneId(kUIButtonTest);
sceneManager->setMinUISceneId(kUIButtonTest);
sceneManager->setMaxUISceneId(kUIButtonTitleEffectTest);
sceneManager->setMaxUISceneId(kUIButtonFlipTest);
Scene* scene = sceneManager->currentUIScene();
Director::getInstance()->replaceScene(scene);
}
@ -127,7 +127,7 @@ g_guisTests[] =
UISceneManager* sceneManager = UISceneManager::sharedUISceneManager();
sceneManager->setCurrentUISceneId(kUIImageViewTest);
sceneManager->setMinUISceneId(kUIImageViewTest);
sceneManager->setMaxUISceneId(kUIImageViewTest_ContentSize);
sceneManager->setMaxUISceneId(kUIImageViewFlipTest);
Scene* scene = sceneManager->currentUIScene();
Director::getInstance()->replaceScene(scene);
}

View File

@ -290,7 +290,7 @@ bool UIButtonTest_Title::init()
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("Button with title", "fonts/Marker Felt.ttf", 30);
Text* alert = Text::create("Button with title, title should be flipped!", "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));
@ -305,7 +305,9 @@ bool UIButtonTest_Title::init()
CCASSERT(button->getTitleColor() == Color3B::YELLOW, "Button setTitleColotr & getTitleColor not match!");
button->addTouchEventListener(CC_CALLBACK_2(UIButtonTest_Title::touchEvent, this));
_uiLayer->addChild(button);
button->setFlippedX(true);
auto label = button->getTitleRenderer();
label->setScale(4.0);
button->runAction(RepeatForever::create(Sequence::create(ScaleTo::create(1.0f, 1.2f),
ScaleTo::create(1.0f, 1.0f),nullptr)));
@ -470,14 +472,6 @@ bool UIButtonTestSwitchScale9::init()
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("Button","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);
// Create the button
Button* button = Button::create("cocosui/animationbuttonnormal.png",
@ -550,14 +544,6 @@ bool UIButtonTestZoomScale::init()
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("Button","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);
// Create the button
Button* button = Button::create("cocosui/animationbuttonnormal.png",
@ -622,14 +608,6 @@ bool UIButtonTextOnly::init()
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("Button","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);
// Create the button
auto button = Button::create();
@ -673,14 +651,6 @@ bool UIButtonIgnoreContentSizeTest::init()
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("Button","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);
// Create the button
auto button = Button::create("cocosui/animationbuttonnormal.png",
@ -741,14 +711,6 @@ bool UIButtonTitleEffectTest::init()
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 20));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("Button","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);
// Create the button
auto button = Button::create("cocosui/animationbuttonnormal.png",
@ -779,5 +741,57 @@ bool UIButtonTitleEffectTest::init()
}
return false;
}
// UIButtonFlipTest
UIButtonFlipTest::UIButtonFlipTest()
: _displayValueLabel(nullptr)
{
}
UIButtonFlipTest::~UIButtonFlipTest()
{
}
bool UIButtonFlipTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();
// Add a label in which the button events will be displayed
_displayValueLabel = Text::create("Button X Flipped", "fonts/Marker Felt.ttf",20);
_displayValueLabel->setNormalizedPosition(Vec2(0.3, 0.7));
_uiLayer->addChild(_displayValueLabel);
// Create the button
auto button = Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
button->setNormalizedPosition(Vec2(0.3f, 0.5f));
button->setTitleText("PLAY GAME");
button->setTitleFontName("fonts/Marker Felt.ttf");
button->setZoomScale(0.3f);
button->setScale(2.0f);
button->setFlippedX(true);
button->setPressedActionEnabled(true);
_uiLayer->addChild(button);
// Create the button
auto button2 = Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
button2->setNormalizedPosition(Vec2(0.8f, 0.5f));
button2->setTitleText("PLAY GAME");
button2->setFlippedY(true);
_uiLayer->addChild(button2);
auto titleLabel = Text::create("Button Y flipped", "Arial", 20);
titleLabel->setNormalizedPosition(Vec2(0.8, 0.7));
this->addChild(titleLabel);
return true;
}
return false;
}

View File

@ -153,4 +153,16 @@ protected:
UI_SCENE_CREATE_FUNC(UIButtonTitleEffectTest)
Text* _displayValueLabel;
};
class UIButtonFlipTest : public UIScene
{
public:
UIButtonFlipTest();
~UIButtonFlipTest();
bool init();
protected:
UI_SCENE_CREATE_FUNC(UIButtonFlipTest)
Text* _displayValueLabel;
};
#endif /* defined(__TestCpp__UIButtonTest__) */

View File

@ -129,3 +129,55 @@ bool UIImageViewTest_ContentSize::init()
}
return false;
}
// UIImageViewFlipTest
bool UIImageViewFlipTest::init()
{
if (UIScene::init())
{
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("Images/blocks9ss.plist");
Size widgetSize = _widget->getContentSize();
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));
_uiLayer->addChild(alert);
// Create the imageview
ImageView* imageView = ImageView::create("blocks9r.png", Widget::TextureResType::PLIST);
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));
_uiLayer->addChild(imageView);
auto toggleButton = Button::create();
toggleButton->setTitleText("Toggle FlipX");
toggleButton->setPosition(imageView->getPosition() + Vec2(-50, - imageView->getContentSize().height/2 - 20));
this->addChild(toggleButton);
toggleButton->addClickEventListener([=](Ref*){
imageView->setFlippedX(!imageView->isFlippedX());
});
auto toggleScale9 = Button::create();
toggleScale9->setTitleText("Toggle Scale9");
toggleScale9->setPosition(imageView->getPosition() + Vec2(+50, - imageView->getContentSize().height/2- 20));
this->addChild(toggleScale9);
toggleScale9->addClickEventListener([=](Ref*){
imageView->setScale9Enabled(!imageView->isScale9Enabled());
//after switching scale9, you must call setContentSize to keep the size not change
imageView->setContentSize(Size(250, 115));
});
return true;
}
return false;
}

View File

@ -54,4 +54,13 @@ protected:
UI_SCENE_CREATE_FUNC(UIImageViewTest_ContentSize)
};
class UIImageViewFlipTest : public UIScene
{
public:
bool init();
protected:
UI_SCENE_CREATE_FUNC(UIImageViewFlipTest)
};
#endif /* defined(__TestCpp__UIImageViewTest__) */

View File

@ -44,6 +44,7 @@ static const char* s_testArray[] =
"UIButtonTextOnly",
"UIButtonIgnoreContentSizeTest",
"UIButtonTitleEffectTest",
"UIButtonFlipTest",
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
"UIEditBoxTest",
@ -55,6 +56,7 @@ static const char* s_testArray[] =
"UIImageViewTest",
"UIImageViewTest_Scale9",
"UIImageViewTest_ContentSize",
"UIImageViewFlipTest",
"UILoadingBarTest_Left",
"UILoadingBarTest_Right",
@ -218,6 +220,8 @@ Scene *UISceneManager::currentUIScene()
return UIButtonIgnoreContentSizeTest::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIButtonTitleEffectTest:
return UIButtonTitleEffectTest::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIButtonFlipTest:
return UIButtonFlipTest::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUICheckBoxTest:
return UICheckBoxTest::sceneWithTitle(s_testArray[_currentUISceneId]);
@ -234,6 +238,9 @@ Scene *UISceneManager::currentUIScene()
return UIImageViewTest_Scale9::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIImageViewTest_ContentSize:
return UIImageViewTest_ContentSize::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUIImageViewFlipTest:
return UIImageViewFlipTest::sceneWithTitle(s_testArray[_currentUISceneId]);
case kUILoadingBarTest_Left:
return UILoadingBarTest_Left::sceneWithTitle(s_testArray[_currentUISceneId]);

View File

@ -41,6 +41,7 @@ enum
kUIButtonTextOnly,
kUIButtonIgnoreContentSizeTest,
kUIButtonTitleEffectTest,
kUIButtonFlipTest,
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_TIZEN) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
kUIEditBoxTest,
#endif
@ -50,6 +51,8 @@ enum
kUIImageViewTest,
kUIImageViewTest_Scale9,
kUIImageViewTest_ContentSize,
kUIImageViewFlipTest,
kUILoadingBarTest_Left,
kUILoadingBarTest_Right,
kUILoadingBarTest_Left_Scale9,