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"
|
2013-09-03 18:22:03 +08:00
|
|
|
#include "event_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"
|
2013-09-03 18:22:03 +08:00
|
|
|
#include "event_dispatcher/CCTouchEventListener.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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
static std::vector<unsigned int> ccarray_to_std_vector(Array* pArray)
|
2012-11-30 21:13:25 +08:00
|
|
|
{
|
|
|
|
std::vector<unsigned int> ret;
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj;
|
2012-11-30 21:13:25 +08:00
|
|
|
CCARRAY_FOREACH(pArray, pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
Integer* pInteger = static_cast<Integer*>(pObj);
|
2012-11-30 21:13:25 +08:00
|
|
|
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
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
Menu::~Menu()
|
|
|
|
{
|
2013-09-16 22:23:07 +08:00
|
|
|
CCLOGINFO("In the destructor of Menu. %p", this);
|
2013-09-03 18:22:03 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Menu* Menu::create()
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Menu::create(NULL, NULL);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Menu * Menu::create(MenuItem* 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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Menu *pRet = Menu::createWithItems(item, args);
|
2012-11-14 18:05:15 +08:00
|
|
|
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Menu* Menu::createWithArray(Array* pArrayOfItems)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Menu *pRet = new Menu();
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Menu* Menu::createWithItems(MenuItem* item, va_list args)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* pArray = NULL;
|
2012-11-14 18:05:15 +08:00
|
|
|
if( item )
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pArray = Array::create(item, NULL);
|
|
|
|
MenuItem *i = va_arg(args, MenuItem*);
|
2012-11-14 18:05:15 +08:00
|
|
|
while(i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
pArray->addObject(i);
|
2013-06-20 14:13:12 +08:00
|
|
|
i = va_arg(args, MenuItem*);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
2012-11-14 18:05:15 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
return Menu::createWithArray(pArray);
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Menu* Menu::createWithItem(MenuItem* item)
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
return Menu::create(item, NULL);
|
2012-11-14 18:05:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Menu::init()
|
2012-11-14 18:05:15 +08:00
|
|
|
{
|
|
|
|
return initWithArray(NULL);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool Menu::initWithArray(Array* pArrayOfItems)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
if (Layer::init())
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
|
2012-11-16 14:51:48 +08:00
|
|
|
setTouchEnabled(true);
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_enabled = true;
|
2012-03-22 14:22:06 +08:00
|
|
|
// menu in the center of the screen
|
2013-07-12 06:24:23 +08:00
|
|
|
Size s = Director::getInstance()->getWinSize();
|
2012-03-22 14:22:06 +08:00
|
|
|
|
2012-06-15 15:10:40 +08:00
|
|
|
this->ignoreAnchorPointForPosition(true);
|
2013-07-12 14:11:55 +08:00
|
|
|
setAnchorPoint(Point(0.5f, 0.5f));
|
2012-03-22 14:22:06 +08:00
|
|
|
this->setContentSize(s);
|
|
|
|
|
2013-07-12 14:11:55 +08:00
|
|
|
setPosition(Point(s.width/2, s.height/2));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
if (pArrayOfItems != NULL)
|
|
|
|
{
|
|
|
|
int z=0;
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObj = NULL;
|
2012-04-19 14:35:52 +08:00
|
|
|
CCARRAY_FOREACH(pArrayOfItems, pObj)
|
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
MenuItem* item = static_cast<MenuItem*>(pObj);
|
2012-04-19 14:35:52 +08:00
|
|
|
this->addChild(item, z);
|
|
|
|
z++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// [self alignItemsVertically];
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem = NULL;
|
2013-07-26 08:47:42 +08:00
|
|
|
_state = Menu::State::WAITING;
|
2013-02-28 10:15:09 +08:00
|
|
|
|
|
|
|
// enable cascade color and opacity on menus
|
|
|
|
setCascadeColorEnabled(true);
|
|
|
|
setCascadeOpacityEnabled(true);
|
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
/*
|
|
|
|
* override add:
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::addChild(Node * child)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Layer::addChild(child);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::addChild(Node * child, int zOrder)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Layer::addChild(child, zOrder);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::addChild(Node * child, int zOrder, int tag)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT( dynamic_cast<MenuItem*>(child) != NULL, "Menu only supports MenuItem objects as children");
|
2013-06-20 14:13:12 +08:00
|
|
|
Layer::addChild(child, zOrder, tag);
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Menu::onEnter()
|
|
|
|
{
|
|
|
|
Layer::onEnter();
|
|
|
|
|
|
|
|
setTouchEnabled(true);
|
|
|
|
setTouchMode(Touch::DispatchMode::ONE_BY_ONE);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::onExit()
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
if (_state == Menu::State::TRACKING_TOUCH)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_selectedItem)
|
2013-01-17 15:04:48 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem->unselected();
|
|
|
|
_selectedItem = NULL;
|
2013-01-17 15:04:48 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 08:47:42 +08:00
|
|
|
_state = Menu::State::WAITING;
|
2011-07-28 14:24:24 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Layer::onExit();
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::removeChild(Node* child, bool cleanup)
|
2013-01-17 15:04:48 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem *pMenuItem = dynamic_cast<MenuItem*>(child);
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(pMenuItem != NULL, "Menu only supports MenuItem objects as children");
|
2013-01-17 15:04:48 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_selectedItem == pMenuItem)
|
2013-01-17 15:04:48 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem = NULL;
|
2013-01-17 15:04:48 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::removeChild(child, cleanup);
|
2013-01-17 15:04:48 +08:00
|
|
|
}
|
|
|
|
|
2012-03-22 14:22:06 +08:00
|
|
|
//Menu - Events
|
2012-04-13 14:11:35 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool Menu::onTouchBegan(Touch* touch, Event* event)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
if (_state != Menu::State::WAITING || ! _visible || !_enabled)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
for (Node *c = this->_parent; c != NULL; c = c->getParent())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
if (c->isVisible() == false)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem = this->itemForTouch(touch);
|
|
|
|
if (_selectedItem)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
_state = Menu::State::TRACKING_TOUCH;
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem->selected();
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return true;
|
|
|
|
}
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
return false;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-08-02 15:45:27 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Menu::onTouchEnded(Touch* touch, Event* event)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
CCASSERT(_state == Menu::State::TRACKING_TOUCH, "[Menu ccTouchEnded] -- invalid state");
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_selectedItem)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem->unselected();
|
|
|
|
_selectedItem->activate();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-26 08:47:42 +08:00
|
|
|
_state = Menu::State::WAITING;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Menu::onTouchCancelled(Touch* touch, Event* event)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
CCASSERT(_state == Menu::State::TRACKING_TOUCH, "[Menu ccTouchCancelled] -- invalid state");
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_selectedItem)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem->unselected();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-26 08:47:42 +08:00
|
|
|
_state = Menu::State::WAITING;
|
2012-03-22 14:22:06 +08:00
|
|
|
}
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void Menu::onTouchMoved(Touch* touch, Event* event)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 08:47:42 +08:00
|
|
|
CCASSERT(_state == Menu::State::TRACKING_TOUCH, "[Menu ccTouchMoved] -- invalid state");
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem *currentItem = this->itemForTouch(touch);
|
2013-09-03 18:22:03 +08:00
|
|
|
if (currentItem != _selectedItem)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_selectedItem)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem->unselected();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem = currentItem;
|
|
|
|
if (_selectedItem)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_selectedItem->selected();
|
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
|
|
|
//Menu - Alignment
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::alignItemsVertically()
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::alignItemsVerticallyWithPadding(float padding)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
float height = -padding;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObject)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Node* child = dynamic_cast<Node*>(pObject);
|
|
|
|
if (child)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
height += child->getContentSize().height * child->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;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObject)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Node* child = dynamic_cast<Node*>(pObject);
|
|
|
|
if (child)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
child->setPosition(Point(0, y - child->getContentSize().height * child->getScaleY() / 2.0f));
|
|
|
|
y -= child->getContentSize().height * child->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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::alignItemsHorizontally(void)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::alignItemsHorizontallyWithPadding(float padding)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
float width = -padding;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObject)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Node* child = dynamic_cast<Node*>(pObject);
|
|
|
|
if (child)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
width += child->getContentSize().width * child->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;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObject)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Node* child = dynamic_cast<Node*>(pObject);
|
|
|
|
if (child)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
child->setPosition(Point(x + child->getContentSize().width * child->getScaleX() / 2.0f, 0));
|
|
|
|
x += child->getContentSize().width * child->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
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void Menu::alignItemsInColumns(int columns, ...)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
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
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void Menu::alignItemsInColumns(int columns, va_list args)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(columns >= 0, "Columns must be >= 0");
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* rows = Array::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
while (columns)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
rows->addObject(Integer::create(columns));
|
2013-07-20 13:01:27 +08:00
|
|
|
columns = va_arg(args, int);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-11-30 21:13:25 +08:00
|
|
|
alignItemsInColumnsWithArray(rows);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::alignItemsInColumnsWithArray(Array* rowsArray)
|
2012-11-30 21:13:25 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObject)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Node* child = dynamic_cast<Node*>(pObject);
|
|
|
|
if (child)
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2013-07-20 13:01:27 +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
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(rowColumns, "");
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
float tmp = child->getContentSize().height;
|
2012-04-19 14:35:52 +08:00
|
|
|
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
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(! columnsOccupied, "");
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Size winSize = Director::getInstance()->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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObject)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Node* child = dynamic_cast<Node*>(pObject);
|
|
|
|
if (child)
|
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;
|
|
|
|
}
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
float tmp = child->getContentSize().height;
|
2012-04-19 14:35:52 +08:00
|
|
|
rowHeight = (unsigned int)((rowHeight >= tmp || isnan(tmp)) ? rowHeight : tmp);
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
child->setPosition(Point(x - winSize.width / 2,
|
|
|
|
y - child->getContentSize().height / 2));
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void Menu::alignItemsInRows(int rows, ...)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
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
|
|
|
|
2013-07-20 13:01:27 +08:00
|
|
|
void Menu::alignItemsInRows(int rows, va_list args)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Array* pArray = Array::create();
|
2012-04-19 14:35:52 +08:00
|
|
|
while (rows)
|
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
pArray->addObject(Integer::create(rows));
|
2013-07-20 13:01:27 +08:00
|
|
|
rows = va_arg(args, int);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-11-30 21:13:25 +08:00
|
|
|
alignItemsInRowsWithArray(pArray);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void Menu::alignItemsInRowsWithArray(Array* columnArray)
|
2012-11-30 21:13:25 +08:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObject)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Node* child = dynamic_cast<Node*>(pObject);
|
|
|
|
if (child)
|
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
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(column < columns.size(), "");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
columnRows = columns[column];
|
|
|
|
// can't have zero rows on a column
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(columnRows, "");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// columnWidth = fmaxf(columnWidth, [item contentSize].width);
|
2013-07-26 07:27:24 +08:00
|
|
|
float tmp = child->getContentSize().width;
|
2012-04-19 14:35:52 +08:00
|
|
|
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
columnHeight += (int)(child->getContentSize().height + 5);
|
2012-04-19 14:35:52 +08:00
|
|
|
++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.
|
2013-07-20 13:01:27 +08:00
|
|
|
CCASSERT(! rowsOccupied, "");
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Size winSize = Director::getInstance()->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
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_children, pObject)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Node* child = dynamic_cast<Node*>(pObject);
|
|
|
|
if (child)
|
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);
|
2013-07-26 07:27:24 +08:00
|
|
|
float tmp = child->getContentSize().width;
|
2012-04-19 14:35:52 +08:00
|
|
|
columnWidth = (unsigned int)((columnWidth >= tmp || isnan(tmp)) ? columnWidth : tmp);
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
child->setPosition(Point(x + columnWidths[column] / 2,
|
2012-04-19 14:35:52 +08:00
|
|
|
y - winSize.height / 2));
|
|
|
|
|
2013-07-26 07:27:24 +08:00
|
|
|
y -= child->getContentSize().height + 10;
|
2012-04-19 14:35:52 +08:00
|
|
|
++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
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
MenuItem* Menu::itemForTouch(Touch *touch)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Point touchLocation = touch->getLocation();
|
2011-07-28 14:24:24 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_children && _children->count() > 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-20 14:13:12 +08:00
|
|
|
Object* pObject = NULL;
|
2013-09-03 18:22:03 +08:00
|
|
|
CCARRAY_FOREACH_REVERSE(_children, pObject)
|
2012-03-22 14:22:06 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
MenuItem* child = dynamic_cast<MenuItem*>(pObject);
|
|
|
|
if (child && child->isVisible() && child->isEnabled())
|
2011-07-28 14:24:24 +08:00
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
Point local = child->convertToNodeSpace(touchLocation);
|
|
|
|
Rect r = child->rect();
|
2013-07-12 14:47:36 +08:00
|
|
|
r.origin = Point::ZERO;
|
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
|
|
|
{
|
2013-07-26 07:27:24 +08:00
|
|
|
return child;
|
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
|