Hub

Slider Code

Thumb_cococ111

Brief

Slider code and its video. If you're using v2.0 and higher version, I suggest you to use extensions/CCControlExtension/CCControlSlider

Description

Slider code :. If you’re using v2.0 and higher version, I suggest you to use extensions/CCControlExtension/CCControlSlider

`#include "SlidingMenuGrid.h"

SlidingMenuGrid::SlidingMenuGrid()
{

}

SlidingMenuGrid::~SlidingMenuGrid()
{

}

bool SlidingMenuGrid::init()
{
if ( !CCLayer::init() )
{
return false;
}
return true;
}

SlidingMenuGrid* SlidingMenuGrid::menuWithArray(CCMutableArray<CCMenuItemSprite*>* 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<CCMenuItemSprite*>* 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<CCMenuItemSprite*>* 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();
}`

Comments

Sign up for our newsletter to keep up with the latest developments, releases and updates for Cocos2d-x.