Make full use of C++11 Features, COCOS2DX dialog box implementation

Source: Internet
Author: User
Tags addchild

In general, a dialog box is divided into modal dialogs and non-modal dialogs, modal dialogs that are required to respond to the dialog box when the user wants to do something other than the dialog box. such as clicking the OK or Cancel button closes the dialog box. The modeless (Modeless) dialog, also known as a modeless dialog box, allows you to manipulate other windows when the user opens a modeless dialog box. For example, Windows provides a Notepad program in the Find dialog box. (Copy from Baidu Encyclopedia)

Now I need a list of the dialog box features as follows:

1. The need to display a message that may be a conversation may be a problem. 2. There must be a confirmation button, there may be a Cancel button 3. Confirm the function of the button can be customized

Here is my idea of implementation.

1. First, there is a function to initialize the dialog box, the dialog is initialized to memory, because the dialog is a very common function, so directly into the memory just fine. 2. Then the dialog box needs to load function and remove function, both of which are the core functions of the dialog Box 3. The dialog requires a button callback, a confirmation, a cancellation, a confirmation that there may be some other function to be performed, we can use callbacks to implement, Cancel to close the dialog directly, nothing to say. 4. The dialog box is divided into modal and non-modal, the modal is to add a touch screen in the dialog layer.

Following the above ideas, my function for dialog boxes is designed as follows:

dialog box protected://Initialization dialog box void InitDialog ();/* Load dialog box * @parm: Msg message displays a prompt message on the interface * @parm: The fun callback function gives the OK button to call the * @parm: isneedcance L need to cancel the button * @parm: Ismodaldialog modal Dialog */void Loaddialog (std::string msg,std::function<void (ref*) > Fun  , BOOL Isneedcancel = False,bool Ismodaldialog = true);//Dialog loading complete void Loaddialogfinish (armature *a, Movementeventtype B, std :: string c);//Hide dialog box void Hidedialog ();//Hide dialog box complete void hidedialogfinish (armature *a, Movementeventtype B, std::string c); /dialog Cancel button callback void Pressdialogcancelbtn (Ref *psender, widget::toucheventtype type);//dialog box confirm button callback void PRESSDIALOGCONFIRMBTN (Ref *psender, widget::toucheventtype type);

Why is there a dialog box loaded complete and the dialog box is hidden? Because I've made two animations for loading and hiding dialogs, these two additional functions are required.


OK not to say, the following is the specific implementation of the function:

----------------------------------------------------------dialog Box------------------------------------------------------- Initialize the dialog box void Momoscene::initdialog () {armaturedatamanager::getinstance ()->addarmaturefileinfo ("publish/    Dialoganimation.exportjson ");    Dialog = Armature::create ("dialoganimation");  Dialog->retain ();D ialog->setposition (Getcenterpoint ()); M_ui_dialog->addchild (Dialog); Dialog->setvisible (false);//load Information m_dialogmsg = Label::create (); M_dialogmsg->retain ();m_dialogmsg-> SetVisible (false); M_dialogmsg->setcolor (color3b::white); M_dialogmsg->enableglow (color4b::white); m_ Dialogmsg->setposition (Point (0,0));D ialog->addchild (m_dialogmsg,20);//Load Confirmation button m_dialogconfirm = button:: Create ("Dialog_btn/confirm.png", "dialog_btn/confirm_press.png"); M_dialogconfirm->retain (); m_DialogConfirm- >setposition (Point ( -80,-100)); M_dialogconfirm->setvisible (false);D ialog->addchild (m_dialogconfirm);// Load Cancel button M_dialogcancel = Button::create ("Dialog_btn/cacel.png", "Dialog_btn/cacel_press.png "); M_dialogcancel->retain (); M_dialogcancel->setposition (Point (80,-100)); m_ Dialogcancel->setvisible (False);D ialog->addchild (m_dialogcancel);} Load dialog box void Momoscene::loaddialog (std::string msg,std::function<void (ref*) > Fun,bool isneedcancel,bool Ismodaldialog) {dialog->setvisible (true);D ialog->getanimation ()->playwithindex (0); Dialog->getanimation ()->setmovementeventcallfunc (Cc_callback_3 (momoscene::loaddialogfinish,this)); m_ Dialogmsg->setstring (msg); m_dialog_confirmcallback = fun;//need to close button if (Isneedcancel = = True) {m_dialogconfirm- >addtoucheventlistener (Cc_callback_2 (momoscene::P ressdialogconfirmbtn,this));m_dialogcancel-> Addtoucheventlistener (Cc_callback_2 (momoscene::P ressdialogcancelbtn, this)); M_dialogconfirm->setvisible (True ); M_dialogcancel->setvisible (true);} Else{m_dialogconfirm->addtoucheventlistener (Cc_callback_2 (momoscene::P ressdialogconfirmbtn,this)); m_ Dialogconfirm->setposition (Point (0,-100)); M_dialoGconfirm->setvisible (True); m_dialogcancel->setvisible (false);} Modal dialogs need to be done with an engulfing touch layer if (Ismodaldialog = = True) {Auto Listener1 = Eventlistenertouchonebyone::create ();// Create a touch monitor listener1->setswallowtouches (true);//settings do not want to pass down touch Listener1->ontouchbegan = [] (touch* touch, event*  Event) {Cclog ("Touch Swallow");   return true; };_eventdispatcher->addeventlistenerwithscenegraphpriority (Listener1,m_ui_dialog);}} void Momoscene::loaddialogfinish (armature *a, Movementeventtype B, std::string c) {//Animation completion if (b = = complete) {dialog-> Getanimation ()->setmovementeventcallfunc (nullptr); m_dialogmsg->setvisible (True);}} Hides the dialog box void Momoscene::hidedialog () {m_dialogmsg->setvisible (false); m_dialogconfirm->setvisible (false); m_ Dialogcancel->setvisible (false);//Remove Touch screen _eventdispatcher->removeeventlistenersfortarget (m_UI_Dialog);D Ialog->getanimation ()->playwithindex (1); Dialog->getanimation ()->setmovementeventcallfunc (Cc_callback_3 (Momoscene::hidedialogfinish,this));} Hide dialog Box Complete VOID Momoscene::hidedialogfinish (armature *a, Movementeventtype B, std::string c) {//Animation completion if (b = = complete) {dialog-> Getanimation ()->setmovementeventcallfunc (nullptr); a->setvisible (false); a->setopacity (255);}} dialog box Cancel button void Momoscene::P ressdialogcancelbtn (Ref *psender, Widget::toucheventtype type) {switch (type) {case Widget:: Toucheventtype::ended:hidedialog (); break;}} dialog box Confirmation button void Momoscene::P ressdialogconfirmbtn (Ref *psender, Widget::toucheventtype type) {switch (type) {case Widget: : Toucheventtype::ended:hidedialog (); if (m_dialog_confirmcallback! = nullptr) m_dialog_confirmcallback (NULL);

It is worth mentioning that all variables beginning with m_ are member variables of the class, and the definitions of these variables are given below, with several variables initialized in the constructor of the class and given

Flag variable public:bool m_isneedexit;//ui variable public:sprite* m_spr_exittip; Exit hint armature* Dialog; dialog box label* m_dialogmsg; dialog box message button* m_dialogconfirm; dialog box Confirmation button button* m_dialogcancel;  dialog box Cancel button//Layer variable layer* m_ui_dialog;//dialog Layer top layer* m_ui_tool;//toolbar hierarchy High level layer* m_ui_game;//game layer Low layer* M_ui_background ;//Background layer bottom//callback function variable public:std::function<void (ref*) > M_dialog_confirmcallback; dialog box Confirmation callback//------------------------------function----------------------------------public:momoscene (): M_isneedexit ( False), M_dialog_confirmcallback (nullptr) {};~momoscene () {};

Here's how to use the dialog box:

Auto fun = [=] (ref* e) {cclog ("OK you enter the Confirm"); Loaddialog ("You Enter Continue", fun,true);



The above dialog box is implemented as follows: (because the material is not very good, in order to better display the effect, I show the dialog box when the menu removed)




I don't know how to show a GIF picture.


Make full use of C++11 Features, COCOS2DX dialog box implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.