[Cocos2dx note 012] some simple UI configuration classes, cocos2dx012

Source: Internet
Author: User

[Cocos2dx note 012] some simple UI configuration classes, cocos2dx012
Cocostudio can be used to load the edited UI, but it is too complicated. Especially after the UI is added, some problems are found in the touch screen events. It is too troublesome to directly use the program to write and load the UI. A ini-based UI configuration class has been added for some time. Currently, only CCSprite and plist are loaded. Others can be added later.
Header file
# Ifndef _ X_UI_H _
# Define _ X_UI_H _
# Include <cocos2d. h>
Namespace zdh
{
USING_NS_CC;
Void CreateByXUI (CCNode * paramParent, const char * paramFileName );
}
# Endif source file
# Include "xui. h"
# Include "xini. h"
# Include "xlog. h"

Namespace zdh
{
Namespace xui
{
// Configure //--------------------------------------------------------------------------------------
Int GetIntValue (XIniText: TSection * paramSection, const char * paramKeyName)
{
Auto pV = paramSection-> getEntry (paramKeyName );
If (isNULL (pV) return 0;
Else return pV-> getValue (). getField (). ToIntDef (0 );
}
// Configure //--------------------------------------------------------------------------------------
Int GetDoubleValue (XIniText: TSection * paramSection, const char * paramKeyName)
{
Auto pV = paramSection-> getEntry (paramKeyName );
If (isNULL (pV) return 0;
Else return pV-> getValue (). getField (). ToIntDef (0 );
}
// Configure //--------------------------------------------------------------------------------------
Const XAnsiString & GetStringValue (XIniText: TSection * paramSection, const char * paramKeyName)
{
Static const XAnsiString strEmpty;
Auto pV = paramSection-> getEntry (paramKeyName );
If (isNULL (pV) return strEmpty;
Else return pV-> getValue (). getField ();
}
};

// Configure //--------------------------------------------------------------------------------------
Void CreateSpriteByXUI (CCNode * paramParent, XIniText: TSection * paramSpriteSection)
{
XInt ix = xui: GetIntValue (paramSpriteSection, "x ");
XInt iy = xui: GetIntValue (paramSpriteSection, "y ");
XInt izOrder = xui: GetIntValue (paramSpriteSection, "zOrder ");
Const XAnsiString & pImageName = xui: GetStringValue (paramSpriteSection, "image ");
XInt iTag = xui: GetIntValue (paramSpriteSection, "tag ");
CCSprite * pSprite = NULL;
If (pImageName [0] = ':') // if it is read from the Cache
{
PSprite = CCSprite: createWithSpriteFrameName (pImageName. c_str () + 1 );
}
Else
{
PSprite = CCSprite: create (pImageName. c_str ());
}
PSprite-> setPosition (ix, iy );
PSprite-> setAnchorPoint (0, 0 );
PSprite-> setTag (iTag );
PSprite-> setZOrder (izOrder );
ParamParent-> addChild (pSprite, izOrder );
}

Void LoadSpriteFrameByPList (CCNode */* paramParent */, XIniText: TSection * paramSection)
{
Const XAnsiString & pPListName = xui: GetStringValue (paramSection, "filename ");
CCSpriteFrameCache: sharedSpriteFrameCache ()-> addSpriteFramesWithFile (pPListName. c_str ());
}

// Configure //--------------------------------------------------------------------------------------
Void CreateByXUI (CCNode * paramParent, const char * paramFileName)
{
Std: string strFullFileName = CCFileUtils: sharedFileUtils ()-> fullPathForFilename (paramFileName );
Unsigned long dwGetSize = 0;
Const unsigned char * pData = CCFileUtils: sharedFileUtils ()-> getFileData (strFullFileName. c_str (), "rb", & dwGetSize );
ZDH_INFO ("Load XUI: % s size = % u", paramFileName, dwGetSize );
If (dwGetSize = 0)
{
If (isNotNULL (pData) delete [] pData;
Return;
}

Std: string strData (const char *) pData, dwGetSize );
Std: stringstream ss (strData );
XIniText stIni;
If (! StIni. Load (ss ))
{
ZDH_INFO ("Load XUI Fail, % s", paramFileName );
Return;
}
For (int s = 0; s <stIni. getSectionCount (); s ++)
{
Auto ction = stIni. getSection (s );
Auto pType = ction-> getEntry ("type ");
If (isNULL (pType ))
{
ZDH_INFO ("Section = [% s] not exist key: \" type \ "", function-> getSectionName (). c_str ());
Continue;
}
Const XAnsiString & paramTypeValue = pType-> getValue (). getField ();
If (paramTypeValue = "CCSprite ")
{
CreateSpriteByXUI (paramParent, callback ction );
}
Else if (paramTypeValue = "plist ")
{
LoadSpriteFrameByPList (paramParent, callback ction );
}
}
}
} Configuration file
# Support for UTF-8 formats
[Gk_label.png]
Type = CCSprite
Image = gk_label.png
Tag = 1
X = 18
Y = 914
ZOrder = 1

[Mb_label.png]
Type = CCSprite
Image =: mb_label.png · # indicates loading images from CCSpriteFrameCache starting with a colon
Tag = 1
X = 348
Y = 916
ZOrder = 1

[Score_label.png]
Type = CCSprite
Image = score_label.png
Tag = 1
X = 258
Y = 855
ZOrder = 1

[Game_star.plist]
# Batch Loading
Type = plist
Filename = game_star.plist related ttexini and XAnsiString, refer to my open source code


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.