2012-09-10 14:13:55 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2010 Sangwoo Im
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-10 14:13:55 +08:00
|
|
|
http://www.cocos2d-x.org
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-10 14:13:55 +08:00
|
|
|
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:
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-10 14:13:55 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-10 14:13:55 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
#include "cocos2d.h"
|
|
|
|
#include "CCTableView.h"
|
|
|
|
#include "CCTableViewCell.h"
|
|
|
|
#include "menu_nodes/CCMenu.h"
|
|
|
|
#include "CCSorting.h"
|
|
|
|
#include "layers_scenes_transitions_nodes/CCLayer.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableView* TableView::create(TableViewDataSource* dataSource, Size size)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
return TableView::create(dataSource, size, NULL);
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableView* TableView::create(TableViewDataSource* dataSource, Size size, Node *container)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
TableView *table = new TableView();
|
2012-09-09 22:34:32 +08:00
|
|
|
table->initWithViewSize(size, container);
|
|
|
|
table->autorelease();
|
|
|
|
table->setDataSource(dataSource);
|
2013-04-04 22:57:43 +08:00
|
|
|
table->_updateCellPositions();
|
2012-09-09 22:34:32 +08:00
|
|
|
table->_updateContentSize();
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool TableView::initWithViewSize(Size size, Node* container/* = NULL*/)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
if (ScrollView::initWithViewSize(size,container))
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
_cellsUsed = new ArrayForObjectSorting();
|
2013-08-22 10:15:47 +08:00
|
|
|
_cellsUsed->init();
|
2013-06-20 14:15:53 +08:00
|
|
|
_cellsFreed = new ArrayForObjectSorting();
|
2013-08-22 10:15:47 +08:00
|
|
|
_cellsFreed->init();
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices = new std::set<unsigned int>();
|
2013-07-26 22:55:41 +08:00
|
|
|
_vordering = VerticalFillOrder::BOTTOM_UP;
|
|
|
|
this->setDirection(Direction::VERTICAL);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView::setDelegate(this);
|
2012-09-09 22:34:32 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableView::TableView()
|
2013-07-26 22:55:41 +08:00
|
|
|
: _touchedCell(nullptr)
|
|
|
|
, _indices(nullptr)
|
|
|
|
, _cellsUsed(nullptr)
|
|
|
|
, _cellsFreed(nullptr)
|
|
|
|
, _dataSource(nullptr)
|
|
|
|
, _tableViewDelegate(nullptr)
|
|
|
|
, _oldDirection(Direction::NONE)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableView::~TableView()
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
CC_SAFE_DELETE(_indices);
|
|
|
|
CC_SAFE_RELEASE(_cellsUsed);
|
|
|
|
CC_SAFE_RELEASE(_cellsFreed);
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
2013-07-26 22:55:41 +08:00
|
|
|
void TableView::setVerticalFillOrder(VerticalFillOrder fillOrder)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_vordering != fillOrder) {
|
|
|
|
_vordering = fillOrder;
|
|
|
|
if (_cellsUsed->count() > 0) {
|
2012-09-09 22:34:32 +08:00
|
|
|
this->reloadData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 22:55:41 +08:00
|
|
|
TableView::VerticalFillOrder TableView::getVerticalFillOrder()
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _vordering;
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::reloadData()
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-07-26 22:55:41 +08:00
|
|
|
_oldDirection = Direction::NONE;
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* pObj = NULL;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_cellsUsed, pObj)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
TableViewCell* cell = static_cast<TableViewCell*>(pObj);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_tableViewDelegate != NULL) {
|
|
|
|
_tableViewDelegate->tableCellWillRecycle(this, cell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_cellsFreed->addObject(cell);
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->reset();
|
|
|
|
if (cell->getParent() == this->getContainer())
|
|
|
|
{
|
|
|
|
this->getContainer()->removeChild(cell, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices->clear();
|
|
|
|
_cellsUsed->release();
|
2013-06-20 14:15:53 +08:00
|
|
|
_cellsUsed = new ArrayForObjectSorting();
|
2013-08-22 10:15:47 +08:00
|
|
|
_cellsUsed->init();
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
this->_updateCellPositions();
|
2012-09-09 22:34:32 +08:00
|
|
|
this->_updateContentSize();
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_dataSource->numberOfCellsInTableView(this) > 0)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
this->scrollViewDidScroll(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell *TableView::cellAtIndex(unsigned int idx)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell *found = NULL;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_indices->find(idx) != _indices->end())
|
2013-02-21 10:06:22 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
found = (TableViewCell *)_cellsUsed->objectWithObjectID(idx);
|
2013-02-21 10:06:22 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-02-21 10:06:22 +08:00
|
|
|
return found;
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::updateCellAtIndex(unsigned int idx)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2012-10-16 11:31:04 +08:00
|
|
|
if (idx == CC_INVALID_INDEX)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int uCountOfItems = _dataSource->numberOfCellsInTableView(this);
|
2012-10-16 11:31:04 +08:00
|
|
|
if (0 == uCountOfItems || idx > uCountOfItems-1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell* cell = this->cellAtIndex(idx);
|
2012-10-16 11:31:04 +08:00
|
|
|
if (cell)
|
|
|
|
{
|
2012-09-09 22:34:32 +08:00
|
|
|
this->_moveCellOutOfSight(cell);
|
2013-04-04 22:57:43 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
cell = _dataSource->tableCellAtIndex(this, idx);
|
2012-09-09 22:34:32 +08:00
|
|
|
this->_setIndexForCell(idx, cell);
|
|
|
|
this->_addCellIfNecessary(cell);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::insertCellAtIndex(unsigned int idx)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2012-10-16 11:31:04 +08:00
|
|
|
if (idx == CC_INVALID_INDEX)
|
|
|
|
{
|
2012-09-09 22:34:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-10-16 11:31:04 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int uCountOfItems = _dataSource->numberOfCellsInTableView(this);
|
2012-10-16 11:31:04 +08:00
|
|
|
if (0 == uCountOfItems || idx > uCountOfItems-1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell* cell = NULL;
|
2012-10-16 11:31:04 +08:00
|
|
|
int newIdx = 0;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
cell = (TableViewCell*)_cellsUsed->objectWithObjectID(idx);
|
2013-04-10 17:13:19 +08:00
|
|
|
if (cell)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
newIdx = _cellsUsed->indexOfSortedObject(cell);
|
|
|
|
for (unsigned int i=newIdx; i<_cellsUsed->count(); i++)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
cell = (TableViewCell*)_cellsUsed->objectAtIndex(i);
|
2012-09-09 22:34:32 +08:00
|
|
|
this->_setIndexForCell(cell->getIdx()+1, cell);
|
|
|
|
}
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// [_indices shiftIndexesStartingAtIndex:idx by:1];
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
//insert a new cell
|
2013-06-15 14:03:30 +08:00
|
|
|
cell = _dataSource->tableCellAtIndex(this, idx);
|
2012-09-09 22:34:32 +08:00
|
|
|
this->_setIndexForCell(idx, cell);
|
|
|
|
this->_addCellIfNecessary(cell);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
this->_updateCellPositions();
|
2012-09-09 22:34:32 +08:00
|
|
|
this->_updateContentSize();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::removeCellAtIndex(unsigned int idx)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2012-10-16 11:31:04 +08:00
|
|
|
if (idx == CC_INVALID_INDEX)
|
|
|
|
{
|
2012-09-09 22:34:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int uCountOfItems = _dataSource->numberOfCellsInTableView(this);
|
2012-10-16 11:31:04 +08:00
|
|
|
if (0 == uCountOfItems || idx > uCountOfItems-1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int newIdx = 0;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell* cell = this->cellAtIndex(idx);
|
2013-04-04 22:57:43 +08:00
|
|
|
if (!cell)
|
|
|
|
{
|
2012-09-09 22:34:32 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
newIdx = _cellsUsed->indexOfSortedObject(cell);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
//remove first
|
|
|
|
this->_moveCellOutOfSight(cell);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices->erase(idx);
|
2013-04-04 22:57:43 +08:00
|
|
|
this->_updateCellPositions();
|
2013-06-15 14:03:30 +08:00
|
|
|
// [_indices shiftIndexesStartingAtIndex:idx+1 by:-1];
|
|
|
|
for (unsigned int i=_cellsUsed->count()-1; i > newIdx; i--)
|
2013-04-04 22:57:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
cell = (TableViewCell*)_cellsUsed->objectAtIndex(i);
|
2012-09-09 22:34:32 +08:00
|
|
|
this->_setIndexForCell(cell->getIdx()-1, cell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell *TableView::dequeueCell()
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell *cell;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_cellsFreed->count() == 0) {
|
2012-09-09 22:34:32 +08:00
|
|
|
cell = NULL;
|
|
|
|
} else {
|
2013-06-20 14:15:53 +08:00
|
|
|
cell = (TableViewCell*)_cellsFreed->objectAtIndex(0);
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->retain();
|
2013-06-15 14:03:30 +08:00
|
|
|
_cellsFreed->removeObjectAtIndex(0);
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->autorelease();
|
|
|
|
}
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::_addCellIfNecessary(TableViewCell * cell)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
if (cell->getParent() != this->getContainer())
|
|
|
|
{
|
|
|
|
this->getContainer()->addChild(cell);
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_cellsUsed->insertSortedObject(cell);
|
|
|
|
_indices->insert(cell->getIdx());
|
|
|
|
// [_indices addIndex:cell.idx];
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::_updateContentSize()
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-07-12 14:47:36 +08:00
|
|
|
Size size = Size::ZERO;
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int cellsCount = _dataSource->numberOfCellsInTableView(this);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
if (cellsCount > 0)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
float maxPosition = _vCellsPositions[cellsCount];
|
2013-04-04 22:57:43 +08:00
|
|
|
|
|
|
|
switch (this->getDirection())
|
|
|
|
{
|
2013-07-26 22:55:41 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2013-07-12 14:30:26 +08:00
|
|
|
size = Size(maxPosition, _viewSize.height);
|
2013-04-04 22:57:43 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-07-12 14:30:26 +08:00
|
|
|
size = Size(_viewSize.width, maxPosition);
|
2013-04-04 22:57:43 +08:00
|
|
|
break;
|
|
|
|
}
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
this->setContentSize(size);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_oldDirection != _direction)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-07-26 22:55:41 +08:00
|
|
|
if (_direction == Direction::HORIZONTAL)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
this->setContentOffset(Point(0,0));
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
this->setContentOffset(Point(0,this->minContainerOffset().y));
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_oldDirection = _direction;
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point TableView::_offsetFromIndex(unsigned int index)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point offset = this->__offsetFromIndex(index);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
const Size cellSize = _dataSource->tableCellSizeForIndex(this, index);
|
2013-07-26 22:55:41 +08:00
|
|
|
if (_vordering == VerticalFillOrder::TOP_DOWN)
|
2013-04-04 22:57:43 +08:00
|
|
|
{
|
2012-09-09 22:34:32 +08:00
|
|
|
offset.y = this->getContainer()->getContentSize().height - offset.y - cellSize.height;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
Point TableView::__offsetFromIndex(unsigned int index)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
Point offset;
|
|
|
|
Size cellSize;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
switch (this->getDirection())
|
|
|
|
{
|
2013-07-26 22:55:41 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2013-07-12 14:11:55 +08:00
|
|
|
offset = Point(_vCellsPositions[index], 0.0f);
|
2012-09-09 22:34:32 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-07-12 14:11:55 +08:00
|
|
|
offset = Point(0.0f, _vCellsPositions[index]);
|
2012-09-09 22:34:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
unsigned int TableView::_indexFromOffset(Point offset)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
int index = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
const int maxIdx = _dataSource->numberOfCellsInTableView(this)-1;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-07-26 22:55:41 +08:00
|
|
|
if (_vordering == VerticalFillOrder::TOP_DOWN)
|
2013-04-04 22:57:43 +08:00
|
|
|
{
|
|
|
|
offset.y = this->getContainer()->getContentSize().height - offset.y;
|
|
|
|
}
|
|
|
|
index = this->__indexFromOffset(offset);
|
2013-04-10 17:13:19 +08:00
|
|
|
if (index != -1)
|
2013-04-04 22:57:43 +08:00
|
|
|
{
|
|
|
|
index = MAX(0, index);
|
2013-04-10 17:13:19 +08:00
|
|
|
if (index > maxIdx)
|
2013-04-04 22:57:43 +08:00
|
|
|
{
|
|
|
|
index = CC_INVALID_INDEX;
|
|
|
|
}
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
int TableView::__indexFromOffset(Point offset)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-04-04 22:57:43 +08:00
|
|
|
int low = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
int high = _dataSource->numberOfCellsInTableView(this) - 1;
|
2013-04-04 22:57:43 +08:00
|
|
|
float search;
|
|
|
|
switch (this->getDirection())
|
|
|
|
{
|
2013-07-26 22:55:41 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2013-04-04 22:57:43 +08:00
|
|
|
search = offset.x;
|
2012-09-09 22:34:32 +08:00
|
|
|
break;
|
|
|
|
default:
|
2013-04-04 22:57:43 +08:00
|
|
|
search = offset.y;
|
2012-09-09 22:34:32 +08:00
|
|
|
break;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
while (high >= low)
|
|
|
|
{
|
|
|
|
int index = low + (high - low) / 2;
|
2013-06-15 14:03:30 +08:00
|
|
|
float cellStart = _vCellsPositions[index];
|
|
|
|
float cellEnd = _vCellsPositions[index + 1];
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
if (search >= cellStart && search <= cellEnd)
|
|
|
|
{
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
else if (search < cellStart)
|
|
|
|
{
|
|
|
|
high = index - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
low = index + 1;
|
|
|
|
}
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
if (low <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
|
|
|
return -1;
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::_moveCellOutOfSight(TableViewCell *cell)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_tableViewDelegate != NULL) {
|
|
|
|
_tableViewDelegate->tableCellWillRecycle(this, cell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_cellsFreed->addObject(cell);
|
|
|
|
_cellsUsed->removeSortedObject(cell);
|
|
|
|
_indices->erase(cell->getIdx());
|
|
|
|
// [_indices removeIndex:cell.idx];
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->reset();
|
|
|
|
if (cell->getParent() == this->getContainer()) {
|
|
|
|
this->getContainer()->removeChild(cell, true);;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::_setIndexForCell(unsigned int index, TableViewCell *cell)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-07-12 14:11:55 +08:00
|
|
|
cell->setAnchorPoint(Point(0.0f, 0.0f));
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->setPosition(this->_offsetFromIndex(index));
|
|
|
|
cell->setIdx(index);
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::_updateCellPositions() {
|
2013-06-15 14:03:30 +08:00
|
|
|
int cellsCount = _dataSource->numberOfCellsInTableView(this);
|
|
|
|
_vCellsPositions.resize(cellsCount + 1, 0.0);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
if (cellsCount > 0)
|
|
|
|
{
|
|
|
|
float currentPos = 0;
|
2013-06-20 14:15:53 +08:00
|
|
|
Size cellSize;
|
2013-04-10 17:13:19 +08:00
|
|
|
for (int i=0; i < cellsCount; i++)
|
2013-04-04 22:57:43 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_vCellsPositions[i] = currentPos;
|
|
|
|
cellSize = _dataSource->tableCellSizeForIndex(this, i);
|
2013-04-04 22:57:43 +08:00
|
|
|
switch (this->getDirection())
|
|
|
|
{
|
2013-07-26 22:55:41 +08:00
|
|
|
case Direction::HORIZONTAL:
|
2013-04-04 22:57:43 +08:00
|
|
|
currentPos += cellSize.width;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
currentPos += cellSize.height;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_vCellsPositions[cellsCount] = currentPos;//1 extra value allows us to get right/bottom of the last cell
|
2013-04-04 22:57:43 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::scrollViewDidScroll(ScrollView* view)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
unsigned int uCountOfItems = _dataSource->numberOfCellsInTableView(this);
|
2012-10-16 11:31:04 +08:00
|
|
|
if (0 == uCountOfItems)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_tableViewDelegate != NULL) {
|
|
|
|
_tableViewDelegate->scrollViewDidScroll(this);
|
2012-12-05 04:32:13 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-10-16 11:31:04 +08:00
|
|
|
unsigned int startIdx = 0, endIdx = 0, idx = 0, maxIdx = 0;
|
2013-07-11 16:38:58 +08:00
|
|
|
Point offset = this->getContentOffset() * -1;
|
2012-10-16 11:31:04 +08:00
|
|
|
maxIdx = MAX(uCountOfItems-1, 0);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-07-26 22:55:41 +08:00
|
|
|
if (_vordering == VerticalFillOrder::TOP_DOWN)
|
2012-10-16 11:31:04 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
offset.y = offset.y + _viewSize.height/this->getContainer()->getScaleY();
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
startIdx = this->_indexFromOffset(offset);
|
2013-02-26 19:59:14 +08:00
|
|
|
if (startIdx == CC_INVALID_INDEX)
|
|
|
|
{
|
|
|
|
startIdx = uCountOfItems - 1;
|
|
|
|
}
|
|
|
|
|
2013-07-26 22:55:41 +08:00
|
|
|
if (_vordering == VerticalFillOrder::TOP_DOWN)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
offset.y -= _viewSize.height/this->getContainer()->getScaleY();
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
else
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
offset.y += _viewSize.height/this->getContainer()->getScaleY();
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
offset.x += _viewSize.width/this->getContainer()->getScaleX();
|
2013-04-10 17:13:19 +08:00
|
|
|
|
|
|
|
endIdx = this->_indexFromOffset(offset);
|
2013-02-26 19:59:14 +08:00
|
|
|
if (endIdx == CC_INVALID_INDEX)
|
|
|
|
{
|
|
|
|
endIdx = uCountOfItems - 1;
|
|
|
|
}
|
|
|
|
|
2012-09-10 13:39:12 +08:00
|
|
|
#if 0 // For Testing.
|
2013-06-20 14:15:53 +08:00
|
|
|
Object* pObj;
|
2012-09-09 22:34:32 +08:00
|
|
|
int i = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_cellsUsed, pObj)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
TableViewCell* pCell = static_cast<TableViewCell*>(pObj);
|
2013-07-24 06:20:22 +08:00
|
|
|
log("cells Used index %d, value = %d", i, pCell->getIdx());
|
2012-09-09 22:34:32 +08:00
|
|
|
i++;
|
|
|
|
}
|
2013-07-24 06:20:22 +08:00
|
|
|
log("---------------------------------------");
|
2012-09-09 22:34:32 +08:00
|
|
|
i = 0;
|
2013-06-15 14:03:30 +08:00
|
|
|
CCARRAY_FOREACH(_cellsFreed, pObj)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-07-09 14:29:51 +08:00
|
|
|
TableViewCell* pCell = static_cast<TableViewCell*>(pObj);
|
2013-07-24 06:20:22 +08:00
|
|
|
log("cells freed index %d, value = %d", i, pCell->getIdx());
|
2012-09-09 22:34:32 +08:00
|
|
|
i++;
|
|
|
|
}
|
2013-07-24 06:20:22 +08:00
|
|
|
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
2012-09-10 13:39:12 +08:00
|
|
|
#endif
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_cellsUsed->count() > 0)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell* cell = (TableViewCell*)_cellsUsed->objectAtIndex(0);
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
idx = cell->getIdx();
|
|
|
|
while(idx <startIdx)
|
|
|
|
{
|
|
|
|
this->_moveCellOutOfSight(cell);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_cellsUsed->count() > 0)
|
2012-09-10 13:39:12 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
cell = (TableViewCell*)_cellsUsed->objectAtIndex(0);
|
2013-04-10 17:13:19 +08:00
|
|
|
idx = cell->getIdx();
|
2012-09-10 13:39:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-09 22:34:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_cellsUsed->count() > 0)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell *cell = (TableViewCell*)_cellsUsed->lastObject();
|
2012-09-09 22:34:32 +08:00
|
|
|
idx = cell->getIdx();
|
|
|
|
|
|
|
|
while(idx <= maxIdx && idx > endIdx)
|
|
|
|
{
|
|
|
|
this->_moveCellOutOfSight(cell);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_cellsUsed->count() > 0)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
cell = (TableViewCell*)_cellsUsed->lastObject();
|
2012-09-09 22:34:32 +08:00
|
|
|
idx = cell->getIdx();
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-10 13:39:12 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-09-09 22:34:32 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
for (unsigned int i=startIdx; i <= endIdx; i++)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
//if ([_indices containsIndex:i])
|
|
|
|
if (_indices->find(i) != _indices->end())
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this->updateCellAtIndex(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::ccTouchEnded(Touch *pTouch, Event *pEvent)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
if (!this->isVisible()) {
|
|
|
|
return;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchedCell){
|
2013-07-17 09:16:04 +08:00
|
|
|
Rect bb = this->getBoundingBox();
|
2013-06-15 14:03:30 +08:00
|
|
|
bb.origin = _parent->convertToWorldSpace(bb.origin);
|
2013-02-26 19:59:14 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (bb.containsPoint(pTouch->getLocation()) && _tableViewDelegate != NULL)
|
2013-02-26 19:59:14 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
|
|
|
_tableViewDelegate->tableCellTouched(this, _touchedCell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchedCell = NULL;
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView::ccTouchEnded(pTouch, pEvent);
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool TableView::ccTouchBegan(Touch *pTouch, Event *pEvent)
|
2012-11-03 08:26:43 +08:00
|
|
|
{
|
|
|
|
if (!this->isVisible()) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
bool touchResult = ScrollView::ccTouchBegan(pTouch, pEvent);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_touches->count() == 1) {
|
2012-09-09 22:34:32 +08:00
|
|
|
unsigned int index;
|
2013-06-20 14:15:53 +08:00
|
|
|
Point point;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
point = this->getContainer()->convertTouchToNodeSpace(pTouch);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-11-03 08:26:43 +08:00
|
|
|
index = this->_indexFromOffset(point);
|
2013-02-26 19:59:14 +08:00
|
|
|
if (index == CC_INVALID_INDEX)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchedCell = NULL;
|
2013-02-26 19:59:14 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchedCell = this->cellAtIndex(index);
|
2013-02-26 19:59:14 +08:00
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchedCell && _tableViewDelegate != NULL) {
|
|
|
|
_tableViewDelegate->tableCellHighlight(this, _touchedCell);
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
else if(_touchedCell) {
|
|
|
|
if(_tableViewDelegate != NULL) {
|
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchedCell = NULL;
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return touchResult;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::ccTouchMoved(Touch *pTouch, Event *pEvent)
|
2012-11-03 08:26:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView::ccTouchMoved(pTouch, pEvent);
|
2012-11-03 08:26:43 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchedCell && isTouchMoved()) {
|
|
|
|
if(_tableViewDelegate != NULL) {
|
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchedCell = NULL;
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void TableView::ccTouchCancelled(Touch *pTouch, Event *pEvent)
|
2012-11-03 08:26:43 +08:00
|
|
|
{
|
2013-06-20 14:15:53 +08:00
|
|
|
ScrollView::ccTouchCancelled(pTouch, pEvent);
|
2012-11-03 08:26:43 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_touchedCell) {
|
|
|
|
if(_tableViewDelegate != NULL) {
|
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchedCell = NULL;
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|