Header file:
//
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{//Inherit Layercolor classes for easy change of layer color and transparency
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; dialog box size
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 file:
//
PopAlertDialog.cpp
Macstudycocos2dx
//
Created by Wangwei on 15/6/8.
//
//
#include "PopAlertDialog.h"
Popalertdialog::P Opalertdialog ()
:
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);
Sets the color of the pop-up layer, specified as light gray
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) {
Creates a popup dialog box that specifies the background map and size.
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);//Specify the size of the dialog box
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 ();
}
Call:
menuitemsprite* item1=menuitemsprite::create (Btn_normal_sprite, Btn_select_sprite,nullptr,cc_callback_1 ( Menuitemsprittest::select_learn,this));
Select_learn Method:
void Menuitemsprittest::select_learn (object* psender) {
Log ("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::p opbuttoncallback));
Popup->addbutton ("Btnreturn.png", "Btnreturn.png", "OK", 0);
Popup->addbutton ("Btnreturn.png", "Btnreturn.png", "Cancel", 1);
This->addchild (popup);
}
void Menuitemsprittest::p opbuttoncallback (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 (Pure Code Implementation Popup Dialog/Cue box/warning box)