Cocosstdio Project incubation-action card game-system Design-ui system

Source: Internet
Author: User
Tags key string map data structure

Based on the solution, our UI is made with the Cocostdio UI editor, and the resources exported through the UI editor are (. json file + broken graph).

This will come up with a JSON file, such as A.json, and then a bunch of broken graphs. Why not use the big picture? Because, we are going to texturepacker the broken map to do the resource optimization.

Such a splicing interface, you can have planning or art or other people to do. The program can write the code at peace of mind, only need to do some special functions, let the people who make the interface name the control. Then the program is OK based on these naming programs.

In fact, we are more concerned about how these interfaces are regularly scheduled.

In software development, we should pay attention to a problem called Mvc,model-view-control in fact, all of our architecture is based on this up. To do the GUI architecture we have to do the following points.

1.UI Resource onboarding and release

2.UI interface and logic functions are separated. In fact, the production of resources once again, the program write function. Then, through the middle layer of code, the relationship between the two are linked together. Do the separation of code and logic.

The 3.UI scheduling problem has been mentioned in the previous article.

Well, on the above 3 questions, we start the design. According to the general design idea, we first made a singleton class Cguimgr

Students who do not understand a single case, you can check the design pattern---what is a single case?

Class Cguimgr{protected:cguimgr (); ~cguimgr (); static cguimgr m_inst;public:static cguimgr* getinst (); void Init (); cguiunit* Creategui (const char* _name); cguiunit* Getgui (const char* _name,bool _create = true); void Delgui (const char* _name); void Registergui (cguiunit* _gui); v OID update (float _ftime);p rotected:typedef std::map<string, cguiunit*> Guipool; Guipool m_guipool;typedef std::map<string, string> guirestable; Guirestable m_guirestable;};

  

Use the Singleton class Cguimgr to control its basic element cguiunit (just some of the code only picks up the key parts)

Each GUI encapsulates a GUI node for the corresponding resource file class Cguiunit:p ublic touchgroup//touchgroup inherited Cclayer, it can accept the message of the Oh, {friend class Cguimgr;pro Tected:cguiunit (const char* _name,const char* _resname) ~cguiunit ();p ublic:static cguiunit* Createinlua (const char* _ Name,const char* _resname); virtual void onEnter (); virtual void onExit (); virtual bool Load (); virtual bool UnLoad (); void SE     Tresfile (const char* _file); const char* getName (); void select_callback (unsigned int _colorid);             Exact pickup of virtual void adaptescreen ();      The screen is adapted to void run (float ftime);p rotected:void _bind (); void _unbind (); bool isload;string m_name;  The name of the configuration of the table, but also the only name, equivalent to key this is quite important, all the correlation really depends on the key string m_resfilename; Resource file name layout* m_playerout;//to the node bool m_badapted that should be exported from the Res file;  Cguiactionqueue* M_pactque; Sequential animation, this is very important oh};

One of the things we need to focus on is cguimgr to manage Cguiunit. Then each of the cguiunit corresponds to an editor exported file. Of course we will give each interface a name (string). Of course, these relationships will be written in the table, by the planning to fill out.

Attention:

On the cguimgr there is a function called void Registergui (cguiunit* _gui); This function assists in registering the basic elements of guiunit into a management class.

Cguiunit::cguiunit (constChar* _name,constChar* _resname): Isload ( false ), M_name (_name), M_playerout (NULL), M_resfilename (_resname), m_badapted (false), M_pactque ( NULL) {    new  cguiactionqueue;    Init ();    Cguimgr::getinst ()->registergui (this);}

That is, Cguiunit is automatically registered during the construction process. This will not be lost for every Cguiunit object.

//Cguimgr Cguimgr::m_inst; Cguimgr*Cguimgr::getinst () {return&M_inst;} Cguimgr::cguimgr () {}cguimgr::~cguimgr () {}voidcguimgr::init ()//Parse Table {typedef map<int,td_uires*>Uirespool; Uirespool::iterator it= Ccltbasedatatbl::inst ()M_mapuires.begin ();  while(It!=ccltbasedatatbl::inst ()M_mapuires.end ()) {Td_uires* T_uires_data = it->second; if(t_uires_data) {stringTmp_name = t_uires_data->Uiname (); stringTmp_value = t_uires_data->Resname (); M_guirestable[tmp_name]=Tmp_value; } It++;  } Create_gui (Cguijingqian);    This macro has a bit of meaning ... Create_gui (cguierrormessage);} Cguiunit* Cguimgr::creategui (Const Char*_name) {    returnGetgui (_name,true);} Cguiunit* Cguimgr::getgui (Const Char* _name,BOOL_create)//guaranteed to give you a UI object, but _create is set to true{guipool::iterator it=M_guipool.find (_name); if(it!=m_guipool.end ())returnIt->second; if(_create) {guirestable::iterator it_res=M_guirestable.find (_name); if(it_res!=M_guirestable.end ()) {Cguiunit* GUI =NewCguiunit (_name,it_res->second.c_str ()); GUI-Load (); returnGUI; }    }    returnNULL;}voidCguimgr::registergui (cguiunit*_gui)//register back for management {if(_gui) M_guipool[_gui->getname ()] =_gui;}voidCguimgr::D Elgui (Const Char*_name) {Guipool::iterator it=M_guipool.find (_name); if(it!=M_guipool.end ()) {        if(It->second! =NULL) It->second->release ();    M_guipool.erase (IT); }}voidCguimgr::update (float_ftime)//All UI gives you a chance to update, with no need to see your own {guipool::iterator iter=M_guipool.begin ();  for(; ITER! = M_guipool.end (); + +ITER) {        if(Iter->second = =NULL)Continue; ITER->second->run (_ftime); }}

The management class Cguimgr important implementations:

The init () initialization function, mainly parsing the tables, resolves the corresponding relationships of the resources. and exists in the map data structure of the management class.

Creategui () and Getgui () are actually created to create a Cguiunit base object, where the Getgui () function has a parameter called _create, which means that the excuse is that you will get an object. If it is true, it means I have to get the Cguiunit object I want, and if there is no such object in memory, create a new one.

In fact, when we write, this will be set to true, if in some judgment statement, it will be set to false. In this way, we guarantee that there is only one Guiunit object of the same name in memory, there is only one object in the UI, and the rest is the relationship of UI switching.

Let's take a look at the macro!!!

The following blog I give it a name called Project incubation record haha a little meaning!

Cocosstdio Project incubation-action card game-system Design-ui system

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.