cocos2d-x高階UI控制元件詳細介紹

Norton-Linux核心研究發表於2020-04-07

對應官網的CCControlExtention。可以通過TestCpp的EntentionTest檢視效果。

http://cocos2d-x.org/projects/cocos2d-x/wiki/CCControlExtension


以下內容使用了擴充套件庫,因此要新增庫路徑和名稱空間

#include "cocos-ext.h" 
USING_NS_CC_EXT;

並在屬性中配置extension路徑,加入libextensions.lib庫

CCScale9Sprite——九宮格Sprite

按照九宮格的方式縮放圖片的工具,和android的9patch一樣作用。可以使得按鈕變得漂亮起來。但不能處理點選事件。

            例子:

                CCSize  screenSize = CCDirector::sharedDirector()->getWinSize();

		CCRect rect = CCRectMake(0,0,15,32);         //從原始圖片中讀取的範圍,引數分別為座標x,y和寬度,高度
		CCRect rect_inside = CCRectMake(4,4,7,24);   //原始圖片中,可變的部分,即用來放大的。引數解釋同上(x,y,width,height)。
		CCSize rect_9Size = CCSizeMake(screenSize.width/2,screenSize.height/2);
		CCScale9Sprite* button = cocos2d::extension::CCScale9Sprite::create("button.png", rect,rect_inside);
		 //button->setContentSize(CCSizeMake(screenSize.width, 57));
		 button->setPreferredSize(rect_9Size);      //希望得到的最終按鈕大小
		 button->setPosition(ccp(screenSize.width/2, screenSize.height/ 2.0f));
		 addChild(button);
   
注意CCScale9Sprite::Create,如果沒有輸入rect,實際是是把圖片按九宮格等比劃分為9份。底呼叫了的層程式碼如下。沒有輸入rect,就預設rect為CCRectZero
看看定義const CCRect CCRectZero = CCRectMake(0,0,0,0);   即,0大小的Rect。可以參照CCControlButton的例子,它是直接呼叫create(char* file)的。

bool CCScale9Sprite::initWithFile(const char* file, CCRect rect,  CCRect capInsets)
{
    CCAssert(file != NULL, "Invalid file for sprite");
    
    CCSpriteBatchNode *batchnode = CCSpriteBatchNode::create(file, 9);
    bool pReturn = this->initWithBatchNode(batchnode, rect, capInsets);
    return pReturn;
}
bool CCScale9Sprite::initWithBatchNode(CCSpriteBatchNode* batchnode, CCRect rect, bool rotated, CCRect capInsets)
{
    if(batchnode)
    {
        this->updateWithBatchNode(batchnode, rect, rotated, capInsets);
        this->setAnchorPoint(ccp(0.5f, 0.5f));
    }
    this->m_positionsAreDirty = true;
    
    return true;
}


這就是button.png,可以在cocos2d-x的專案中搜到,是一個漸變圖。放大後效果



CCControlButton——事件按鈕

與CCScale9Sprite不同,Button控制元件可以處理事件,且該控制元件需要傳入CCScale9Sprite作為引數。
控制事件CCControlEvent,可以處理很多動作細節。

CCControl.h中定義如下
/** Number of kinds of control event. */
#define kControlEventTotalNumber 9


/** Kinds of possible events for the control objects. */
enum 
{
    CCControlEventTouchDown           = 1 << 0,    // A touch-down event in the control.
    CCControlEventTouchDragInside     = 1 << 1,    // An event where a finger is dragged inside the bounds of the control.
    CCControlEventTouchDragOutside    = 1 << 2,    // An event where a finger is dragged just outside the bounds of the control. 
    CCControlEventTouchDragEnter      = 1 << 3,    // An event where a finger is dragged into the bounds of the control.
    CCControlEventTouchDragExit       = 1 << 4,    // An event where a finger is dragged from within a control to outside its bounds.
    CCControlEventTouchUpInside       = 1 << 5,    // A touch-up event in the control where the finger is inside the bounds of the control. 
    CCControlEventTouchUpOutside      = 1 << 6,    // A touch-up event in the control where the finger is outside the bounds of the control.
    CCControlEventTouchCancel         = 1 << 7,    // A system event canceling the current touches for the control.
    CCControlEventValueChanged        = 1 << 8      // A touch dragging or otherwise manipulating a control, causing it to emit a series of different values.
};
typedef unsigned int CCControlEvent;


/** The possible state for a control.  */
enum 
{
    CCControlStateNormal       = 1 << 0, // The normal, or default state of a control—that is, enabled but neither selected nor highlighted.
    CCControlStateHighlighted  = 1 << 1, // Highlighted state of a control. A control enters this state when a touch down, drag inside or drag enter is performed. You can retrieve and set this value through the highlighted property.
    CCControlStateDisabled     = 1 << 2, // Disabled state of a control. This state indicates that the control is currently disabled. You can retrieve and set this value through the enabled property.
    CCControlStateSelected     = 1 << 3  // Selected state of a control. This state indicates that the control is currently selected. You can retrieve and set this value through the selected property.
};
typedef unsigned int CCControlState;

/** Number of kinds of control event. */
#define kControlEventTotalNumber 9

使用例子:(記得在標頭檔案宣告時引入標頭檔案、命名控制元件,配置CCControl.h的路徑,因為用到了CCControlEvent進行宣告)
cocos2d::extension::CCScale9Sprite *backgroundButton = cocos2d::extension::CCScale9Sprite::create("button.png");
cocos2d::extension::CCScale9Sprite *backgroundHighlightedButton = cocos2d::extension::CCScale9Sprite::create("buttonHighlighted.png");
// Creates a button with this string as title
CCLabelTTF *titleButton = CCLabelTTF::create("hello", "Marker Felt", 30);    
titleButton->setColor(ccc3(159, 168, 176));
cocos2d::extension::CCControlButton *newbutton = cocos2d::extension::CCControlButton::create(titleButton, backgroundButton);
newbutton->setBackgroundSpriteForState(backgroundHighlightedButton, cocos2d::extension::CCControlStateHighlighted);
newbutton->setTitleColorForState(ccWHITE, cocos2d::extension::CCControlStateHighlighted);
newbutton->setPosition(ccp (screenSize.width / 2, screenSize.height / 2));	

		 newbutton->addTargetWithActionForControlEvents(this,cccontrol_selector(HelloWorld::touchDown),CCControlEventTouchDown);
	 	 //注意,addTargetWithActionForControlEvents是public的,addTargetWithActionForControlEvent是protected的,不要少了s,否則會報錯:無法訪問 protected 成員
		 // addTargetWithActionForControlEvents迴圈呼叫了addTargetWithActionForControlEvent
		 
		 addChild(newbutton);
可參考以下程式碼 

void CCControl::addTargetWithActionForControlEvents(CCObject* target, SEL_CCControlHandler action, CCControlEvent controlEvents)
{
    // For each control events
    for (int i = 0; i < kControlEventTotalNumber; i++)
    {
        // If the given controlEvents bitmask contains the curent event
        if ((controlEvents & (1 << i)))
        {
            this->addTargetWithActionForControlEvent(target, action, 1<<i);            
        }
    }
}

CCControlSlider——拖動條


CCControlColorPicker——顏色選擇器


CCControlSwitch——開關控制元件


CCControlPotentionmeter——壓力計


CCControlStepper——分段控制元件


CCScrollView——滾動檢視


CCTabelView——列表檢視

一個列表可能有很多cell看不到,假如只能看到5個cell,則整個表只會生成6個cell的內容,然後在準備顯示新的一個cell時候,把看不見的那一個cell清空給新的cell。
所以看到TestCpp例子時,會先查詢有沒有可用的Cell,如果沒有則生成一個新的;如果有則迴圈利用,只需要更新label,如下程式碼

CCTableViewCell* TableViewTestLayer::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
    CCString *string = CCString::createWithFormat("%d", idx);
    CCTableViewCell *cell = table->dequeueCell();
    if (!cell) {
        cell = new CustomTableViewCell();
        cell->autorelease();
        CCSprite *sprite = CCSprite::create("Images/Icon.png");
        sprite->setAnchorPoint(CCPointZero);
        sprite->setPosition(ccp(0, 0));
        cell->addChild(sprite);


        CCLabelTTF *label = CCLabelTTF::create(string->getCString(), "Helvetica", 20.0);
        label->setPosition(CCPointZero);
		label->setAnchorPoint(CCPointZero);
        label->setTag(123);        //設定tag
        cell->addChild(label);
    }
    else
    {
        CCLabelTTF *label = (CCLabelTTF*)cell->getChildByTag(123);   //根據tag獲取已經初始化的cell
        label->setString(string->getCString());     
    }




    return cell;
}

要使用tableVIew要記得繼承介面

class TableViewTestLayer : public cocos2d::CCLayer, public cocos2d::extension::CCTableViewDataSource, public cocos2d::extension::CCTableViewDelegate

CCTableViewDataSource的初始化函式

    static CCTableView* create(CCTableViewDataSource* dataSource, CCSize size, CCNode *container);


從資料來源 public cocos2d::extension::CCTableViewDataSource裡面繼承了以下函式。

    virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);
    virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
    virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);

而CCTableViewDelegate 和又是繼承ScrollView的 

class CCTableViewDelegate : public CCScrollViewDelegate。要實現以下兩個介面(如果沒有事件處理,可以不寫方法到{}裡)

    virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view) {};
    virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view) {}

CCTableViewDelegate 可以呼叫以下函式,反饋點選的Cell

void TableViewTestLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
    CCLOG("cell touched at index: %i", cell->getIdx());
}

點選事件結束時候,判斷哪個cell被點選(可以借鑑這種方法去定義其它控制元件)

void CCTableView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
    if (!this->isVisible()) {
        return;
    }
    if (m_pTouches->count() == 1 && !this->isTouchMoved()) {
        unsigned int        index;
        CCTableViewCell   *cell;
        CCPoint           point;
        
        point = this->getContainer()->convertTouchToNodeSpace(pTouch);
        if (m_eVordering == kCCTableViewFillTopDown) {
            CCSize cellSize = m_pDataSource->cellSizeForTable(this);
            point.y -= cellSize.height;
        }
        index = this->_indexFromOffset(point);
        cell  = this->_cellWithIndex(index);
        
        if (cell) {
            m_pTableViewDelegate->tableCellTouched(this, cell);   //注意在這裡呼叫tableCellTouched
        }
    }
    CCScrollView::ccTouchEnded(pTouch, pEvent);
}




相關文章