Merge pull request #1297 from dumganhar/tableview-win32

fixed #1458: Adding CCTableView support.
This commit is contained in:
James Chen 2012-09-09 23:52:18 -07:00
commit c9ca584726
31 changed files with 1486 additions and 100 deletions

View File

@ -145,6 +145,9 @@ OBJECTS = ../actions/CCAction.o \
../../extensions/CCBReader/CCSpriteLoader.o \
../../extensions/CCBReader/CCLayerLoader.o \
../../extensions/GUI/CCScrollView/CCScrollView.o \
../../extensions/GUI/CCScrollView/CCSorting.o \
../../extensions/GUI/CCScrollView/CCTableView.o \
../../extensions/GUI/CCScrollView/CCTableViewCell.o \
../../extensions/GUI/CCControlExtension/CCControlButton.o \
../../extensions/GUI/CCControlExtension/CCControlColourPicker.o \
../../extensions/GUI/CCControlExtension/CCControl.o \

View File

@ -1 +1 @@
e78f546d2199a6e37906db590c8a47033f3febd8
6dfcf25474cd85fc1f77e3578c3b54357cdc0446

View File

@ -35,6 +35,9 @@ GUI/CCControlExtension/CCMenuPassive.cpp \
GUI/CCControlExtension/CCScale9Sprite.cpp \
GUI/CCControlExtension/CCSpacer.cpp \
GUI/CCScrollView/CCScrollView.cpp \
GUI/CCScrollView/CCTableView.cpp \
GUI/CCScrollView/CCTableViewCell.cpp \
GUI/CCScrollView/CCSorting.cpp \
GUI/CCEditBox/CCEditBox.cpp \
GUI/CCEditBox/CCEditBoxImplAndroid.cpp \
network/HttpClient.cpp

View File

@ -2,18 +2,6 @@
#include "CCBSelectorResolver.h"
#include "CCBMemberVariableAssigner.h"
#define PROPERTY_POSITION "position"
#define PROPERTY_CONTENTSIZE "contentSize"
#define PROPERTY_ANCHORPOINT "anchorPoint"
#define PROPERTY_SCALE "scale"
#define PROPERTY_ROTATION "rotation"
#define PROPERTY_TAG "tag"
#define PROPERTY_IGNOREANCHORPOINTFORPOSITION "ignoreAnchorPointForPosition"
#define PROPERTY_VISIBLE "visible"
#define ASSERT_FAIL_UNEXPECTED_PROPERTY(PROPERTY) CCLog("Unexpected property: '%s'!\n", PROPERTY->getCString()); assert(false)
#define ASSERT_FAIL_UNEXPECTED_PROPERTYTYPE(PROPERTYTYPE) CCLog("Unexpected property type: '%d'!\n", PROPERTYTYPE); assert(false)
USING_NS_CC;
NS_CC_EXT_BEGIN

View File

@ -7,6 +7,18 @@
NS_CC_EXT_BEGIN
#define PROPERTY_POSITION "position"
#define PROPERTY_CONTENTSIZE "contentSize"
#define PROPERTY_ANCHORPOINT "anchorPoint"
#define PROPERTY_SCALE "scale"
#define PROPERTY_ROTATION "rotation"
#define PROPERTY_TAG "tag"
#define PROPERTY_IGNOREANCHORPOINTFORPOSITION "ignoreAnchorPointForPosition"
#define PROPERTY_VISIBLE "visible"
#define ASSERT_FAIL_UNEXPECTED_PROPERTY(PROPERTY) CCLog("Unexpected property: '%s'!\n", PROPERTY->getCString()); assert(false)
#define ASSERT_FAIL_UNEXPECTED_PROPERTYTYPE(PROPERTYTYPE) CCLog("Unexpected property type: '%d'!\n", PROPERTYTYPE); assert(false)
#define CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(T) virtual T * createCCNode(cocos2d::CCNode * pParent, cocos2d::extension::CCBReader * pCCBReader) { \
return T::create(); \
}

View File

@ -10,6 +10,14 @@ USING_NS_CC;
NS_CC_EXT_BEGIN
void CCScrollViewLoader::onHandlePropTypeSize(CCNode * pNode, CCNode * pParent, CCString * pPropertyName, CCSize pSize, CCBReader * pCCBReader) {
if(pPropertyName->compare(PROPERTY_CONTENTSIZE) == 0) {
((CCScrollView *)pNode)->setViewSize(pSize);
} else {
CCNodeLoader::onHandlePropTypeSize(pNode, pParent, pPropertyName, pSize, pCCBReader);
}
}
void CCScrollViewLoader::onHandlePropTypeCheck(CCNode * pNode, CCNode * pParent, CCString * pPropertyName, bool pCheck, CCBReader * pCCBReader) {
if(pPropertyName->compare(PROPERTY_CLIPSTOBOUNDS) == 0) {
((CCScrollView *)pNode)->setClippingToBounds(pCheck);
@ -23,6 +31,7 @@ void CCScrollViewLoader::onHandlePropTypeCheck(CCNode * pNode, CCNode * pParent,
void CCScrollViewLoader::onHandlePropTypeCCBFile(CCNode * pNode, CCNode * pParent, CCString * pPropertyName, CCNode * pCCBFileNode, CCBReader * pCCBReader) {
if(pPropertyName->compare(PROPERTY_CONTAINER) == 0) {
((CCScrollView *)pNode)->setContainer(pCCBFileNode);
((CCScrollView *)pNode)->updateInset();
} else {
CCNodeLoader::onHandlePropTypeCCBFile(pNode, pParent, pPropertyName, pCCBFileNode, pCCBReader);
}

View File

@ -16,7 +16,7 @@ class CCScrollViewLoader : public CCNodeLoader {
protected:
CCB_VIRTUAL_NEW_AUTORELEASE_CREATECCNODE_METHOD(CCScrollView);
virtual void onHandlePropTypeSize(CCNode * pNode, CCNode * pParent, CCString * pPropertyName, CCSize pSize, CCBReader * pCCBReader);
virtual void onHandlePropTypeCCBFile(CCNode * pNode, CCNode * pParent, CCString * pPropertyName, CCNode * pCCBFileNode, CCBReader * pCCBReader);
virtual void onHandlePropTypeCheck(CCNode * pNode, CCNode * pParent, CCString * pPropertyName, bool pCheck, CCBReader * pCCBReader);
virtual void onHandlePropTypeFloat(CCNode * pNode, CCNode * pParent, CCString * pPropertyName, float pFloat, CCBReader * pCCBReader);

View File

@ -1,31 +1,27 @@
//
// SWScrollView.m
// SWGameLib
//
// Copyright (c) 2010-2012 cocos2d-x.org
// Copyright (c) 2010 Sangwoo Im
//
// 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 false 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.
//
// Created by Sangwoo Im on 6/3/10.
// Copyright 2010 Sangwoo Im. All rights reserved.
//
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2010 Sangwoo Im
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 "CCScrollView.h"
#include "actions/CCActionInterval.h"
@ -37,6 +33,7 @@
#include "CCDirector.h"
#include "kazmath/GL/matrix.h"
#include "touch_dispatcher/CCTouch.h"
#include "CCEGLView.h"
NS_CC_EXT_BEGIN
@ -53,7 +50,7 @@ CCScrollView::CCScrollView()
, m_pDelegate(NULL)
, m_bDragging(false)
, m_bBounceable(false)
, m_eDirection(CCScrollViewDirectionBoth)
, m_eDirection(kCCScrollViewDirectionBoth)
, m_bClippingToBounds(false)
, m_pContainer(NULL)
, m_bTouchMoved(false)
@ -118,6 +115,8 @@ bool CCScrollView::initWithViewSize(CCSize size, CCNode *container/* = NULL*/)
if (!this->m_pContainer)
{
m_pContainer = CCLayer::create();
this->m_pContainer->ignoreAnchorPointForPosition(false);
this->m_pContainer->setAnchorPoint(ccp(0.0f, 0.0f));
}
this->setViewSize(size);
@ -128,7 +127,7 @@ bool CCScrollView::initWithViewSize(CCSize size, CCNode *container/* = NULL*/)
m_bBounceable = true;
m_bClippingToBounds = true;
//m_pContainer->setContentSize(CCSizeZero);
m_eDirection = CCScrollViewDirectionBoth;
m_eDirection = kCCScrollViewDirectionBoth;
m_pContainer->setPosition(ccp(0.0f, 0.0f));
m_fTouchLength = 0.0f;
@ -309,17 +308,6 @@ void CCScrollView::setZoomScaleInDuration(float s, float dt)
void CCScrollView::setViewSize(CCSize size)
{
m_tViewSize = size;
if (this->m_pContainer != NULL)
{
m_fMaxInset = this->maxContainerOffset();
m_fMaxInset = ccp(m_fMaxInset.x + m_tViewSize.width * INSET_RATIO,
m_fMaxInset.y + m_tViewSize.height * INSET_RATIO);
m_fMinInset = this->minContainerOffset();
m_fMinInset = ccp(m_fMinInset.x - m_tViewSize.width * INSET_RATIO,
m_fMinInset.y - m_tViewSize.height * INSET_RATIO);
}
CCLayer::setContentSize(size);
}
@ -356,13 +344,13 @@ void CCScrollView::relocateContainer(bool animated)
newX = oldPoint.x;
newY = oldPoint.y;
if (m_eDirection == CCScrollViewDirectionBoth || m_eDirection == CCScrollViewDirectionHorizontal)
if (m_eDirection == kCCScrollViewDirectionBoth || m_eDirection == kCCScrollViewDirectionHorizontal)
{
newX = MIN(newX, max.x);
newX = MAX(newX, min.x);
}
if (m_eDirection == CCScrollViewDirectionBoth || m_eDirection == CCScrollViewDirectionVertical)
if (m_eDirection == kCCScrollViewDirectionBoth || m_eDirection == kCCScrollViewDirectionVertical)
{
newY = MIN(newY, max.y);
newY = MAX(newY, min.y);
@ -456,8 +444,26 @@ const CCSize & CCScrollView::getContentSize()
void CCScrollView::setContentSize(const CCSize & size)
{
this->setViewSize(size);
if (this->getContainer() != NULL)
{
this->getContainer()->setContentSize(size);
this->updateInset();
}
}
void CCScrollView::updateInset()
{
if (this->getContainer() != NULL)
{
m_fMaxInset = this->maxContainerOffset();
m_fMaxInset = ccp(m_fMaxInset.x + m_tViewSize.width * INSET_RATIO,
m_fMaxInset.y + m_tViewSize.height * INSET_RATIO);
m_fMinInset = this->minContainerOffset();
m_fMinInset = ccp(m_fMinInset.x - m_tViewSize.width * INSET_RATIO,
m_fMinInset.y - m_tViewSize.height * INSET_RATIO);
}
}
/**
* make sure all children go to the container
*/
@ -495,10 +501,10 @@ void CCScrollView::beforeDraw()
glEnable(GL_SCISSOR_TEST);
float s = this->getScale();
CCDirector *director = CCDirector::sharedDirector();
s *= director->getContentScaleFactor();
glScissor((GLint)screenPos.x, (GLint)screenPos.y, (GLsizei)(m_tViewSize.width*s), (GLsizei)(m_tViewSize.height*s));
// CCDirector *director = CCDirector::sharedDirector();
// s *= director->getContentScaleFactor();
CCEGLView::sharedOpenGLView()->setScissorInPoints(screenPos.x*s, screenPos.y*s, m_tViewSize.width*s, m_tViewSize.height*s);
//glScissor((GLint)screenPos.x, (GLint)screenPos.y, (GLsizei)(m_tViewSize.width*s), (GLsizei)(m_tViewSize.height*s));
}
}
@ -648,10 +654,10 @@ void CCScrollView::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
switch (m_eDirection)
{
case CCScrollViewDirectionVertical:
case kCCScrollViewDirectionVertical:
moveDistance = ccp(0.0f, moveDistance.y);
break;
case CCScrollViewDirectionHorizontal:
case kCCScrollViewDirectionHorizontal:
moveDistance = ccp(moveDistance.x, 0.0f);
break;
default:

View File

@ -1,32 +1,27 @@
//
// SWScrollView.h
// SWGameLib
//
// Copyright (c) 2010-2012 cocos2d-x.org
// Copyright (c) 2010 Sangwoo Im
//
// 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.
//
//
// Created by Sangwoo Im on 6/3/10.
// Copyright 2010 Sangwoo Im. All rights reserved.
//
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2010 Sangwoo Im
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.
****************************************************************************/
#ifndef __CCSCROLLVIEW_H__
#define __CCSCROLLVIEW_H__
@ -42,9 +37,10 @@ NS_CC_EXT_BEGIN
*/
typedef enum {
CCScrollViewDirectionHorizontal = 0,
CCScrollViewDirectionVertical,
CCScrollViewDirectionBoth
kCCScrollViewDirectionNone = -1,
kCCScrollViewDirectionHorizontal = 0,
kCCScrollViewDirectionVertical,
kCCScrollViewDirectionBoth
} CCScrollViewDirection;
class CCScrollView;
@ -195,7 +191,7 @@ public:
* direction allowed to scroll. CCScrollViewDirectionBoth by default.
*/
CCScrollViewDirection getDirection() { return m_eDirection; }
void setDirection(CCScrollViewDirection eDirection) { m_eDirection = eDirection; }
virtual void setDirection(CCScrollViewDirection eDirection) { m_eDirection = eDirection; }
CCScrollViewDelegate* getDelegate() { return m_pDelegate; }
void setDelegate(CCScrollViewDelegate* pDelegate) { m_pDelegate = pDelegate; }
@ -210,6 +206,7 @@ public:
virtual void setContentSize(const CCSize & size);
virtual const CCSize & getContentSize();
void updateInset();
/**
* Determines whether it clips its children or not.
*/

View File

@ -0,0 +1,170 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2010 Sangwoo Im
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 "CCSorting.h"
NS_CC_EXT_BEGIN
class CCSortedObject : public CCObject, public CCSortableObject
{
public:
CCSortedObject() : objectID(0) {}
virtual void setObjectID(unsigned int objectID) { this->objectID = objectID; }
virtual unsigned int getObjectID() { return objectID; }
private:
unsigned int objectID;
};
static int _compareObject(const void * val1, const void * val2)
{
CCSortableObject* operand1;
CCSortableObject* operand2;
operand1 = (CCSortableObject*)val1;
operand2 = (CCSortableObject*)val2;
if (operand1->getObjectID() > operand2->getObjectID())
{
return 1;
}
else if (operand1->getObjectID() < operand2->getObjectID()) {
return -1;
}
return 0;
}
void CCArrayForObjectSorting::insertSortedObject(CCSortableObject* object)
{
unsigned int idx;
CCObject* pObj = dynamic_cast<CCObject*>(object);
CCAssert(pObj, "Invalid parameter.");
idx = this->indexOfSortedObject(object);
this->insertObject(pObj, idx);
}
void CCArrayForObjectSorting::removeSortedObject(CCSortableObject* object)
{
if (this->count() == 0) {
return;
}
unsigned int idx;
CCSortableObject* foundObj;
idx = this->indexOfSortedObject(object);
if (idx < this->count() && idx != CC_INVALID_INDEX) {
foundObj = dynamic_cast<CCSortableObject*>(this->objectAtIndex(idx));
if(foundObj->getObjectID() == object->getObjectID()) {
this->removeObjectAtIndex(idx);
}
}
}
void CCArrayForObjectSorting::setObjectID_ofSortedObject(unsigned int tag, CCSortableObject* object)
{
CCSortableObject* foundObj;
unsigned int idx;
idx = this->indexOfSortedObject(object);
if (idx < this->count() && idx != CC_INVALID_INDEX)
{
foundObj = dynamic_cast<CCSortableObject*>(this->objectAtIndex(idx));
CCObject* pObj = dynamic_cast<CCObject*>(foundObj);
pObj->retain();
if(foundObj->getObjectID() == object->getObjectID()) {
this->removeObjectAtIndex(idx);
foundObj->setObjectID(tag);
this->insertSortedObject(foundObj);
pObj->release();
} else {
pObj->release();
}
}
}
CCSortableObject* CCArrayForObjectSorting::objectWithObjectID(unsigned int tag)
{
if (this->count() == 0) {
return NULL;
}
unsigned int idx;
CCSortableObject* foundObj;
foundObj = new CCSortedObject();
foundObj->setObjectID(tag);
idx = this->indexOfSortedObject(foundObj);
((CCSortedObject*)foundObj)->release();
foundObj = NULL;
if (idx < this->count() && idx != CC_INVALID_INDEX)
{
foundObj = dynamic_cast<CCSortableObject*>(this->objectAtIndex(idx));
if (foundObj->getObjectID() != tag) {
foundObj = NULL;
}
}
return foundObj;
}
unsigned int CCArrayForObjectSorting::indexOfSortedObject(CCSortableObject* object)
{
unsigned int idx = 0;
if (object)
{
// CCObject* pObj = (CCObject*)bsearch((CCObject*)&object, data->arr, data->num, sizeof(CCObject*), _compareObject);
// FIXME: need to use binary search to improve performance
CCObject* pObj = NULL;
unsigned int uPrevObjectID = 0;
unsigned int uOfSortObjectID = object->getObjectID();
CCARRAY_FOREACH(this, pObj)
{
CCSortableObject* pSortableObj = dynamic_cast<CCSortableObject*>(pObj);
unsigned int uCurObjectID = pSortableObj->getObjectID();
if ( (uOfSortObjectID == uCurObjectID)
|| (uOfSortObjectID >= uPrevObjectID && uOfSortObjectID < uCurObjectID))
{
break;
}
uPrevObjectID = uCurObjectID;
idx++;
}
}
else
{
idx = CC_INVALID_INDEX;
}
return idx;
}
NS_CC_EXT_END

View File

@ -0,0 +1,110 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2010 Sangwoo Im
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.
****************************************************************************/
#ifndef __CCSORTING_H__
#define __CCSORTING_H__
#include "cocoa/CCArray.h"
#include "ExtensionMacros.h"
NS_CC_EXT_BEGIN
class CCSortableObject
{
public:
virtual ~CCSortableObject() {}
virtual void setObjectID(unsigned int objectID) = 0;
virtual unsigned int getObjectID() = 0;
};
class CCArrayForObjectSorting : public CCArray
{
public:
CCArrayForObjectSorting() : CCArray() {}
/*!
* Inserts a given object into array.
*
* Inserts a given object into array with key and value that are used in
* sorting. "value" must respond to message, compare:, which returns
* (NSComparisonResult). If it does not respond to the message, it is appended.
* If the compare message does not result NSComparisonResult, sorting behavior
* is not defined. It ignores duplicate entries and inserts next to it.
*
* @param object to insert
*/
void insertSortedObject(CCSortableObject* object);
/*!
* Removes an object in array.
*
* Removes an object with given key and value. If no object is found in array
* with the key and value, no action is taken.
*
* @param value to remove
*/
void removeSortedObject(CCSortableObject* object);
/*!
* Sets a new value of the key for the given object.
*
* In case where sorting value must be changed, this message must be sent to
* keep consistency of being sorted. If it is changed externally, it must be
* sorted completely again.
*
* @param value to set
* @param object the object which has the value
*/
void setObjectID_ofSortedObject(unsigned int tag, CCSortableObject* object);
CCSortableObject* objectWithObjectID(unsigned int tag);
/*!
* Returns an object with given key and value.
*
* Returns an object with given key and value. If no object is found,
* it returns nil.
*
* @param value to locate object
* @return object found or nil.
*/
CCSortableObject* getObjectWithObjectID(unsigned int tag);
/*!
* Returns an index of the object with given key and value.
*
* Returns the index of an object with given key and value.
* If no object is found, it returns an index at which the given object value
* would have been located. If object must be located at the end of array,
* it returns the length of the array, which is out of bound.
*
* @param value to locate object
* @return index of an object found
*/
unsigned int indexOfSortedObject(CCSortableObject* obj);
};
NS_CC_EXT_END
#endif /* __CCSORTING_H__ */

View File

@ -0,0 +1,486 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2010 Sangwoo Im
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 "cocos2d.h"
#include "CCTableView.h"
#include "CCTableViewCell.h"
#include "menu_nodes/CCMenu.h"
#include "support/CCPointExtension.h"
#include "CCSorting.h"
#include "layers_scenes_transitions_nodes/CCLayer.h"
NS_CC_EXT_BEGIN
CCTableView* CCTableView::create(CCTableViewDataSource* dataSource, CCSize size)
{
return CCTableView::create(dataSource, size, NULL);
}
CCTableView* CCTableView::create(CCTableViewDataSource* dataSource, CCSize size, CCNode *container)
{
CCTableView *table = new CCTableView();
table->initWithViewSize(size, container);
table->autorelease();
table->setDataSource(dataSource);
table->_updateContentSize();
return table;
}
bool CCTableView::initWithViewSize(CCSize size, CCNode* container/* = NULL*/)
{
if (CCScrollView::initWithViewSize(size,container))
{
m_pCellsUsed = new CCArrayForObjectSorting();
m_pCellsFreed = new CCArrayForObjectSorting();
m_pIndices = new std::set<unsigned int>();
m_pTableViewDelegate = NULL;
m_eVordering = kCCTableViewFillBottomUp;
this->setDirection(kCCScrollViewDirectionVertical);
CCScrollView::setDelegate(this);
return true;
}
return false;
}
CCTableView::CCTableView()
: m_pIndices(NULL)
, m_pCellsUsed(NULL)
, m_pCellsFreed(NULL)
, m_pDataSource(NULL)
, m_pTableViewDelegate(NULL)
, m_eOldDirection(kCCScrollViewDirectionNone)
{
}
CCTableView::~CCTableView()
{
CC_SAFE_DELETE(m_pIndices);
CC_SAFE_RELEASE(m_pCellsUsed);
CC_SAFE_RELEASE(m_pCellsFreed);
}
void CCTableView::setVerticalFillOrder(CCTableViewVerticalFillOrder fillOrder)
{
if (m_eVordering != fillOrder) {
m_eVordering = fillOrder;
if (m_pCellsUsed->count() > 0) {
this->reloadData();
}
}
}
CCTableViewVerticalFillOrder CCTableView::getVerticalFillOrder()
{
return m_eVordering;
}
void CCTableView::reloadData()
{
CCObject* pObj = NULL;
CCARRAY_FOREACH(m_pCellsUsed, pObj)
{
CCTableViewCell* cell = (CCTableViewCell*)pObj;
m_pCellsFreed->addObject(cell);
cell->reset();
if (cell->getParent() == this->getContainer())
{
this->getContainer()->removeChild(cell, true);
}
}
m_pIndices->clear();
m_pCellsUsed->release();
m_pCellsUsed = new CCArrayForObjectSorting();
this->_updateContentSize();
if (m_pDataSource->numberOfCellsInTableView(this) > 0)
{
this->scrollViewDidScroll(this);
}
}
CCTableViewCell *CCTableView::cellAtIndex(unsigned int idx)
{
return this->_cellWithIndex(idx);
}
void CCTableView::updateCellAtIndex(unsigned int idx)
{
if (idx == CC_INVALID_INDEX || idx > m_pDataSource->numberOfCellsInTableView(this)-1)
{
return;
}
CCTableViewCell *cell;
cell = this->_cellWithIndex(idx);
if (cell) {
this->_moveCellOutOfSight(cell);
}
cell = m_pDataSource->tableCellAtIndex(this, idx);
this->_setIndexForCell(idx, cell);
this->_addCellIfNecessary(cell);
}
void CCTableView::insertCellAtIndex(unsigned int idx)
{
if (idx == CC_INVALID_INDEX || idx > m_pDataSource->numberOfCellsInTableView(this)-1) {
return;
}
CCTableViewCell *cell;
int newIdx;
cell = (CCTableViewCell*)m_pCellsUsed->objectWithObjectID(idx);
if (cell)
{
newIdx = m_pCellsUsed->indexOfSortedObject(cell);
for (unsigned int i=newIdx; i<m_pCellsUsed->count(); i++)
{
cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(i);
this->_setIndexForCell(cell->getIdx()+1, cell);
}
}
// [m_pIndices shiftIndexesStartingAtIndex:idx by:1];
//insert a new cell
cell = m_pDataSource->tableCellAtIndex(this, idx);
this->_setIndexForCell(idx, cell);
this->_addCellIfNecessary(cell);
this->_updateContentSize();
}
void CCTableView::removeCellAtIndex(unsigned int idx)
{
if (idx == CC_INVALID_INDEX || idx > m_pDataSource->numberOfCellsInTableView(this)-1) {
return;
}
CCTableViewCell *cell;
unsigned int newIdx;
cell = this->_cellWithIndex(idx);
if (!cell) {
return;
}
newIdx = m_pCellsUsed->indexOfSortedObject(cell);
//remove first
this->_moveCellOutOfSight(cell);
m_pIndices->erase(idx);
// [m_pIndices shiftIndexesStartingAtIndex:idx+1 by:-1];
for (unsigned int i=m_pCellsUsed->count()-1; i > newIdx; i--) {
cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(i);
this->_setIndexForCell(cell->getIdx()-1, cell);
}
}
CCTableViewCell *CCTableView::dequeueCell()
{
CCTableViewCell *cell;
if (m_pCellsFreed->count() == 0) {
cell = NULL;
} else {
cell = (CCTableViewCell*)m_pCellsFreed->objectAtIndex(0);
cell->retain();
m_pCellsFreed->removeObjectAtIndex(0);
cell->autorelease();
}
return cell;
}
void CCTableView::_addCellIfNecessary(CCTableViewCell * cell)
{
if (cell->getParent() != this->getContainer())
{
this->getContainer()->addChild(cell);
}
m_pCellsUsed->insertSortedObject(cell);
m_pIndices->insert(cell->getIdx());
// [m_pIndices addIndex:cell.idx];
}
void CCTableView::_updateContentSize()
{
CCSize size, cellSize;
unsigned int cellCount;
cellSize = m_pDataSource->cellSizeForTable(this);
cellCount = m_pDataSource->numberOfCellsInTableView(this);
switch (this->getDirection())
{
case kCCScrollViewDirectionHorizontal:
size = CCSizeMake(cellCount * cellSize.width, cellSize.height);
break;
default:
size = CCSizeMake(cellSize.width, cellCount * cellSize.height);
break;
}
this->setContentSize(size);
if (m_eOldDirection != m_eDirection)
{
if (m_eDirection == kCCScrollViewDirectionHorizontal)
{
this->setContentOffset(ccp(0,0));
}
else
{
this->setContentOffset(ccp(0,this->minContainerOffset().y));
}
m_eOldDirection = m_eDirection;
}
}
CCPoint CCTableView::_offsetFromIndex(unsigned int index)
{
CCPoint offset = this->__offsetFromIndex(index);
const CCSize cellSize = m_pDataSource->cellSizeForTable(this);
if (m_eVordering == kCCTableViewFillTopDown) {
offset.y = this->getContainer()->getContentSize().height - offset.y - cellSize.height;
}
return offset;
}
CCPoint CCTableView::__offsetFromIndex(unsigned int index)
{
CCPoint offset;
CCSize cellSize;
cellSize = m_pDataSource->cellSizeForTable(this);
switch (this->getDirection()) {
case kCCScrollViewDirectionHorizontal:
offset = ccp(cellSize.width * index, 0.0f);
break;
default:
offset = ccp(0.0f, cellSize.height * index);
break;
}
return offset;
}
unsigned int CCTableView::_indexFromOffset(CCPoint offset)
{
int index = 0;
const int maxIdx = m_pDataSource->numberOfCellsInTableView(this)-1;
const CCSize cellSize = m_pDataSource->cellSizeForTable(this);
if (m_eVordering == kCCTableViewFillTopDown) {
offset.y = this->getContainer()->getContentSize().height - offset.y - cellSize.height;
}
index = MAX(0, this->__indexFromOffset(offset));
index = MIN(index, maxIdx);
return index;
}
int CCTableView::__indexFromOffset(CCPoint offset)
{
int index = 0;
CCSize cellSize;
cellSize = m_pDataSource->cellSizeForTable(this);
switch (this->getDirection()) {
case kCCScrollViewDirectionHorizontal:
index = offset.x/cellSize.width;
break;
default:
index = offset.y/cellSize.height;
break;
}
return index;
}
CCTableViewCell* CCTableView::_cellWithIndex(unsigned int cellIndex)
{
CCTableViewCell *found;
found = NULL;
// if ([m_pIndices containsIndex:cellIndex])
if (m_pIndices->find(cellIndex) != m_pIndices->end())
{
found = (CCTableViewCell *)m_pCellsUsed->objectWithObjectID(cellIndex);
}
return found;
}
void CCTableView::_moveCellOutOfSight(CCTableViewCell *cell)
{
m_pCellsFreed->addObject(cell);
m_pCellsUsed->removeSortedObject(cell);
m_pIndices->erase(cell->getIdx());
// [m_pIndices removeIndex:cell.idx];
cell->reset();
if (cell->getParent() == this->getContainer()) {
this->getContainer()->removeChild(cell, true);;
}
}
void CCTableView::_setIndexForCell(unsigned int index, CCTableViewCell *cell)
{
cell->setAnchorPoint(ccp(0.0f, 0.0f));
cell->setPosition(this->_offsetFromIndex(index));
cell->setIdx(index);
}
void CCTableView::scrollViewDidScroll(CCScrollView* view)
{
unsigned int startIdx = 0, endIdx = 0, idx = 0, maxIdx = 0;
CCPoint offset;
offset = ccpMult(this->getContentOffset(), -1);
maxIdx = MAX(m_pDataSource->numberOfCellsInTableView(this)-1, 0);
const CCSize cellSize = m_pDataSource->cellSizeForTable(this);
if (m_eVordering == kCCTableViewFillTopDown) {
offset.y = offset.y + m_tViewSize.height/this->getContainer()->getScaleY() - cellSize.height;
}
startIdx = this->_indexFromOffset(offset);
if (m_eVordering == kCCTableViewFillTopDown)
{
offset.y -= m_tViewSize.height/this->getContainer()->getScaleY();
}
else
{
offset.y += m_tViewSize.height/this->getContainer()->getScaleY();
}
offset.x += m_tViewSize.width/this->getContainer()->getScaleX();
endIdx = this->_indexFromOffset(offset);
#if 0 // For Testing.
CCObject* pObj;
int i = 0;
CCARRAY_FOREACH(m_pCellsUsed, pObj)
{
CCTableViewCell* pCell = (CCTableViewCell*)pObj;
CCLog("cells Used index %d, value = %d", i, pCell->getIdx());
i++;
}
CCLog("---------------------------------------");
i = 0;
CCARRAY_FOREACH(m_pCellsFreed, pObj)
{
CCTableViewCell* pCell = (CCTableViewCell*)pObj;
CCLog("cells freed index %d, value = %d", i, pCell->getIdx());
i++;
}
CCLog("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
#endif
if (m_pCellsUsed->count() > 0)
{
CCTableViewCell* cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(0);
idx = cell->getIdx();
while(idx <startIdx)
{
this->_moveCellOutOfSight(cell);
if (m_pCellsUsed->count() > 0)
{
cell = (CCTableViewCell*)m_pCellsUsed->objectAtIndex(0);
idx = cell->getIdx();
}
else
{
break;
}
}
}
if (m_pCellsUsed->count() > 0)
{
CCTableViewCell *cell = (CCTableViewCell*)m_pCellsUsed->lastObject();
idx = cell->getIdx();
while(idx <= maxIdx && idx > endIdx)
{
this->_moveCellOutOfSight(cell);
if (m_pCellsUsed->count() > 0)
{
cell = (CCTableViewCell*)m_pCellsUsed->lastObject();
idx = cell->getIdx();
}
else
{
break;
}
}
}
for (unsigned int i=startIdx; i <= endIdx; i++)
{
//if ([m_pIndices containsIndex:i])
if (m_pIndices->find(i) != m_pIndices->end())
{
continue;
}
this->updateCellAtIndex(i);
}
}
void CCTableView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
{
if (!this->isVisible()) {
return;
}
if (m_pTouches->count() == 1 && !this->isTouchMoved()) {
unsigned int index;
CCTableViewCell *cell;
CCPoint point;
point = this->getContainer()->convertTouchToNodeSpace(pTouch);
if (m_eVordering == kCCTableViewFillTopDown) {
CCSize cellSize = m_pDataSource->cellSizeForTable(this);
point.y -= cellSize.height;
}
index = this->_indexFromOffset(point);
cell = this->_cellWithIndex(index);
if (cell) {
m_pTableViewDelegate->tableCellTouched(this, cell);
}
}
CCScrollView::ccTouchEnded(pTouch, pEvent);
}
NS_CC_EXT_END

View File

@ -0,0 +1,229 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2010 Sangwoo Im
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.
****************************************************************************/
#ifndef __CCTABLEVIEW_H__
#define __CCTABLEVIEW_H__
#include "CCScrollView.h"
#include "CCTableViewCell.h"
#include <set>
NS_CC_EXT_BEGIN
class CCTableView;
class CCArrayForObjectSorting;
typedef enum {
kCCTableViewFillTopDown,
kCCTableViewFillBottomUp
} CCTableViewVerticalFillOrder;
/**
* Sole purpose of this delegate is to single touch event in this version.
*/
class CCTableViewDelegate : public CCScrollViewDelegate
{
public:
/**
* Delegate to respond touch event
*
* @param table table contains the given cell
* @param cell cell that is touched
*/
virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell) = 0;
};
/**
* Data source that governs table backend data.
*/
class CCTableViewDataSource
{
public:
/**
* cell height for a given table.
*
* @param table table to hold the instances of Class
* @return cell size
*/
virtual CCSize cellSizeForTable(CCTableView *table) = 0;
/**
* a cell instance at a given index
*
* @param idx index to search for a cell
* @return cell found at idx
*/
virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx) = 0;
/**
* Returns number of cells in a given table view.
*
* @return number of cells
*/
virtual unsigned int numberOfCellsInTableView(CCTableView *table) = 0;
};
/**
* UITableView counterpart for cocos2d for iphone.
*
* this is a very basic, minimal implementation to bring UITableView-like component into cocos2d world.
*
*/
class CCTableView : public CCScrollView, public CCScrollViewDelegate
{
public:
CCTableView();
virtual ~CCTableView();
/**
* An intialized table view object
*
* @param dataSource data source
* @param size view size
* @return table view
*/
static CCTableView* create(CCTableViewDataSource* dataSource, CCSize size);
/**
* An initialized table view object
*
* @param dataSource data source;
* @param size view size
* @param container parent object for cells
* @return table view
*/
static CCTableView* create(CCTableViewDataSource* dataSource, CCSize size, CCNode *container);
/**
* data source
*/
CCTableViewDataSource* getDataSource() { return m_pDataSource; }
void setDataSource(CCTableViewDataSource* source) { m_pDataSource = source; }
/**
* delegate
*/
CCTableViewDelegate* getDelegate() { return m_pTableViewDelegate; }
void setDelegate(CCTableViewDelegate* pDelegate) { m_pTableViewDelegate = pDelegate; }
/**
* determines how cell is ordered and filled in the view.
*/
void setVerticalFillOrder(CCTableViewVerticalFillOrder order);
CCTableViewVerticalFillOrder getVerticalFillOrder();
bool initWithViewSize(CCSize size, CCNode* container = NULL);
/**
* Updates the content of the cell at a given index.
*
* @param idx index to find a cell
*/
void updateCellAtIndex(unsigned int idx);
/**
* Inserts a new cell at a given index
*
* @param idx location to insert
*/
void insertCellAtIndex(unsigned int idx);
/**
* Removes a cell at a given index
*
* @param idx index to find a cell
*/
void removeCellAtIndex(unsigned int idx);
/**
* reloads data from data source. the view will be refreshed.
*/
void reloadData();
/**
* Dequeues a free cell if available. nil if not.
*
* @return free cell
*/
CCTableViewCell *dequeueCell();
/**
* Returns an existing cell at a given index. Returns nil if a cell is nonexistent at the moment of query.
*
* @param idx index
* @return a cell at a given index
*/
CCTableViewCell *cellAtIndex(unsigned int idx);
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view) {}
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
protected:
/**
* vertical direction of cell filling
*/
CCTableViewVerticalFillOrder m_eVordering;
/**
* index set to query the indexes of the cells used.
*/
std::set<unsigned int>* m_pIndices;
//NSMutableIndexSet *indices_;
/**
* cells that are currently in the table
*/
CCArrayForObjectSorting* m_pCellsUsed;
/**
* free list of cells
*/
CCArrayForObjectSorting* m_pCellsFreed;
/**
* weak link to the data source object
*/
CCTableViewDataSource* m_pDataSource;
/**
* weak link to the delegate object
*/
CCTableViewDelegate* m_pTableViewDelegate;
CCScrollViewDirection m_eOldDirection;
int __indexFromOffset(CCPoint offset);
unsigned int _indexFromOffset(CCPoint offset);
CCPoint __offsetFromIndex(unsigned int index);
CCPoint _offsetFromIndex(unsigned int index);
void _updateContentSize();
CCTableViewCell* _cellWithIndex(unsigned int cellIndex);
void _moveCellOutOfSight(CCTableViewCell *cell);
void _setIndexForCell(unsigned int index, CCTableViewCell *cell);
void _addCellIfNecessary(CCTableViewCell * cell);
};
NS_CC_EXT_END
#endif /* __CCTABLEVIEW_H__ */

View File

@ -0,0 +1,55 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2010 Sangwoo Im
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 "CCTableViewCell.h"
NS_CC_EXT_BEGIN
void CCTableViewCell::reset()
{
m_uIdx = CC_INVALID_INDEX;
}
void CCTableViewCell::setObjectID(unsigned int uIdx)
{
m_uIdx = uIdx;
}
unsigned int CCTableViewCell::getObjectID()
{
return m_uIdx;
}
unsigned int CCTableViewCell::getIdx()
{
return m_uIdx;
}
void CCTableViewCell::setIdx(unsigned int uIdx)
{
m_uIdx = uIdx;
}
NS_CC_EXT_END

View File

@ -0,0 +1,59 @@
/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2010 Sangwoo Im
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.
****************************************************************************/
#ifndef __CCTABLEVIEWCELL_H__
#define __CCTABLEVIEWCELL_H__
#include "base_nodes/CCNode.h"
#include "CCSorting.h"
NS_CC_EXT_BEGIN
/**
* Abstract class for SWTableView cell node
*/
class CCTableViewCell: public CCNode, public CCSortableObject
{
public:
CCTableViewCell() {}
/**
* The index used internally by SWTableView and its subclasses
*/
unsigned int getIdx();
void setIdx(unsigned int uIdx);
/**
* Cleans up any resources linked to this cell and resets <code>idx</code> property.
*/
void reset();
void setObjectID(unsigned int uIdx);
unsigned int getObjectID();
private:
unsigned int m_uIdx;
};
NS_CC_EXT_END
#endif /* __CCTABLEVIEWCELL_H__ */

View File

@ -27,6 +27,7 @@
#include "GUI/CCControlExtension/CCControlExtensions.h"
#include "GUI/CCScrollView/CCScrollView.h"
#include "GUI/CCScrollView/CCTableView.h"
#include "GUI/CCEditBox/CCEditBox.h"
#include "network/HttpRequest.h"

View File

@ -421,6 +421,30 @@
RelativePath="..\GUI\CCScrollView\CCScrollView.h"
>
</File>
<File
RelativePath="..\GUI\CCScrollView\CCSorting.cpp"
>
</File>
<File
RelativePath="..\GUI\CCScrollView\CCSorting.h"
>
</File>
<File
RelativePath="..\GUI\CCScrollView\CCTableView.cpp"
>
</File>
<File
RelativePath="..\GUI\CCScrollView\CCTableView.h"
>
</File>
<File
RelativePath="..\GUI\CCScrollView\CCTableViewCell.cpp"
>
</File>
<File
RelativePath="..\GUI\CCScrollView\CCTableViewCell.h"
>
</File>
</Filter>
</Filter>
<Filter

View File

@ -109,6 +109,9 @@
<ClCompile Include="..\GUI\CCControlExtension\CCScale9Sprite.cpp" />
<ClCompile Include="..\GUI\CCControlExtension\CCSpacer.cpp" />
<ClCompile Include="..\GUI\CCScrollView\CCScrollView.cpp" />
<ClCompile Include="..\GUI\CCScrollView\CCSorting.cpp" />
<ClCompile Include="..\GUI\CCScrollView\CCTableView.cpp" />
<ClCompile Include="..\GUI\CCScrollView\CCTableViewCell.cpp" />
<ClCompile Include="..\network\HttpClient.cpp" />
</ItemGroup>
<ItemGroup>
@ -149,6 +152,9 @@
<ClInclude Include="..\GUI\CCScrollView\CCScrollView.h" />
<ClInclude Include="..\cocos-ext.h" />
<ClInclude Include="..\ExtensionMacros.h" />
<ClInclude Include="..\GUI\CCScrollView\CCSorting.h" />
<ClInclude Include="..\GUI\CCScrollView\CCTableView.h" />
<ClInclude Include="..\GUI\CCScrollView\CCTableViewCell.h" />
<ClInclude Include="..\network\HttpClient.h" />
<ClInclude Include="..\network\HttpRequest.h" />
<ClInclude Include="..\network\HttpResponse.h" />

View File

@ -111,6 +111,15 @@
<ClCompile Include="..\network\HttpClient.cpp">
<Filter>GUI\network</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCScrollView\CCSorting.cpp">
<Filter>GUI\CCScrollView</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCScrollView\CCTableView.cpp">
<Filter>GUI\CCScrollView</Filter>
</ClCompile>
<ClCompile Include="..\GUI\CCScrollView\CCTableViewCell.cpp">
<Filter>GUI\CCScrollView</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\CCBReader\CCBFileLoader.h">
@ -229,5 +238,14 @@
<ClInclude Include="..\network\HttpResponse.h">
<Filter>GUI\network</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCScrollView\CCSorting.h">
<Filter>GUI\CCScrollView</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCScrollView\CCTableView.h">
<Filter>GUI\CCScrollView</Filter>
</ClInclude>
<ClInclude Include="..\GUI\CCScrollView\CCTableViewCell.h">
<Filter>GUI\CCScrollView</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -51,6 +51,8 @@ Classes/ExtensionsTest/CocosBuilderTest/ButtonTest/ButtonTestLayer.cpp \
Classes/ExtensionsTest/CocosBuilderTest/MenuTest/MenuTestLayer.cpp \
Classes/ExtensionsTest/NetworkTest/HttpClientTest.cpp \
Classes/ExtensionsTest/EditBoxTest/EditBoxTest.cpp \
Classes/ExtensionsTest/TableViewTest/TableViewTestScene.cpp \
Classes/ExtensionsTest/TableViewTest/CustomTableViewCell.cpp \
Classes/FontTest/FontTest.cpp \
Classes/IntervalTest/IntervalTest.cpp \
Classes/KeypadTest/KeypadTest.cpp \

View File

@ -4,6 +4,8 @@
#include "ControlExtensionTest/CCControlSceneManager.h"
#include "CocosBuilderTest/CocosBuilderTest.h"
#include "NetworkTest/HttpClientTest.h"
#include "TableViewTest/TableViewTestScene.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "EditBoxTest/EditBoxTest.h"
#endif
@ -23,6 +25,7 @@ enum
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
TEST_EDITBOX,
#endif
TEST_TABLEVIEW,
TEST_MAX_COUNT,
};
@ -33,8 +36,9 @@ static const std::string testsName[TEST_MAX_COUNT] =
"CocosBuilderTest",
"HttpClientTest",
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) || (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
"EditBoxTest"
"EditBoxTest",
#endif
"TableViewTest"
};
////////////////////////////////////////////////////////
@ -104,6 +108,11 @@ void ExtensionsMainLayer::menuCallback(CCObject* pSender)
}
break;
#endif
case TEST_TABLEVIEW:
{
runTableViewTest();
}
break;
default:
break;
}

View File

@ -0,0 +1,19 @@
#include "CustomTableViewCell.h"
USING_NS_CC;
void CustomTableViewCell::draw()
{
CCTableViewCell::draw();
// draw bounding box
// CCPoint pos = getPosition();
// CCSize size = CCSizeMake(178, 200);
// CCPoint vertices[4]={
// ccp(pos.x+1, pos.y+1),
// ccp(pos.x+size.width-1, pos.y+1),
// ccp(pos.x+size.width-1, pos.y+size.height-1),
// ccp(pos.x+1, pos.y+size.height-1),
// };
// ccDrawColor4B(0, 0, 255, 255);
// ccDrawPoly(vertices, 4, true);
}

View File

@ -0,0 +1,14 @@
#ifndef __CUSTOMTABELVIEWCELL_H__
#define __CUSTOMTABELVIEWCELL_H__
#include "cocos2d.h"
#include "cocos-ext.h"
class CustomTableViewCell : public cocos2d::extension::CCTableViewCell
{
public:
virtual void draw();
};
#endif /* __CUSTOMTABELVIEWCELL_H__ */

View File

@ -0,0 +1,99 @@
#include "TableViewTestScene.h"
#include "CustomTableViewCell.h"
#include "../ExtensionsTest.h"
USING_NS_CC;
USING_NS_CC_EXT;
void runTableViewTest()
{
CCScene *pScene = CCScene::create();
TableViewTestLayer *pLayer = TableViewTestLayer::create();
pScene->addChild(pLayer);
CCDirector::sharedDirector()->replaceScene(pScene);
}
// on "init" you need to initialize your instance
bool TableViewTestLayer::init()
{
if ( !CCLayer::init() )
{
return false;
}
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCTableView* tableView = CCTableView::create(this, CCSizeMake(250, 60));
tableView->setDirection(kCCScrollViewDirectionHorizontal);
tableView->setPosition(ccp(20,winSize.height/2-30));
tableView->setDelegate(this);
this->addChild(tableView);
tableView->reloadData();
tableView = CCTableView::create(this, CCSizeMake(60, 280));
tableView->setDirection(kCCScrollViewDirectionVertical);
tableView->setPosition(ccp(winSize.width-150,winSize.height/2-120));
tableView->setDelegate(this);
tableView->setVerticalFillOrder(kCCTableViewFillTopDown);
this->addChild(tableView);
tableView->reloadData();
// Back Menu
CCMenuItemFont *itemBack = CCMenuItemFont::create("Back", this, menu_selector(TableViewTestLayer::toExtensionsMainLayer));
itemBack->setPosition(ccp(winSize.width - 50, 25));
CCMenu *menuBack = CCMenu::create(itemBack, NULL);
menuBack->setPosition(CCPointZero);
addChild(menuBack);
return true;
}
void TableViewTestLayer::toExtensionsMainLayer(cocos2d::CCObject *sender)
{
ExtensionsTestScene *pScene = new ExtensionsTestScene();
pScene->runThisTest();
pScene->release();
}
void TableViewTestLayer::tableCellTouched(CCTableView* table, CCTableViewCell* cell)
{
CCLOG("cell touched at index: %i", cell->getIdx());
}
CCSize TableViewTestLayer::cellSizeForTable(CCTableView *table)
{
return CCSizeMake(60, 60);
}
CCTableViewCell* TableViewTestLayer::tableCellAtIndex(CCTableView *table, unsigned int idx)
{
CCString *string = CCString::createWithFormat("%d", idx);
CCTableViewCell *cell = table->dequeueCell();
if (!cell) {
cell = new CustomTableViewCell();
cell->autorelease();
CCSprite *sprite = CCSprite::create("Images/Icon.png");
sprite->setAnchorPoint(CCPointZero);
sprite->setPosition(ccp(0, 0));
cell->addChild(sprite);
CCLabelTTF *label = CCLabelTTF::create(string->getCString(), "Helvetica", 20.0);
label->setPosition(CCPointZero);
label->setAnchorPoint(CCPointZero);
label->setTag(123);
cell->addChild(label);
}
else
{
CCLabelTTF *label = (CCLabelTTF*)cell->getChildByTag(123);
label->setString(string->getCString());
}
return cell;
}
unsigned int TableViewTestLayer::numberOfCellsInTableView(CCTableView *table)
{
return 20;
}

View File

@ -0,0 +1,26 @@
#ifndef __TABLEVIEWTESTSCENE_H__
#define __TABLEVIEWTESTSCENE_H__
#include "cocos2d.h"
#include "cocos-ext.h"
void runTableViewTest();
class TableViewTestLayer : public cocos2d::CCLayer, public cocos2d::extension::CCTableViewDataSource, public cocos2d::extension::CCTableViewDelegate
{
public:
virtual bool init();
void toExtensionsMainLayer(cocos2d::CCObject *sender);
CREATE_FUNC(TableViewTestLayer);
virtual void scrollViewDidScroll(cocos2d::extension::CCScrollView* view) {};
virtual void scrollViewDidZoom(cocos2d::extension::CCScrollView* view) {}
virtual void tableCellTouched(cocos2d::extension::CCTableView* table, cocos2d::extension::CCTableViewCell* cell);
virtual cocos2d::CCSize cellSizeForTable(cocos2d::extension::CCTableView *table);
virtual cocos2d::extension::CCTableViewCell* tableCellAtIndex(cocos2d::extension::CCTableView *table, unsigned int idx);
virtual unsigned int numberOfCellsInTableView(cocos2d::extension::CCTableView *table);
};
#endif // __TABLEVIEWTESTSCENE_H__

View File

@ -1 +1 @@
13fb5118be504c0cb162fe003715353d23316944
48422011360e4d8706df417f7061b830a3b9613a

View File

@ -77,6 +77,8 @@ OBJECTS = ../Classes/AccelerometerTest/AccelerometerTest.o \
../Classes/ExtensionsTest/ControlExtensionTest/CCControlSceneManager.o \
../Classes/ExtensionsTest/ControlExtensionTest/CCControlSliderTest/CCControlSliderTest.o \
../Classes/ExtensionsTest/ControlExtensionTest/CCControlSwitchTest/CCControlSwitchTest.o \
../Classes/ExtensionsTest/TableViewTest/TableViewTestScene.o \
../Classes/ExtensionsTest/TableViewTest/CustomTableViewCell.o \
../Classes/ExtensionsTest/ExtensionsTest.o \
../Classes/ExtensionsTest/NotificationCenterTest/NotificationCenterTest.o \
../Classes/ExtensionsTest/NetworkTest/HttpClientTest.o \

View File

@ -1 +1 @@
f5750ec007bd00599c51249ab60343d337af43c9
05fb7e1549cf656c61a502a07712b042ff7eec9b

View File

@ -1023,6 +1023,26 @@
>
</File>
</Filter>
<Filter
Name="TableViewTest"
>
<File
RelativePath="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.cpp"
>
</File>
<File
RelativePath="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.h"
>
</File>
<File
RelativePath="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.cpp"
>
</File>
<File
RelativePath="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.h"
>
</File>
</Filter>
</Filter>
<Filter
Name="NodeTest"

View File

@ -108,6 +108,8 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\Classes\ExtensionsTest\NetworkTest\HttpClientTest.cpp" />
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.cpp" />
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="..\Classes\AppDelegate.cpp" />
<ClCompile Include="..\Classes\controller.cpp" />
@ -190,6 +192,8 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\Classes\ExtensionsTest\NetworkTest\HttpClientTest.h" />
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.h" />
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.h" />
<ClInclude Include="main.h" />
<ClInclude Include="..\Classes\AppDelegate.h" />
<ClInclude Include="..\Classes\controller.h" />

View File

@ -190,6 +190,9 @@
<Filter Include="Classes\ExtensionsTest\NetworkTest">
<UniqueIdentifier>{28e8af6a-0e73-4069-bdf4-735138ed886d}</UniqueIdentifier>
</Filter>
<Filter Include="Classes\ExtensionsTest\TableViewTest">
<UniqueIdentifier>{2603ae57-b062-4281-9daf-c925634eaeb4}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
@ -432,6 +435,12 @@
<ClCompile Include="..\Classes\ExtensionsTest\NetworkTest\HttpClientTest.cpp">
<Filter>Classes\ExtensionsTest\NetworkTest</Filter>
</ClCompile>
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.cpp">
<Filter>Classes\ExtensionsTest\TableViewTest</Filter>
</ClCompile>
<ClCompile Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.cpp">
<Filter>Classes\ExtensionsTest\TableViewTest</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="main.h">
@ -848,5 +857,11 @@
<ClInclude Include="..\Classes\ExtensionsTest\NetworkTest\HttpClientTest.h">
<Filter>Classes\ExtensionsTest\NetworkTest</Filter>
</ClInclude>
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\CustomTableViewCell.h">
<Filter>Classes\ExtensionsTest\TableViewTest</Filter>
</ClInclude>
<ClInclude Include="..\Classes\ExtensionsTest\TableViewTest\TableViewTestScene.h">
<Filter>Classes\ExtensionsTest\TableViewTest</Filter>
</ClInclude>
</ItemGroup>
</Project>