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 "CCTableView.h"
|
|
|
|
#include "CCTableViewCell.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2014-01-07 22:15:20 +08:00
|
|
|
TableView* TableView::create()
|
|
|
|
{
|
|
|
|
return TableView::create(nullptr, Size::ZERO);
|
|
|
|
}
|
2013-12-07 14:17:47 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableView* TableView::create(TableViewDataSource* dataSource, Size size)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
return TableView::create(dataSource, size, nullptr);
|
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;
|
|
|
|
}
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
bool TableView::initWithViewSize(Size size, Node* container/* = nullptr*/)
|
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
|
|
|
{
|
2014-01-07 22:15:20 +08:00
|
|
|
CC_SAFE_DELETE(_indices);
|
2013-12-13 17:53:03 +08:00
|
|
|
_indices = new std::set<ssize_t>();
|
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)
|
|
|
|
, _dataSource(nullptr)
|
|
|
|
, _tableViewDelegate(nullptr)
|
|
|
|
, _oldDirection(Direction::NONE)
|
2013-12-07 14:17:47 +08:00
|
|
|
, _isUsedCellsDirty(false)
|
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);
|
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-12-07 14:17:47 +08:00
|
|
|
if (_vordering != fillOrder)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_vordering = fillOrder;
|
2013-12-07 14:17:47 +08:00
|
|
|
if (!_cellsUsed.empty())
|
|
|
|
{
|
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-04-10 17:13:19 +08:00
|
|
|
|
2013-12-20 05:34:41 +08:00
|
|
|
for(const auto &cell : _cellsUsed) {
|
2014-07-10 00:45:27 +08:00
|
|
|
if(_tableViewDelegate != nullptr) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_tableViewDelegate->tableCellWillRecycle(this, cell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
_cellsFreed.pushBack(cell);
|
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->reset();
|
|
|
|
if (cell->getParent() == this->getContainer())
|
|
|
|
{
|
|
|
|
this->getContainer()->removeChild(cell, true);
|
|
|
|
}
|
2013-12-20 05:34:41 +08:00
|
|
|
}
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices->clear();
|
2013-12-07 14:17:47 +08:00
|
|
|
_cellsUsed.clear();
|
|
|
|
|
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-12-12 12:07:20 +08:00
|
|
|
TableViewCell *TableView::cellAtIndex(ssize_t idx)
|
2012-09-09 22:34:32 +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-12-07 14:17:47 +08:00
|
|
|
for (const auto& cell : _cellsUsed)
|
|
|
|
{
|
|
|
|
if (cell->getIdx() == idx)
|
|
|
|
{
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
}
|
2013-02-21 10:06:22 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
return nullptr;
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
void TableView::updateCellAtIndex(ssize_t 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-11-18 15:55:58 +08:00
|
|
|
long countOfItems = _dataSource->numberOfCellsInTableView(this);
|
|
|
|
if (0 == countOfItems || idx > countOfItems-1)
|
2012-10-16 11:31:04 +08:00
|
|
|
{
|
|
|
|
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-12-12 12:07:20 +08:00
|
|
|
void TableView::insertCellAtIndex(ssize_t 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-11-18 15:55:58 +08:00
|
|
|
long countOfItems = _dataSource->numberOfCellsInTableView(this);
|
|
|
|
if (0 == countOfItems || idx > countOfItems-1)
|
2012-10-16 11:31:04 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-18 15:55:58 +08:00
|
|
|
long newIdx = 0;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
auto cell = cellAtIndex(idx);
|
2013-04-10 17:13:19 +08:00
|
|
|
if (cell)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
newIdx = _cellsUsed.getIndex(cell);
|
|
|
|
// Move all cells behind the inserted position
|
|
|
|
for (long i = newIdx; i < _cellsUsed.size(); i++)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
cell = _cellsUsed.at(i);
|
2012-09-09 22:34:32 +08:00
|
|
|
this->_setIndexForCell(cell->getIdx()+1, cell);
|
|
|
|
}
|
|
|
|
}
|
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-12-12 12:07:20 +08:00
|
|
|
void TableView::removeCellAtIndex(ssize_t 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-11-18 15:55:58 +08:00
|
|
|
long uCountOfItems = _dataSource->numberOfCellsInTableView(this);
|
2012-10-16 11:31:04 +08:00
|
|
|
if (0 == uCountOfItems || idx > uCountOfItems-1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t 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-12-07 14:17:47 +08:00
|
|
|
newIdx = _cellsUsed.getIndex(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-12-07 14:17:47 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
for (ssize_t i = _cellsUsed.size()-1; i > newIdx; i--)
|
2013-04-04 22:57:43 +08:00
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
cell = _cellsUsed.at(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-12-07 14:17:47 +08:00
|
|
|
if (_cellsFreed.empty()) {
|
2014-07-10 00:45:27 +08:00
|
|
|
cell = nullptr;
|
2012-09-09 22:34:32 +08:00
|
|
|
} else {
|
2013-12-07 14:17:47 +08:00
|
|
|
cell = _cellsFreed.at(0);
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->retain();
|
2013-12-11 17:53:45 +08:00
|
|
|
_cellsFreed.erase(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-12-07 14:17:47 +08:00
|
|
|
_cellsUsed.pushBack(cell);
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices->insert(cell->getIdx());
|
2013-12-07 14:17:47 +08:00
|
|
|
_isUsedCellsDirty = true;
|
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-12-12 12:07:20 +08:00
|
|
|
ssize_t 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
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
this->setContentOffset(Vec2(0,0));
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
this->setContentOffset(Vec2(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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TableView::_offsetFromIndex(ssize_t index)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 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;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 TableView::__offsetFromIndex(ssize_t index)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 offset;
|
2013-06-20 14:15:53 +08:00
|
|
|
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:
|
2014-05-15 01:07:09 +08:00
|
|
|
offset = Vec2(_vCellsPositions[index], 0.0f);
|
2012-09-09 22:34:32 +08:00
|
|
|
break;
|
|
|
|
default:
|
2014-05-15 01:07:09 +08:00
|
|
|
offset = Vec2(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;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
long TableView::_indexFromOffset(Vec2 offset)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-11-18 15:55:58 +08:00
|
|
|
long index = 0;
|
|
|
|
const long 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;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
long TableView::__indexFromOffset(Vec2 offset)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-11-18 15:55:58 +08:00
|
|
|
long low = 0;
|
|
|
|
long 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)
|
|
|
|
{
|
2013-11-18 15:55:58 +08:00
|
|
|
long 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
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
if(_tableViewDelegate != nullptr) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_tableViewDelegate->tableCellWillRecycle(this, cell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
_cellsFreed.pushBack(cell);
|
2013-12-12 14:45:30 +08:00
|
|
|
_cellsUsed.eraseObject(cell);
|
2013-12-07 14:17:47 +08:00
|
|
|
_isUsedCellsDirty = true;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_indices->erase(cell->getIdx());
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->reset();
|
2013-12-07 14:17:47 +08:00
|
|
|
|
|
|
|
if (cell->getParent() == this->getContainer())
|
|
|
|
{
|
2012-09-09 22:34:32 +08:00
|
|
|
this->getContainer()->removeChild(cell, true);;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
void TableView::_setIndexForCell(ssize_t index, TableViewCell *cell)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2014-05-15 01:07:09 +08:00
|
|
|
cell->setAnchorPoint(Vec2(0.0f, 0.0f));
|
2012-09-09 22:34:32 +08:00
|
|
|
cell->setPosition(this->_offsetFromIndex(index));
|
|
|
|
cell->setIdx(index);
|
|
|
|
}
|
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
void TableView::_updateCellPositions()
|
|
|
|
{
|
|
|
|
long cellsCount = _dataSource->numberOfCellsInTableView(this);
|
2013-06-15 14:03:30 +08:00
|
|
|
_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-12-07 14:17:47 +08:00
|
|
|
long countOfItems = _dataSource->numberOfCellsInTableView(this);
|
|
|
|
if (0 == countOfItems)
|
2012-10-16 11:31:04 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
if (_isUsedCellsDirty)
|
|
|
|
{
|
|
|
|
_isUsedCellsDirty = false;
|
2013-12-17 17:45:29 +08:00
|
|
|
std::sort(_cellsUsed.begin(), _cellsUsed.end(), [](TableViewCell *a, TableViewCell *b) -> bool{
|
2013-12-07 14:17:47 +08:00
|
|
|
return a->getIdx() < b->getIdx();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
if(_tableViewDelegate != nullptr) {
|
2013-06-15 14:03:30 +08:00
|
|
|
_tableViewDelegate->scrollViewDidScroll(this);
|
2012-12-05 04:32:13 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-12-12 12:07:20 +08:00
|
|
|
ssize_t startIdx = 0, endIdx = 0, idx = 0, maxIdx = 0;
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 offset = this->getContentOffset() * -1;
|
2013-12-07 14:17:47 +08:00
|
|
|
maxIdx = MAX(countOfItems-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)
|
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
startIdx = countOfItems - 1;
|
2013-02-26 19:59:14 +08:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
endIdx = countOfItems - 1;
|
2013-02-26 19:59:14 +08:00
|
|
|
}
|
|
|
|
|
2012-09-10 13:39:12 +08:00
|
|
|
#if 0 // For Testing.
|
2014-02-20 10:53:49 +08:00
|
|
|
Ref* 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-12-07 14:17:47 +08:00
|
|
|
if (!_cellsUsed.empty())
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
auto cell = _cellsUsed.at(0);
|
2012-09-09 22:34:32 +08:00
|
|
|
idx = cell->getIdx();
|
2013-12-07 14:17:47 +08:00
|
|
|
|
|
|
|
while(idx < startIdx)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
this->_moveCellOutOfSight(cell);
|
2013-12-07 14:17:47 +08:00
|
|
|
if (!_cellsUsed.empty())
|
2012-09-10 13:39:12 +08:00
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
cell = _cellsUsed.at(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-12-07 14:17:47 +08:00
|
|
|
if (!_cellsUsed.empty())
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
auto cell = _cellsUsed.back();
|
2012-09-09 22:34:32 +08:00
|
|
|
idx = cell->getIdx();
|
|
|
|
|
|
|
|
while(idx <= maxIdx && idx > endIdx)
|
|
|
|
{
|
|
|
|
this->_moveCellOutOfSight(cell);
|
2013-12-07 14:17:47 +08:00
|
|
|
if (!_cellsUsed.empty())
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
cell = _cellsUsed.back();
|
2012-09-09 22:34:32 +08:00
|
|
|
idx = cell->getIdx();
|
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
|
|
|
|
2013-11-18 15:55:58 +08:00
|
|
|
for (long i = startIdx; i <= endIdx; i++)
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_indices->find(i) != _indices->end())
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this->updateCellAtIndex(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TableView::onTouchEnded(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
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
if (bb.containsPoint(pTouch->getLocation()) && _tableViewDelegate != nullptr)
|
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
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
_touchedCell = nullptr;
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
ScrollView::onTouchEnded(pTouch, pEvent);
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool TableView::onTouchBegan(Touch *pTouch, Event *pEvent)
|
2012-11-03 08:26:43 +08:00
|
|
|
{
|
2013-12-07 14:17:47 +08:00
|
|
|
if (!this->isVisible())
|
|
|
|
{
|
2012-11-03 08:26:43 +08:00
|
|
|
return false;
|
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool touchResult = ScrollView::onTouchBegan(pTouch, pEvent);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
if(_touches.size() == 1)
|
|
|
|
{
|
|
|
|
long index;
|
2014-05-15 01:07:09 +08:00
|
|
|
Vec2 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)
|
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
_touchedCell = nullptr;
|
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
|
|
|
}
|
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
if (_touchedCell && _tableViewDelegate != nullptr)
|
2013-12-07 14:17:47 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_tableViewDelegate->tableCellHighlight(this, _touchedCell);
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
}
|
2013-12-07 14:17:47 +08:00
|
|
|
else if (_touchedCell)
|
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
if(_tableViewDelegate != nullptr)
|
2013-12-07 14:17:47 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
_touchedCell = nullptr;
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return touchResult;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TableView::onTouchMoved(Touch *pTouch, Event *pEvent)
|
2012-11-03 08:26:43 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
ScrollView::onTouchMoved(pTouch, pEvent);
|
2012-11-03 08:26:43 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
if (_touchedCell && isTouchMoved())
|
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
if(_tableViewDelegate != nullptr)
|
2013-12-07 14:17:47 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
_touchedCell = nullptr;
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void TableView::onTouchCancelled(Touch *pTouch, Event *pEvent)
|
2012-11-03 08:26:43 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
ScrollView::onTouchCancelled(pTouch, pEvent);
|
2012-11-03 08:26:43 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
if (_touchedCell)
|
|
|
|
{
|
2014-07-10 00:45:27 +08:00
|
|
|
if(_tableViewDelegate != nullptr)
|
2013-12-07 14:17:47 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
2013-01-24 03:04:49 +08:00
|
|
|
}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2014-07-10 00:45:27 +08:00
|
|
|
_touchedCell = nullptr;
|
2012-11-03 08:26:43 +08:00
|
|
|
}
|
2012-09-09 22:34:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|