axmol/cocos2dx/menu_nodes/CCMenu.cpp

602 lines
15 KiB
C++
Raw Normal View History

2010-08-10 12:49:16 +08:00
/****************************************************************************
Copyright (c) 2010-2011 cocos2d-x.org
Copyright (c) 2008-2010 Ricardo Quesada
2010-08-10 12:49:16 +08:00
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"
2010-08-25 10:19:20 +08:00
#include "CCTouchDispatcher.h"
#include "CCTouch.h"
#include "CCStdC.h"
2010-08-10 17:21:01 +08:00
#include <vector>
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);
CC_SAFE_DELETE(pRet)
2010-08-10 12:49:16 +08:00
return NULL;
}
2010-08-10 17:21:01 +08:00
2011-05-31 14:04:14 +08:00
#if CC_ENABLE_LUA
CCMenu* CCMenu::menuWithItem(CCMenuItem* item)
{
return menuWithItems(item, NULL);
}
#endif
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
CCSize 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
CCRect r;
CCApplication::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
}
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;
return true;
2010-08-10 12:49:16 +08:00
}
2010-08-10 17:21:01 +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
{
// we can not use RTTI, so we do not known the type of object
/*CCAssert( 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
2011-03-18 09:39:34 +08:00
bool CCMenu::ccTouchBegan(CCTouch* touch, CCEvent* event)
2010-08-10 12:49:16 +08:00
{
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
2011-03-18 09:39:34 +08:00
void CCMenu::ccTouchEnded(CCTouch *touch, CCEvent* event)
2010-08-10 12:49:16 +08:00
{
CCAssert(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
2011-03-18 09:39:34 +08:00
void CCMenu::ccTouchCancelled(CCTouch *touch, CCEvent* event)
2010-08-10 12:49:16 +08:00
{
CCAssert(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
2011-03-18 09:39:34 +08:00
void CCMenu::ccTouchMoved(CCTouch* touch, CCEvent* event)
2010-08-10 12:49:16 +08:00
{
CCAssert(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
}
}
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
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
2010-08-26 15:09:02 +08:00
{
height += pChild->getContentSize().height * pChild->getScaleY() + padding;
2010-08-26 15:09:02 +08:00
}
}
2010-08-10 12:49:16 +08:00
}
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
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
pChild->setPosition(ccp(0, y - pChild->getContentSize().height * pChild->getScaleY() / 2.0f));
y -= pChild->getContentSize().height * pChild->getScaleY() + padding;
}
}
2010-08-10 12:49:16 +08:00
}
}
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)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
width += pChild->getContentSize().width * pChild->getScaleX() + padding;
2010-08-26 15:09:02 +08:00
}
}
2010-08-10 17:21:01 +08:00
}
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)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
pChild->setPosition(ccp(x + pChild->getContentSize().width * pChild->getScaleX() / 2.0f, 0));
x += pChild->getContentSize().width * pChild->getScaleX() + padding;
2010-08-26 15:09:02 +08:00
}
}
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)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
assert(row < rows.size());
rowColumns = rows[row];
// can not have zero columns on a row
assert(rowColumns);
float tmp = pChild->getContentSize().height;
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
++columnsOccupied;
if (columnsOccupied >= rowColumns)
{
height += rowHeight + 5;
columnsOccupied = 0;
rowHeight = 0;
++row;
}
}
}
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
CCSize 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)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
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 + 10;
++columnsOccupied;
if (columnsOccupied >= rowColumns)
{
y -= rowHeight + 5;
columnsOccupied = 0;
rowColumns = 0;
rowHeight = 0;
++row;
}
}
}
2010-08-10 17:21:01 +08:00
}
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)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
// check if too many menu items for the amount of rows/columns
assert(column < columns.size());
columnRows = columns[column];
// can't have zero rows on a column
assert(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;
}
}
}
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
CCSize 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)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
if (columnRows == 0)
{
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;
}
}
}
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)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
CCRGBAProtocol *pRGBAProtocol = pChild->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)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
CCRGBAProtocol *pRGBAProtocol = pChild->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
{
CCPoint 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)
{
CCObject* pObject = NULL;
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild && pChild->getIsVisible() && ((CCMenuItem*)pChild)->getIsEnabled())
{
CCPoint local = pChild->convertToNodeSpace(touchLocation);
CCRect r = ((CCMenuItem*)pChild)->rect();
r.origin = CCPointZero;
if (CCRect::CCRectContainsPoint(r, local))
{
return (CCMenuItem*)pChild;
2010-08-10 17:21:01 +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
}