From d9cc8fa34678c5a0fc63683a61ba10f652ba02bc Mon Sep 17 00:00:00 2001 From: James Chen Date: Sun, 9 Sep 2012 22:34:32 +0800 Subject: [PATCH] issue #1458: Adding CCTableView support. Updated win32 project configuration. --- extensions/CCBReader/CCNodeLoader.cpp | 12 - extensions/CCBReader/CCNodeLoader.h | 12 + extensions/CCBReader/CCScrollViewLoader.cpp | 9 + extensions/CCBReader/CCScrollViewLoader.h | 2 +- extensions/GUI/CCScrollView/CCScrollView.cpp | 26 +- extensions/GUI/CCScrollView/CCScrollView.h | 3 +- extensions/GUI/CCScrollView/CCSorting.cpp | 175 +++++++ extensions/GUI/CCScrollView/CCSorting.h | 114 ++++ extensions/GUI/CCScrollView/CCTableView.cpp | 485 ++++++++++++++++++ extensions/GUI/CCScrollView/CCTableView.h | 231 +++++++++ .../GUI/CCScrollView/CCTableViewCell.cpp | 59 +++ extensions/GUI/CCScrollView/CCTableViewCell.h | 63 +++ extensions/cocos-ext.h | 1 + extensions/proj.win32/libExtensions.vcproj | 24 + .../Classes/ExtensionsTest/ExtensionsTest.cpp | 11 +- .../TableViewTest/CustomTableViewCell.cpp | 19 + .../TableViewTest/CustomTableViewCell.h | 14 + .../TableViewTest/TableViewTestScene.cpp | 99 ++++ .../TableViewTest/TableViewTestScene.h | 26 + samples/TestCpp/proj.win32/TestCpp.vcproj | 20 + 20 files changed, 1378 insertions(+), 27 deletions(-) create mode 100644 extensions/GUI/CCScrollView/CCSorting.cpp create mode 100644 extensions/GUI/CCScrollView/CCSorting.h create mode 100644 extensions/GUI/CCScrollView/CCTableView.cpp create mode 100644 extensions/GUI/CCScrollView/CCTableView.h create mode 100644 extensions/GUI/CCScrollView/CCTableViewCell.cpp create mode 100644 extensions/GUI/CCScrollView/CCTableViewCell.h create mode 100644 samples/TestCpp/Classes/ExtensionsTest/TableViewTest/CustomTableViewCell.cpp create mode 100644 samples/TestCpp/Classes/ExtensionsTest/TableViewTest/CustomTableViewCell.h create mode 100644 samples/TestCpp/Classes/ExtensionsTest/TableViewTest/TableViewTestScene.cpp create mode 100644 samples/TestCpp/Classes/ExtensionsTest/TableViewTest/TableViewTestScene.h diff --git a/extensions/CCBReader/CCNodeLoader.cpp b/extensions/CCBReader/CCNodeLoader.cpp index 9a32f421e8..01c4b7d0f8 100644 --- a/extensions/CCBReader/CCNodeLoader.cpp +++ b/extensions/CCBReader/CCNodeLoader.cpp @@ -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 diff --git a/extensions/CCBReader/CCNodeLoader.h b/extensions/CCBReader/CCNodeLoader.h index e74e7c012a..53d06bc1ea 100644 --- a/extensions/CCBReader/CCNodeLoader.h +++ b/extensions/CCBReader/CCNodeLoader.h @@ -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(); \ } diff --git a/extensions/CCBReader/CCScrollViewLoader.cpp b/extensions/CCBReader/CCScrollViewLoader.cpp index ed7ddd6448..b5c4a8830e 100644 --- a/extensions/CCBReader/CCScrollViewLoader.cpp +++ b/extensions/CCBReader/CCScrollViewLoader.cpp @@ -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)->setViewSize(CCSizeMake(240, 320)); } else { CCNodeLoader::onHandlePropTypeCCBFile(pNode, pParent, pPropertyName, pCCBFileNode, pCCBReader); } diff --git a/extensions/CCBReader/CCScrollViewLoader.h b/extensions/CCBReader/CCScrollViewLoader.h index b415e656b6..57baff99c3 100644 --- a/extensions/CCBReader/CCScrollViewLoader.h +++ b/extensions/CCBReader/CCScrollViewLoader.h @@ -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); diff --git a/extensions/GUI/CCScrollView/CCScrollView.cpp b/extensions/GUI/CCScrollView/CCScrollView.cpp index 992408e495..45f1c0bef4 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.cpp +++ b/extensions/GUI/CCScrollView/CCScrollView.cpp @@ -118,6 +118,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); @@ -309,17 +311,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); } @@ -456,7 +447,18 @@ const CCSize & CCScrollView::getContentSize() void CCScrollView::setContentSize(const CCSize & size) { - this->setViewSize(size); + // this->setViewSize(size); + if (this->getContainer() != NULL) + { + this->getContainer()->setContentSize(size); + + 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 diff --git a/extensions/GUI/CCScrollView/CCScrollView.h b/extensions/GUI/CCScrollView/CCScrollView.h index 1537245988..f5e98d5ac7 100644 --- a/extensions/GUI/CCScrollView/CCScrollView.h +++ b/extensions/GUI/CCScrollView/CCScrollView.h @@ -42,6 +42,7 @@ NS_CC_EXT_BEGIN */ typedef enum { + CCScrollViewDirectionNone = -1, CCScrollViewDirectionHorizontal = 0, CCScrollViewDirectionVertical, CCScrollViewDirectionBoth @@ -195,7 +196,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; } diff --git a/extensions/GUI/CCScrollView/CCSorting.cpp b/extensions/GUI/CCScrollView/CCSorting.cpp new file mode 100644 index 0000000000..5b795d73a1 --- /dev/null +++ b/extensions/GUI/CCScrollView/CCSorting.cpp @@ -0,0 +1,175 @@ +// +// NSMutableArraySorting.m +// SWGameLib +// +// Copyright (c) 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 10/10/09. +// Copyright 2009 Sangwoo Im. All rights reserved. +// + +#include "CCSorting.h" +//#include "SWDebug.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(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(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(this->objectAtIndex(idx)); + CCObject* pObj = dynamic_cast(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(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(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 diff --git a/extensions/GUI/CCScrollView/CCSorting.h b/extensions/GUI/CCScrollView/CCSorting.h new file mode 100644 index 0000000000..5fa0d3a614 --- /dev/null +++ b/extensions/GUI/CCScrollView/CCSorting.h @@ -0,0 +1,114 @@ +// +// NSMutableArraySorting.h +// SWGameLib +// +// Copyright (c) 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 10/10/09. +// Copyright 2009 Sangwoo Im. All rights reserved. +// + +#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__ */ + diff --git a/extensions/GUI/CCScrollView/CCTableView.cpp b/extensions/GUI/CCScrollView/CCTableView.cpp new file mode 100644 index 0000000000..f520511dd0 --- /dev/null +++ b/extensions/GUI/CCScrollView/CCTableView.cpp @@ -0,0 +1,485 @@ +// +// SWTableView.m +// SWGameLib +// +// Copyright (c) 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. +// +#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::viewWithDataSource(CCTableViewDataSource* dataSource, CCSize size) +{ + return CCTableView::viewWithDataSource(dataSource, size, NULL); +} + +CCTableView* CCTableView::viewWithDataSource(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)) + { + cellsUsed_ = new CCArrayForObjectSorting(); + cellsFreed_ = new CCArrayForObjectSorting(); + indices_ = new std::set(); + tDelegate_ = NULL; + vordering_ = CCTableViewFillBottomUp; + this->setDirection(CCScrollViewDirectionVertical); + + CCScrollView::setDelegate(this); + return true; + } + return false; +} + +CCTableView::CCTableView() +: indices_(NULL) +, cellsUsed_(NULL) +, cellsFreed_(NULL) +, dataSource_(NULL) +, tDelegate_(NULL) +, m_eOldDirection(CCScrollViewDirectionNone) +{ + +} + +CCTableView::~CCTableView() +{ + CC_SAFE_DELETE(indices_); + CC_SAFE_RELEASE(cellsUsed_); + CC_SAFE_RELEASE(cellsFreed_); +} + +void CCTableView::setVerticalFillOrder(CCTableViewVerticalFillOrder fillOrder) +{ + if (vordering_ != fillOrder) { + vordering_ = fillOrder; + if (cellsUsed_->count() > 0) { + this->reloadData(); + } + } +} + +CCTableViewVerticalFillOrder CCTableView::getVerticalFillOrder() +{ + return vordering_; +} + +void CCTableView::reloadData() +{ + CCObject* pObj = NULL; + CCARRAY_FOREACH(cellsUsed_, pObj) + { + CCTableViewCell* cell = (CCTableViewCell*)pObj; + cellsFreed_->addObject(cell); + cell->reset(); + if (cell->getParent() == this->getContainer()) + { + this->getContainer()->removeChild(cell, true); + } + } + + indices_->clear(); + cellsUsed_->release(); + cellsUsed_ = new CCArrayForObjectSorting(); + + this->_updateContentSize(); + if (dataSource_->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 > dataSource_->numberOfCellsInTableView(this)-1) + { + return; + } + + CCTableViewCell *cell; + + cell = this->_cellWithIndex(idx); + if (cell) { + this->_moveCellOutOfSight(cell); + } + cell = dataSource_->tableCellAtIndex(this, idx); + this->_setIndexForCell(idx, cell); + this->_addCellIfNecessary(cell); +} + +void CCTableView::insertCellAtIndex(unsigned int idx) +{ + if (idx == CC_INVALID_INDEX || idx > dataSource_->numberOfCellsInTableView(this)-1) { + return; + } + CCTableViewCell *cell; + int newIdx; + + cell = (CCTableViewCell*)cellsUsed_->objectWithObjectID(idx); + if (cell) + { + newIdx = cellsUsed_->indexOfSortedObject(cell); + for (unsigned int i=newIdx; icount(); i++) + { + cell = (CCTableViewCell*)cellsUsed_->objectAtIndex(i); + this->_setIndexForCell(cell->getIdx()+1, cell); + } + } + + // [indices_ shiftIndexesStartingAtIndex:idx by:1]; + + //insert a new cell + cell = dataSource_->tableCellAtIndex(this, idx); + this->_setIndexForCell(idx, cell); + this->_addCellIfNecessary(cell); + + this->_updateContentSize(); +} + +void CCTableView::removeCellAtIndex(unsigned int idx) +{ + if (idx == CC_INVALID_INDEX || idx > dataSource_->numberOfCellsInTableView(this)-1) { + return; + } + + CCTableViewCell *cell; + unsigned int newIdx; + + cell = this->_cellWithIndex(idx); + if (!cell) { + return; + } + + newIdx = cellsUsed_->indexOfSortedObject(cell); + + //remove first + this->_moveCellOutOfSight(cell); + + indices_->erase(idx); +// [indices_ shiftIndexesStartingAtIndex:idx+1 by:-1]; + for (unsigned int i=cellsUsed_->count()-1; i > newIdx; i--) { + cell = (CCTableViewCell*)cellsUsed_->objectAtIndex(i); + this->_setIndexForCell(cell->getIdx()-1, cell); + } +} + +CCTableViewCell *CCTableView::dequeueCell() +{ + CCTableViewCell *cell; + + if (cellsFreed_->count() == 0) { + cell = NULL; + } else { + cell = (CCTableViewCell*)cellsFreed_->objectAtIndex(0); + cell->retain(); + cellsFreed_->removeObjectAtIndex(0); + cell->autorelease(); + } + return cell; +} + +void CCTableView::_addCellIfNecessary(CCTableViewCell * cell) +{ + if (cell->getParent() != this->getContainer()) + { + this->getContainer()->addChild(cell); + } + cellsUsed_->insertSortedObject(cell); + indices_->insert(cell->getIdx()); + // [indices_ addIndex:cell.idx]; +} + +void CCTableView::_updateContentSize() +{ + CCSize size, cellSize; + unsigned int cellCount; + + cellSize = dataSource_->cellSizeForTable(this); + cellCount = dataSource_->numberOfCellsInTableView(this); + + switch (this->getDirection()) + { + case CCScrollViewDirectionHorizontal: + 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 == CCScrollViewDirectionHorizontal) + { + 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 = dataSource_->cellSizeForTable(this); + if (vordering_ == CCTableViewFillTopDown) { + offset.y = this->getContainer()->getContentSize().height - offset.y - cellSize.height; + } + return offset; +} + +CCPoint CCTableView::__offsetFromIndex(unsigned int index) +{ + CCPoint offset; + CCSize cellSize; + + cellSize = dataSource_->cellSizeForTable(this); + switch (this->getDirection()) { + case CCScrollViewDirectionHorizontal: + 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 = dataSource_->numberOfCellsInTableView(this)-1; + + const CCSize cellSize = dataSource_->cellSizeForTable(this); + if (vordering_ == CCTableViewFillTopDown) { + 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 = dataSource_->cellSizeForTable(this); + + switch (this->getDirection()) { + case CCScrollViewDirectionHorizontal: + 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 ([indices_ containsIndex:cellIndex]) + if (indices_->find(cellIndex) != indices_->end()) + { + found = (CCTableViewCell *)cellsUsed_->objectWithObjectID(cellIndex); + } + + return found; +} + +void CCTableView::_moveCellOutOfSight(CCTableViewCell *cell) +{ + cellsFreed_->addObject(cell); + cellsUsed_->removeSortedObject(cell); + indices_->erase(cell->getIdx()); + // [indices_ 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) +{ + CCLOG("container pos x = %f, y = %f", getContainer()->getPosition().x, getContainer()->getPosition().y); + unsigned int startIdx = 0, endIdx = 0, idx = 0, maxIdx = 0; + CCPoint offset; + + offset = ccpMult(this->getContentOffset(), -1); + maxIdx = MAX(dataSource_->numberOfCellsInTableView(this)-1, 0); + + const CCSize cellSize = dataSource_->cellSizeForTable(this); + + if (vordering_ == CCTableViewFillTopDown) { + offset.y = offset.y + m_tViewSize.height/this->getContainer()->getScaleY() - cellSize.height; + } + startIdx = this->_indexFromOffset(offset); + + if (vordering_ == CCTableViewFillTopDown) + { + 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); + + CCObject* pObj; + int i = 0; + CCARRAY_FOREACH(cellsUsed_, pObj) + { + CCTableViewCell* pCell = (CCTableViewCell*)pObj; + CCLog("cells Used index %d, value = %d", i, pCell->getIdx()); + i++; + } + CCLog("---------------------------------------"); + i = 0; + CCARRAY_FOREACH(cellsFreed_, pObj) + { + CCTableViewCell* pCell = (CCTableViewCell*)pObj; + CCLog("cells freed index %d, value = %d", i, pCell->getIdx()); + i++; + } + CCLog("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); + if (cellsUsed_->count() > 0) + { + CCTableViewCell* cell = (CCTableViewCell*)cellsUsed_->objectAtIndex(0); + + idx = cell->getIdx(); + while(idx _moveCellOutOfSight(cell); + if (cellsUsed_->count() > 0) { + cell = (CCTableViewCell*)cellsUsed_->objectAtIndex(0); + idx = cell->getIdx(); + } else { + break; + } + CCLog("idx 0 = %d", idx); + } + } + if (cellsUsed_->count() > 0) + { + CCTableViewCell *cell = (CCTableViewCell*)cellsUsed_->lastObject(); + idx = cell->getIdx(); + + while(idx <= maxIdx && idx > endIdx) + { + this->_moveCellOutOfSight(cell); + if (cellsUsed_->count() > 0) + { + cell = (CCTableViewCell*)cellsUsed_->lastObject(); + idx = cell->getIdx(); + + } else { + break; + } + CCLog("idx 1 = %d, maxIdx = %d, endIdx = %d", idx, maxIdx, endIdx); + } + } + + for (unsigned int i=startIdx; i <= endIdx; i++) + { + //if ([indices_ containsIndex:i]) + if (indices_->find(i) != indices_->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 (vordering_ == CCTableViewFillTopDown) { + CCSize cellSize = dataSource_->cellSizeForTable(this); + point.y -= cellSize.height; + } + index = this->_indexFromOffset(point); + cell = this->_cellWithIndex(index); + + if (cell) { + tDelegate_->table_cellTouched(this, cell); + } + } + CCScrollView::ccTouchEnded(pTouch, pEvent); +} + +NS_CC_EXT_END diff --git a/extensions/GUI/CCScrollView/CCTableView.h b/extensions/GUI/CCScrollView/CCTableView.h new file mode 100644 index 0000000000..5b19efb655 --- /dev/null +++ b/extensions/GUI/CCScrollView/CCTableView.h @@ -0,0 +1,231 @@ +// +// SWTableView.h +// SWGameLib +// +// Copyright (c) 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. +// + +#ifndef __CCTABLEVIEW_H__ +#define __CCTABLEVIEW_H__ + +#include "CCScrollView.h" +#include "CCTableViewCell.h" + +#include + +NS_CC_EXT_BEGIN + +class CCTableView; +class CCArrayForObjectSorting; + +typedef enum { + CCTableViewFillTopDown, + CCTableViewFillBottomUp +} 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 table_cellTouched(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(); + /** + * vertical direction of cell filling + */ + CCTableViewVerticalFillOrder vordering_; + + /** + * data source + */ + CCTableViewDataSource* getDataSource() { return dataSource_; } + void setDataSource(CCTableViewDataSource* source) { dataSource_ = source; } + /** + * delegate + */ + CCTableViewDelegate* getDelegate() { return tDelegate_; } + void setDelegate(CCTableViewDelegate* pDelegate) { tDelegate_ = pDelegate; } + + /** + * determines how cell is ordered and filled in the view. + */ + void setVerticalFillOrder(CCTableViewVerticalFillOrder order); + CCTableViewVerticalFillOrder getVerticalFillOrder(); + + /** + * An intialized table view object + * + * @param dataSource data source + * @param size view size + * @return table view + */ + static CCTableView* viewWithDataSource(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* viewWithDataSource(CCTableViewDataSource* dataSource, CCSize size, CCNode *container); + + 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: + /** + * index set to query the indexes of the cells used. + */ + std::set* indices_; + //NSMutableIndexSet *indices_; + /** + * cells that are currently in the table + */ + CCArrayForObjectSorting *cellsUsed_; + /** + * free list of cells + */ + CCArrayForObjectSorting *cellsFreed_; + /** + * weak link to the data source object + */ + CCTableViewDataSource* dataSource_; + /** + * weak link to the delegate object + */ + CCTableViewDelegate* tDelegate_; + + 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__ */ + diff --git a/extensions/GUI/CCScrollView/CCTableViewCell.cpp b/extensions/GUI/CCScrollView/CCTableViewCell.cpp new file mode 100644 index 0000000000..304e7e1c73 --- /dev/null +++ b/extensions/GUI/CCScrollView/CCTableViewCell.cpp @@ -0,0 +1,59 @@ +// +// SWTableViewCell.m +// SWGameLib +// +// Copyright (c) 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. +// + +#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 diff --git a/extensions/GUI/CCScrollView/CCTableViewCell.h b/extensions/GUI/CCScrollView/CCTableViewCell.h new file mode 100644 index 0000000000..b095a4da27 --- /dev/null +++ b/extensions/GUI/CCScrollView/CCTableViewCell.h @@ -0,0 +1,63 @@ +// +// SWTableViewCell.h +// SWGameLib +// +// Copyright (c) 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. +// + +#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 idx property. + */ + void reset(); + + void setObjectID(unsigned int uIdx); + unsigned int getObjectID(); +private: + unsigned int m_uIdx; +}; + +NS_CC_EXT_END + +#endif /* __CCTABLEVIEWCELL_H__ */ diff --git a/extensions/cocos-ext.h b/extensions/cocos-ext.h index ac03fefe0b..8842b1ceeb 100644 --- a/extensions/cocos-ext.h +++ b/extensions/cocos-ext.h @@ -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" diff --git a/extensions/proj.win32/libExtensions.vcproj b/extensions/proj.win32/libExtensions.vcproj index 81df3bbfce..36ad9bc313 100644 --- a/extensions/proj.win32/libExtensions.vcproj +++ b/extensions/proj.win32/libExtensions.vcproj @@ -421,6 +421,30 @@ RelativePath="..\GUI\CCScrollView\CCScrollView.h" > + + + + + + + + + + + + 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::viewWithDataSource(this, CCSizeMake(250, 60)); + tableView->setDirection(CCScrollViewDirectionHorizontal); + tableView->setPosition(ccp(20,winSize.height/2-30)); + tableView->setDelegate(this); + this->addChild(tableView); + tableView->reloadData(); + + tableView = CCTableView::viewWithDataSource(this, CCSizeMake(60, 280)); + tableView->setDirection(CCScrollViewDirectionVertical); + tableView->setPosition(ccp(winSize.width-150,winSize.height/2-120)); + tableView->setDelegate(this); + tableView->setVerticalFillOrder(CCTableViewFillTopDown); + 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::table_cellTouched(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; +} diff --git a/samples/TestCpp/Classes/ExtensionsTest/TableViewTest/TableViewTestScene.h b/samples/TestCpp/Classes/ExtensionsTest/TableViewTest/TableViewTestScene.h new file mode 100644 index 0000000000..3573f75744 --- /dev/null +++ b/samples/TestCpp/Classes/ExtensionsTest/TableViewTest/TableViewTestScene.h @@ -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 table_cellTouched(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__ diff --git a/samples/TestCpp/proj.win32/TestCpp.vcproj b/samples/TestCpp/proj.win32/TestCpp.vcproj index 0ac12a7742..f664a59cc3 100644 --- a/samples/TestCpp/proj.win32/TestCpp.vcproj +++ b/samples/TestCpp/proj.win32/TestCpp.vcproj @@ -1023,6 +1023,26 @@ > + + + + + + + + + +