cocos2d-x源码总目录
http://blog.youkuaiyun.com/u011225840/article/details/31743129
1.准备工作
想弄懂可循环的CCscrollView,首先请阅读cocos2d-x本身的CCscrollView源码http://blog.youkuaiyun.com/u011225840/article/details/30033501(我已经添加注释,方便阅读)。
2.源码展示
因为源码我想放到git上,所以注释都是用的英文,如果这部分源码有人有问题,请在评论区留言,我会逐一回答。
总体说下,CCCycleScrollview继承了CCscrollView以及CCscrollViewDelegate,这样保证了代码的可扩展性。
同时,CCCycleScrollview参考了tableView,使用了CCCycleCell,将部分界面和数据部分解耦。
/****************************************************************************
Author : poplaryang
2014- 06
http://blog.youkuaiyun.com/u011225840
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.
****************************************************************************/
#include "cocos2d.h"
#include "cocos-ext.h"
#include <vector>
using namespace cocos2d;
using namespace cocos2d::extension;
#define SCROLL_DEACCEL_RATE 0.95f
#define SCROLL_DEACCEL_DIST 1.0f
/*
abstract class
the class defines that the what a cycleCell needs to do.
*/
class CCCycleCell :public CCNode
{
public:
//The cell has been selected ,so you can do something with that, Like highlight or what ever you want.
virtual void getSelected() = 0;
//The cell must show the different view with the different index. Like, the hours within 12.
virtual void updateWithIndex(unsigned int index) = 0;
unsigned int getIndex(){return m_index;}
protected:
unsigned int m_index;
};
//The moving direction
enum MovingDirection
{
Left = 1,
Right = 2,
Up,
Down
};
//The direction that the view allowd to move, the view can't do the both like CCScrollView.
enum Direction
{
CycleDirectionHorizontal=1,
CycleDirectionVertical
};
/*
The view that can show the data with a cycle way.(forgive my awful English ....)
Eg. The clock the view in iphone, the view will show 0 after 23,otherwise ,you move the opposite direction 23,22,21....until 0.
*/
class CCCycleScrollView: public CCScrollView,public CCScrollViewDelegate
{
public:
CCCycleScrollView();
virtual ~CCCycleScrollView();
/*
The background node is the background that will be show cyclely.
The nodeCount is the count that every background node can hold the cycleCell.
The totalCount is the count that how many different data in the cycle cell.Eg...The hour in a day ,can have the 24 totalCount.
*/
static CCCycleScrollView* create(std::vector<CCNode*>& background,std::vector<CCCycleCell*>& cycleCell,unsigned int nodeCount =0,unsigned int totalCount = 0,Direction direction = CycleDirectionHorizontal);
/*
init methods.
*/
bool initWithViewSize(std::vector<CCNode*>& background,std::vector<CCCycleCell*>& cycleCell,unsigned int nodeCount =0,unsigned int totalCount = 0,Direction direction = CycleDirectionHorizontal);
/*
TouchDelegate.
*/
bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
protected:
//CCScrollViewDelegate
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view);
virtual void scrollViewDidStop(CCScrollView* view);
//The delegate must be self, so beyound the class ,anyone can't change the scrollview delegate.
void setDelegate(){};
/*
we should adjust the backgroundNode's posi