axmol/cocos2dx/platform/mac/CCMenu_mac.cpp

628 lines
15 KiB
C++
Raw Normal View History

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"
2010-09-02 16:46:25 +08:00
#include "platform/CCXMath.h"
2010-08-10 17:21:01 +08:00
#include <vector>
#include <float.h>
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
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
{
2010-12-25 11:15:19 +08:00
this->m_bIsMouseEnabled = 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
this->m_tPosition = ccp(s.width/2, s.height/2);
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:
*/
CCNode * CCMenu::addChild(CCNode * child, int zOrder)
{
2010-09-02 14:54:42 +08:00
return CCLayer::addChild(child, zOrder);
2010-08-10 12:49:16 +08:00
}
2010-08-10 17:21:01 +08:00
2010-08-10 12:49:16 +08:00
CCNode * CCMenu::addChild(CCNode * child, int zOrder, int tag)
{
// 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-09-02 14:54:42 +08:00
return 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 17:21:01 +08:00
void CCMenu::registerWithTouchDispatcher()
{
CCLOG("cocos2d: CCMenu: unsupported");
}
CCMenuItem* CCMenu::itemForTouch(CCTouch * touch)
{
CCLOG("cocos2d: CCMenu: unsupported");
return NULL;
}
bool CCMenu::ccTouchBegan(CCTouch* touch, UIEvent* event)
{
CCLOG("cocos2d: CCMenu: unsupported");
return NO;
}
void CCMenu::ccTouchEnded(CCTouch* touch, UIEvent* event)
{
CCLOG("cocos2d: CCMenu: unsupported");
}
void CCMenu::ccTouchCancelled(CCTouch *touch, UIEvent* event)
{
CCLOG("cocos2d: CCMenu: unsupported");
}
void CCMenu::ccTouchMoved(CCTouch* touch, UIEvent* event)
{
CCLOG("cocos2d: CCMenu: unsupported");
}
2010-12-25 11:15:19 +08:00
//Menu - Events
int CCMenu::mouseDelegatePriority()
{
return -1;/** @todo upto-0.99.5 use NSIntegerMin+1 instead*/
}
CCMenuItem* CCMenu::itemForMouseEvent(NSEvent * pEvent)
{
CGPoint touchLocation = CCDirector::sharedDirector()->convertEventToGL(pEvent);
if (m_pChildren && m_pChildren->count() > 0)
{
NSMutableArray<CCNode*>::NSMutableArrayIterator it;
for (it = m_pChildren->begin(); it != m_pChildren->end(); ++it)
{
if (! *it)
{
break;
}
if ((*it)->getIsVisible() && ((CCMenuItem*)(*it))->getIsEnabled())
{
CGPoint local = (*it)->convertToNodeSpace(touchLocation);
CGRect r = ((CCMenuItem*)(*it))->rect();
r.origin = CGPointZero;
if (CGRect::CGRectContainsPoint(r, local))
{
return (CCMenuItem*)(*it);
}
}
}
}
return NULL;
}
bool CCMenu::ccMouseUp(NSEvent * pEvent)
{
if (m_pSelectedItem)
{
m_pSelectedItem->unselected();
m_pSelectedItem->activate();
m_eState = kCCMenuStateWaiting;
return YES;
}
return NO;
}
bool CCMenu::ccMouseDown(NSEvent * pEvent)
{
if (! getIsVisible())
{
return NO;
}
m_pSelectedItem = itemForMouseEvent(pEvent);
m_pSelectedItem->selected();
if (m_pSelectedItem)
{
m_eState = kCCMenuStateTrackingTouch;
return YES;
}
return NO;
}
bool CCMenu::ccMouseDragged(NSEvent * pEvent)
{
CCMenuItem* currentItem = itemForMouseEvent(pEvent);
if (currentItem && currentItem != m_pSelectedItem)
{
if (m_pSelectedItem)
{
m_pSelectedItem->unselected();
}
m_pSelectedItem = currentItem;
m_pSelectedItem->selected();
}
if (currentItem && m_eState == kCCMenuStateTrackingTouch)
{
return YES;
}
return NO;
}
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
{
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;
2010-09-02 16:46:25 +08:00
rowHeight = (unsigned int)((rowHeight >= tmp || CCXMath::isnanCocos2d(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;
2010-09-02 16:46:25 +08:00
rowHeight = (unsigned int)((rowHeight >= tmp || CCXMath::isnanCocos2d(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;
2010-09-02 16:46:25 +08:00
columnWidth = (unsigned int)((columnWidth >= tmp || CCXMath::isnanCocos2d(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;
2010-09-02 16:46:25 +08:00
columnWidth = (unsigned int)((columnWidth >= tmp || CCXMath::isnanCocos2d(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;
}
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;
}
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
}