coco2dx3.1 弹出框实现

前端之家收集整理的这篇文章主要介绍了coco2dx3.1 弹出框实现前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2dx 弹出框 网上有好多实现 但是都不是想要的

此弹出框功能自定义添加按钮 title 内容 模态实现

直接上代码

文件

  1. #ifndef _POP_LAYER_H_
  2. #define _POP_LAYER_H_
  3.  
  4. #include "cocos2d.h"
  5. #include "extensions/cocos-ext.h"
  6. USING_NS_CC;
  7.  
  8. using namespace cocos2d::extension;
  9.  
  10. class PopupLayer: public CCLayer{
  11. public:
  12. PopupLayer();
  13. ~PopupLayer();
  14.  
  15. virtual bool init();
  16. CREATE_FUNC(PopupLayer);
  17.  
  18. //void registerWithTouchDispatcher(void);
  19. //bool onTouchBegan(cocos2d::CCTouch *pTouch,cocos2d::CCEvent *pEvent);
  20.  
  21. static PopupLayer* create(const char* backgroundImage);
  22.  
  23. void setTitle(const char* title,int fontsize = 20);
  24. void setContentText(const char* text,int fontsize = 20,int padding = 50,int paddintTop = 100);
  25.  
  26. void setCallbackFunc(Ref* target,SEL_CallFuncN callfun);
  27.  
  28. bool addButton(const char* normalImage,const char* selectedImage,const char* title,int tag = 0);
  29. virtual void onEnter();
  30. virtual void onExit();
  31.  
  32. void buttonCallback(Ref* pSender);
  33.  
  34. bool onTouchBegan(Touch* touch,Event* event);
  35. void onTouchMoved(Touch* touch,Event* event);
  36. void onTouchEnded(Touch* touch,Event* event);
  37.  
  38. private:
  39.  
  40.  
  41. // 文字内容两边的空白区
  42. int m_contentPadding;
  43. int m_contentPaddingTop;
  44.  
  45. CCObject* m_callbackListener;
  46. SEL_CallFuncN m_callback;
  47.  
  48. CC_SYNTHESIZE_RETAIN(Menu*,m__pMenu,MenuButton);
  49. CC_SYNTHESIZE_RETAIN(Sprite*,m__sfBackGround,SpriteBackGround);
  50. CC_SYNTHESIZE_RETAIN(Scale9Sprite*,m__s9BackGround,Sprite9BackGround);
  51. CC_SYNTHESIZE_RETAIN(CCLabelTTF*,m__ltTitle,LabelTitle);
  52. CC_SYNTHESIZE_RETAIN(CCLabelTTF*,m__ltContentText,LabelContentText);
  53.  
  54.  
  55. };
  56.  
  57. #endif;

类名字 随便起的 大家就不要吐槽了………………


cpp文件:

  1. #include "Poplayer.h"
  2.  
  3. PopupLayer::PopupLayer():
  4. m__pMenu(NULL),m_contentPadding(0),m_contentPaddingTop(0),m_callbackListener(NULL),m_callback(NULL),m__sfBackGround(NULL),m__s9BackGround(NULL),m__ltContentText(NULL),m__ltTitle(NULL)
  5. {
  6.  
  7. }
  8.  
  9. PopupLayer::~PopupLayer(){
  10. CC_SAFE_RELEASE(m__pMenu);
  11. CC_SAFE_RELEASE(m__sfBackGround);
  12. CC_SAFE_RELEASE(m__ltContentText);
  13. CC_SAFE_RELEASE(m__ltTitle);
  14. CC_SAFE_RELEASE(m__s9BackGround);
  15. }
  16.  
  17. bool PopupLayer::init(){
  18. bool bRef = false;
  19. do{
  20. CC_BREAK_IF(!CCLayer::init());
  21. this->setContentSize(CCSizeZero);
  22.  
  23. // 初始化需要的 Menu
  24. CCMenu* menu = CCMenu::create();
  25. menu->setPosition(CCPointZero);
  26. setMenuButton(menu);
  27.  
  28. setTouchEnabled(true);
  29.  
  30. auto listener = EventListenerTouchOneByOne::create();
  31. listener->setSwallowTouches(true);
  32.  
  33. listener->onTouchBegan = CC_CALLBACK_2(PopupLayer::onTouchBegan,this);
  34. listener->onTouchMoved = CC_CALLBACK_2(PopupLayer::onTouchMoved,this);
  35. listener->onTouchEnded = CC_CALLBACK_2(PopupLayer::onTouchEnded,this);
  36.  
  37. _eventDispatcher->addEventListenerWithSceneGraPHPriority(listener,this);
  38.  
  39.  
  40. bRef = true;
  41. }while(0);
  42. return bRef;
  43. }
  44.  
  45.  
  46. bool PopupLayer::onTouchBegan(Touch* pTouch,Event* event)
  47. {
  48.  
  49. CCLog("PopupLayer touch");
  50. return true;
  51. }
  52.  
  53. void PopupLayer::onTouchMoved(Touch* touch,Event* event)
  54. {
  55. log("move");
  56. }
  57.  
  58. void PopupLayer::onTouchEnded(Touch* touch,Event* event)
  59. {
  60. log("ended");
  61. }
  62.  
  63. PopupLayer* PopupLayer::create(const char *backgroundImage){
  64. PopupLayer* ml = PopupLayer::create();
  65. ml->setSpriteBackGround(Sprite::create(backgroundImage));
  66. ml->setSprite9BackGround(Scale9Sprite::create(backgroundImage));
  67. return ml;
  68. }
  69.  
  70. void PopupLayer::setTitle(const char *title,int fontsize){
  71. CCLabelTTF* ltfTitle = LabelTTF::create(title,"Arial",fontsize);
  72. setLabelTitle(ltfTitle);
  73. }
  74.  
  75. void PopupLayer::setContentText(const char *text,int fontsize,int padding,int paddingTop){
  76. CCLabelTTF* ltf = CCLabelTTF::create(text,fontsize);
  77. setLabelContentText(ltf);
  78. m_contentPadding = padding;
  79. m_contentPaddingTop = paddingTop;
  80. }
  81.  
  82. void PopupLayer::setCallbackFunc(Ref* target,SEL_CallFuncN callfun){
  83. m_callbackListener = target;
  84. m_callback = callfun;
  85. }
  86.  
  87.  
  88. bool PopupLayer::addButton(const char *normalImage,const char *selectedImage,const char *title,int tag){
  89. CCSize winSize = CCDirector::sharedDirector()->getWinSize();
  90. CCPoint pCenter = ccp(winSize.width / 2,winSize.height / 2);
  91.  
  92. // 创建图片菜单按钮
  93. CCMenuItemImage* menuImage = CCMenuItemImage::create(normalImage,selectedImage,this,menu_selector(PopupLayer::buttonCallback));
  94. menuImage->setTag(tag);
  95. menuImage->setPosition(pCenter);
  96.  
  97. // 添加文字说明并设置位置
  98. CCSize imenu = menuImage->getContentSize();
  99. CCLabelTTF* ttf = CCLabelTTF::create(title,20);
  100. ttf->setColor(ccc3(0,0));
  101. ttf->setPosition(ccp(imenu.width / 2,imenu.height / 2));
  102. menuImage->addChild(ttf);
  103.  
  104. getMenuButton()->addChild(menuImage);
  105. return true;
  106. }
  107.  
  108. void PopupLayer::buttonCallback(Ref* pSender){
  109. Node* node = (Node*)pSender;
  110. CCLog("touch tag: %d",node->getTag());
  111. if (m_callback && m_callbackListener){
  112. (m_callbackListener->*m_callback)(node);
  113. }
  114. int tag = node->getTag();
  115. switch(tag)
  116. {
  117. case 0:
  118. {
  119.  
  120. }
  121. case 1:
  122. {
  123.  
  124. }
  125. default:
  126. break;
  127. }
  128. this->removeFromParent();
  129. }
  130.  
  131. void PopupLayer::onEnter(){
  132. CCLayer::onEnter();
  133.  
  134. CCSize winSize = CCDirector::sharedDirector()->getWinSize();
  135. CCPoint pCenter = ccp(winSize.width / 2,winSize.height / 2);
  136.  
  137. CCSize contentSize;
  138. // 设定好参数,在运行时加载
  139. if (getContentSize().equals(CCSizeZero)) {
  140. getSpriteBackGround()->setPosition(ccp(winSize.width / 2,winSize.height / 2));
  141. this->addChild(getSpriteBackGround(),0);
  142. contentSize = getSpriteBackGround()->getTexture()->getContentSize();
  143. } else {
  144. Scale9Sprite *background = getSprite9BackGround();
  145. background->setContentSize(getContentSize());
  146. background->setPosition(ccp(winSize.width / 2,winSize.height / 2));
  147. this->addChild(background,0);
  148. contentSize = getContentSize();
  149. }
  150.  
  151.  
  152. // 添加按钮,并设置其位置
  153. this->addChild(getMenuButton());
  154. float btnWidth = contentSize.width / (getMenuButton()->getChildrenCount() + 1);
  155.  
  156. auto array = getMenuButton()->getChildren();
  157. Ref* pObj = NULL;
  158.  
  159. for(int i = 0;i< array.capacity();i++)
  160. {
  161. Node* node = array.at(i);
  162. node->setPosition(ccp( winSize.width / 2 - contentSize.width / 2 + btnWidth * (i + 1),winSize.height / 2 - contentSize.height / 3));
  163. }
  164.  
  165. // 显示对话框标题
  166. if (getLabelTitle()){
  167. getLabelTitle()->setPosition(ccpAdd(pCenter,ccp(0,contentSize.height / 2 - 35.0f)));
  168. this->addChild(getLabelTitle());
  169. }
  170.  
  171. // 显示文本内容
  172. if (getLabelContentText()){
  173. CCLabelTTF* ltf = getLabelContentText();
  174. ltf->setPosition(ccp(winSize.width / 2,winSize.height / 2));
  175. ltf->setDimensions(CCSizeMake(contentSize.width - m_contentPadding * 2,contentSize.height - m_contentPaddingTop));
  176. ltf->setHorizontalAlignment(kCCTextAlignmentLeft);
  177. this->addChild(ltf);
  178. }
  179.  
  180. // 弹出效果
  181. CCAction* popupLayer = CCSequence::create(CCScaleTo::create(0.0,0.0),CCScaleTo::create(0.06,1.05),CCScaleTo::create(0.08,0.95),1.0),NULL);
  182. this->runAction(popupLayer);
  183.  
  184. }
  185.  
  186. void PopupLayer::onExit(){
  187. CCLog("popup on exit.");
  188. CCLayer::onExit();
  189. }

使用方法:
  1. PopupLayer* pl = PopupLayer::create("popuplayer/BackGround.png");
  2. // ContentSize 是可选的设置,可以不设置,如果设置把它当作 9 图缩放
  3. pl->setContentSize(CCSizeMake(400,350));
  4. pl->setTitle("test");
  5. pl->setContentText(G2U("你好,弹出框"),20,60,250);
  6. // 设置回调函数,回调传回一个 CCNode 以获取 tag 判断点击的按钮
  7. // 这只是作为一种封装实现,如果使用 delegate 那就能够更灵活的控制参数了
  8. pl->setCallbackFunc(this,callfuncN_selector(LoginScene::buttonCallback));
  9. // 添加按钮,设置图片文字,tag 信息
  10. pl->addButton("popuplayer/pop_button.png","popuplayer/pop_button.png",G2U("确定"),0);
  11. pl->addButton("popuplayer/pop_button.png",G2U("取消"),1);
  12. // 添加到当前层
  13. this->addChild(pl);
G2U是 gbk转UTF_8 要不然的话 中文会乱码

函数如下:

  1. char* G2U(const char* gb2312)
  2. {
  3. int len = MultiByteToWideChar(CP_ACP,gb2312,-1,NULL,0);
  4. wchar_t* wstr = new wchar_t[len+1];
  5. memset(wstr,len+1);
  6. MultiByteToWideChar(CP_ACP,wstr,len);
  7. len = WideCharToMultiByte(CP_UTF8,NULL);
  8. char* str = new char[len+1];
  9. memset(str,len+1);
  10. WideCharToMultiByte(CP_UTF8,str,len,NULL);
  11. if(wstr) delete[] wstr;
  12. return str;
  13. }
希望大家喜欢

猜你在找的Cocos2d-x相关文章