//使用说明:
/*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);
}