CocoGUILIB常用控件例子

头文件库的引入:

#include "cocos2d.h"
USING_NS_CC;
#include "cocos-ext.h"
USING_NS_CC_EXT;

头文件定义一个变量:

private:
	UILoadingBar* loadingbar;

在CPP里面导入头文件:

#include "CocoGUILIB/System/CocosGUI.h"

  

在init里面:

CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);


        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);
        this->addChild(pMenu, 1);

		UILayer* ul = UILayer::create();
		ul->setContentSize(ccp(480,320));
		ul->scheduleUpdate();
		this->addChild(ul,100,100);

		UIButton* ub = UIButton::create();
		ub->setTouchEnable(true);
		ub->setPosition(ccp(100,220));
		ub->setTextures("bn.png","bp.png","bn.png");
		ub->setName("button");
		ub->setScale9Enable(true);
		ub->setScale9Size(CCSizeMake(160,80));
		ub->setCapInsets(CCRectZero);
		ub->addReleaseEvent(this,coco_releaseselector(HelloWorld::buttonPress));
		ul->addWidget(ub);

		UITextButton* utb = UITextButton::create();
		utb->setPosition(ccp(230,220));
		utb->setTextures("bn.png","bp.png","bn.png");
		utb->setText("Button");
		utb->setFontName("黑体");
		utb->setTouchEnable(true);
		utb->setName("UITextButton");
		utb->setFontSize(20);
		utb->addReleaseEvent(this,coco_releaseselector(HelloWorld::buttonPress));
		ul->addWidget(utb);


		//背景 背景被选中 勾选 背景不可用  够不可用
		UICheckBox* ucb =UICheckBox::create();
		ucb->setTextures("0.png","2.png","9.png","4.png","5.png");
		ucb->setSelectedState(true);
		/**
		WidgetStateNone = -1,
		WidgetStateNormal,
		WidgetStateSelected,
		WidgetStateDisabled
		**/
		ucb->setPressState(WidgetStateDisabled);
		ucb->setPosition(ccp(324,220));
		ucb->setName("checkbox");
		ucb->setTouchEnable(true);
		ucb->addSelectEvent(this,coco_selectselector(HelloWorld::buttonPress));
		ucb->addUnSelectEvent(this,coco_selectselector(HelloWorld::buttonPress));
		ul->addWidget(ucb);


		UIImageView* uiv = UIImageView::create();
		uiv->setTexture("ccicon.png");
		uiv->setPosition(ccp(400,220));
		uiv->setName("imageview");
		ul->addWidget(uiv);

		UILabel* ulb = UILabel::create();
		ulb->setPosition(ccp(72,100));
		ulb->setText("UILABEL");
		ulb->setFontName("黑体");
		ulb->setFontSize(24);
		ul->addWidget(ulb);

		UITextField* utf = UITextField::create();
		utf->setPosition(ccp(250,100));
		utf->setText("UITextField");
		utf->setFontName("黑体");
		utf->setFontSize(24);
		utf->setPlaceHolder("FUCK YOU");
		utf->setMaxLength(15);
		utf->setMaxLengthEnable(true);
		utf->setTouchEnable(true);
		ul->addWidget(utf);


		UITextArea* uta = UITextArea::create();
		uta->setPosition(ccp(450,100));
		uta->setTextAreaSize(CCSizeMake(200,100));
		uta->setTextColor(255,225,0);
		/************************************************************************/
		/*     kCCTextAlignmentLeft,
		kCCTextAlignmentCenter,
		kCCTextAlignmentRight,                                                                     */
		/************************************************************************/
		uta->setTextHorizontalAlignment(kCCTextAlignmentLeft);
		/************************************************************************/
		/*     kCCVerticalTextAlignmentTop,
		kCCVerticalTextAlignmentCenter,
		kCCVerticalTextAlignmentBottom,                                                                     */
		/************************************************************************/
		uta->setTextVerticalAlignment(kCCVerticalTextAlignmentTop);
		uta->setText("UIText\nArea");
		uta->setFontSize(24);
		uta->setFontName("黑体");
		//uta->set
		ul->addWidget(uta);


		UILabelAtlas* ula = UILabelAtlas::create();
		ula->setPosition(ccp(50,50));
		ula->setProperty("074084","labelatlas.png",17,22,"0");
		ul->addWidget(ula);

		UILoadingBar* ulba = UILoadingBar::create();
		ulba->setTexture("loadingbar.png");
		ulba->setPosition(ccp(250,50));
		//设置朝向
		/************************************************************************/
		/*     LoadingBarTypeLeft,
		LoadingBarTypeRight                                                                     */
		/************************************************************************/
		ulba->setDirection(LoadingBarTypeLeft);
		ulba->setPercent(90);
		ul->addWidget(ulba);
		loadingbar = ulba;


		UISlider* usd = UISlider::create();
		//usd->setBarTextureScale9Enable(true);
		usd->setBarTexture("slidbar.png");
		//usd->setScale9Size(CCSizeMake(160,80));
		//usd->setCapInsets(CCRectZero);
		//usd->setBarLength()
		usd->setSlidBallTextures("s1.png","s2.png","CloseSelected.png");
		usd->setPosition(ccp(250,30));
		usd->setTouchEnable(true);
		usd->addPercentChangedEvent(this,coco_pushselector(HelloWorld::buttonPress));
		ul->addWidget(usd);

 回调函数:

void HelloWorld::buttonPress(CCObject* object)
{
	UIButton* button = dynamic_cast<UIButton*>(object);
	if (button)
	{
		CCLog("name=%s",button->getName());
	}
	UICheckBox* checkbox = dynamic_cast<UICheckBox*>(object);
	if (checkbox)
	{
		CCLog("ischeck=%d",checkbox->getSelectedState());
	}

	UISlider* slider = dynamic_cast<UISlider*>(object);
	if (slider)
	{
		UILayer* uilayer = dynamic_cast<UILayer*>(this->getChildByTag(100));
		if (uilayer)
		{
			if (slider&&loadingbar)
			{
				int p = slider->getPercent();
				loadingbar->setPercent(p);
			}
		}
		

		
		
	}
	
	
}

猜你喜欢

转载自3895177.iteye.com/blog/1927889