cocos2d-x CCMenuItem自动排列
2013-06-20 16:36
393 查看
//使用说明: /*get the size of background, otherwise the menu content size will be set as WinSize menuPurchase->setContentSize(CCSizeMake(backSize.width,backSize.height)); menuPurchase->alignItemsToCenterWithTopPadding(20);*/ //CCMenu的自动排列不大好用,排列效果不理想,所以自定义了个CCMenu,使得更人性化,大家可以在这基础上进行扩展。 //Edit By Dark #ifndef __PurchaseMenu_H_ #define __PurchaseMenu_H_ #include "cocos2d.h" USING_NS_CC; #define USING_NS_PURCHASE_MENU using namespace puchase_menu static int kDefaultPadding = 5; namespace puchase_menu{ class PurchaseMenu : public CCMenu{ public: //PurchaseMenu() //{} //virtual ~PurchaseMenu(){} //dynamic methods PurchaseMenu * menuWithItems(CCMenuItem* item, ...); PurchaseMenu * menuWithItem(CCMenuItem* item); //static methods //static create PurchaseMenu and autorelease static PurchaseMenu* create(); static PurchaseMenu* create(CCMenuItem* item, ...); static PurchaseMenu* createWithItem(CCMenuItem* item); virtual void draw(); //align void alignItemsVerticallyToCenter(void); void alignItemsVerticallyToCenterWithPadding(float padding); void alignItemsToCenter(void); void alignItemsToCenterWithPadding(float itemPadding,float topPadding); void alignItemsToCenterWithTopPadding(float topPadding); }; } #endif
#include "PurchaseMenu.h" USING_NS_CC; USING_NS_PURCHASE_MENU; PurchaseMenu* PurchaseMenu::menuWithItems(CCMenuItem* item, ...) { va_list args; va_start(args,item); PurchaseMenu *pRet = new PurchaseMenu();//这里记得要注意 if (pRet && pRet->initWithItems(item, args)) { pRet->autorelease(); va_end(args); return pRet; } va_end(args); CC_SAFE_DELETE(pRet) ; return NULL; } PurchaseMenu* PurchaseMenu::menuWithItem(CCMenuItem* item) { return menuWithItems(item, NULL); } PurchaseMenu* PurchaseMenu::create() { return PurchaseMenu::create(NULL, NULL); } PurchaseMenu * PurchaseMenu::create(CCMenuItem* item, ...) { va_list args; va_start(args,item); PurchaseMenu *pRet = new PurchaseMenu(); //key point if (pRet && pRet->initWithItems(item, args)) { pRet->autorelease(); va_end(args); return pRet; } va_end(args); CC_SAFE_DELETE(pRet); return NULL; } PurchaseMenu* PurchaseMenu::createWithItem(CCMenuItem* item) { return PurchaseMenu::create(item, NULL); } /** Override this method to draw your own node. The following GL states will be enabled by default: - glEnableClientState(GL_VERTEX_ARRAY); - glEnableClientState(GL_COLOR_ARRAY); - glEnableClientState(GL_TEXTURE_COORD_ARRAY); - glEnable(GL_TEXTURE_2D); AND YOU SHOULD NOT DISABLE THEM AFTER DRAWING YOUR NODE But if you enable any other GL state, you should disable it after drawing your node. */ void PurchaseMenu::draw(){ //default } void PurchaseMenu::alignItemsVerticallyToCenter(void) { this->alignItemsVerticallyToCenterWithPadding(kDefaultPadding); } void PurchaseMenu::alignItemsVerticallyToCenterWithPadding(float padding) { float total_height = -padding; if (m_pChildren && m_pChildren->count() > 0) { CCObject* pObject = NULL; CCARRAY_FOREACH(m_pChildren, pObject) { CCNode* pChild = dynamic_cast<CCNode*>(pObject); if (pChild) { total_height += pChild->getContentSize().height * pChild->getScaleY() + padding; } } } float y = total_height / 2.0f; if (m_pChildren && m_pChildren->count() > 0) { CCObject* pObject = NULL; CCARRAY_FOREACH(m_pChildren, pObject) { CCNode* pChild = dynamic_cast<CCNode*>(pObject); if (pChild) { CCSize menuSize = this->getContentSize(); pChild->setPosition(ccp(menuSize.width/2, y - pChild->getContentSize().height * pChild->getScaleY() / 2.0f)); y -= pChild->getContentSize().height * pChild->getScaleY() + padding; } } } } void PurchaseMenu::alignItemsToCenter(void){ this->alignItemsToCenterWithPadding(kDefaultPadding,kDefaultPadding); } void PurchaseMenu::alignItemsToCenterWithPadding(float paddingBetweenItem,float paddingFromTop ){ float total_height = -paddingBetweenItem; CCSize menuSize = this->getContentSize(); if (m_pChildren && m_pChildren->count() > 0) { CCObject* pObject = NULL; CCARRAY_FOREACH(m_pChildren, pObject) { CCNode* pChild = dynamic_cast<CCNode*>(pObject); if (pChild) { total_height += pChild->getContentSize().height * pChild->getScaleY() + paddingBetweenItem; } } } float y = total_height / 2.0f + menuSize.height/2.0f - paddingFromTop; if (m_pChildren && m_pChildren->count() > 0) { CCObject* pObject = NULL; CCARRAY_FOREACH(m_pChildren, pObject) { CCNode* pChild = dynamic_cast<CCNode*>(pObject); if (pChild) { pChild->setPosition(ccp(menuSize.width/2, y - pChild->getContentSize().height * pChild->getScaleY())); y -= pChild->getContentSize().height * pChild->getScaleY() + paddingBetweenItem; } } } } void PurchaseMenu::alignItemsToCenterWithTopPadding(float paddingOfTop){ alignItemsToCenterWithPadding(kDefaultPadding,paddingOfTop); }
相关文章推荐
- cocos2d-x TestLua 之 CCMenuItemImage 菜单类
- cocos2D(八)---- CCMenu && CCMenuItem
- Cocos2d-x ——菜单层(CCMenuItemLabel) 一
- Cocos2d自定义组件|继承CCMenu或CCMenuItem
- cocos2d-x 开关按钮 三态按钮 的实现 CCMenuItemToggle
- cocos2d-x CCmenuSpriteItem 用getChildByTag()获取;
- cocos2d-x ——菜单层(CCMenuItemFont)二
- cocos2d-x 菜单项 CCMenuItem 居中缩放
- cocos2d-x TestLua 之 CCMenuItemLabel CCMenuItemSprite CCMenuItemFont 菜单类
- cocos2d-x学习笔记-CCMenu和CCMenuItem详解
- Cocos2d-x CCMenuItemSprite按钮图片旋转问题
- cocos2d-X 节点(CCMenuItem.h)API
- cocos2d-x学习笔记-CCMenu和CCMenuItem详解
- CCMenuItemImage 让按钮被点击时显示另一张图片(cocos2d-x基础)
- cocos2d-x TestLua 之 CCMenuItemLabel CCMenuItemSprite CCMenuItemFont 菜单类
- cocos2d-x ——菜单层(CCMenuItemFont)二
- 在 cocos2d 中判断互相重叠的 CCMenuItem
- cocos2D(八)---- CCMenu && CCMenuItem
- 在 cocos2d 中判断互相重叠的 CCMenuItem
- Cocos2d-x ——菜单层(CCMenuItemLabel) 一