Add clone method to LayoutParameter

This commit is contained in:
CaiWenzhi 2014-01-17 13:10:04 +08:00
parent 602133a071
commit bea46fa290
4 changed files with 108 additions and 44 deletions

View File

@ -57,6 +57,23 @@ LayoutParameterType LayoutParameter::getLayoutType() const
return _layoutParameterType;
}
LayoutParameter* LayoutParameter::clone()
{
LayoutParameter* clonedParameter = createCloneInstance();
clonedParameter->copyProperties(this);
return clonedParameter;
}
LayoutParameter* LayoutParameter::createCloneInstance()
{
return LayoutParameter::create();
}
void LayoutParameter::copyProperties(LayoutParameter *model)
{
_margin = model->_margin;
}
LinearLayoutParameter* LinearLayoutParameter::create()
{
LinearLayoutParameter* parameter = new LinearLayoutParameter();
@ -79,6 +96,21 @@ LinearGravity LinearLayoutParameter::getGravity() const
return _linearGravity;
}
LayoutParameter* LinearLayoutParameter::createCloneInstance()
{
return LinearLayoutParameter::create();
}
void LinearLayoutParameter::copyProperties(LayoutParameter *model)
{
LayoutParameter::copyProperties(model);
LinearLayoutParameter* parameter = dynamic_cast<LinearLayoutParameter*>(model);
if (parameter)
{
setGravity(parameter->_linearGravity);
}
}
RelativeLayoutParameter* RelativeLayoutParameter::create()
{
RelativeLayoutParameter* parameter = new RelativeLayoutParameter();
@ -121,6 +153,23 @@ const char* RelativeLayoutParameter::getRelativeName() const
return _relativeLayoutName.c_str();
}
LayoutParameter* RelativeLayoutParameter::createCloneInstance()
{
return RelativeLayoutParameter::create();
}
void RelativeLayoutParameter::copyProperties(LayoutParameter *model)
{
LayoutParameter::copyProperties(model);
RelativeLayoutParameter* parameter = dynamic_cast<RelativeLayoutParameter*>(model);
if (parameter)
{
setAlign(parameter->_relativeAlign);
setRelativeName(parameter->_relativeLayoutName.c_str());
setRelativeToWidgetName(parameter->_relativeWidgetName.c_str());
}
}
}
NS_CC_END

View File

@ -86,6 +86,10 @@ public:
* @return LayoutParameterType
*/
LayoutParameterType getLayoutType() const;
LayoutParameter* clone();
virtual LayoutParameter* createCloneInstance();
virtual void copyProperties(LayoutParameter* model);
protected:
Margin _margin;
LayoutParameterType _layoutParameterType;
@ -130,6 +134,8 @@ public:
* @return LinearGravity
*/
LinearGravity getGravity() const;
virtual LayoutParameter* createCloneInstance();
virtual void copyProperties(LayoutParameter* model);
protected:
LinearGravity _linearGravity;
};
@ -202,6 +208,9 @@ public:
* @return name
*/
const char* getRelativeName() const;
virtual LayoutParameter* createCloneInstance();
virtual void copyProperties(LayoutParameter* model);
protected:
RelativeAlign _relativeAlign;
std::string _relativeWidgetName;

View File

@ -334,6 +334,7 @@ void LoadingBar::copySpecialProperties(Widget *widget)
loadTexture(loadingBar->_textureFile.c_str(), loadingBar->_renderBarTexType);
setCapInsets(loadingBar->_capInsets);
setPercent(loadingBar->_percent);
setDirection(loadingBar->_barType);
}
}

View File

@ -1065,6 +1065,11 @@ void Widget::copyProperties(Widget *widget)
setOpacity(widget->getOpacity());
setCascadeOpacityEnabled(widget->isCascadeOpacityEnabled());
setCascadeColorEnabled(widget->isCascadeColorEnabled());
Map<int, LayoutParameter*>& layoutParameterDic = widget->_layoutParameterDictionary;
for (auto iter = layoutParameterDic.begin(); iter != layoutParameterDic.end(); ++iter)
{
setLayoutParameter(iter->second->clone());
}
onSizeChanged();
}