2010-08-10 12:49:16 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010 cocos2d-x.org
|
|
|
|
|
|
|
|
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 "CCXApplication.h"
|
2010-08-25 10:19:20 +08:00
|
|
|
#include "CGPointExtension.h"
|
|
|
|
#include "CCTouchDispatcher.h"
|
|
|
|
#include "CCTouch.h"
|
2011-01-15 18:05:35 +08:00
|
|
|
#include "ccxStdC.h"
|
2010-08-10 17:21:01 +08:00
|
|
|
|
|
|
|
#include <vector>
|
2011-01-15 18:05:35 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
using namespace std;
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
namespace cocos2d{
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
enum
|
|
|
|
{
|
2010-08-10 12:49:16 +08:00
|
|
|
kDefaultPadding = 5,
|
|
|
|
};
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
//
|
|
|
|
//CCMenu
|
|
|
|
//
|
|
|
|
CCMenu * CCMenu::menuWithItems(CCMenuItem* item, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args,item);
|
|
|
|
CCMenu *pRet = new CCMenu();
|
2010-08-19 14:09:40 +08:00
|
|
|
if (pRet && pRet->initWithItems(item, args))
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
|
|
|
va_end(args);
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
va_end(args);
|
2010-08-19 14:09:40 +08:00
|
|
|
CCX_SAFE_DELETE(pRet)
|
2010-08-10 12:49:16 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
bool CCMenu::initWithItems(CCMenuItem* item, va_list args)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-09-02 14:54:42 +08:00
|
|
|
if (CCLayer::init())
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
this->m_bIsTouchEnabled = true;
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
// menu in the center of the screen
|
2010-11-11 11:18:58 +08:00
|
|
|
CGSize s = CCDirector::sharedDirector()->getWinSize();
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
this->m_bIsRelativeAnchorPoint = false;
|
|
|
|
m_tAnchorPoint = ccp(0.5f, 0.5f);
|
|
|
|
this->setContentSize(s);
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
// XXX: in v0.7, winSize should return the visible size
|
|
|
|
// XXX: so the bar calculation should be done there
|
2011-01-22 16:24:54 +08:00
|
|
|
CGRect r;
|
|
|
|
ccxApplication::sharedApplication().statusBarFrame(&r);
|
2010-11-11 11:18:58 +08:00
|
|
|
ccDeviceOrientation orientation = CCDirector::sharedDirector()->getDeviceOrientation();
|
2010-08-10 17:21:01 +08:00
|
|
|
if (orientation == CCDeviceOrientationLandscapeLeft || orientation == CCDeviceOrientationLandscapeRight)
|
|
|
|
{
|
2010-08-10 12:49:16 +08:00
|
|
|
s.height -= r.size.width;
|
2010-08-10 17:21:01 +08:00
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
else
|
2010-08-10 17:21:01 +08:00
|
|
|
{
|
2010-08-10 12:49:16 +08:00
|
|
|
s.height -= r.size.height;
|
2010-08-10 17:21:01 +08:00
|
|
|
}
|
2011-01-05 17:01:08 +08:00
|
|
|
setPosition(ccp(s.width/2, s.height/2));
|
2010-08-10 12:49:16 +08:00
|
|
|
|
|
|
|
int z=0;
|
|
|
|
|
|
|
|
if (item)
|
|
|
|
{
|
|
|
|
this->addChild(item, z);
|
|
|
|
CCMenuItem *i = va_arg(args, CCMenuItem*);
|
2010-08-10 17:21:01 +08:00
|
|
|
while (i)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
z++;
|
|
|
|
this->addChild(i, z);
|
|
|
|
i = va_arg(args, CCMenuItem*);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// [self alignItemsVertically];
|
|
|
|
|
|
|
|
m_pSelectedItem = NULL;
|
2010-12-25 11:15:19 +08:00
|
|
|
m_eState = kCCMenuStateWaiting;
|
2010-09-04 12:02:52 +08:00
|
|
|
return true;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-09-04 12:02:52 +08:00
|
|
|
return false;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
/*
|
|
|
|
* override add:
|
|
|
|
*/
|
2010-12-25 11:15:19 +08:00
|
|
|
void CCMenu::addChild(CCNode * child, int zOrder)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-12-25 11:15:19 +08:00
|
|
|
CCLayer::addChild(child, zOrder);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-12-25 11:15:19 +08:00
|
|
|
void CCMenu::addChild(CCNode * child, int zOrder, int tag)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-08-31 11:20:37 +08:00
|
|
|
// we can not use RTTI, so we do not known the type of object
|
|
|
|
/*NSAssert( dynamic_cast<CCMenuItem*>(child) != NULL, L"Menu only supports MenuItem objects as children");*/
|
2010-12-25 11:15:19 +08:00
|
|
|
CCLayer::addChild(child, zOrder, tag);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
|
2010-12-25 11:15:19 +08:00
|
|
|
void CCMenu::onExit()
|
|
|
|
{
|
|
|
|
if (m_eState == kCCMenuStateTrackingTouch)
|
|
|
|
{
|
|
|
|
m_pSelectedItem->unselected();
|
|
|
|
m_eState = kCCMenuStateWaiting;
|
|
|
|
m_pSelectedItem = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLayer::onExit();
|
|
|
|
}
|
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
//Menu - Events
|
|
|
|
void CCMenu::registerWithTouchDispatcher()
|
|
|
|
{
|
2010-12-25 11:15:19 +08:00
|
|
|
CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, kCCMenuTouchPriority, true);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
bool CCMenu::ccTouchBegan(CCTouch* touch, UIEvent* event)
|
|
|
|
{
|
2010-12-25 11:15:19 +08:00
|
|
|
if (m_eState != kCCMenuStateWaiting || ! m_bIsVisible)
|
2010-08-10 17:21:01 +08:00
|
|
|
{
|
2010-08-10 12:49:16 +08:00
|
|
|
return false;
|
2010-08-10 17:21:01 +08:00
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
m_pSelectedItem = this->itemForTouch(touch);
|
2010-08-10 17:21:01 +08:00
|
|
|
if (m_pSelectedItem)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-12-25 11:15:19 +08:00
|
|
|
m_eState = kCCMenuStateTrackingTouch;
|
2010-08-19 16:45:18 +08:00
|
|
|
m_pSelectedItem->selected();
|
2010-08-10 12:49:16 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
void CCMenu::ccTouchEnded(CCTouch *touch, UIEvent* event)
|
|
|
|
{
|
2010-12-25 11:15:19 +08:00
|
|
|
NSAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");
|
2010-08-19 16:45:18 +08:00
|
|
|
if (m_pSelectedItem)
|
|
|
|
{
|
|
|
|
m_pSelectedItem->unselected();
|
|
|
|
m_pSelectedItem->activate();
|
|
|
|
}
|
2010-12-25 11:15:19 +08:00
|
|
|
m_eState = kCCMenuStateWaiting;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
void CCMenu::ccTouchCancelled(CCTouch *touch, UIEvent* event)
|
|
|
|
{
|
2010-12-25 11:15:19 +08:00
|
|
|
NSAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchCancelled] -- invalid state");
|
2010-08-19 16:45:18 +08:00
|
|
|
if (m_pSelectedItem)
|
|
|
|
{
|
|
|
|
m_pSelectedItem->unselected();
|
|
|
|
}
|
2010-12-25 11:15:19 +08:00
|
|
|
m_eState = kCCMenuStateWaiting;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
void CCMenu::ccTouchMoved(CCTouch* touch, UIEvent* event)
|
|
|
|
{
|
2010-12-25 11:15:19 +08:00
|
|
|
NSAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchMoved] -- invalid state");
|
2010-08-10 12:49:16 +08:00
|
|
|
CCMenuItem *currentItem = this->itemForTouch(touch);
|
|
|
|
if (currentItem != m_pSelectedItem)
|
|
|
|
{
|
2010-08-19 16:45:18 +08:00
|
|
|
if (m_pSelectedItem)
|
|
|
|
{
|
|
|
|
m_pSelectedItem->unselected();
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
m_pSelectedItem = currentItem;
|
2010-08-19 16:45:18 +08:00
|
|
|
if (m_pSelectedItem)
|
|
|
|
{
|
|
|
|
m_pSelectedItem->selected();
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-27 09:40:45 +08:00
|
|
|
int CCMenu::mouseDelegatePriority()
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCMenu: unsupported");
|
|
|
|
return -1;/** @todo upto-0.99.5 use NSIntegerMin+1 instead*/
|
|
|
|
}
|
|
|
|
|
|
|
|
CCMenuItem* CCMenu::itemForMouseEvent(NSEvent * pEvent)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCMenu: unsupported");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCMenu::ccMouseUp(NSEvent * pEvent)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCMenu: unsupported");
|
2010-12-31 17:56:46 +08:00
|
|
|
return false;
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCMenu::ccMouseDown(NSEvent * pEvent)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCMenu: unsupported");
|
2010-12-31 17:56:46 +08:00
|
|
|
return false;
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CCMenu::ccMouseDragged(NSEvent * pEvent)
|
|
|
|
{
|
|
|
|
CCLOG("cocos2d: CCMenu: unsupported");
|
2010-12-31 17:56:46 +08:00
|
|
|
return false;
|
2010-12-27 09:40:45 +08:00
|
|
|
}
|
|
|
|
|
2010-08-27 14:13:32 +08:00
|
|
|
void CCMenu::destroy(void)
|
|
|
|
{
|
|
|
|
release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCMenu::keep(void)
|
|
|
|
{
|
|
|
|
retain();
|
|
|
|
}
|
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
//Menu - Alignment
|
|
|
|
void CCMenu::alignItemsVertically()
|
|
|
|
{
|
|
|
|
return this->alignItemsVerticallyWithPadding(kDefaultPadding);
|
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
void CCMenu::alignItemsVerticallyWithPadding(float padding)
|
|
|
|
{
|
|
|
|
float height = -padding;
|
2010-08-10 17:21:01 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
2010-08-10 17:21:01 +08:00
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-08-26 15:09:02 +08:00
|
|
|
if (!(*it))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
height += (*it)->getContentSize().height * (*it)->getScaleY() + padding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float y = height / 2.0f;
|
2010-08-10 17:21:01 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
2010-08-10 17:21:01 +08:00
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-08-26 15:09:02 +08:00
|
|
|
if (!(*it))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-28 15:43:13 +08:00
|
|
|
(*it)->setPosition(ccp(0, y - (*it)->getContentSize().height * (*it)->getScaleY() / 2.0f));
|
2010-08-10 12:49:16 +08:00
|
|
|
y -= (*it)->getContentSize().height * (*it)->getScaleY() + padding;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
void CCMenu::alignItemsHorizontally(void)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-08-10 17:21:01 +08:00
|
|
|
return this->alignItemsHorizontallyWithPadding(kDefaultPadding);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
void CCMenu::alignItemsHorizontallyWithPadding(float padding)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
float width = -padding;
|
2010-08-10 17:21:01 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
2010-08-26 15:09:02 +08:00
|
|
|
if (!(*it))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
width += (*it)->getContentSize().width * (*it)->getScaleX() + padding;
|
|
|
|
}
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
|
|
|
|
float x = -width / 2.0f;
|
2010-08-10 17:21:01 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
2010-08-26 15:09:02 +08:00
|
|
|
if (!(*it))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
(*it)->setPosition(ccp(x + (*it)->getContentSize().width * (*it)->getScaleX() / 2.0f, 0));
|
|
|
|
x += (*it)->getContentSize().width * (*it)->getScaleX() + padding;
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
void CCMenu::alignItemsInColumns(unsigned int columns, ...)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, columns);
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
this->alignItemsInColumns(columns, args);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
void CCMenu::alignItemsInColumns(unsigned int columns, va_list args)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-08-28 15:24:33 +08:00
|
|
|
vector<unsigned int> rows;
|
2010-08-10 17:21:01 +08:00
|
|
|
while (columns)
|
|
|
|
{
|
|
|
|
rows.push_back(columns);
|
|
|
|
columns = va_arg(args, unsigned int);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int height = -5;
|
2010-08-10 17:21:01 +08:00
|
|
|
unsigned int row = 0;
|
|
|
|
unsigned int rowHeight = 0;
|
|
|
|
unsigned int columnsOccupied = 0;
|
|
|
|
unsigned int rowColumns;
|
|
|
|
|
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
|
|
|
// if it has no value, break
|
|
|
|
if (! *it)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-11-23 13:41:10 +08:00
|
|
|
assert(row < rows.size());
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
rowColumns = rows[row];
|
|
|
|
// can not have zero columns on a row
|
|
|
|
assert(rowColumns);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
float tmp = (*it)->getContentSize().height;
|
2011-01-15 18:05:35 +08:00
|
|
|
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
++columnsOccupied;
|
|
|
|
if (columnsOccupied >= rowColumns)
|
|
|
|
{
|
|
|
|
height += rowHeight + 5;
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
columnsOccupied = 0;
|
|
|
|
rowHeight = 0;
|
|
|
|
++row;
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
// check if too many rows/columns for available menu items
|
|
|
|
assert(! columnsOccupied);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-11-11 11:18:58 +08:00
|
|
|
CGSize winSize = CCDirector::sharedDirector()->getWinSize();
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
row = 0;
|
|
|
|
rowHeight = 0;
|
|
|
|
rowColumns = 0;
|
|
|
|
float w;
|
|
|
|
float x;
|
|
|
|
float y = (float)(height / 2);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
|
|
|
if (! *it)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
if (rowColumns == 0)
|
|
|
|
{
|
|
|
|
rowColumns = rows[row];
|
|
|
|
w = winSize.width / (1 + rowColumns);
|
|
|
|
x = w;
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
float tmp = (*it)->getContentSize().height;
|
2011-01-15 18:05:35 +08:00
|
|
|
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
(*it)->setPosition(ccp(x - winSize.width / 2,
|
|
|
|
y - (*it)->getContentSize().height / 2));
|
|
|
|
|
|
|
|
x += w + 10;
|
|
|
|
++columnsOccupied;
|
|
|
|
|
|
|
|
if (columnsOccupied >= rowColumns)
|
|
|
|
{
|
|
|
|
y -= rowHeight + 5;
|
|
|
|
|
|
|
|
columnsOccupied = 0;
|
|
|
|
rowColumns = 0;
|
|
|
|
rowHeight = 0;
|
|
|
|
++row;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
void CCMenu::alignItemsInRows(unsigned int rows, ...)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, rows);
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
this->alignItemsInColumns(rows, args);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
void CCMenu::alignItemsInRows(unsigned int rows, va_list args)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-08-10 17:21:01 +08:00
|
|
|
vector<unsigned int> columns;
|
|
|
|
while (rows)
|
|
|
|
{
|
|
|
|
columns.push_back(rows);
|
|
|
|
rows = va_arg(args, unsigned int);
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +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;
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
|
|
|
if (! *it)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if too many menu items for the amount of rows/columns
|
|
|
|
assert(column < columns.size());
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
columnRows = columns[column];
|
|
|
|
// can't have zero rows on a column
|
|
|
|
assert(columnRows);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
// columnWidth = fmaxf(columnWidth, [item contentSize].width);
|
|
|
|
float tmp = (*it)->getContentSize().width;
|
2011-01-15 18:05:35 +08:00
|
|
|
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
columnHeight += (int)((*it)->getContentSize().height + 5);
|
|
|
|
++rowsOccupied;
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
if (rowsOccupied >= columnRows)
|
|
|
|
{
|
|
|
|
columnWidths.push_back(columnWidth);
|
|
|
|
columnHeights.push_back(columnHeight);
|
|
|
|
width += columnWidth + 10;
|
|
|
|
|
|
|
|
rowsOccupied = 0;
|
|
|
|
columnWidth = 0;
|
|
|
|
columnHeight = -5;
|
|
|
|
++column;
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
// check if too many rows/columns for available menu items.
|
|
|
|
assert(! rowsOccupied);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-11-11 11:18:58 +08:00
|
|
|
CGSize winSize = CCDirector::sharedDirector()->getWinSize();
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
column = 0;
|
|
|
|
columnWidth = 0;
|
|
|
|
columnRows = 0;
|
|
|
|
float x = (float)(-width / 2);
|
|
|
|
float y;
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
|
|
|
if (! *it)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
if (columnRows == 0)
|
|
|
|
{
|
|
|
|
columnRows = columns[column];
|
|
|
|
y = (float) columnHeights[column];
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
// columnWidth = fmaxf(columnWidth, [item contentSize].width);
|
|
|
|
float tmp = (*it)->getContentSize().width;
|
2011-01-15 18:05:35 +08:00
|
|
|
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
|
2010-08-10 17:21:01 +08:00
|
|
|
|
|
|
|
(*it)->setPosition(ccp(x + columnWidths[column] / 2,
|
|
|
|
y - winSize.height / 2));
|
|
|
|
|
|
|
|
y -= (*it)->getContentSize().height + 10;
|
|
|
|
++rowsOccupied;
|
|
|
|
|
|
|
|
if (rowsOccupied >= columnRows)
|
|
|
|
{
|
|
|
|
x += columnWidth + 5;
|
|
|
|
rowsOccupied = 0;
|
|
|
|
columnRows = 0;
|
|
|
|
columnWidth = 0;
|
|
|
|
++column;
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
// Opacity Protocol
|
2010-08-10 12:49:16 +08:00
|
|
|
|
|
|
|
/** Override synthesized setOpacity to recurse items */
|
2010-08-10 17:21:01 +08:00
|
|
|
void CCMenu::setOpacity(GLubyte var)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-08-10 17:21:01 +08:00
|
|
|
m_cOpacity = var;
|
|
|
|
|
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
|
|
|
if (! *it)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-08-31 11:20:37 +08:00
|
|
|
|
|
|
|
CCRGBAProtocol *pRGBAProtocol = (*it)->convertToRGBAProtocol();
|
|
|
|
if (pRGBAProtocol)
|
|
|
|
{
|
|
|
|
pRGBAProtocol->setOpacity(m_cOpacity);
|
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
|
|
|
GLubyte CCMenu::getOpacity(void)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
return m_cOpacity;
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
void CCMenu::setColor(cocos2d::ccColor3B var)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-08-10 17:21:01 +08:00
|
|
|
m_tColor = var;
|
|
|
|
|
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
|
|
|
if (! *it)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-08-31 11:20:37 +08:00
|
|
|
CCRGBAProtocol *pRGBAProtocol = (*it)->convertToRGBAProtocol();
|
|
|
|
if (pRGBAProtocol)
|
|
|
|
{
|
|
|
|
pRGBAProtocol->setColor(m_tColor);
|
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
|
|
|
ccColor3B CCMenu::getColor(void)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
|
|
|
return m_tColor;
|
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
CCMenuItem* CCMenu::itemForTouch(cocos2d::CCTouch *touch)
|
2010-08-10 12:49:16 +08:00
|
|
|
{
|
2010-08-10 17:21:01 +08:00
|
|
|
CGPoint touchLocation = touch->locationInView(touch->view());
|
2010-11-11 11:18:58 +08:00
|
|
|
touchLocation = CCDirector::sharedDirector()->convertToGL(touchLocation);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
if (m_pChildren && m_pChildren->count() > 0)
|
|
|
|
{
|
|
|
|
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
|
|
|
|
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
|
|
|
|
{
|
|
|
|
if (! *it)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
// ignore invisible and disabled items: issue #779, #866
|
2010-08-31 11:23:54 +08:00
|
|
|
if ((*it)->getIsVisible() && ((CCMenuItem*)(*it))->getIsEnabled())
|
2010-08-10 17:21:01 +08:00
|
|
|
{
|
|
|
|
CGPoint local = (*it)->convertToNodeSpace(touchLocation);
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-31 11:23:54 +08:00
|
|
|
CGRect r = ((CCMenuItem*)(*it))->rect();
|
2010-08-10 17:21:01 +08:00
|
|
|
r.origin = CGPointZero;
|
2010-08-10 12:49:16 +08:00
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
if (CGRect::CGRectContainsPoint(r, local))
|
|
|
|
{
|
2010-08-31 11:23:54 +08:00
|
|
|
return (CCMenuItem*)(*it);
|
2010-08-10 17:21:01 +08:00
|
|
|
}
|
|
|
|
}
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
2010-08-10 17:21:01 +08:00
|
|
|
|
|
|
|
return NULL;
|
2010-08-10 12:49:16 +08:00
|
|
|
}
|
|
|
|
|
2010-08-10 17:21:01 +08:00
|
|
|
}
|