【Cocos2d-x】源码分析之 2d/ui/Widget
转自:http://blog.csdn.net/yhhwatl/article/details/26487731
从今天开始 咱也模仿 红孩儿这些大牛分析源码 ,由于水平有限 不对之处欢迎狂喷。哈哈。
- #ifndef__UIWIDGET_H__
- #define__UIWIDGET_H__
-
- #include"ui/CCProtectedNode.h"
- #include"ui/UILayoutDefine.h"
- #include"ui/UILayoutParameter.h"
- #include"ui/GUIDefine.h"
-
- NS_CC_BEGIN
-
- namespaceui{
-
- typedefenum
- {
- BRIGHT_NONE=-1,
- BRIGHT_NORMAL,
- BRIGHT_HIGHLIGHT
- }BrightStyle;
-
- enum
- {
- WidgetTypeWidget,119)">//control基本UIWidget默认
- WidgetTypeContainer
- }WidgetType;
-
- enum
- {
- UI_TEX_TYPE_LOCAL=0,119)">//meanslocalfile
- UI_TEX_TYPE_PLIST=1
- }TextureResType;
-
- enumclassTouchEventType
- {
- TOUCH_EVENT_BEGAN,119)">//开始
- TOUCH_EVENT_MOVED,119)">//移动
- TOUCH_EVENT_ENDED,119)">//结束
- TOUCH_EVENT_CANCELED
- };
-
- enum
- {
- SIZE_ABSOLUTE,119)">//绝对值
- SIZE_PERCENT
- }SizeType;
-
- enum
- {
- POSITION_ABSOLUTE,119)">//绝对真实位置
- POSITION_PERCENT
- }PositionType;
-
- enum
- {
- WIDGET_SIZE,119)">//size
- WIDGET_POSITION
-
- }WidgetInfoType;
-
-
- typedefstd::function<void(Ref*,TouchEventType)>SEL_TouchEvent;
-
-
-
-
- #definetoucheventselector(_SELECTOR)(SEL_TouchEvent)(&_SELECTOR)
-
- classWidget:publicProtectedNode
- {
- public:
- Widget(void);
- virtual~Widget();
- staticWidget*create();
-
- virtualvoidsetEnabled(boolenabled);
- boolisEnabled()const;
-
- voidsetBright(boolbright);
-
- boolisBright()/**是否可以触摸(响应事件)默认true*/
- voidsetTouchEnabled(boolenabled);
-
- voidsetBrightStyle(BrightStylestyle);
-
- boolisTouchEnabled()/**是否被选中默认false*/
- boolisFocused()/**是否被选中默认false*/
- voidsetFocused(boolfucosed);
-
- floatgetLeftInParent();
-
- floatgetBottomInParent();
-
- floatgetRightInParent();
-
- floatgetTopInParent();
-
-
- virtualWidget*getChildByName(constchar*name);
-
- voidvisit(cocos2d::Renderer*renderer,constkmMat4&parentTransform,boolparentTransformUpdated)override;
-
-
- voidaddTouchEventListener(SEL_TouchEventselector);
-
- voidlogInfo(WidgetInfoType_type);
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- voidsetPosition(constPoint&pos)override;
-
- voidsetPositionPercent(constPoint&percent);
-
- constPoint&getPositionPercent();
-
-
- voidsetPositionType(PositionTypetype);
-
-
- PositionTypegetPositionType()const;
-
-
- voidsetFlippedX(boolflippedX);
-
-
- virtualboolisFlippedX(){return_flippedX;};
-
-
- voidsetFlippedY(boolflippedY);
-
-
- boolisFlippedY(){return_flippedY;};
-
- voidsetColor(constColor3B&color)override;
-
- voidsetOpacity(GLubyteopacity)override;
-
- constColor3B&getColor()constoverride{return_color;};
-
- GLubytegetOpacity()return_opacity;};
-
-
- CC_DEPRECATED_ATTRIBUTEboolisFlipX(){returnisFlippedX();};
-
- CC_DEPRECATED_ATTRIBUTEvoidsetFlipX(boolflipX){setFlippedX(flipX);};
-
- CC_DEPRECATED_ATTRIBUTEboolisFlipY(){returnisFlippedY();};
-
- CC_DEPRECATED_ATTRIBUTEvoidsetFlipY(boolflipY){setFlippedY(flipY);};
-
-
- voiddidNotSelectSelf();
-
-
- boolclippingParentAreaContainPoint(constPoint&pt);
-
-
- voidcheckChildInfo(inthandleState,Widget*sender,255); font-weight:bold">constPoint&touchPoint);
-
-
- constPoint&getTouchStartPos();
-
-
- constPoint&getTouchMovePos();
-
-
- constPoint&getTouchEndPos();
-
-
- voidsetName(char*name);
-
-
- char*getName()const;
-
-
- WidgetTypegetWidgetType()/**设置控件大小*/
- voidsetSize(constSize&size);
- voidsetSizeW(int&sizeW);
- voidsetSizeH(int&sizeH);
-
- voidsetSizePercent(constPoint&percent);
-
-
- voidsetSizeType(SizeTypetype);
-
-
- SizeTypegetSizeType()/**得到size*/
- constSize&getSize()/**得到Customsize*/
- constSize&getCustomSize()/**得到layoutsize*/
- constSize&getLayoutSize(){return_size;};
-
-
- constPoint&getSizePercent()/***/
- boolhitTest(constPoint&pt);
-
- boolonTouchBegan(Touch*touch,Event*unusedEvent);
- voidonTouchMoved(Touch*touch,Event*unusedEvent);
- voidonTouchEnded(Touch*touch,255); font-weight:bold">voidonTouchCancelled(Touch*touch,Event*unusedEvent);
-
-
- voidsetLayoutParameter(LayoutParameter*parameter);
-
-
- LayoutParameter*getLayoutParameter(LayoutParameterTypetype);
-
-
- voidignoreContentAdaptWithSize(boolignore);
-
-
- boolisIgnoreContentAdaptWithSize()/**得到世界位置*/
- PointgetWorldPosition();
-
-
- virtualNode*getVirtualRenderer();
-
-
- constSize&getVirtualRendererSize()const;
-
-
-
-
- std::stringgetDescription()constoverride;
-
- Widget*clone();
-
- voidonEnter()override;
- voidonExit()override;
-
- voidupdateSizeAndPosition();
-
- voidupdateSizeAndPosition(constSize&parentSize);
-
-
- voidsetActionTag(inttag);
- intgetActionTag();
-
- CC_CONSTRUCTOR_ACCESS:
-
- boolinit()override;
-
- protected:
-
- voidonSizeChanged();
-
-
- voidinitRenderer();
-
-
- voidonPressStateChangedToNormal();
-
-
- voidonPressStateChangedToPressed();
-
-
- voidonPressStateChangedToDisabled();
-
- voidpushDownEvent();
- voidmoveEvent();
- voidreleaseUpEvent();
- voidcancelUpEvent();
-
- voidupdateTextureColor(){};
- voidupdateTextureOpacity(){};
- voidupdateTextureRGBA(){};
- voidupdateFlippedX(){};
- voidupdateFlippedY(){};
-
- voidupdateColorToRenderer(Node*renderer);
- voidupdateOpacityToRenderer(Node*renderer);
- voidupdateRGBAToRenderer(Node*renderer);
- voidcopyProperties(Widget*model);
-
- virtualWidget*createCloneInstance();
- voidcopySpecialProperties(Widget*model);
- voidcopyClonedWidgetChildren(Widget*model);
- Widget*getWidgetParent();
- voidupdateContentSizeWithTextureSize(constSize&size);
-
- voidadaptRenderers(){};
- protected:
- bool_enabled;
- bool_bright;
- bool_touchEnabled;
- bool_touchPassedEnabled;
- bool_focus;
- BrightStyle_brightStyle;
- Point_touchStartPos;
- Point_touchMovePos;
- Point_touchEndPos;
-
- SEL_TouchEvent_touchEventSelector;
- std::string_name;
- WidgetType_widgetType;
- int_actionTag;
- Size_size;
- Size_customSize;
- bool_ignoreSize;
- bool_affectByClipping;
- SizeType_sizeType;
- Point_sizePercent;
- PositionType_positionType;
- Point_positionPercent;
- bool_reorderWidgetChildDirty;
- bool_hitted;
- EventListenerTouchOneByOne*_touchListener;
- Color3B_color;
- GLubyte_opacity;
- bool_flippedX;
- bool_flippedY;
- Map<int,LayoutParameter*>_layoutParameterDictionary;
-
- };
- }
-
- NS_CC_END
-
- #endif/*defined(__Widget__)*/
copy
/****************************************************************************
#include"ui/UIWidget.h"
#include"ui/UILayout.h"
#include"ui/UIHelper.h"
NS_CC_BEGIN
namespaceui{
Widget::Widget():
_enabled(true),
_bright( _touchEnabled(false),
_touchPassedEnabled( _focus( _brightStyle(BRIGHT_NONE),
_touchStartPos(Point::ZERO),
_touchMovePos(Point::ZERO),
_touchEndPos(Point::ZERO),
_touchEventSelector(nullptr),
_name("default"),
_widgetType(WidgetTypeWidget),
_actionTag(0),
_size(Size::ZERO),
_customSize(Size::ZERO),
_ignoreSize( _affectByClipping( _sizeType(SIZE_ABSOLUTE),
_sizePercent(Point::ZERO),
_positionType(POSITION_ABSOLUTE),
_positionPercent(Point::ZERO),
_reorderWidgetChildDirty( _hitted( _touchListener(nullptr),
_color(Color3B::WHITE),
_opacity(255),
_flippedX( _flippedY(false)
{
}
Widget::~Widget()
{
_touchEventSelector=nullptr;
setTouchEnabled(false);
}
Widget*Widget::create()
{
Widget*widget=newWidget();
if(widget&&widget->init())
{
widget->autorelease();
returnwidget;
}
CC_SAFE_DELETE(widget);
returnnullptr;
}
boolWidget::init()
{
if(ProtectedNode::init())
{
initRenderer();
setBright(true);
ignoreContentAdaptWithSize(//默认内容不适应size大小
setAnchorPoint(Point(0.5f,0.5f));
returntrue;
}
false;
}
voidWidget::onEnter()
{
updateSizeAndPosition();
ProtectedNode::onEnter();
}
voidWidget::onExit()
{
unscheduleUpdate();
ProtectedNode::onExit();
}
voidWidget::visit(Renderer*renderer,51); font-weight:bold">boolparentTransformUpdated)
{
if(_enabled)
{
adaptRenderers();
ProtectedNode::visit(renderer,parentTransform,parentTransformUpdated);
}
}
Widget*Widget::getWidgetParent()
{
dynamic_cast<Widget*>(getParent());
}
voidWidget::setEnabled(boolenabled)
{
_enabled=enabled;
for(auto&child:_children)
{
if(child)
{
Widget*widgetChild=dynamic_cast<Widget*>(child);
if(widgetChild)
{
widgetChild->setEnabled(enabled);
}
}
}
for(auto&child:_protectedChildren)
{
if(widgetChild)
{
widgetChild->setEnabled(enabled);
}
}
}
}
Widget*Widget::getChildByName(char*name)
{
for(auto&child:_children)
{
if(child)
{
Widget*widgetChild=dynamic_cast<Widget*>(child);
if(widgetChild)
{
if(strcmp(widgetChild->getName(),name)==0)
{
returnwidgetChild;
}
}
}
}
returnnullptr;
}
voidWidget::initRenderer()
{
}
voidWidget::setSizeW(int&sizeW)
{
_customSize.width=sizeW;
if(_ignoreSize)
{
_size=getVirtualRendererSize();
}
else
{
_size.width=sizeW;
}
if(_running)
{
Widget*widgetParent=getWidgetParent();
SizepSize;
if(widgetParent)
{
pSize=widgetParent->getSize();
}
else
{
pSize=_parent->getContentSize();
}
floatspx=0.0f;
floatspy=0.0f;
if(pSize.width>0.0f)
{
spx=_customSize.width/pSize.width;
}
if(pSize.height>0.0f)
{
spy=_customSize.height/pSize.height;
}
_sizePercent=Point(spx,spy);
}
onSizeChanged();
}
voidWidget::setSizeH(int&sizeH)
{
_customSize.height=sizeH;
if(_ignoreSize)
{
_size=getVirtualRendererSize();
}
else
{
_size.height=sizeH;
}
if(_running)
{
Widget*widgetParent=getWidgetParent();
SizepSize;
if(widgetParent)
{
pSize=widgetParent->getSize();
}
else
{
pSize=_parent->getContentSize();
}
floatspx=0.0f;
floatspy=0.0f;
if(pSize.width>0.0f)
{
spx=_customSize.width/pSize.width;
}
if(pSize.height>0.0f)
{
spy=_customSize.height/pSize.height;
}
_sizePercent=Point(spx,spy);
}
onSizeChanged();
}
voidWidget::setSize(constSize&size)
{
_customSize=size;
else
{
_size=size;
}
}
onSizeChanged();
}
voidWidget::setSizePercent(constPoint&percent)
{
_sizePercent=percent;
SizecSize=_customSize;
if(_running)
{
Widget*widgetParent=getWidgetParent();
if(widgetParent)
{
cSize=Size(widgetParent->getSize().width*percent.x,widgetParent->getSize().height*percent.y);
}
else
{
cSize=Size(_parent->getContentSize().width*percent.x,_parent->getContentSize().height*percent.y);
}
}
else
{
_size=cSize;
}
_customSize=cSize;
onSizeChanged();
}
voidWidget::updateSizeAndPosition()
{
Widget*widgetParent=getWidgetParent();
SizepSize;
if(widgetParent)
{
pSize=widgetParent->getLayoutSize();
}
else
{
pSize=_parent->getContentSize();
}
updateSizeAndPosition(pSize);
}
voidWidget::updateSizeAndPosition(constcocos2d::Size&parentSize)
{
switch(_sizeType)
{
caseSIZE_ABSOLUTE:
{
if(_ignoreSize)
{
_size=getVirtualRendererSize();
}
else
{
_size=_customSize;
}
if(parentSize.width>0.0f)
{
spx=_customSize.width/parentSize.width;
}
if(parentSize.height>0.0f)
{
spy=_customSize.height/parentSize.height;
}
_sizePercent=Point(spx,spy);
break;
}
caseSIZE_PERCENT:
{
SizecSize=Size(parentSize.width*_sizePercent.x,parentSize.height*_sizePercent.y);
//不忽略自定义大小
{
_size=cSize;
}
_customSize=cSize;
default:
break;
}
onSizeChanged();
PointabsPos=getPosition();
switch(_positionType)
{
casePOSITION_ABSOLUTE:
{
if(parentSize.width<=0.0f||parentSize.height<=0.0f)
{
_positionPercent=Point::ZERO;
}
else
{
_positionPercent=Point(absPos.x/parentSize.width,absPos.y/parentSize.height);
}
break;
}
casePOSITION_PERCENT:
{
absPos=Point(parentSize.width*_positionPercent.x,parentSize.height*_positionPercent.y);
break;
}
setPosition(absPos);
}
voidWidget::setSizeType(SizeTypetype)
{
_sizeType=type;
}
SizeTypeWidget::getSizeType()const
{
return_sizeType;
}
voidWidget::ignoreContentAdaptWithSize(boolignore)
{
if(_ignoreSize==ignore)
{
return;
}
_ignoreSize=ignore;
//忽略自定义size
{
Sizes=getVirtualRendererSize();
_size=s;
}
//不忽略自定义size
{
_size=_customSize;
}
onSizeChanged();
}
boolWidget::isIgnoreContentAdaptWithSize()const
{
return_ignoreSize;
}
constSize&Widget::getSize()const
{
return_size;
}
constSize&Widget::getCustomSize()return_customSize;
}
constPoint&Widget::getSizePercent()return_sizePercent;
}
PointWidget::getWorldPosition()
{
returnconvertToWorldSpace(Point(_anchorPoint.x*_contentSize.width,_anchorPoint.y*_contentSize.height));
}
Node*Widget::getVirtualRenderer()
{
this;
}
voidWidget::onSizeChanged()
{
setContentSize(_size);
for(auto&child:getChildren())
{
Widget*widgetChild=if(widgetChild)
{
widgetChild->updateSizeAndPosition();
}
}
}
constSize&Widget::getVirtualRendererSize()return_contentSize;
}
voidWidget::updateContentSizeWithTextureSize(constcocos2d::Size&size)
{
if(_ignoreSize)
{
_size=size;
}
else
{
_size=_customSize;
}
onSizeChanged();
}
voidWidget::setTouchEnabled(boolenable)
{
if(enable==_touchEnabled)
{
return;
}
_touchEnabled=enable;
if(_touchEnabled)
{
_touchListener=EventListenerTouchOneByOne::create();
CC_SAFE_RETAIN(_touchListener);
_touchListener->setSwallowTouches(//不向下触摸,简单点来说,比如有两个sprite,A和B,A在上B在下(位置重叠),触摸A的时候,B不会受到影响
_touchListener->onTouchBegan=CC_CALLBACK_2(Widget::onTouchBegan,this);
_touchListener->onTouchMoved=CC_CALLBACK_2(Widget::onTouchMoved,255); font-weight:bold">this);
_touchListener->onTouchEnded=CC_CALLBACK_2(Widget::onTouchEnded,255); font-weight:bold">this);
_touchListener->onTouchCancelled=CC_CALLBACK_2(Widget::onTouchCancelled,255); font-weight:bold">this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener,255); font-weight:bold">this);
}
else
{
_eventDispatcher->removeEventListener(_touchListener);
CC_SAFE_RELEASE_NULL(_touchListener);
}
}
boolWidget::isTouchEnabled()return_touchEnabled;
}
boolWidget::isFocused()return_focus;
}
voidWidget::setFocused(boolfucos)
{
if(fucos==_focus)
{
return;
}
_focus=fucos;
if(_bright)
{
if(_focus)
{
setBrightStyle(BRIGHT_HIGHLIGHT);
}
//如果没有选中
{
setBrightStyle(BRIGHT_NORMAL);
}
}
//如果不亮
{
onPressStateChangedToDisabled();
}
}
voidWidget::setBright(boolbright)
{
_bright=bright;
//如果亮
{
_brightStyle=BRIGHT_NONE;
setBrightStyle(BRIGHT_NORMAL);
}
voidWidget::setBrightStyle(BrightStylestyle)
{
if(_brightStyle==style)
{
return;
}
_brightStyle=style;
switch(_brightStyle)
{
caseBRIGHT_NORMAL:
onPressStateChangedToNormal();
break;
caseBRIGHT_HIGHLIGHT:
onPressStateChangedToPressed();
break;
default:
break;
}
}
voidWidget::onPressStateChangedToNormal()
{
}
voidWidget::onPressStateChangedToPressed()
{
}
voidWidget::onPressStateChangedToDisabled()
{
}
voidWidget::didNotSelectSelf()
{
}
voidWidget::logInfo(WidgetInfoType_type)
{
log("widgetanchorx=%f,y=%f",getAnchorPoint().x,getAnchorPoint().y);
if(_type==WIDGET_SIZE){
log("widgetsizewidth=%f,height=%f",_size.width,_size.height);
}
if(_type==WIDGET_POSITION){
log("widgetpositionx=%f,getPositionX(),getPositionY());
}
}
boolWidget::onTouchBegan(Touch*touch,Event*unusedEvent)
{
_hitted=false;
if(isEnabled()&&isTouchEnabled())
{
_touchStartPos=touch->getLocation();
if(hitTest(_touchStartPos)&&clippingParentAreaContainPoint(_touchStartPos))
{
_hitted=true;
}
}
if(!_hitted)
{
//停止
}
setFocused(//得到焦点
Widget*widgetParent=getWidgetParent();
if(widgetParent)
{
widgetParent->checkChildInfo(0,255); font-weight:bold">this,_touchStartPos);
}
pushDownEvent();
return!_touchPassedEnabled;
}
voidWidget::onTouchMoved(Touch*touch,Event*unusedEvent)
{
_touchMovePos=touch->getLocation();
setFocused(hitTest(_touchMovePos));
Widget*widgetParent=getWidgetParent();
if(widgetParent)
{
widgetParent->checkChildInfo(1,_touchMovePos);
}
moveEvent();
}
voidWidget::onTouchEnded(Touch*touch,Event*unusedEvent)
{
_touchEndPos=touch->getLocation();
boolfocus=_focus;
setFocused(false);
Widget*widgetParent=getWidgetParent();
if(widgetParent)
{
widgetParent->checkChildInfo(2,_touchEndPos);
}
if(focus)
{
releaseUpEvent();
}
else
{
cancelUpEvent();
}
}
voidWidget::onTouchCancelled(Touch*touch,Event*unusedEvent)
{
setFocused(false);
cancelUpEvent();
}
voidWidget::pushDownEvent()
{
if(_touchEventSelector)
{
_touchEventSelector(//响应开始按下的回调
}
}
voidWidget::moveEvent()
{
if(_touchEventSelector)
{
_touchEventSelector(//响应移动的回调
}
}
voidWidget::releaseUpEvent()
{
_touchEventSelector(//响应结束的回调
}
}
voidWidget::cancelUpEvent()
{
if(_touchEventSelector)
{
_touchEventSelector(//响应取消按下的回调
}
}
voidWidget::addTouchEventListener(SEL_TouchEventselector)
{
_touchEventSelector=selector;
}
boolWidget::hitTest(constPoint&pt)
{
Pointnsp=convertToNodeSpace(pt);
Rectbb;
bb.size=_contentSize;
if(bb.containsPoint(nsp))
{
//点在size内hit~!
}
false;
}
boolWidget::clippingParentAreaContainPoint(constPoint&pt)
{
_affectByClipping=false;
Widget*parent=getWidgetParent();
Widget*clippingParent=nullptr;
while(parent)
{
Layout*layoutParent=dynamic_cast<Layout*>(parent);
if(layoutParent)
{
if(layoutParent->isClippingEnabled())
{
_affectByClipping=//受裁切的影响
clippingParent=layoutParent;
break;
}
}
parent=parent->getWidgetParent();
}
if(!_affectByClipping)
{
true;
}
if(clippingParent)
{
boolbRet=false;
if(clippingParent->hitTest(pt))
{
bRet=if(bRet)
{
returnclippingParent->clippingParentAreaContainPoint(pt);
}
false;
}
true;
}
voidWidget::checkChildInfo(constPoint&touchPoint)
{
Widget*widgetParent=getWidgetParent();
if(widgetParent)
{
widgetParent->checkChildInfo(handleState,sender,touchPoint);
}
}
voidWidget::setPosition(constPoint&pos)
{
if(_running)
{
Widget*widgetParent=getWidgetParent();
//若有父节点
{
SizepSize=widgetParent->getSize();
if(pSize.width<=0.0f||pSize.height<=0.0f)
{
_positionPercent=Point::ZERO;
}
else
{
_positionPercent=Point(pos.x/pSize.width,pos.y/pSize.height);
}
}
}
ProtectedNode::setPosition(pos);
}
voidWidget::setPositionPercent(constPoint&percent)
{
_positionPercent=percent;
if(_running)
{
Widget*widgetParent=getWidgetParent();
if(widgetParent)
{
SizeparentSize=widgetParent->getSize();
PointabsPos=Point(parentSize.width*_positionPercent.x,119)">//绝对坐标
setPosition(absPos);
}
}
}
constPoint&Widget::getPositionPercent()
{
return_positionPercent;
}
voidWidget::setPositionType(PositionTypetype)
{
_positionType=type;
}
PositionTypeWidget::getPositionType()return_positionType;
}
boolWidget::isBright()return_bright;
}
boolWidget::isEnabled()return_enabled;
}
floatWidget::getLeftInParent()
{
returngetPosition().x-getAnchorPoint().x*_size.width;;
}
floatWidget::getBottomInParent()
{
returngetPosition().y-getAnchorPoint().y*_size.height;;
}
floatWidget::getRightInParent()
{
returngetLeftInParent()+_size.width;
}
floatWidget::getTopInParent()
{
returngetBottomInParent()+_size.height;
}
constPoint&Widget::getTouchStartPos()
{
return_touchStartPos;
}
constPoint&Widget::getTouchMovePos()
{
return_touchMovePos;
}
constPoint&Widget::getTouchEndPos()
{
return_touchEndPos;
}
voidWidget::setName(char*name)
{
_name=name;
}
char*Widget::getName()return_name.c_str();
}
WidgetTypeWidget::getWidgetType()return_widgetType;
}
voidWidget::setLayoutParameter(LayoutParameter*parameter)
{
if(!parameter)
{
return;
}
_layoutParameterDictionary.insert(parameter->getLayoutType(),parameter);
}
LayoutParameter*Widget::getLayoutParameter(LayoutParameterTypetype)
{
dynamic_cast<LayoutParameter*>(_layoutParameterDictionary.at(type));
}
std::stringWidget::getDescription()return"Widget";
}
Widget*Widget::clone()
{
Widget*clonedWidget=createCloneInstance();
clonedWidget->copyProperties(this);
clonedWidget->copyClonedWidgetChildren(//拷贝所有孩子
returnclonedWidget;
}
Widget*Widget::createCloneInstance()
{
returnWidget::create();
}
voidWidget::copyClonedWidgetChildren(Widget*model)
{
auto&modelChildren=model->getChildren();
for(auto&subWidget:modelChildren)
{
Widget*child=dynamic_cast<Widget*>(subWidget);
if(child)
{
addChild(child->clone());
}
}
}
voidWidget::copySpecialProperties(Widget*model)
{
}
voidWidget::copyProperties(Widget*widget)
{
setEnabled(widget->isEnabled());
setVisible(widget->isVisible());
setBright(widget->isBright());
setTouchEnabled(widget->isTouchEnabled());
_touchPassedEnabled=false;
setLocalZOrder(widget->getLocalZOrder());
setTag(widget->getTag());
setName(widget->getName());
setActionTag(widget->getActionTag());
_ignoreSize=widget->_ignoreSize;
_size=widget->_size;
_customSize=widget->_customSize;
copySpecialProperties(widget);
_sizeType=widget->getSizeType();
_sizePercent=widget->_sizePercent;
_positionType=widget->_positionType;
_positionPercent=widget->_positionPercent;
setPosition(widget->getPosition());
setAnchorPoint(widget->getAnchorPoint());
setScaleX(widget->getScaleX());
setScaleY(widget->getScaleY());
setRotation(widget->getRotation());
setRotationSkewX(widget->getRotationSkewX());
setRotationSkewY(widget->getRotationSkewY());
setFlippedX(widget->isFlippedX());
setFlippedY(widget->isFlippedY());
setColor(widget->getColor());
setOpacity(widget->getOpacity());
Map< for(autoiter=layoutParameterDic.begin();iter!=layoutParameterDic.end();++iter)
{
setLayoutParameter(iter->second->clone());
}
onSizeChanged();
}
voidWidget::setColor(constColor3B&color)
{
_color=color;
updateTextureColor();
}
voidWidget::setOpacity(GLubyteopacity)
{
_opacity=opacity;
updateTextureOpacity();
}
voidWidget::setFlippedX(boolflippedX)
{
_flippedX=flippedX;
updateFlippedX();
}
voidWidget::setFlippedY(boolflippedY)
{
_flippedY=flippedY;
updateFlippedY();
}
voidWidget::updateColorToRenderer(Node*renderer)
{
renderer->setColor(_color);
}
voidWidget::updateOpacityToRenderer(Node*renderer)
{
renderer->setOpacity(_opacity);
}
voidWidget::updateRGBAToRenderer(Node*renderer)
{
renderer->setColor(_color);
renderer->setOpacity(_opacity);
}
voidWidget::setActionTag(inttag)
{
_actionTag=tag;
}
intWidget::getActionTag()
{
return_actionTag;
}
}
NS_CC_END
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|