#include "SlidingMenuGrid.h"
SlidingMenuGrid::SlidingMenuGrid()
{
}
SlidingMenuGrid::~SlidingMenuGrid()
{
}
bool SlidingMenuGrid::init()
{
if ( !CCLayer::init() )
{
return false;
}
return true;
}
SlidingMenuGrid* SlidingMenuGrid::menuWithArray(CCMutableArray* items, int cols, int rows, CCPoint pos , CCPoint pad)
{
SlidingMenuGrid *slidingMenu = new SlidingMenuGrid();
if(slidingMenu && slidingMenu->initWithArray(items ,cols ,rows ,pos ,pad ,false))
{
slidingMenu->autorelease();
return slidingMenu;
}
CC_SAFE_DELETE(slidingMenu);
return NULL;
}
SlidingMenuGrid* SlidingMenuGrid::menuWithArray(CCMutableArray* items, int cols, int rows, CCPoint pos , CCPoint pad, bool vertical)
{
SlidingMenuGrid *slidingMenu = new SlidingMenuGrid();
if (slidingMenu && slidingMenu->initWithArray(items ,cols ,rows ,pos ,pad ,vertical))
{
slidingMenu->autorelease();
return slidingMenu;
}
CC_SAFE_DELETE(slidingMenu);
return NULL;
}
bool SlidingMenuGrid::initWithArray(CCMutableArray* items, int cols, int rows, CCPoint pos , CCPoint pad, bool vertical)
{
if( !CCLayer::init() )
{
return false;
}
selectedItem = NULL;
this->setIsTouchEnabled(true);
int z = 1;
CCMutableArray::CCMutableArrayIterator it;
for (it = items->begin(); it != items->end();it++)
{
CCMenuItemSprite* getItem = *it;
this->addChild(getItem, z, z);
++z;
}
padding = pad;
iCurrentPage = 0;
bMoving = false;
menuOrigin = pos;
fMoveDeadZone = 10;
bVerticalPaging = vertical;
fAnimSpeed = 1;
(bVerticalPaging) ? this->buildGridVertical(cols ,rows) : this->buildGrid(cols, rows);
this->setPosition(menuOrigin);
return true;
}
void SlidingMenuGrid::buildGrid(int cols, int rows)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
int col = 0, row = 0;
CCArray* child = this->getChildren();
CCObject* item;
CCARRAY_FOREACH(child, item)
{
CCMenuItemSprite* getItem = (CCMenuItemSprite*) item;
getItem->setPosition(ccp(this->getPosition().x + col * padding.x + (iPageCount * winSize.width), this->getPosition().y - row * padding.y));
++col;
if (col == cols)
{
col = 0;
++row;
if( row == rows )
{
iPageCount++;
col = 0;
row = 0;
}
}
}
srand(time(NULL));
}
void SlidingMenuGrid::buildGridVertical(int cols, int rows)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
int col = 0, row = 0;
CCArray* child = this->getChildren();
CCObject* item;
CCARRAY_FOREACH(child, item)
{
CCMenuItemSprite* getItem = (CCMenuItemSprite*) item;
getItem->setPosition(ccp(this->getPosition().x + col * padding.x , this->getPosition().y - row * padding.y + (iPageCount * winSize.height)));
++col;
if (col == cols)
{
col = 0;
++row;
if( row == rows )
{
iPageCount++;
col = 0;
row = 0;
}
}
}
}
CCMenuItemSprite* SlidingMenuGrid::GetItemWithinTouch(CCTouch* touch)
{
CCPoint touchLocation = CCDirector::sharedDirector()->convertToGL(touch->locationInView(touch->view()));
// Parse our menu items and see if our touch exists within one.
CCArray* child = this->getChildren();
CCObject* item;
CCARRAY_FOREACH(child, item)
{
CCMenuItemSprite* getItem = (CCMenuItemSprite*) item;
CCPoint local = getItem->convertToNodeSpace(touchLocation);
CCRect r = getItem->rect();
r.origin = CCPointZero;
// If the touch was within this item. Return the item.
if (CCRect::CCRectContainsPoint(r, local))
{
return getItem;
}
}
// Didn't touch an item.
return NULL;
}
void SlidingMenuGrid::ccTouchesBegan(CCSet* touches, CCEvent* event)
{
CCTouch *touch = (CCTouch*)(touches->anyObject());
touchOrigin = CCDirector::sharedDirector()->convertToGL(touch->locationInView(touch->view()));
// If we weren't in "waiting" state bail out.
if (state == kCCMenuStateWaiting)
{
// Activate the menu item if we are touching one.
if(!bMoving){
selectedItem = this->GetItemWithinTouch(touch);
if(selectedItem){
selectedItem->selected();
}
}
state = kCCMenuStateTrackingTouch;
}
}
void SlidingMenuGrid::ccTouchesCancelled(CCSet* touches, CCEvent* event)
{
if(selectedItem)
{
selectedItem->unselected();
selectedItem = NULL;
state = kCCMenuStateWaiting;
}
}
void SlidingMenuGrid::ccTouchesMoved(CCSet* touches, CCEvent* event)
{
CCTouch *touch = (CCTouch*)(touches->anyObject());
if(this->GetItemWithinTouch(touch) == NULL && selectedItem){
//Touch Cancelled
selectedItem->unselected();
selectedItem = NULL;
state = kCCMenuStateWaiting;
return;
}
if(this->GetItemWithinTouch(touch) != NULL && selectedItem){
return;
}
// Calculate the current touch point during the move.
touchStop = CCDirector::sharedDirector()->convertToGL(touch->locationInView(touch->view()));
// Distance between the origin of the touch and current touch point.
fMoveDelta = (bVerticalPaging) ? (touchStop.y - touchOrigin.y) : (touchStop.x - touchOrigin.x);
// Set our position.
this->setPosition(this->GetPositionOfCurrentPageWithOffset(fMoveDelta));
bMoving = true;
}
void SlidingMenuGrid::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
if( bMoving )
{
bMoving = false;
// Do we have multiple pages?
if( iPageCount > 1 && (fMoveDeadZone < fabs(fMoveDelta)))
{
// Are we going forward or backward?
bool bForward = (fMoveDelta < 0) ? true : false;
// Do we have a page available?
if(bForward && (iPageCount>iCurrentPage+1))
{
// Increment currently active page.
iCurrentPage++;
}
else if(!bForward && (iCurrentPage > 0))
{
// Decrement currently active page.
iCurrentPage--;
}
}
// Start sliding towards the current page.
this->moveToCurrentPage();
}
// User wasn't sliding menu and simply tapped the screen. Activate the menu item.
else
{
if(selectedItem)
{
selectedItem->unselected();
selectedItem->activate();
}
}
// Back to waiting state.
state = kCCMenuStateWaiting;
}
void SlidingMenuGrid::moveToCurrentPage()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
// Perform the action
CCEaseBounce* action =CCEaseBounce::actionWithAction(CCMoveTo::actionWithDuration(fAnimSpeed*0.3, this->GetPositionOfCurrentPage()));
this->runAction(action);
}
CCPoint SlidingMenuGrid::GetPositionOfCurrentPage()
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
return (bVerticalPaging) ?
CCPointMake(menuOrigin.x,menuOrigin.y-(iCurrentPage*winSize.height))
: CCPointMake((menuOrigin.x-(iCurrentPage*winSize.width)),menuOrigin.y);
}
CCPoint SlidingMenuGrid::GetPositionOfCurrentPageWithOffset(float offset)
{
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
return (bVerticalPaging) ?
CCPointMake(menuOrigin.x,menuOrigin.y-(iCurrentPage*winSize.height)+offset)
: CCPointMake((menuOrigin.x-(iCurrentPage*winSize.width)+offset),menuOrigin.y);
}
// Returns the swiping dead zone.
float SlidingMenuGrid::GetSwipeDeadZone()
{
return fMoveDeadZone;
}
void SlidingMenuGrid::SetSwipeDeadZone(float fValue)
{
fMoveDeadZone = fValue;
}
// Returns wheather or not vertical paging is enabled.
bool SlidingMenuGrid::IsVerticallyPaged()
{
return bVerticalPaging;
}
// Sets the vertical paging flag.
void SlidingMenuGrid::SetVerticalPaging(bool bValue)
{
bVerticalPaging = bValue;
// this->buildGridVertical();
}
SlidingMenuGrid* SlidingMenuGrid::menuWithArray(CCMutableArray<CCMenuItemSprite*>* items, int cols, int rows, CCPoint pos , CCPoint pad)
{
return menuWithArray(items,cols,rows,pos,pad,false);
}
i solved it.
Hello!
I copied the source code ported from cocos2D for iphone (http://brandonreynolds.com/blog/2011/01/09/cocos2d-sliding-menu-grid/), based on the source code on this thread page K pop. I corrected several errors and added a few features (tags for each page)
I am new to this place,
could you please tell me how to integrate this with my HelloWorlsScene.cpp??
I have tried a lot but i keep getting errors!