Automatically generate code based on Cocosbuilder files

Source: Internet
Author: User
Tags class definition strcmp

This article mainly tells how to generate the corresponding code according to the CCB file generated by Cocosbuilder, mainly to generate COCOS2DX corresponding C + + file as an example to illustrate.

Reprint please keep the original address: http://blog.csdn.net/liangneo/article/details/8851870

Cocosbuilder is a very useful open source tool that can be used to edit UI layouts, particle systems, simple frame animations, and more. You can set the UI callback function name in the editor, bind the Edit object to owner or doc Root, and set custom properties for the custom object. The above three points require the appropriate code to support. So after editing the game scene, be sure to write the appropriate code. But writing related code is more cumbersome, it is easy to have errors.

If the scene editor is finished, the automatic generation of the code framework based on the scene file will save a lot of work and reduce the probability of error. The following three parts to one by one to explain how to implement the automatic generation of code, the first part will outline how to bind the code, the second part of the analysis of how to automatically generate code, part III gives code implementation

Control and code binding in a scene editor

1. Menu callback function, as shown in the following figure, you can specify a selector and target, and when set in the editor, the corresponding target object's function with a specific signature is bound to the string specified by selector, and when the menu is pressed, the corresponding function is recalled.

Let's look at how to bind the MenuItem callback to the corresponding code in COCOS2DX.

A. Assuming that the class for document root corresponds to Testautogenlayer, in order to implement the binding Testautogenlayer must inherit a

Cocos2d::extension::ccbselectorresolver, and then implement the function

Virtual Cocos2d::sel_menuhandler onresolveccbccmenuitemselector (Cocos2d::ccobject * ptarget, cocos2d::CCString * Pselectorname);

Complete code binding in this function.

B. The binding code is as follows:

Sel_menuhandler Testautogenlayer::onresolveccbccmenuitemselector (Cocos2d::ccobject *pTarget, cocos2d::CCString * Pselectorname)
{
Ccb_selectorresolver_ccmenuitem_glue (this, "backpressed", testautogenlayer::backpressed );
return false;
}

The above code binds the selector specified value "backpressed" to the backpressed function of the Testautogenlayer::. (Note: The value of selector can be different from the function name)

C. Backpressed's code framework is as follows

void testautogenlayer::backpressed (Cocos2d::ccobject * psender)
{
//callback function implemented
}

2.ControlButton Callback

Suppose a Controlbutton has the following setting in Cocosbuild,


Document root or the Testautogenlayer class, which implements the Ccbselectorresolver Onresolveccbcccontrolselector function in this class, assuming the following code

Sel_cccontrolhandler Testautogenlayer::onresolveccbcccontrolselector (Cocos2d::ccobject *pTarget, cocos2d:: Ccstring *pselectorname)
{
	Ccb_selectorresolver_cccontrol_glue (this, "donotpress", Testautogenlayer::D onotpress);
	return false;
}

The code framework for Donotpressme is as follows

void Testautogenlayer::D onotpress (Ccobject * psender, cocos2d::extension::cccontrolevent pcccontrolevent)
{
//Implement the control callback function here
}

3. Assign the object in the scene to the specified object, select an object, modify the code connection property, as shown below


Optionally assign an object to document root or owner, specify a binding name, and bind to document root as an example, assuming root is the Testautogenlayer class and the bound object is Cclabelttf, In order to implement binding, A.testautogenlayer must inherit from Cocos2d::extension::ccbmembervariableassigner and implement method Onassignccbmembervariable,b. Testautogenlayer must set a member variable of type Cclabelttf


The binding code snippet is as follows

BOOL Testautogenlayer::onassignccbmembervariable (Cocos2d::ccobject *ptarget, cocos2d::ccstring * Pmembervariablename, Cocos2d::ccnode *pnode)
{	Ccb_membervariableassigner_glue (this, "m_string2", CCLABELTTF*,THIS->M_STRING2);
	return false;
}

4. Set custom properties for the customized type, specifically in the edit how to set custom Propertyies is not described, the following is only the main how to bind custom properties to code, Cocosbuilder supports a custom type of 4 kinds: int, Float,string,bool. Suppose you set a variable of 4 custom types in Cocosbuilder, respectively,

M_intval of type int;

M_floatval of float type;

M_stringval of string type;

M_boolvar of type bool;

To implement binding to the class Testautogenlayer,

A. The class must implement the interface of the class Cocos2d::extension::ccbmembervariableassigner Onassignccbcustomproperty

B. Defining variables related to custom types


The binding code is as follows:

BOOL Testautogenlayer::onassignccbcustomproperty (Cocos2d::ccobject *ptarget, const char *pmembervariablename, Cocos2d::extension::ccbvalue *pccbvalue)
{
    if (Ptarget = = this)
    {
		if (0 = = strcmp (pmembervariablename , "M_intvar")) {
			M_intvar = Pccbvalue->getintvalue ();
			return true;
		}
		if (0 = = strcmp (pmembervariablename, "M_floatvar")) {
			M_floatvar = Pccbvalue->getfloatvalue ();
			return true;
		}
		if (0 = = strcmp (pmembervariablename, "M_stringvar")) {
			M_stringvar = Pccbvalue->getstringvalue ();
			return true;
		}
		if (0 = = strcmp (pmembervariablename, "M_bool")) {
			M_bool = Pccbvalue->getboolvalue ();
			return true;
		}
	return false;
}

Two. How to implement automatic code generation

Based on the analysis above, we know that there are four types of binding code that need to be generated, each type of binding needs to generate declarations and implementations (. h files and. cpp files), as well as the definition of the class, constructors, and the tail of the file. Here's what you need to generate code to analyze.

1. MenuItem Callback

A) header file: Generate selector corresponding function declaration

b CPP file, generating an empty selector function body,

c) CPP file generation binding code

2. Controlbutton Callback

A) header file: Generate selector corresponding function declaration

b CPP file, generating an empty selector function body,

c) CPP file generation binding code

3. Scene object assignment to host

A) header file, generate variable declaration

b CPP file, generate binding code

c) Constructor, variable assignment initial value

4. Custom variable assignment to host

A) header file, generate variable declaration

b CPP file, generate binding code

c) Constructor, variable assignment initial value

5. Miscellaneous

A) class definition build

b) Constructor generation

c) header file tail generation

What information is needed to generate this code, to facilitate the description, directly to the code

struct TypeName {
    TypeName (const std::string& t= "", const std::string& n= "")
    : Type (t), name (n) {}
    std::string type;
    std::string name;

    Std::stringm_classname;                        The class name to be generated
    std::string m_baseclass;                        Generating the parent class of the class
    std::vector<typename>m_listassignmember;       Scene Object
    std::vector<typename>m_listcutomproperty;      to assign value The custom variable that needs to be assigned
    Std::vector<std::string>m_listccontorlcallback;//menu callback function name
    STD::VECTOR<STD:: string>m_listmenucallback;    Control callback Function name


Where this information can be obtained from, only the CCB file generated by parsing Cocosbuilder can be obtained.

With the above information, we define some code templates, and replace and copy the code template to generate the relevant code. Described by the generation of the class's definition. The code template for the class is as follows

%class_name%.h//////This
code is auto generate by the toolcreated by  Neo. Email andsonliang@gmail.com////
 
#ifndef __autogen_ccbuilder__%class_name%__
#define__autogen_ ccbuilder__%class_name%__
 
#include "cocos2d.h"
#include "cocos-ext.h"
 
class%class_name%
: publiccocos2d::%base_class%
, Publiccocos2d::extension::ccbselectorresolver
, publiccocos2d::extension :: Ccbmembervariableassigner
, Publiccocos2d::extension::ccnodeloaderlistener
 
{public
:
    % Class_name% ()
:%base_class% ()

When you generate code, read the code template and replace%class_name% with m_classname,%base_class% instead of M_baseclass.

The rest of the reality is similar, not in this one by one note, interested please refer directly to the original code.

The following describes a code generation process



As shown above, the parser first parses the CCB file, generates a list of keywords, and then generates the corresponding code snippet according to the list of keywords

Three implementation code Xcode code

Execution file


Four Summary

With automatic code generation, the duplication of copypaste code has been reduced, and you can avoid making low-level mistakes. The only drawback is that the generated CPP code to be compiled to run without hot start, which is not conducive to rapid development. Viewing results from changes will also go through the intermediate compilation process, generating a CPP file to compile at the time of the release if you generate a script file that can be executed directly, such as LUA, at development time. This is the solution to rapid development. Without losing the operating efficiency of the final product. This will be the best of both worlds, as for how to achieve this effect, I will further discuss in later articles.

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.