转自:http://blog.csdn.net/yhhwatl/article/details/26487731
从今天开始 咱也模仿 红孩儿这些大牛分析源码 ,由于水平有限 不对之处欢迎狂喷。哈哈。
- #ifndef__UIWIDGET_H__ @H_403_46@ #define__UIWIDGET_H__
- @H_403_46@ #include"ui/CCProtectedNode.h"
- #include"ui/UILayoutDefine.h" @H_403_46@ #include"ui/UILayoutParameter.h"
- #include"ui/GUIDefine.h" @H_403_46@
- NS_CC_BEGIN @H_403_46@
- namespaceui{ @H_403_46@
- typedefenum @H_403_46@ {
- BRIGHT_NONE=-1, @H_403_46@ BRIGHT_NORMAL,//正常
- BRIGHT_HIGHLIGHT//高亮按钮点击之后 @H_403_46@ }BrightStyle;//明亮的风格
- @H_403_46@ enum
- { @H_403_46@ WidgetTypeWidget,119)">//control基本UIWidget默认
- WidgetTypeContainer//container容器layout实例化 @H_403_46@ }WidgetType;//ui类型
- @H_403_46@ enum
- { @H_403_46@ UI_TEX_TYPE_LOCAL=0,119)">//meanslocalfile
- UI_TEX_TYPE_PLIST=1//meansspriteframe @H_403_46@ }TextureResType;//贴图类型
- @H_403_46@ enumclassTouchEventType
- { @H_403_46@ TOUCH_EVENT_BEGAN,119)">//开始
- TOUCH_EVENT_MOVED,119)">//移动 @H_403_46@ TOUCH_EVENT_ENDED,119)">//结束
- TOUCH_EVENT_CANCELED//取消 @H_403_46@ };//触摸事件类型
- @H_403_46@ enum
- { @H_403_46@ SIZE_ABSOLUTE,119)">//绝对值
- SIZE_PERCENT//百分比值 @H_403_46@ }SizeType;//size值类型
- @H_403_46@ enum
- { @H_403_46@ POSITION_ABSOLUTE,119)">//绝对真实位置
- POSITION_PERCENT//相对父节点位置百分比 @H_403_46@ }PositionType;//位置类型
- @H_403_46@ enum
- { @H_403_46@ WIDGET_SIZE,119)">//size
- WIDGET_POSITION//position @H_403_46@
- }WidgetInfoType;//位置类型 @H_403_46@ //哦这里说明一下回调函数被我修改了下主要是这样修改之后可以用到C++11的lambda表达式
- //不必在使用的时候多声明一个回调函数了比较方便希望引擎组可以采用这种方式。 @H_403_46@ typedefstd::function<void(Ref*,TouchEventType)>SEL_TouchEvent;
- @H_403_46@ //函数指针指向返回值void参数列表(Ref*,TouchEventType)类型的函数
- //typedefvoid(Ref::*SEL_TouchEvent)(Ref*,TouchEventType); @H_403_46@ //宏定义给函数指针SEL_TouchEvent取别名为toucheventselector
- #definetoucheventselector(_SELECTOR)(SEL_TouchEvent)(&_SELECTOR) @H_403_46@
- classWidget:publicProtectedNode @H_403_46@ {
- public: @H_403_46@ Widget(void);
- virtual~Widget(); @H_403_46@ staticWidget*create();
- /**设置是否触摸&&可见默认true*/ @H_403_46@ virtualvoidsetEnabled(boolenabled);
- boolisEnabled()const; @H_403_46@ /**设置是否明亮默认true*/
- voidsetBright(boolbright); @H_403_46@ /**是否明亮*/
- boolisBright()/**是否可以触摸(响应事件)默认true*/
- voidsetTouchEnabled(boolenabled); @H_403_46@ /**设置明亮风格正常or明亮默认正常*/
- voidsetBrightStyle(BrightStylestyle); @H_403_46@ /**是否可以触摸*/
- boolisTouchEnabled()/**是否被选中默认false*/
- boolisFocused()/**是否被选中默认false*/
- voidsetFocused(boolfucosed); @H_403_46@ /**获取此控件距离左节点的下边界长度*/
- floatgetLeftInParent(); @H_403_46@ /**获取此控件距离父节点的下边界长度*/
- floatgetBottomInParent(); @H_403_46@ /**获取此控件距离父节点的右边界长度*/
- floatgetRightInParent(); @H_403_46@ /**获取此控件距离父节点的上边界长度*/
- floatgetTopInParent(); @H_403_46@
- /**获取一个孩子从容器根据它的名字*/ @H_403_46@ virtualWidget*getChildByName(constchar*name);
- /**渲染重载*/ @H_403_46@ voidvisit(cocos2d::Renderer*renderer,constkmMat4&parentTransform,boolparentTransformUpdated)override;
- @H_403_46@ /**添加回调事件*/
- voidaddTouchEventListener(SEL_TouchEventselector); @H_403_46@ /**打印属性debug*/
- voidlogInfo(WidgetInfoType_type); @H_403_46@
- //cocos2d属性 @H_403_46@ /**
- 基类有的函数为virtual有的不是 @H_403_46@ 不带virtual的是所有子类都拥有这个函数的功能。
- 带virtual的是拥有上述功能之外重载父类的函数想拓展功能用的。 @H_403_46@ 带virtual的函数可以使用多态即父类指针指向子类成员
- */ @H_403_46@
- /** @H_403_46@ *更改在OpenGL坐标系中widget的位置(X,Y)
- *原来的点(0,0)是在屏幕的左下角。 @H_403_46@ *@参数位置在OpenGL坐标widget的位置(X,Y)
- */ @H_403_46@ voidsetPosition(constPoint&pos)override;
- /**同上绝对位置变为百分比*/ @H_403_46@ voidsetPositionPercent(constPoint&percent);
- /**得到在OpenGL坐标系中widget的百分比位置(X,Y)*/ @H_403_46@ constPoint&getPositionPercent();
- @H_403_46@ /**设置位置类型*/
- voidsetPositionType(PositionTypetype); @H_403_46@
- /**得到位置类型*/ @H_403_46@ PositionTypegetPositionType()const;
- @H_403_46@ /**设置该widget是否水平翻转*/
- voidsetFlippedX(boolflippedX); @H_403_46@
- /**该widget是否水平翻转*/ @H_403_46@ virtualboolisFlippedX(){return_flippedX;};
- @H_403_46@ /**该widget是否垂直翻转*/
- voidsetFlippedY(boolflippedY); @H_403_46@
- /**该widget是否垂直翻转*/ @H_403_46@ boolisFlippedY(){return_flippedY;};
- /**设置颜色*/ @H_403_46@ voidsetColor(constColor3B&color)override;
- /**设置透明度*/ @H_403_46@ voidsetOpacity(GLubyteopacity)override;
- /**得到颜色值*/ @H_403_46@ constColor3B&getColor()constoverride{return_color;};
- /**得到透明度*/ @H_403_46@ GLubytegetOpacity()return_opacity;};
- /**弃用的属性*/ @H_403_46@ /**@deprecatedUseisFlippedX()instead*/
- CC_DEPRECATED_ATTRIBUTEboolisFlipX(){returnisFlippedX();}; @H_403_46@ /**@deprecatedUsesetFlippedX()instead*/
- CC_DEPRECATED_ATTRIBUTEvoidsetFlipX(boolflipX){setFlippedX(flipX);}; @H_403_46@ /**@deprecatedUseisFlippedY()instead*/
- CC_DEPRECATED_ATTRIBUTEboolisFlipY(){returnisFlippedY();}; @H_403_46@ /**@deprecatedUsesetFlippedY()instead*/
- CC_DEPRECATED_ATTRIBUTEvoidsetFlipY(boolflipY){setFlippedY(flipY);}; @H_403_46@
- /**当该widget失去焦点会调用*/ @H_403_46@ voiddidNotSelectSelf();
- @H_403_46@ /**检查一个点是否在父节点区域*/
- boolclippingParentAreaContainPoint(constPoint&pt); @H_403_46@
- /**发送触摸事件到widget的父节点*/ @H_403_46@ voidcheckChildInfo(inthandleState,Widget*sender,255); font-weight:bold">constPoint&touchPoint);
- @H_403_46@ /**Getsthetouchbeganpointofwidgetwhenwidgetisselected.*/
- constPoint&getTouchStartPos(); @H_403_46@
- /**Getsthetouchmovepointofwidgetwhenwidgetisselected*/ @H_403_46@ constPoint&getTouchMovePos();
- @H_403_46@ /**Getsthetouchendpointofwidgetwhenwidgetisselected.*/
- constPoint&getTouchEndPos(); @H_403_46@
- /**设置name*/ @H_403_46@ voidsetName(char*name);
- @H_403_46@ /**得到name*/
- char*getName()const; @H_403_46@
- /**得到控件类型*/ @H_403_46@ WidgetTypegetWidgetType()/**设置控件大小*/
- voidsetSize(constSize&size); @H_403_46@ voidsetSizeW(int&sizeW);
- voidsetSizeH(int&sizeH); @H_403_46@ /**设置size百分比*/
- voidsetSizePercent(constPoint&percent); @H_403_46@
- /**size类型*/ @H_403_46@ voidsetSizeType(SizeTypetype);
- @H_403_46@ /**得到size类型*/
- SizeTypegetSizeType()/**得到size*/ @H_403_46@ constSize&getSize()/**得到Customsize*/
- constSize&getCustomSize()/**得到layoutsize*/
- constSize&getLayoutSize(){return_size;};//虚函数供子类重写? @H_403_46@
- /**得到size百分比*/ @H_403_46@ constPoint&getSizePercent()/***/
- boolhitTest(constPoint&pt); @H_403_46@ /**触摸事件*/
- boolonTouchBegan(Touch*touch,Event*unusedEvent); @H_403_46@ voidonTouchMoved(Touch*touch,Event*unusedEvent);
- voidonTouchEnded(Touch*touch,255); font-weight:bold">voidonTouchCancelled(Touch*touch,Event*unusedEvent);
- @H_403_46@ /**设置布局参数*/
- voidsetLayoutParameter(LayoutParameter*parameter); @H_403_46@
- /**得到布局参数*/ @H_403_46@ LayoutParameter*getLayoutParameter(LayoutParameterTypetype);
- @H_403_46@ /**是否忽略size使用texturesize默认true*/
- voidignoreContentAdaptWithSize(boolignore); @H_403_46@
- /**是否忽略size使用texturesize*/ @H_403_46@ boolisIgnoreContentAdaptWithSize()/**得到世界位置*/
- PointgetWorldPosition(); @H_403_46@
- /**得到虚拟渲染器例如一个button的虚拟渲染器是他的texturerenderer贴图渲染器*/ @H_403_46@ virtualNode*getVirtualRenderer();
- @H_403_46@ /**得到虚拟渲染器的size默认是_contentSize不同的子类有自己的重载*/
- constSize&getVirtualRendererSize()const; @H_403_46@
- @H_403_46@
- *得到改控件对应的"classname".
- virtualstd::stringgetDescription()constoverride;
- /**克隆替身*/ @H_403_46@ Widget*clone();
- @H_403_46@ voidonEnter()override;
- voidonExit()override; @H_403_46@ /**更新size和position*/
- voidupdateSizeAndPosition(); @H_403_46@
- voidupdateSizeAndPosition(constSize&parentSize); @H_403_46@
- /*tempaction*/ @H_403_46@ voidsetActionTag(inttag);
- intgetActionTag(); @H_403_46@
- CC_CONSTRUCTOR_ACCESS: @H_403_46@ //initializesstateofwidget.
- boolinit()override; @H_403_46@
- protected: @H_403_46@ //callbackfunctioncalledwhensizechanged.
- voidonSizeChanged(); @H_403_46@
- //initializesrendererofwidget. @H_403_46@ voidinitRenderer();
- @H_403_46@ //回复正常调用.
- voidonPressStateChangedToNormal(); @H_403_46@
- //选中会调用 @H_403_46@ voidonPressStateChangedToPressed();
- @H_403_46@ //变暗会调用
- voidonPressStateChangedToDisabled(); @H_403_46@ //不同的事件
- voidpushDownEvent(); @H_403_46@ voidmoveEvent();
- voidreleaseUpEvent(); @H_403_46@ voidcancelUpEvent();
- //空的虚函数为了多态 @H_403_46@ voidupdateTextureColor(){};
- voidupdateTextureOpacity(){}; @H_403_46@ voidupdateTextureRGBA(){};
- voidupdateFlippedX(){}; @H_403_46@ voidupdateFlippedY(){};
- @H_403_46@ voidupdateColorToRenderer(Node*renderer);
- voidupdateOpacityToRenderer(Node*renderer); @H_403_46@ voidupdateRGBAToRenderer(Node*renderer);
- voidcopyProperties(Widget*model); @H_403_46@
- virtualWidget*createCloneInstance(); @H_403_46@ voidcopySpecialProperties(Widget*model);
- voidcopyClonedWidgetChildren(Widget*model); @H_403_46@ Widget*getWidgetParent();
- voidupdateContentSizeWithTextureSize(constSize&size); @H_403_46@ /**多态适用于所有的子类重载实现自己的逻辑*/
- voidadaptRenderers(){}; @H_403_46@ protected:
- bool_enabled;///<是否可见&&可触摸最高属性 @H_403_46@ bool_bright;///<是否光亮
- bool_touchEnabled;///<是否响应触摸 @H_403_46@ bool_touchPassedEnabled;///<触摸时间是不是按下
- bool_focus;///<是不是焦点 @H_403_46@ BrightStyle_brightStyle;///<明亮风格
- Point_touchStartPos;///<touchbeganpoint @H_403_46@ Point_touchMovePos;///<touchmovedpoint
- Point_touchEndPos;///<touchendedpoint @H_403_46@
- SEL_TouchEvent_touchEventSelector;///回调函数类型 @H_403_46@ std::string_name;///类名
- WidgetType_widgetType;///控件类型 @H_403_46@ int_actionTag;///执行动作的tag
- Size_size;///untransformedsize @H_403_46@ Size_customSize;///自定义size
- bool_ignoreSize;///是否忽略自定义的大小而取资源原本的大小untransformedimgsize默认_size=_customSize @H_403_46@ bool_affectByClipping;
- SizeType_sizeType;///size类型 @H_403_46@ Point_sizePercent;///相对父节点size百分比
- PositionType_positionType;///位置类型 @H_403_46@ Point_positionPercent;///相对父节点的位置百分比
- bool_reorderWidgetChildDirty; @H_403_46@ bool_hitted;
- EventListenerTouchOneByOne*_touchListener;///单点触摸 @H_403_46@ Color3B_color;///颜色值
- GLubyte_opacity;///透明度 @H_403_46@ bool_flippedX;//是否镜像X轴
- bool_flippedY;//是否镜像Y轴 @H_403_46@ Map<int,LayoutParameter*>_layoutParameterDictionary;//布局参数Map
- @H_403_46@ };
- } @H_403_46@
- NS_CC_END @H_403_46@
- #endif/*defined(__Widget__)*/