2011-07-28 14:24:24 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2011 cocos2d-x.org
|
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
#include "CCMenu.h"
|
|
|
|
#include "CCDirector.h"
|
|
|
|
#include "CCApplication.h"
|
|
|
|
#include "CCPointExtension.h"
|
|
|
|
#include "CCTouchDispatcher.h"
|
|
|
|
#include "CCTouch.h"
|
|
|
|
#include "CCStdC.h"
|
|
|
|
|
|
|
|
#include <vector>
|
2011-08-03 10:44:39 +08:00
|
|
|
#include <stdarg.h>
|
2011-07-28 14:24:24 +08:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
NS_CC_BEGIN
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
enum
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
kDefaultPadding = 5,
|
2012-03-22 14:22:06 +08:00
|
|
|
};
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
//
|
|
|
|
//CCMenu
|
|
|
|
//
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
CCMenu* CCMenu::node()
|
|
|
|
{
|
|
|
|
return menuWithItem(NULL);
|
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
CCMenu * CCMenu::menuWithItems(CCMenuItem* item, ...)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args,item);
|
|
|
|
CCMenu *pRet = new CCMenu();
|
|
|
|
if (pRet && pRet->initWithItems(item, args))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
va_end(args);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
va_end(args);
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
return NULL;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
CCMenu* CCMenu::menuWithArray(CCArray* pArrayOfItems)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCMenu *pRet = new CCMenu();
|
|
|
|
if (pRet && pRet->initWithArray(pArrayOfItems))
|
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
CCMenu* CCMenu::menuWithItem(CCMenuItem* item)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return menuWithItems(item, NULL);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
bool CCMenu::init()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return initWithArray(NULL);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
bool CCMenu::initWithItems(CCMenuItem* item, va_list args)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCArray* pArray = NULL;
|
|
|
|
if( item )
|
|
|
|
{
|
|
|
|
pArray = CCArray::arrayWithObject(item);
|
|
|
|
CCMenuItem *i = va_arg(args, CCMenuItem*);
|
|
|
|
while(i)
|
|
|
|
{
|
|
|
|
pArray->addObject(i);
|
|
|
|
i = va_arg(args, CCMenuItem*);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return initWithArray(pArray);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
bool CCMenu::initWithArray(CCArray* pArrayOfItems)
|
|
|
|
{
|
|
|
|
if (CCLayer::init())
|
|
|
|
{
|
|
|
|
setIsTouchEnabled(true);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
m_bEnabled = true;
|
2012-03-22 14:22:06 +08:00
|
|
|
// menu in the center of the screen
|
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
|
2012-06-08 16:22:57 +08:00
|
|
|
this->setIgnoreAnchorPointForPosition(true);
|
2012-03-22 14:22:06 +08:00
|
|
|
setAnchorPoint(ccp(0.5f, 0.5f));
|
|
|
|
this->setContentSize(s);
|
|
|
|
|
|
|
|
setPosition(ccp(s.width/2, s.height/2));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pArrayOfItems != NULL)
|
|
|
|
{
|
|
|
|
int z=0;
|
|
|
|
CCObject* pObj = NULL;
|
|
|
|
CCARRAY_FOREACH(pArrayOfItems, pObj)
|
|
|
|
{
|
|
|
|
CCMenuItem* item = (CCMenuItem*)pObj;
|
|
|
|
this->addChild(item, z);
|
|
|
|
z++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// [self alignItemsVertically];
|
2012-03-22 14:22:06 +08:00
|
|
|
m_pSelectedItem = NULL;
|
|
|
|
m_eState = kCCMenuStateWaiting;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
/*
|
|
|
|
* override add:
|
|
|
|
*/
|
|
|
|
void CCMenu::addChild(CCNode * child)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayer::addChild(child);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::addChild(CCNode * child, int zOrder)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCLayer::addChild(child, zOrder);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::addChild(CCNode * child, int zOrder, int tag)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert( dynamic_cast<CCMenuItem*>(child) != NULL, "Menu only supports MenuItem objects as children");
|
|
|
|
CCLayer::addChild(child, zOrder, tag);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::onExit()
|
|
|
|
{
|
|
|
|
if (m_eState == kCCMenuStateTrackingTouch)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
m_pSelectedItem->unselected();
|
|
|
|
m_eState = kCCMenuStateWaiting;
|
|
|
|
m_pSelectedItem = NULL;
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
CCLayer::onExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Menu - Events
|
2012-04-13 14:11:35 +08:00
|
|
|
|
2012-06-06 15:16:02 +08:00
|
|
|
void CCMenu::setHandlerPriority(int newPriority)
|
2012-04-13 14:11:35 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
|
|
|
|
pDispatcher->setPriority(newPriority, this);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenu::registerWithTouchDispatcher()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
|
|
|
pDirector->getTouchDispatcher()->addTargetedDelegate(this, kCCMenuHandlerPriority, true);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(event);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_eState != kCCMenuStateWaiting || ! m_bIsVisible || !m_bEnabled)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CCNode *c = this->m_pParent; c != NULL; c = c->getParent())
|
|
|
|
{
|
|
|
|
if (c->getIsVisible() == false)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_pSelectedItem = this->itemForTouch(touch);
|
|
|
|
if (m_pSelectedItem)
|
|
|
|
{
|
|
|
|
m_eState = kCCMenuStateTrackingTouch;
|
|
|
|
m_pSelectedItem->selected();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-08-02 15:45:27 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::ccTouchEnded(CCTouch *touch, CCEvent* event)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(touch);
|
|
|
|
CC_UNUSED_PARAM(event);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");
|
|
|
|
if (m_pSelectedItem)
|
|
|
|
{
|
|
|
|
m_pSelectedItem->unselected();
|
|
|
|
m_pSelectedItem->activate();
|
|
|
|
}
|
|
|
|
m_eState = kCCMenuStateWaiting;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::ccTouchCancelled(CCTouch *touch, CCEvent* event)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(touch);
|
|
|
|
CC_UNUSED_PARAM(event);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchCancelled] -- invalid state");
|
|
|
|
if (m_pSelectedItem)
|
|
|
|
{
|
|
|
|
m_pSelectedItem->unselected();
|
|
|
|
}
|
|
|
|
m_eState = kCCMenuStateWaiting;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::ccTouchMoved(CCTouch* touch, CCEvent* event)
|
|
|
|
{
|
|
|
|
CC_UNUSED_PARAM(event);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchMoved] -- invalid state");
|
|
|
|
CCMenuItem *currentItem = this->itemForTouch(touch);
|
|
|
|
if (currentItem != m_pSelectedItem)
|
|
|
|
{
|
|
|
|
if (m_pSelectedItem)
|
|
|
|
{
|
|
|
|
m_pSelectedItem->unselected();
|
|
|
|
}
|
|
|
|
m_pSelectedItem = currentItem;
|
|
|
|
if (m_pSelectedItem)
|
|
|
|
{
|
|
|
|
m_pSelectedItem->selected();
|
|
|
|
}
|
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
//Menu - Alignment
|
|
|
|
void CCMenu::alignItemsVertically()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->alignItemsVerticallyWithPadding(kDefaultPadding);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::alignItemsVerticallyWithPadding(float padding)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
float height = -padding;
|
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
height += pChild->getContentSize().height * pChild->getScaleY() + padding;
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float y = height / 2.0f;
|
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
pChild->setPosition(ccp(0, y - pChild->getContentSize().height * pChild->getScaleY() / 2.0f));
|
|
|
|
y -= pChild->getContentSize().height * pChild->getScaleY() + padding;
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::alignItemsHorizontally(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->alignItemsHorizontallyWithPadding(kDefaultPadding);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::alignItemsHorizontallyWithPadding(float padding)
|
|
|
|
{
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float width = -padding;
|
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
width += pChild->getContentSize().width * pChild->getScaleX() + padding;
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float x = -width / 2.0f;
|
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
pChild->setPosition(ccp(x + pChild->getContentSize().width * pChild->getScaleX() / 2.0f, 0));
|
2012-04-19 14:35:52 +08:00
|
|
|
x += pChild->getContentSize().width * pChild->getScaleX() + padding;
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::alignItemsInColumns(unsigned int columns, ...)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, columns);
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->alignItemsInColumns(columns, args);
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
va_end(args);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::alignItemsInColumns(unsigned int columns, va_list args)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
vector<unsigned int> rows;
|
|
|
|
while (columns)
|
|
|
|
{
|
|
|
|
rows.push_back(columns);
|
|
|
|
columns = va_arg(args, unsigned int);
|
|
|
|
}
|
|
|
|
|
|
|
|
int height = -5;
|
|
|
|
unsigned int row = 0;
|
|
|
|
unsigned int rowHeight = 0;
|
|
|
|
unsigned int columnsOccupied = 0;
|
|
|
|
unsigned int rowColumns;
|
|
|
|
|
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCAssert(row < rows.size(), "");
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
rowColumns = rows[row];
|
|
|
|
// can not have zero columns on a row
|
|
|
|
CCAssert(rowColumns, "");
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float tmp = pChild->getContentSize().height;
|
|
|
|
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
++columnsOccupied;
|
|
|
|
if (columnsOccupied >= rowColumns)
|
|
|
|
{
|
|
|
|
height += rowHeight + 5;
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
columnsOccupied = 0;
|
|
|
|
rowHeight = 0;
|
|
|
|
++row;
|
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// check if too many rows/columns for available menu items
|
|
|
|
CCAssert(! columnsOccupied, "");
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
row = 0;
|
|
|
|
rowHeight = 0;
|
|
|
|
rowColumns = 0;
|
|
|
|
float w = 0.0;
|
|
|
|
float x = 0.0;
|
|
|
|
float y = (float)(height / 2);
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
if (rowColumns == 0)
|
|
|
|
{
|
|
|
|
rowColumns = rows[row];
|
|
|
|
w = winSize.width / (1 + rowColumns);
|
|
|
|
x = w;
|
|
|
|
}
|
|
|
|
|
|
|
|
float tmp = pChild->getContentSize().height;
|
|
|
|
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
|
|
|
|
|
|
|
|
pChild->setPosition(ccp(x - winSize.width / 2,
|
|
|
|
y - pChild->getContentSize().height / 2));
|
|
|
|
|
|
|
|
x += w;
|
|
|
|
++columnsOccupied;
|
|
|
|
|
|
|
|
if (columnsOccupied >= rowColumns)
|
|
|
|
{
|
|
|
|
y -= rowHeight + 5;
|
|
|
|
|
|
|
|
columnsOccupied = 0;
|
|
|
|
rowColumns = 0;
|
|
|
|
rowHeight = 0;
|
|
|
|
++row;
|
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::alignItemsInRows(unsigned int rows, ...)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args, rows);
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
this->alignItemsInRows(rows, args);
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
va_end(args);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::alignItemsInRows(unsigned int rows, va_list args)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
vector<unsigned int> columns;
|
|
|
|
while (rows)
|
|
|
|
{
|
|
|
|
columns.push_back(rows);
|
|
|
|
rows = va_arg(args, unsigned int);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<unsigned int> columnWidths;
|
|
|
|
vector<unsigned int> columnHeights;
|
|
|
|
|
|
|
|
int width = -10;
|
|
|
|
int columnHeight = -5;
|
|
|
|
unsigned int column = 0;
|
|
|
|
unsigned int columnWidth = 0;
|
|
|
|
unsigned int rowsOccupied = 0;
|
|
|
|
unsigned int columnRows;
|
|
|
|
|
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
// check if too many menu items for the amount of rows/columns
|
2012-04-19 14:35:52 +08:00
|
|
|
CCAssert(column < columns.size(), "");
|
|
|
|
|
|
|
|
columnRows = columns[column];
|
|
|
|
// can't have zero rows on a column
|
|
|
|
CCAssert(columnRows, "");
|
|
|
|
|
|
|
|
// columnWidth = fmaxf(columnWidth, [item contentSize].width);
|
|
|
|
float tmp = pChild->getContentSize().width;
|
|
|
|
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
|
|
|
|
|
|
|
|
columnHeight += (int)(pChild->getContentSize().height + 5);
|
|
|
|
++rowsOccupied;
|
|
|
|
|
|
|
|
if (rowsOccupied >= columnRows)
|
|
|
|
{
|
|
|
|
columnWidths.push_back(columnWidth);
|
|
|
|
columnHeights.push_back(columnHeight);
|
|
|
|
width += columnWidth + 10;
|
|
|
|
|
|
|
|
rowsOccupied = 0;
|
|
|
|
columnWidth = 0;
|
|
|
|
columnHeight = -5;
|
|
|
|
++column;
|
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// check if too many rows/columns for available menu items.
|
|
|
|
CCAssert(! rowsOccupied, "");
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
column = 0;
|
|
|
|
columnWidth = 0;
|
|
|
|
columnRows = 0;
|
|
|
|
float x = (float)(-width / 2);
|
|
|
|
float y = 0.0;
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
if (columnRows == 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
columnRows = columns[column];
|
|
|
|
y = (float) columnHeights[column];
|
|
|
|
}
|
|
|
|
|
|
|
|
// columnWidth = fmaxf(columnWidth, [item contentSize].width);
|
|
|
|
float tmp = pChild->getContentSize().width;
|
|
|
|
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
|
|
|
|
|
|
|
|
pChild->setPosition(ccp(x + columnWidths[column] / 2,
|
|
|
|
y - winSize.height / 2));
|
|
|
|
|
|
|
|
y -= pChild->getContentSize().height + 10;
|
|
|
|
++rowsOccupied;
|
|
|
|
|
|
|
|
if (rowsOccupied >= columnRows)
|
|
|
|
{
|
|
|
|
x += columnWidth + 5;
|
|
|
|
rowsOccupied = 0;
|
|
|
|
columnRows = 0;
|
|
|
|
columnWidth = 0;
|
|
|
|
++column;
|
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
// Opacity Protocol
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
/** Override synthesized setOpacity to recurse items */
|
|
|
|
void CCMenu::setOpacity(GLubyte var)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_cOpacity = var;
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(pChild);
|
2012-04-19 14:35:52 +08:00
|
|
|
if (pRGBAProtocol)
|
|
|
|
{
|
|
|
|
pRGBAProtocol->setOpacity(m_cOpacity);
|
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
GLubyte CCMenu::getOpacity(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_cOpacity;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
void CCMenu::setColor(const ccColor3B& var)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
m_tColor = var;
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCRGBAProtocol *pRGBAProtocol = dynamic_cast<CCRGBAProtocol*>(pChild);
|
|
|
|
if (pRGBAProtocol)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
pRGBAProtocol->setColor(m_tColor);
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
const ccColor3B& CCMenu::getColor(void)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return m_tColor;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
CCMenuItem* CCMenu::itemForTouch(CCTouch *touch)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint touchLocation = touch->locationInView();
|
|
|
|
touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCObject* pObject = NULL;
|
|
|
|
CCARRAY_FOREACH(m_pChildren, pObject)
|
|
|
|
{
|
|
|
|
CCNode* pChild = dynamic_cast<CCNode*>(pObject);
|
|
|
|
if (pChild && pChild->getIsVisible() && ((CCMenuItem*)pChild)->getIsEnabled())
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCPoint local = pChild->convertToNodeSpace(touchLocation);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCRect r = ((CCMenuItem*)pChild)->rect();
|
|
|
|
r.origin = CCPointZero;
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
if (CCRect::CCRectContainsPoint(r, local))
|
|
|
|
{
|
|
|
|
return (CCMenuItem*)pChild;
|
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return NULL;
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
|
|
|
|
NS_CC_END
|