2011-07-28 14:24:24 +08:00
|
|
|
/****************************************************************************
|
2012-06-14 15:13:16 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-28 14:24:24 +08:00
|
|
|
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"
|
2012-06-19 16:20:46 +08:00
|
|
|
#include "support/CCPointExtension.h"
|
|
|
|
#include "touch_dispatcher/CCTouchDispatcher.h"
|
|
|
|
#include "touch_dispatcher/CCTouch.h"
|
2011-07-28 14:24:24 +08:00
|
|
|
#include "CCStdC.h"
|
2012-11-30 21:13:25 +08:00
|
|
|
#include "cocoa/CCInteger.h"
|
2011-07-28 14:24:24 +08:00
|
|
|
|
|
|
|
#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-11-30 21:13:25 +08:00
|
|
|
static std::vector<unsigned int> ccarray_to_std_vector(CCArray* pArray)
|
|
|
|
{
|
|
|
|
std::vector<unsigned int> ret;
|
|
|
|
CCObject* pObj;
|
|
|
|
CCARRAY_FOREACH(pArray, pObj)
|
|
|
|
{
|
|
|
|
CCInteger* pInteger = (CCInteger*)pObj;
|
|
|
|
ret.push_back((unsigned int)pInteger->getValue());
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
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-06-14 15:13:16 +08:00
|
|
|
CCMenu* CCMenu::create()
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
return CCMenu::create(NULL, NULL);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenu * CCMenu::create(CCMenuItem* item, ...)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
va_list args;
|
|
|
|
va_start(args,item);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
|
|
|
CCMenu *pRet = CCMenu::createWithItems(item, args);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
va_end(args);
|
2012-12-06 18:51:33 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
return pRet;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-07-23 22:49:11 +08:00
|
|
|
CCMenu* CCMenu::createWithArray(CCArray* pArrayOfItems)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
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-11-14 18:05:15 +08:00
|
|
|
CCMenu* CCMenu::createWithItems(CCMenuItem* item, va_list args)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCArray* pArray = NULL;
|
2012-11-14 18:05:15 +08:00
|
|
|
if( item )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-14 16:05:58 +08:00
|
|
|
pArray = CCArray::create(item, NULL);
|
2012-04-19 14:35:52 +08:00
|
|
|
CCMenuItem *i = va_arg(args, CCMenuItem*);
|
2012-11-14 18:05:15 +08:00
|
|
|
while(i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pArray->addObject(i);
|
|
|
|
i = va_arg(args, CCMenuItem*);
|
|
|
|
}
|
|
|
|
}
|
2012-11-14 18:05:15 +08:00
|
|
|
|
|
|
|
return CCMenu::createWithArray(pArray);
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-11-14 18:05:15 +08:00
|
|
|
CCMenu* CCMenu::createWithItem(CCMenuItem* item)
|
|
|
|
{
|
|
|
|
return CCMenu::create(item, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCMenu::init()
|
|
|
|
{
|
|
|
|
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::initWithArray(CCArray* pArrayOfItems)
|
|
|
|
{
|
|
|
|
if (CCLayer::init())
|
|
|
|
{
|
2012-11-14 18:05:15 +08:00
|
|
|
setTouchPriority(kCCMenuHandlerPriority);
|
2012-11-16 14:51:48 +08:00
|
|
|
setTouchMode(kCCTouchesOneByOne);
|
|
|
|
setTouchEnabled(true);
|
2012-03-22 14:22:06 +08:00
|
|
|
|
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-15 15:10:40 +08:00
|
|
|
this->ignoreAnchorPointForPosition(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-11-14 18:05:15 +08:00
|
|
|
if (m_eState != kCCMenuStateWaiting || ! m_bVisible || !m_bEnabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CCNode *c = this->m_pParent; c != NULL; c = c->getParent())
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
if (c->isVisible() == false)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
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-11-30 21:13:25 +08:00
|
|
|
CCArray* rows = CCArray::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
while (columns)
|
|
|
|
{
|
2012-11-30 21:13:25 +08:00
|
|
|
rows->addObject(CCInteger::create(columns));
|
2012-04-19 14:35:52 +08:00
|
|
|
columns = va_arg(args, unsigned int);
|
|
|
|
}
|
2012-11-30 21:13:25 +08:00
|
|
|
alignItemsInColumnsWithArray(rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenu::alignItemsInColumnsWithArray(CCArray* rowsArray)
|
|
|
|
{
|
|
|
|
vector<unsigned int> rows = ccarray_to_std_vector(rowsArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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-11-30 21:13:25 +08:00
|
|
|
CCArray* pArray = CCArray::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
while (rows)
|
|
|
|
{
|
2012-11-30 21:13:25 +08:00
|
|
|
pArray->addObject(CCInteger::create(rows));
|
2012-04-19 14:35:52 +08:00
|
|
|
rows = va_arg(args, unsigned int);
|
|
|
|
}
|
2012-11-30 21:13:25 +08:00
|
|
|
alignItemsInRowsWithArray(pArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenu::alignItemsInRowsWithArray(CCArray* columnArray)
|
|
|
|
{
|
|
|
|
vector<unsigned int> columns = ccarray_to_std_vector(columnArray);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-01-14 15:51:53 +08:00
|
|
|
const ccColor3B& CCMenu::getColor(void)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
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-07-31 17:41:53 +08:00
|
|
|
CCPoint touchLocation = touch->getLocation();
|
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)
|
|
|
|
{
|
2012-10-21 16:13:57 +08:00
|
|
|
CCMenuItem* pChild = dynamic_cast<CCMenuItem*>(pObject);
|
|
|
|
if (pChild && pChild->isVisible() && pChild->isEnabled())
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2012-03-22 14:22:06 +08:00
|
|
|
CCPoint local = pChild->convertToNodeSpace(touchLocation);
|
2012-10-21 16:13:57 +08:00
|
|
|
CCRect r = pChild->rect();
|
2012-04-19 14:35:52 +08:00
|
|
|
r.origin = CCPointZero;
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
if (r.containsPoint(local))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-10-21 16:13:57 +08:00
|
|
|
return pChild;
|
2012-04-19 14:35:52 +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
|
|
|
return NULL;
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
2012-03-22 14:22:06 +08:00
|
|
|
|
|
|
|
NS_CC_END
|