Merge pull request #12214 from andyque/optimize-ui-memory-usage

Optimize ui memory usage
This commit is contained in:
子龙山人 2015-06-12 10:11:21 +08:00
commit 7a9b48bd83
13 changed files with 307 additions and 90 deletions

View File

@ -46,6 +46,9 @@ _frontCrossDisabledRenderer(nullptr),
_isSelected(true),
_checkBoxEventListener(nullptr),
_checkBoxEventSelector(nullptr),
_isBackgroundSelectedTextureLoaded(false),
_isBackgroundDisabledTextureLoaded(false),
_isFrontCrossDisabledTextureLoaded(false),
_backGroundTexType(TextureResType::LOCAL),
_backGroundSelectedTexType(TextureResType::LOCAL),
_frontCrossTexType(TextureResType::LOCAL),
@ -54,11 +57,6 @@ _frontCrossDisabledTexType(TextureResType::LOCAL),
_zoomScale(0.1f),
_backgroundTextureScaleX(1.0),
_backgroundTextureScaleY(1.0),
_backGroundFileName(""),
_backGroundSelectedFileName(""),
_frontCrossFileName(""),
_backGroundDisabledFileName(""),
_frontCrossDisabledFileName(""),
_backGroundBoxRendererAdaptDirty(true),
_backGroundSelectedBoxRendererAdaptDirty(true),
_frontCrossRendererAdaptDirty(true),
@ -189,11 +187,10 @@ void CheckBox::loadTextures(const std::string& backGround,
void CheckBox::loadTextureBackGround(const std::string& backGround,TextureResType texType)
{
if (backGround.empty() || (_backGroundFileName == backGround && _backGroundTexType == texType))
if (backGround.empty())
{
return;
}
_backGroundFileName = backGround;
_backGroundTexType = texType;
switch (_backGroundTexType)
{
@ -206,23 +203,34 @@ void CheckBox::loadTextureBackGround(const std::string& backGround,TextureResTyp
default:
break;
}
this->setupBackgroundTexture();
}
void CheckBox::setupBackgroundTexture()
{
this->updateChildrenDisplayedRGBA();
updateContentSizeWithTextureSize(_backGroundBoxRenderer->getContentSize());
_backGroundBoxRendererAdaptDirty = true;
}
void CheckBox::loadTextureBackGround(SpriteFrame* spriteFrame)
{
_backGroundBoxRenderer->setSpriteFrame(spriteFrame);
this->setupBackgroundTexture();
}
void CheckBox::loadTextureBackGroundSelected(const std::string& backGroundSelected,TextureResType texType)
{
if (backGroundSelected.empty() ||
(_backGroundSelectedFileName == backGroundSelected && _backGroundSelectedTexType == texType))
if (backGroundSelected.empty())
{
return;
}
_backGroundSelectedFileName = backGroundSelected;
_backGroundSelectedTexType = texType;
_isBackgroundSelectedTextureLoaded = true;
switch (_backGroundSelectedTexType)
{
case TextureResType::LOCAL:
@ -234,19 +242,27 @@ void CheckBox::loadTextureBackGroundSelected(const std::string& backGroundSelect
default:
break;
}
this->updateChildrenDisplayedRGBA();
this->setupBackgroundSelectedTexture();
}
void CheckBox::loadTextureBackGroundSelected(SpriteFrame* spriteframe)
{
this->_backGroundSelectedBoxRenderer->setSpriteFrame(spriteframe);
this->setupBackgroundSelectedTexture();
}
void CheckBox::setupBackgroundSelectedTexture()
{
this->updateChildrenDisplayedRGBA();
_backGroundSelectedBoxRendererAdaptDirty = true;
}
void CheckBox::loadTextureFrontCross(const std::string& cross,TextureResType texType)
{
if (cross.empty() || (_frontCrossFileName == cross && _frontCrossTexType == texType))
if (cross.empty())
{
return;
}
_frontCrossFileName = cross;
_frontCrossTexType = texType;
switch (_frontCrossTexType)
{
@ -259,21 +275,29 @@ void CheckBox::loadTextureFrontCross(const std::string& cross,TextureResType tex
default:
break;
}
this->updateChildrenDisplayedRGBA();
this->setupFrontCrossTexture();
}
void CheckBox::loadTextureFrontCross(SpriteFrame* spriteFrame)
{
this->_frontCrossRenderer->setSpriteFrame(spriteFrame);
this->setupFrontCrossTexture();
}
void CheckBox::setupFrontCrossTexture()
{
this->updateChildrenDisplayedRGBA();
_frontCrossRendererAdaptDirty = true;
}
void CheckBox::loadTextureBackGroundDisabled(const std::string& backGroundDisabled,TextureResType texType)
{
if (backGroundDisabled.empty() ||
(_backGroundDisabledFileName == backGroundDisabled && _backGroundDisabledTexType == texType))
if (backGroundDisabled.empty())
{
return;
}
_backGroundDisabledFileName = backGroundDisabled;
_backGroundDisabledTexType = texType;
_isBackgroundDisabledTextureLoaded = true;
switch (_backGroundDisabledTexType)
{
case TextureResType::LOCAL:
@ -285,7 +309,17 @@ void CheckBox::loadTextureBackGroundDisabled(const std::string& backGroundDisabl
default:
break;
}
this->setupBackgroundDisable();
}
void CheckBox::loadTextureBackGroundDisabled(SpriteFrame* spriteframe)
{
this->_backGroundBoxDisabledRenderer->setSpriteFrame(spriteframe);
this->setupBackgroundDisable();
}
void CheckBox::setupBackgroundDisable()
{
this->updateChildrenDisplayedRGBA();
_backGroundBoxDisabledRendererAdaptDirty = true;
@ -293,13 +327,12 @@ void CheckBox::loadTextureBackGroundDisabled(const std::string& backGroundDisabl
void CheckBox::loadTextureFrontCrossDisabled(const std::string& frontCrossDisabled,TextureResType texType)
{
if (frontCrossDisabled.empty() ||
(_frontCrossDisabledFileName == frontCrossDisabled && _frontCrossDisabledTexType == texType))
if (frontCrossDisabled.empty())
{
return;
}
_frontCrossDisabledFileName = frontCrossDisabled;
_frontCrossDisabledTexType = texType;
_isFrontCrossDisabledTextureLoaded = true;
switch (_frontCrossDisabledTexType)
{
case TextureResType::LOCAL:
@ -311,9 +344,19 @@ void CheckBox::loadTextureFrontCrossDisabled(const std::string& frontCrossDisabl
default:
break;
}
this->setupFrontCrossDisableTexture();
this->updateChildrenDisplayedRGBA();
}
void CheckBox::loadTextureFrontCrossDisabled(SpriteFrame* spriteframe)
{
this->_frontCrossDisabledRenderer->setSpriteFrame(spriteframe);
this->setupFrontCrossDisableTexture();
}
void CheckBox::setupFrontCrossDisableTexture()
{
this->updateChildrenDisplayedRGBA();
_frontCrossDisabledRendererAdaptDirty = true;
}
@ -361,7 +404,7 @@ void CheckBox::onPressStateChangedToPressed()
_backGroundBoxRenderer->setGLProgramState(this->getNormalGLProgramState());
_frontCrossRenderer->setGLProgramState(this->getNormalGLProgramState());
if (_backGroundSelectedFileName.empty())
if (!_isBackgroundSelectedTextureLoaded)
{
_backGroundBoxRenderer->setScale(_backgroundTextureScaleX + _zoomScale,
_backgroundTextureScaleY + _zoomScale);
@ -379,7 +422,8 @@ void CheckBox::onPressStateChangedToPressed()
void CheckBox::onPressStateChangedToDisabled()
{
if (_backGroundDisabledFileName.empty() || _frontCrossDisabledFileName.empty())
if (!_isBackgroundDisabledTextureLoaded
|| !_isFrontCrossDisabledTextureLoaded)
{
_backGroundBoxRenderer->setGLProgramState(this->getGrayGLProgramState());
_frontCrossRenderer->setGLProgramState(this->getGrayGLProgramState());
@ -653,11 +697,11 @@ void CheckBox::copySpecialProperties(Widget *widget)
CheckBox* checkBox = dynamic_cast<CheckBox*>(widget);
if (checkBox)
{
loadTextureBackGround(checkBox->_backGroundFileName, checkBox->_backGroundTexType);
loadTextureBackGroundSelected(checkBox->_backGroundSelectedFileName, checkBox->_backGroundSelectedTexType);
loadTextureFrontCross(checkBox->_frontCrossFileName, checkBox->_frontCrossTexType);
loadTextureBackGroundDisabled(checkBox->_backGroundDisabledFileName, checkBox->_backGroundDisabledTexType);
loadTextureFrontCrossDisabled(checkBox->_frontCrossDisabledFileName, checkBox->_frontCrossDisabledTexType);
loadTextureBackGround(checkBox->_backGroundBoxRenderer->getSpriteFrame());
loadTextureBackGroundSelected(checkBox->_backGroundSelectedBoxRenderer->getSpriteFrame());
loadTextureFrontCross(checkBox->_frontCrossRenderer->getSpriteFrame());
loadTextureBackGroundDisabled(checkBox->_backGroundBoxDisabledRenderer->getSpriteFrame());
loadTextureFrontCrossDisabled(checkBox->_frontCrossDisabledRenderer->getSpriteFrame());
setSelected(checkBox->_isSelected);
_checkBoxEventListener = checkBox->_checkBoxEventListener;
_checkBoxEventSelector = checkBox->_checkBoxEventSelector;
@ -666,6 +710,9 @@ void CheckBox::copySpecialProperties(Widget *widget)
_zoomScale = checkBox->_zoomScale;
_backgroundTextureScaleX = checkBox->_backgroundTextureScaleX;
_backgroundTextureScaleY = checkBox->_backgroundTextureScaleY;
_isBackgroundSelectedTextureLoaded = checkBox->_isBackgroundSelectedTextureLoaded;
_isBackgroundDisabledTextureLoaded = checkBox->_isBackgroundDisabledTextureLoaded;
_isFrontCrossDisabledTextureLoaded = checkBox->_isFrontCrossDisabledTextureLoaded;
}
}

View File

@ -262,6 +262,17 @@ protected:
virtual void onPressStateChangedToNormal() override;
virtual void onPressStateChangedToPressed() override;
virtual void onPressStateChangedToDisabled() override;
void setupBackgroundTexture();
void loadTextureBackGround(SpriteFrame* spriteFrame);
void setupBackgroundSelectedTexture();
void loadTextureBackGroundSelected(SpriteFrame* spriteFrame);
void setupFrontCrossTexture();
void loadTextureFrontCross(SpriteFrame* spriteframe);
void setupBackgroundDisable();
void loadTextureBackGroundDisabled(SpriteFrame* spriteframe);
void setupFrontCrossDisableTexture();
void loadTextureFrontCrossDisabled(SpriteFrame* spriteframe);
void selectedEvent();
void unSelectedEvent();
@ -304,6 +315,9 @@ protected:
ccCheckBoxCallback _checkBoxEventCallback;
bool _isBackgroundSelectedTextureLoaded;
bool _isBackgroundDisabledTextureLoaded;
bool _isFrontCrossDisabledTextureLoaded;
TextureResType _backGroundTexType;
TextureResType _backGroundSelectedTexType;
TextureResType _frontCrossTexType;
@ -313,12 +327,7 @@ protected:
float _zoomScale;
float _backgroundTextureScaleX;
float _backgroundTextureScaleY;
std::string _backGroundFileName;
std::string _backGroundSelectedFileName;
std::string _frontCrossFileName;
std::string _backGroundDisabledFileName;
std::string _frontCrossDisabledFileName;
bool _backGroundBoxRendererAdaptDirty;
bool _backGroundSelectedBoxRendererAdaptDirty;
bool _frontCrossRendererAdaptDirty;

View File

@ -40,7 +40,6 @@ _scale9Enabled(false),
_prevIgnoreSize(true),
_capInsets(Rect::ZERO),
_imageRenderer(nullptr),
_textureFile(""),
_imageTexType(TextureResType::LOCAL),
_imageTextureSize(_contentSize),
_imageRendererAdaptDirty(true)
@ -118,11 +117,10 @@ void ImageView::initRenderer()
void ImageView::loadTexture(const std::string& fileName, TextureResType texType)
{
if (fileName.empty() || (_textureFile == fileName && _imageTexType == texType))
if (fileName.empty())
{
return;
}
_textureFile = fileName;
_imageTexType = texType;
switch (_imageTexType)
{
@ -135,9 +133,20 @@ void ImageView::loadTexture(const std::string& fileName, TextureResType texType)
default:
break;
}
this->setupTexture();
}
void ImageView::loadTexture(SpriteFrame* spriteframe)
{
_imageRenderer->initWithSpriteFrame(spriteframe);
this->setupTexture();
}
void ImageView::setupTexture()
{
_imageTextureSize = _imageRenderer->getContentSize();
this->updateChildrenDisplayedRGBA();
updateContentSizeWithTextureSize(_imageTextureSize);
@ -293,7 +302,11 @@ void ImageView::copySpecialProperties(Widget *widget)
{
_prevIgnoreSize = imageView->_prevIgnoreSize;
setScale9Enabled(imageView->_scale9Enabled);
loadTexture(imageView->_textureFile, imageView->_imageTexType);
auto imageSprite = imageView->_imageRenderer->getSprite();
if(nullptr != imageSprite)
{
loadTexture(imageSprite->getSpriteFrame());
}
setCapInsets(imageView->_capInsets);
}
}

View File

@ -132,6 +132,8 @@ protected:
virtual void onSizeChanged() override;
virtual void adaptRenderers() override;
void loadTexture(SpriteFrame* spriteframe);
void setupTexture();
void imageTextureScaleChangedWithSize();
virtual Widget* createCloneInstance() override;
@ -141,7 +143,6 @@ protected:
bool _prevIgnoreSize;
Rect _capInsets;
Scale9Sprite* _imageRenderer;
std::string _textureFile;
TextureResType _imageTexType;
Size _imageTextureSize;
bool _imageRendererAdaptDirty;

View File

@ -45,7 +45,6 @@ _barRendererTextureSize(Size::ZERO),
_scale9Enabled(false),
_prevIgnoreSize(true),
_capInsets(Rect::ZERO),
_textureFile(""),
_barRendererAdaptDirty(true)
{
}
@ -148,7 +147,6 @@ void LoadingBar::loadTexture(const std::string& texture,TextureResType texType)
return;
}
_renderBarTexType = texType;
_textureFile = texture;
switch (_renderBarTexType)
{
case TextureResType::LOCAL:
@ -160,9 +158,19 @@ void LoadingBar::loadTexture(const std::string& texture,TextureResType texType)
default:
break;
}
this->setupTexture();
}
void LoadingBar::loadTexture(SpriteFrame* spriteframe)
{
this->_barRenderer->initWithSpriteFrame(spriteframe);
this->setupTexture();
}
void LoadingBar::setupTexture()
{
_barRendererTextureSize = _barRenderer->getContentSize();
switch (_direction)
{
case Direction::LEFT:
@ -193,7 +201,7 @@ void LoadingBar::loadTexture(const std::string& texture,TextureResType texType)
barRendererScaleChangedWithSize();
updateContentSizeWithTextureSize(_barRendererTextureSize);
this->updateProgressBar();
_barRendererAdaptDirty = true;
}
@ -396,7 +404,11 @@ void LoadingBar::copySpecialProperties(Widget *widget)
{
_prevIgnoreSize = loadingBar->_prevIgnoreSize;
setScale9Enabled(loadingBar->_scale9Enabled);
loadTexture(loadingBar->_textureFile, loadingBar->_renderBarTexType);
auto barSprite = loadingBar->_barRenderer->getSprite();
if(nullptr != barSprite)
{
loadTexture(barSprite->getSpriteFrame());
}
setCapInsets(loadingBar->_capInsets);
setPercent(loadingBar->_percent);
setDirection(loadingBar->_direction);

View File

@ -178,6 +178,9 @@ protected:
void setScale9Scale();
void updateProgressBar();
void barRendererScaleChangedWithSize();
void setupTexture();
void loadTexture(SpriteFrame* spriteframe);
virtual void adaptRenderers() override;
@ -193,7 +196,6 @@ protected:
bool _scale9Enabled;
bool _prevIgnoreSize;
Rect _capInsets;
std::string _textureFile;
bool _barRendererAdaptDirty;
};

View File

@ -53,11 +53,8 @@ _prevIgnoreSize(true),
_zoomScale(0.1f),
_sliderBallNormalTextureScaleX(1.0),
_sliderBallNormalTextureScaleY(1.0),
_textureFile(""),
_progressBarTextureFile(""),
_slidBallNormalTextureFile(""),
_slidBallPressedTextureFile(""),
_slidBallDisabledTextureFile(""),
_isSliderBallPressedTextureLoaded(false),
_isSliderBallDisabledTexturedLoaded(false),
_capInsetsBarRenderer(Rect::ZERO),
_capInsetsProgressBarRenderer(Rect::ZERO),
_sliderEventListener(nullptr),
@ -152,7 +149,6 @@ void Slider::loadBarTexture(const std::string& fileName, TextureResType texType)
{
return;
}
_textureFile = fileName;
_barTexType = texType;
switch (_barTexType)
{
@ -165,6 +161,16 @@ void Slider::loadBarTexture(const std::string& fileName, TextureResType texType)
default:
break;
}
this->setupBarTexture();
}
void Slider::loadBarTexture(SpriteFrame* spriteframe)
{
_barRenderer->initWithSpriteFrame(spriteframe);
this->setupBarTexture();
}
void Slider::setupBarTexture()
{
this->updateChildrenDisplayedRGBA();
_barRendererAdaptDirty = true;
_progressBarRendererDirty = true;
@ -178,7 +184,6 @@ void Slider::loadProgressBarTexture(const std::string& fileName, TextureResType
{
return;
}
_progressBarTextureFile = fileName;
_progressBarTexType = texType;
switch (_progressBarTexType)
{
@ -191,6 +196,17 @@ void Slider::loadProgressBarTexture(const std::string& fileName, TextureResType
default:
break;
}
this->setupProgressBarTexture();
}
void Slider::loadProgressBarTexture(SpriteFrame* spriteframe)
{
_progressBarRenderer->initWithSpriteFrame(spriteframe);
this->setupProgressBarTexture();
}
void Slider::setupProgressBarTexture()
{
this->updateChildrenDisplayedRGBA();
_progressBarRenderer->setAnchorPoint(Vec2(0.0f, 0.5f));
_progressBarTextureSize = _progressBarRenderer->getContentSize();
@ -291,7 +307,6 @@ void Slider::loadSlidBallTextureNormal(const std::string& normal,TextureResType
{
return;
}
_slidBallNormalTextureFile = normal;
_ballNTexType = texType;
switch (_ballNTexType)
{
@ -306,6 +321,11 @@ void Slider::loadSlidBallTextureNormal(const std::string& normal,TextureResType
}
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTextureNormal(SpriteFrame* spriteframe)
{
_slidBallNormalRenderer->setSpriteFrame(spriteframe);
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTexturePressed(const std::string& pressed,TextureResType texType)
{
@ -313,8 +333,8 @@ void Slider::loadSlidBallTexturePressed(const std::string& pressed,TextureResTyp
{
return;
}
_slidBallPressedTextureFile = pressed;
_ballPTexType = texType;
_isSliderBallPressedTextureLoaded = true;
switch (_ballPTexType)
{
case TextureResType::LOCAL:
@ -329,13 +349,20 @@ void Slider::loadSlidBallTexturePressed(const std::string& pressed,TextureResTyp
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTextureDisabled(const std::string& disabled,TextureResType texType)
void Slider::loadSlidBallTexturePressed(SpriteFrame* spriteframe)
{
_slidBallPressedRenderer->setSpriteFrame(spriteframe);
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTextureDisabled(const std::string& disabled,TextureResType texType)
{
if (disabled.empty())
{
return;
}
_slidBallDisabledTextureFile = disabled;
_isSliderBallDisabledTexturedLoaded = true;
_ballDTexType = texType;
switch (_ballDTexType)
{
@ -351,6 +378,12 @@ void Slider::loadSlidBallTexturePressed(const std::string& pressed,TextureResTyp
this->updateChildrenDisplayedRGBA();
}
void Slider::loadSlidBallTextureDisabled(SpriteFrame* spriteframe)
{
_slidBallDisabledRenderer->setSpriteFrame(spriteframe);
this->updateChildrenDisplayedRGBA();
}
void Slider::setPercent(int percent)
{
if (percent > 100)
@ -589,7 +622,7 @@ void Slider::onPressStateChangedToPressed()
_slidBallNormalRenderer->setGLProgramState(this->getNormalGLProgramState());
if (_slidBallPressedTextureFile.empty())
if (!_isSliderBallPressedTextureLoaded)
{
_slidBallNormalRenderer->setScale(_sliderBallNormalTextureScaleX + _zoomScale,
_sliderBallNormalTextureScaleY + _zoomScale);
@ -604,7 +637,7 @@ void Slider::onPressStateChangedToPressed()
void Slider::onPressStateChangedToDisabled()
{
if (_slidBallDisabledTextureFile.empty())
if (!_isSliderBallDisabledTexturedLoaded)
{
_slidBallNormalRenderer->setGLProgramState(this->getGrayGLProgramState());
}
@ -648,12 +681,22 @@ void Slider::copySpecialProperties(Widget *widget)
{
_prevIgnoreSize = slider->_prevIgnoreSize;
setScale9Enabled(slider->_scale9Enabled);
loadBarTexture(slider->_textureFile, slider->_barTexType);
loadProgressBarTexture(slider->_progressBarTextureFile, slider->_progressBarTexType);
loadSlidBallTextureNormal(slider->_slidBallNormalTextureFile, slider->_ballNTexType);
loadSlidBallTexturePressed(slider->_slidBallPressedTextureFile, slider->_ballPTexType);
loadSlidBallTextureDisabled(slider->_slidBallDisabledTextureFile, slider->_ballDTexType);
auto barSprite = slider->_barRenderer->getSprite();
if (nullptr != barSprite)
{
loadBarTexture(barSprite->getSpriteFrame());
}
auto progressSprite = slider->_progressBarRenderer->getSprite();
if (nullptr != progressSprite)
{
loadProgressBarTexture(progressSprite->getSpriteFrame());
}
loadSlidBallTextureNormal(slider->_slidBallNormalRenderer->getSpriteFrame());
loadSlidBallTexturePressed(slider->_slidBallPressedRenderer->getSpriteFrame());
loadSlidBallTextureDisabled(slider->_slidBallDisabledRenderer->getSpriteFrame());
setPercent(slider->getPercent());
_isSliderBallPressedTextureLoaded = slider->_isSliderBallPressedTextureLoaded;
_isSliderBallDisabledTexturedLoaded = slider->_isSliderBallDisabledTexturedLoaded;
_sliderEventListener = slider->_sliderEventListener;
_sliderEventSelector = slider->_sliderEventSelector;
_eventCallback = slider->_eventCallback;

View File

@ -257,6 +257,14 @@ protected:
virtual void onPressStateChangedToPressed() override;
virtual void onPressStateChangedToDisabled() override;
virtual void onSizeChanged() override;
void setupBarTexture();
void loadBarTexture(SpriteFrame* spriteframe);
void setupProgressBarTexture();
void loadProgressBarTexture(SpriteFrame* spriteframe);
void loadSlidBallTextureNormal(SpriteFrame* spriteframe);
void loadSlidBallTexturePressed(SpriteFrame* spriteframe);
void loadSlidBallTextureDisabled(SpriteFrame* spriteframe);
void barRendererScaleChangedWithSize();
void progressBarRendererScaleChangedWithSize();
@ -283,12 +291,9 @@ protected:
float _zoomScale;
float _sliderBallNormalTextureScaleX;
float _sliderBallNormalTextureScaleY;
std::string _textureFile;
std::string _progressBarTextureFile;
std::string _slidBallNormalTextureFile;
std::string _slidBallPressedTextureFile;
std::string _slidBallDisabledTextureFile;
bool _isSliderBallPressedTextureLoaded;
bool _isSliderBallDisabledTexturedLoaded;
Rect _capInsetsBarRenderer;
Rect _capInsetsProgressBarRenderer;

View File

@ -7,6 +7,7 @@ UICheckBoxTests::UICheckBoxTests()
{
ADD_TEST_CASE(UICheckBoxTest);
ADD_TEST_CASE(UICheckBoxDefaultBehaviorTest);
ADD_TEST_CASE(UICheckBoxCopyTest);
}
// UICheckBoxTest
@ -141,3 +142,50 @@ bool UICheckBoxDefaultBehaviorTest::init()
return false;
}
// UICheckBoxCopyTest
UICheckBoxCopyTest::UICheckBoxCopyTest()
: _displayValueLabel(nullptr)
{
}
UICheckBoxCopyTest::~UICheckBoxCopyTest()
{
}
bool UICheckBoxCopyTest::init()
{
if (UIScene::init())
{
Size widgetSize = _widget->getContentSize();;
// Add a label in which the checkbox events will be displayed
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1));
_displayValueLabel->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
_uiLayer->addChild(_displayValueLabel);
// Add the alert
Text* alert = Text::create("Two checkbox are identical.","fonts/Marker Felt.ttf",20 );
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 checkbox
CheckBox* checkBox = CheckBox::create("cocosui/check_box_normal.png",
"cocosui/check_box_active.png");
checkBox->setPosition(Vec2(widgetSize.width / 2.0f - 50, widgetSize.height / 2.0f));
_uiLayer->addChild(checkBox);
auto checkboxCopy = checkBox->clone();
checkboxCopy->setPosition(checkBox->getPosition() + Vec2(50,0));
_uiLayer->addChild(checkboxCopy);
return true;
}
return false;
}

View File

@ -58,4 +58,17 @@ protected:
cocos2d::ui::Text* _displayValueLabel;
};
class UICheckBoxCopyTest : public UIScene
{
public:
CREATE_FUNC(UICheckBoxCopyTest);
UICheckBoxCopyTest();
~UICheckBoxCopyTest();
virtual bool init() override;
protected:
cocos2d::ui::Text* _displayValueLabel;
};
#endif /* defined(__TestCpp__UICheckBoxTest__) */

View File

@ -60,11 +60,16 @@ bool UIImageViewTest_Scale9::init()
// Create the imageview
ImageView* imageView = ImageView::create("cocosui/buttonHighlighted.png");
imageView->setScale9Enabled(true);
imageView->setContentSize(Size(300, 115));
imageView->setPosition(Vec2(widgetSize.width / 2.0f,
imageView->setContentSize(Size(200, 115));
imageView->setPosition(Vec2(widgetSize.width / 2.0f - 100,
widgetSize.height / 2.0f));
_uiLayer->addChild(imageView);
_uiLayer->addChild(imageView);
auto imageCopy = imageView->clone();
imageCopy->setPosition(Vec2(widgetSize.width / 2.0f + 100,
widgetSize.height / 2.0f));
_uiLayer->addChild(imageCopy);
return true;
}

View File

@ -35,9 +35,11 @@ bool UILoadingBarTest_Left::init()
Size widgetSize = _widget->getContentSize();
// Add the alert
Text* alert = Text::create("LoadingBar left", "fonts/Marker Felt.ttf", 30);
Text* alert = Text::create("Test LoadingBar Change Direction",
"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));
alert->setPosition(Vec2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f - alert->getContentSize().height * 1.75f));
_uiLayer->addChild(alert);
// Create the loading bar
@ -45,22 +47,37 @@ bool UILoadingBarTest_Left::init()
loadingBar->setTag(0);
loadingBar->setPosition(Vec2(widgetSize.width / 2.0f,
widgetSize.height / 2.0f + loadingBar->getContentSize().height / 4.0f));
auto loadingBarCopy = (LoadingBar*)loadingBar->clone();
loadingBarCopy->setTag(1);
loadingBarCopy->setPosition(loadingBar->getPosition()
+ Vec2(0, -40));
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
Button* button = Button::create("cocosui/animationbuttonnormal.png",
"cocosui/animationbuttonpressed.png");
button->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 50));
button->setTitleText("Click to change direction!");
// button->addTouchEventListener(this, toucheventselector(UIButtonTest::touchEvent));
button->addTouchEventListener([=](Ref*, Widget::TouchEventType type){
if (type == Widget::TouchEventType::ENDED) {
if (loadingBar->getDirection() == LoadingBar::Direction::LEFT) {
button->addTouchEventListener([=](Ref*, Widget::TouchEventType type)
{
if (type == Widget::TouchEventType::ENDED)
{
if (loadingBar->getDirection() == LoadingBar::Direction::LEFT)
{
loadingBar->setDirection(LoadingBar::Direction::RIGHT);
}else{
loadingBarCopy->setDirection(LoadingBar::Direction::LEFT);
}
else
{
loadingBar->setDirection(LoadingBar::Direction::LEFT);
loadingBarCopy->setDirection(LoadingBar::Direction::RIGHT);
}
}
});
_uiLayer->addChild(loadingBar,1);
_uiLayer->addChild(loadingBarCopy,2);
_uiLayer->addChild(button);
_uiLayer->addChild(loadingBar);
return true;
}
@ -75,7 +92,9 @@ void UILoadingBarTest_Left::update(float delta)
_count = 0;
}
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
LoadingBar* loadingBarCopy = static_cast<LoadingBar*>(_uiLayer->getChildByTag(1));
loadingBar->setPercent(_count);
loadingBarCopy->setPercent(_count);
}
// UILoadingBarTest_Right
@ -383,4 +402,4 @@ void UILoadingBarReloadTexture::update(float delta)
}
LoadingBar* loadingBar = static_cast<LoadingBar*>(_uiLayer->getChildByTag(0));
loadingBar->setPercent(_count);
}
}

View File

@ -232,7 +232,7 @@ bool UISliderNormalDefaultTest::init()
widgetSize.height / 2.0f + 50));
_uiLayer->addChild(slider);
Slider* sliderScale9 = Slider::create("cocosui/sliderTrack2.png", "cocosui/sliderThumb.png");
Slider* sliderScale9 = (Slider*)slider->clone();
sliderScale9->setScale9Enabled(true);
sliderScale9->setCapInsets(Rect(0, 0, 0, 0));
sliderScale9->setZoomScale(1.0);
@ -287,7 +287,7 @@ bool UISliderDisabledDefaultTest::init()
widgetSize.height / 2.0f + 50));
_uiLayer->addChild(slider);
Slider* sliderScale9 = Slider::create("cocosui/slidbar.png", "cocosui/sliderballnormal.png");
Slider* sliderScale9 = (Slider*)slider->clone();
sliderScale9->setScale9Enabled(true);
sliderScale9->setEnabled(false);
sliderScale9->setBright(false);