头文件:

//

//  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

//  macstudycocos2dx

//

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

//

//

#include "PopAlertDialog.h"

PopAlertDialog::PopAlertDialog()

:

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)

{

}

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,const char* title,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),

ScaleTo::create(0.08,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);

popup->addButton("btnreturn.png", "btnreturn.png", "Cancel",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();

}

}

cocos2dx 3.x(纯代码实现弹出对话框/提示框/警告框)的更多相关文章

  1. 定义Foo() 函数,弹出对话框提示当前选中的是第几个单选框

    function foo(){ var ele = document.getElementsByName("radioElement"); for(var i = 0;i<e ...

  2. Android 手机卫士--弹出对话框

    在<Android 手机卫士--解析json与消息机制发送不同类型消息>一文中,消息机制发送不同类型的信息还没有完全实现,在出现异常的时候,应该弹出吐司提示异常,代码如下: private ...

  3. cocos2d-x学习日志(12) --弹出对话框的设计与实现

    我们时常需要这么些功能,弹出一个层,给与用户一些提示,这也是一种模态窗口,在没有对当前对话框进行确认的时候,不能继续往下操作. 功能分析 我们设计一个对话框,对话框上有几个按钮(个数可定制),当然有个 ...

  4. php网页,想弹出对话框, 消息框 简单代码

    php网页,想弹出对话框, 消息框 简单代码 <?php echo "<script language=\"JavaScript\">alert(\&q ...

  5. JQuery EasyUI弹出对话框解决Asp.net服务器控件无法执行后台代码的方法(转)

    原文:JQuery EasyUI弹出对话框解决Asp.net服务器控件无法执行后台代码的方法 jquery-easyui是一个基于jquery的图形界面插件,利用easyui可以创建很多好看的网页界面 ...

  6. QT常用代码之加载动态库和弹出对话框

    作者:朱金灿 来源:http://blog.csdn.net/clever101 加载动态库的代码: typedef void (*Execute)(); // 定义导出函数类型 QString st ...

  7. 10.JAVA之GUI编程弹出对话框Dialog

    在上节基础上添加对话框显示错误信息. 代码如下: /*弹出对话框显示错误信息,对话框一般不单独出现,一般依赖于窗体.*/ /*练习-列出指定目录内容*/ import java.awt.Button; ...

  8. 【Telerik】弹出对话框RadWindow,确认删除信息

    要做一个删除功能,但是删除前正常都要弹出对话框确认一下是否删除信息,防止误删信息.

  9. java selenium (十一) 操作弹出对话框

    Web 开发人员通常需要利用JavaScript弹出对话框来给用户一些信息提示, 包括以下几种类型 阅读目录 对话框类型 1.  警告框: 用于提示用户相关信息的验证结果, 错误或警告等 2. 提示框 ...

随机推荐

  1. 树剖||树链剖分||线段树||BZOJ4034||Luogu3178||[HAOI2015]树上操作

    题面:P3178 [HAOI2015]树上操作 好像其他人都嫌这道题太容易了懒得讲,好吧那我讲. 题解:第一个操作和第二个操作本质上是一样的,所以可以合并.唯一值得讲的点就是:第二个操作要求把某个节点 ...

  2. js点击按钮保存数据到本地

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. NLP任务:给定一句话,找出这句话中你想要的关键词,包括起始结束索引

    在实际的nlp实际任务中,你有一大堆的人工标注的关键词,来新的一句话,找出这句话中的关键词,以便你以后使用,那如何来做呢? 1)用到正则的 finditer()方法,返回你匹配的关键词的迭代对象,包含 ...

  4. layer开启与关闭加载层

    // 开启加载层 layer.load(2, { shade: [0.6, '#fff'], content: '数据加载中...', success: function (layero) { lay ...

  5. arcgisengine实现矩形转面

    面文件都有几何类型. arcengine在绘图时,不规则的多边形的几何类型是esriGeometryPolygon,矩形的几何类型是esriGeometryEnvelope,圆的几何类型是esriGe ...

  6. invariant theory 不变量理论

    https://baike.baidu.com/item/不变量理论/9224903?fr=aladdininvariant theory 一组几何元素由 k个参数组成的向量 P1表示.若 T为某一变 ...

  7. ms sql server 游标

    很简单的一个小例子. /****** Object: StoredProcedure [dbo].[usp_test] Script Date: 10/28/2016 15:08:31 ******/ ...

  8. jquery基础学习之事件篇(三)

    一.鼠标事件 click 单击 与 dbclick 双击 用于监听用户的点击操作,在同一元素上同时绑定 click 和 dblclick 事件是不可取的...根据浏览器支持不同一个点击事件是由mous ...

  9. Maven项目常见的小问题

    pom.xml文件头报错 场景 例Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:2.12.4 from ...

  10. Nand Flash 驱动框架

    框架入口源文件:s3c_nand.c (可根据入口源文件,再按着框架到内核走一遍) 内核版本:linux_2.6.22.6   硬件平台:JZ2440 以下是驱动框架: 以下是驱动代码 s3c_nan ...