cocos2dx3.6 弹出对话框的实现

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

文件

//

// PopAlertDialog.h

// macstudycocos2dx

// Created by WangWei on 15/6/8.

//


#ifndef __macstudycocos2dx__PopAlertDialog__

#define __macstudycocos2dx__PopAlertDialog__


#pragma once

#include "cocos2d.h"

#include "cocos-ext.h"



USING_NS_CC;

USING_NS_CC_EXT;



class PopAlertDialog:public LayerColor{ //继承LayerColor类,方便更改layer的颜色和透明度

public:

PopAlertDialog();

~PopAlertDialog();

virtual bool init();

CREATE_FUNC(PopAlertDialog);

static PopAlertDialog* create(const char* backgroudImage,Size dialogSize);

bool onTouchBegan(Touch* touch,Event* event);

void onTouchMoved(Touch* touch,Event* event);

void onTouchEnded(Touch* touch,Event* event);

void setTitle(const char* title,int fontsize=20);

void setContentText(const char* text,int fontsize=20,int padding=50,int paddingTop=50);

void setCallBackFunc(Ref* target,SEL_CallFuncN callfun);

bool addButton(const char* normalImage,const char* selectedImage,const char* title,int tag=0);

virtual void onEnter();

virtual void onExit();

void backgroundFinish();

private:

void buttonCallBack(Ref* pSender);

int m_contentPadding;

int m_contentPaddingTop;

Size m_dialogContentSize; //对话框大小

Ref* m_callbackListener;

SEL_CallFuncN m_callback;

CC_SYNTHESIZE_RETAIN(Menu*,m__pMenu,MenuButton);

CC_SYNTHESIZE_RETAIN(Sprite*,m__sfBackGround,SpriteBackGround);

CC_SYNTHESIZE_RETAIN(Scale9Sprite*,m__s9BackGround,Sprite9BackGround);

CC_SYNTHESIZE_RETAIN(LabelTTF*,m__ltTitle,LabelTitle);

CC_SYNTHESIZE_RETAIN(LabelTTF*,m__ltContentText,LabelContentText);

};



#endif /* defined(__macstudycocos2dx__PopAlertDialog__) */



cpp文件

// PopAlertDialog.cpp

//


#include "PopAlertDialog.h"


PopAlertDialog::PopAlertDialog()

:

m__pMenu(NULL)

,m_contentPadding(0)

(0)

(NULL)

(NULL)

(NULL)

(NULL)

(NULL)

(NULL)

{

}


PopAlertDialog::~PopAlertDialog(){

CC_SAFE_RELEASE(m__pMenu);

CC_SAFE_RELEASE(m__sfBackGround);

CC_SAFE_RELEASE(m__s9BackGround);

CC_SAFE_RELEASE(m__ltContentText);

CC_SAFE_RELEASE(m__ltTitle);

}


bool PopAlertDialog::init(){

if (!CCLayerColor::init()) {

return false;

}


Menu* menu=Menu::create();

menu->setPosition(CCPointZero);

setMenuButton(menu);

auto listener=EventListenerTouchOneByOne::create();

listener->setSwallowTouches(true);

listener->onTouchBegan=CC_CALLBACK_2(PopAlertDialog::onTouchBegan,this);

listener->onTouchMoved=CC_CALLBACK_2(PopAlertDialog::onTouchMoved,this);

listener->onTouchEnded=CC_CALLBACK_2(PopAlertDialog::onTouchEnded,this);

auto dispatcher=Director::getInstance()->getEventDispatcher();

dispatcher->addEventListenerWithSceneGraPHPriority(listener,this);

//设置弹出层的颜色,指定为淡灰色

setColor(Color3B::GRAY);

setOpacity(128);


return true;

}


bool PopAlertDialog::onTouchBegan(Touch* touch,Event* event){

return true;

}


void PopAlertDialog::onTouchMoved(Touch* touch,Event* event){


}



void PopAlertDialog::onTouchEnded(Touch* touch,Event* event){


}



PopAlertDialog* PopAlertDialog::create(const char* backgoundImage,Size dialogSize){

//创建弹出对话框,指定背景图和大小。

PopAlertDialog* layer=PopAlertDialog::create();

//layer->setSpriteBackGround(Sprite::create(backgoundImage));

layer->setSprite9BackGround(Scale9Sprite::create(backgoundImage));

layer->m_dialogContentSize=dialogSize;

return layer;

}




void PopAlertDialog::setTitle(const char* title,int fontsize /*=20*/){

LabelTTF* label=LabelTTF::create(title,"",fontsize);

label->setColor(Color3B::RED);

setLabelTitle(label);

}


void PopAlertDialog::setContentText(const char* text,int fontsize,int padding,int paddingTop){

LabelTTF* ltf=LabelTTF::create(text,fontsize);

ltf->setColor(Color3B::BLUE);

setLabelContentText(ltf);

m_contentPadding=padding;

m_contentPaddingTop=paddingTop;

}


void PopAlertDialog::setCallBackFunc(Ref*target,SEL_CallFuncN callfun){

m_callbackListener=target;

m_callback=callfun;

}


bool PopAlertDialog::addButton(const char *normalImage, const char *selectedImage,int tag){

Size winSize=Director::getInstance()->getWinSize();

Point center_point=Point(winSize.width/2,winSize.height/2);

auto menuImage=MenuItemImage::create(

normalImage,

selectedImage,

CC_CALLBACK_1(PopAlertDialog::buttonCallBack,this) );

menuImage->setTag(tag);

menuImage->setPosition(center_point);

Size menuSize=menuImage->getContentSize();

Label* labelttf=Label::createWithTTF(title,"fonts/arial.ttf", 15);

labelttf->setColor(Color3B(Color3B::BLACK));

labelttf->setPosition(Point(menuSize.width/2,menuSize.height/2));

menuImage->addChild(labelttf);

getMenuButton()->addChild(menuImage);

return true;

}


void PopAlertDialog::buttonCallBack(Ref* pSender){

Node* node=dynamic_cast<Node*>(pSender);

//log("[========PopAlertDialog:buttonCallBack=======]touch tag:%d",node->getTag());

if (m_callback&&m_callbackListener) {

(m_callbackListener->*m_callback)(node);

}

this->removeFromParentAndCleanup(true);

}


void PopAlertDialog::onEnter(){

//log("PopAlertDialog::onEnter is loading");

LayerColor::onEnter();

Size winSize=Director::getInstance()->getWinSize();

Point center=Point(winSize.width/2,winSize.height/2);


//Sprite* background=getSpriteBackGround();

Scale9Sprite* background=getSprite9BackGround();

background->setContentSize(m_dialogContentSize);//指定对话框大小

background->setPosition(Point(winSize.width/2,winSize.height/2));

this->addChild(background,0,0);

Action* popupActions=Sequence::create( ScaleTo::create(0.0,0.0),

ScaleTo::create(0.06,1.05),

ScaleTo::create(0.08,0.95),1.0),

CallFunc::create(CC_CALLBACK_0(PopAlertDialog::backgroundFinish,this))

,NULL);

background->runAction(popupActions);

}



void PopAlertDialog::backgroundFinish(){

Size winSize=Director::getInstance()->getWinSize();

Point pCenter=Point(winSize.width/2,winSize.height/2);

this->addChild(getMenuButton());

float btnWidth=m_dialogContentSize.width/(getMenuButton()->getChildrenCount()+1);

Vector<Node*> vector=getMenuButton()->getChildren();

Ref* pObj=NULL;

int i=0;

for (Node*pObj:vector){

Node* node=dynamic_cast<Node*>(pObj);

node->setPosition(Point(winSize.width/2-m_dialogContentSize.width/2+btnWidth*(i+1),winSize.height/2-m_dialogContentSize.height/3));

i++;

}

if (getLabelTitle()) {

getLabelTitle()->setPosition(ccpAdd(pCenter,ccp(0,m_dialogContentSize.height/2-35.0f)));

this->addChild(getLabelTitle());

}

if (getLabelContentText()) {

CCLabelTTF* ltf=getLabelContentText();

ltf->setPosition(ccp(winSize.width/2,winSize.height/2));

ltf->setDimensions(CCSizeMake(m_dialogContentSize.width-m_contentPadding*2,m_dialogContentSize.height-m_contentPaddingTop));

ltf->setHorizontalAlignment(kCCTextAlignmentLeft);

this->addChild(ltf);

}

}


void PopAlertDialog::onExit(){

log("PopAlertDialog onExit");

CCLayerColor::onExit();

}



调用

MenuItemSprite* item1=MenuItemSprite::create(btn_normal_sprite,btn_select_sprite,nullptr,CC_CALLBACK_1(MenuItemSpritTest::select_learn,this));


select_learn方法

void MenuItemSpritTest::select_learn(Object* pSender){

log("you had select this button");

PopAlertDialog* popup=PopAlertDialog::create("background01.png",Size(800,250));

popup->setTitle("Message");

popup->setContentText("This is a test message!",20,50,150);

popup->setCallBackFunc(this,callfuncN_selector(MenuItemSpritTest::popButtonCallback));

popup->addButton("btnreturn.png","btnreturn.png","OK",0);

,1);

this->addChild(popup);

}


void MenuItemSpritTest::popButtonCallback(Node* pNode){

log("[==========]button call back.tag %d",pNode->getTag());

if (pNode->getTag()==0) {

Director::getInstance()->end();

}

if (pNode->getTag()==1) {

//pNode->getParent()->removeFromParent();

pNode->removeFromParent();

}

}


效果图:


原文链接:https://www.f2er.com/cocos2dx/342509.html

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