optimize ui::CheckBox memory consumption

This commit is contained in:
andyque 2015-06-04 10:09:17 +08:00
parent f7e89db0ea
commit 91ddb802e1
4 changed files with 152 additions and 38 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;

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

@ -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__) */