Merge pull request #429 from leon-li/master

add CCMenu::init() and CCMenu::menu() to create empty menus
This commit is contained in:
minggo 2011-07-27 23:45:49 -07:00
commit 4d92b15152
2 changed files with 222 additions and 198 deletions

View File

@ -55,8 +55,12 @@ namespace cocos2d{
, m_pSelectedItem(NULL)
{}
virtual ~CCMenu(){}
/** creates a CCMenu with it's items */
static CCMenu* menuWithItems(CCMenuItem* item, ...);
/** creates an empty CCMenu */
static CCMenu* node();
/** creates a CCMenu with it's items */
static CCMenu* menuWithItems(CCMenuItem* item, ...);
/** creates a CCMenu with it's item, then use addChild() to add
* other items. It is used for script, it can't init with undetermined
@ -64,6 +68,8 @@ namespace cocos2d{
*/
static CCMenu*menuWithItem(CCMenuItem* item);
/** initializes an empty CCMenu */
bool init();
/** initializes a CCMenu with it's items */
bool initWithItems(CCMenuItem* item, va_list args);
@ -129,4 +135,4 @@ namespace cocos2d{
};
}
#endif//__CCMENU_H_
#endif//__CCMENU_H_

View File

@ -1,40 +1,40 @@
/****************************************************************************
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>
using namespace std;
namespace cocos2d{
/****************************************************************************
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>
using namespace std;
namespace cocos2d{
enum
{
@ -44,6 +44,18 @@ namespace cocos2d{
//
//CCMenu
//
CCMenu* CCMenu::node()
{
CCMenu *menu = new CCMenu();
if (menu && menu->init()) {
menu->autorelease();
return menu;
}
CC_SAFE_DELETE(menu)
return 0;
}
CCMenu * CCMenu::menuWithItems(CCMenuItem* item, ...)
{
va_list args;
@ -59,18 +71,24 @@ namespace cocos2d{
CC_SAFE_DELETE(pRet)
return NULL;
}
CCMenu* CCMenu::menuWithItem(CCMenuItem* item)
{
return menuWithItems(item, NULL);
}
CCMenu* CCMenu::menuWithItem(CCMenuItem* item)
{
return menuWithItems(item, NULL);
}
bool CCMenu::init()
{
va_list args;
return initWithItems(0, args);
}
bool CCMenu::initWithItems(CCMenuItem* item, va_list args)
{
if (CCLayer::init())
{
this->m_bIsTouchEnabled = true;
if (CCLayer::init())
{
this->m_bIsTouchEnabled = true;
// menu in the center of the screen
CCSize s = CCDirector::sharedDirector()->getWinSize();
@ -133,14 +151,14 @@ namespace cocos2d{
void CCMenu::onExit()
{
if (m_eState == kCCMenuStateTrackingTouch)
{
m_pSelectedItem->unselected();
m_eState = kCCMenuStateWaiting;
m_pSelectedItem = NULL;
}
CCLayer::onExit();
if (m_eState == kCCMenuStateTrackingTouch)
{
m_pSelectedItem->unselected();
m_eState = kCCMenuStateWaiting;
m_pSelectedItem = NULL;
}
CCLayer::onExit();
}
//Menu - Events
@ -171,11 +189,11 @@ namespace cocos2d{
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchEnded] -- invalid state");
if (m_pSelectedItem)
{
if (m_pSelectedItem)
{
m_pSelectedItem->unselected();
m_pSelectedItem->activate();
}
}
m_eState = kCCMenuStateWaiting;
}
@ -184,10 +202,10 @@ namespace cocos2d{
CC_UNUSED_PARAM(touch);
CC_UNUSED_PARAM(event);
CCAssert(m_eState == kCCMenuStateTrackingTouch, "[Menu ccTouchCancelled] -- invalid state");
if (m_pSelectedItem)
{
if (m_pSelectedItem)
{
m_pSelectedItem->unselected();
}
}
m_eState = kCCMenuStateWaiting;
}
@ -198,15 +216,15 @@ namespace cocos2d{
CCMenuItem *currentItem = this->itemForTouch(touch);
if (currentItem != m_pSelectedItem)
{
if (m_pSelectedItem)
{
if (m_pSelectedItem)
{
m_pSelectedItem->unselected();
}
}
m_pSelectedItem = currentItem;
if (m_pSelectedItem)
{
if (m_pSelectedItem)
{
m_pSelectedItem->selected();
}
}
}
}
@ -235,11 +253,11 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
{
height += pChild->getContentSize().height * pChild->getScaleY() + padding;
}
}
if (pChild)
{
height += pChild->getContentSize().height * pChild->getScaleY() + padding;
}
}
}
float y = height / 2.0f;
@ -249,7 +267,7 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
if (pChild)
{
pChild->setPosition(ccp(0, y - pChild->getContentSize().height * pChild->getScaleY() / 2.0f));
y -= pChild->getContentSize().height * pChild->getScaleY() + padding;
@ -273,7 +291,7 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
if (pChild)
{
width += pChild->getContentSize().width * pChild->getScaleX() + padding;
}
@ -287,9 +305,9 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
if (pChild)
{
pChild->setPosition(ccp(x + pChild->getContentSize().width * pChild->getScaleX() / 2.0f, 0));
pChild->setPosition(ccp(x + pChild->getContentSize().width * pChild->getScaleX() / 2.0f, 0));
x += pChild->getContentSize().width * pChild->getScaleX() + padding;
}
}
@ -327,28 +345,28 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
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;
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;
}
}
}
}
}
// check if too many rows/columns for available menu items
@ -369,35 +387,35 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
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;
++columnsOccupied;
if (columnsOccupied >= rowColumns)
{
y -= rowHeight + 5;
columnsOccupied = 0;
rowColumns = 0;
rowHeight = 0;
++row;
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;
}
}
}
}
}
}
@ -436,35 +454,35 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
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;
// 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;
}
}
}
}
}
// check if too many rows/columns for available menu items.
@ -484,34 +502,34 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
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;
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;
}
}
}
}
}
}
@ -528,15 +546,15 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
if (pChild)
{
CCRGBAProtocol *pRGBAProtocol = pChild->convertToRGBAProtocol();
if (pRGBAProtocol)
{
pRGBAProtocol->setOpacity(m_cOpacity);
CCRGBAProtocol *pRGBAProtocol = pChild->convertToRGBAProtocol();
if (pRGBAProtocol)
{
pRGBAProtocol->setOpacity(m_cOpacity);
}
}
}
}
}
}
@ -555,15 +573,15 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild)
if (pChild)
{
CCRGBAProtocol *pRGBAProtocol = pChild->convertToRGBAProtocol();
if (pRGBAProtocol)
{
pRGBAProtocol->setColor(m_tColor);
CCRGBAProtocol *pRGBAProtocol = pChild->convertToRGBAProtocol();
if (pRGBAProtocol)
{
pRGBAProtocol->setColor(m_tColor);
}
}
}
}
}
}
@ -583,22 +601,22 @@ namespace cocos2d{
CCARRAY_FOREACH(m_pChildren, pObject)
{
CCNode* pChild = (CCNode*) pObject;
if (pChild && pChild->getIsVisible() && ((CCMenuItem*)pChild)->getIsEnabled())
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;
CCPoint local = pChild->convertToNodeSpace(touchLocation);
CCRect r = ((CCMenuItem*)pChild)->rect();
r.origin = CCPointZero;
if (CCRect::CCRectContainsPoint(r, local))
{
return (CCMenuItem*)pChild;
}
}
}
}
}
return NULL;
}
}
}