2020-10-18 12:31:45 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2010 Sangwoo Im
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
|
|
|
|
2022-07-10 09:47:27 +08:00
|
|
|
https://adxeproject.github.io/
|
2020-10-18 12:31:45 +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:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCTableView.h"
|
|
|
|
#include "CCTableViewCell.h"
|
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableViewDelegate::tableCellHighlight(TableView* /*table*/, TableViewCell* /*cell*/) {}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableViewDelegate::tableCellUnhighlight(TableView* /*table*/, TableViewCell* /*cell*/) {}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableViewDelegate::tableCellWillRecycle(TableView* /*table*/, TableViewCell* /*cell*/) {}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Size TableViewDataSource::tableCellSizeForIndex(TableView* table, ssize_t /*idx*/)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
return cellSizeForTable(table);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Size TableViewDataSource::cellSizeForTable(TableView* /*table*/)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
return Size::ZERO;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableView* TableView::create()
|
|
|
|
{
|
|
|
|
return TableView::create(nullptr, Size::ZERO);
|
|
|
|
}
|
|
|
|
|
|
|
|
TableView* TableView::create(TableViewDataSource* dataSource, Size size)
|
|
|
|
{
|
|
|
|
return TableView::create(dataSource, size, nullptr);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TableView* TableView::create(TableViewDataSource* dataSource, Size size, Node* container)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
TableView* table = new TableView();
|
2020-10-18 12:31:45 +08:00
|
|
|
table->initWithViewSize(size, container);
|
|
|
|
table->autorelease();
|
|
|
|
table->setDataSource(dataSource);
|
|
|
|
table->_updateCellPositions();
|
|
|
|
table->_updateContentSize();
|
|
|
|
|
|
|
|
return table;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool TableView::initWithViewSize(Size size, Node* container /* = nullptr*/)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (ScrollView::initWithViewSize(size, container))
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_indices);
|
2021-12-25 10:04:45 +08:00
|
|
|
_indices = new std::set<ssize_t>();
|
|
|
|
_vordering = VerticalFillOrder::BOTTOM_UP;
|
2020-10-18 12:31:45 +08:00
|
|
|
this->setDirection(Direction::VERTICAL);
|
|
|
|
|
|
|
|
ScrollView::setDelegate(this);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableView::TableView()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _touchedCell(nullptr)
|
|
|
|
, _indices(nullptr)
|
|
|
|
, _dataSource(nullptr)
|
|
|
|
, _tableViewDelegate(nullptr)
|
|
|
|
, _oldDirection(Direction::NONE)
|
|
|
|
, _isUsedCellsDirty(false)
|
|
|
|
{}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
TableView::~TableView()
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(_indices);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::setVerticalFillOrder(VerticalFillOrder fillOrder)
|
|
|
|
{
|
|
|
|
if (_vordering != fillOrder)
|
|
|
|
{
|
|
|
|
_vordering = fillOrder;
|
|
|
|
if (!_cellsUsed.empty())
|
|
|
|
{
|
|
|
|
this->reloadData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TableView::VerticalFillOrder TableView::getVerticalFillOrder()
|
|
|
|
{
|
|
|
|
return _vordering;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::reloadData()
|
|
|
|
{
|
|
|
|
_oldDirection = Direction::NONE;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (const auto& cell : _cellsUsed)
|
|
|
|
{
|
|
|
|
if (_tableViewDelegate != nullptr)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
_tableViewDelegate->tableCellWillRecycle(this, cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
_cellsFreed.pushBack(cell);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
cell->reset();
|
|
|
|
if (cell->getParent() == this->getContainer())
|
|
|
|
{
|
|
|
|
this->getContainer()->removeChild(cell, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_indices->clear();
|
|
|
|
_cellsUsed.clear();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
this->_updateCellPositions();
|
|
|
|
this->_updateContentSize();
|
|
|
|
if (_dataSource->numberOfCellsInTableView(this) > 0)
|
|
|
|
{
|
|
|
|
this->scrollViewDidScroll(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TableViewCell* TableView::cellAtIndex(ssize_t idx)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (_indices->find(idx) != _indices->end())
|
|
|
|
{
|
|
|
|
for (const auto& cell : _cellsUsed)
|
|
|
|
{
|
|
|
|
if (cell->getIdx() == idx)
|
|
|
|
{
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::updateCellAtIndex(ssize_t idx)
|
|
|
|
{
|
|
|
|
if (idx == CC_INVALID_INDEX)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
long countOfItems = _dataSource->numberOfCellsInTableView(this);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (0 == countOfItems || idx > countOfItems - 1)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
TableViewCell* cell = this->cellAtIndex(idx);
|
|
|
|
if (cell)
|
|
|
|
{
|
|
|
|
this->_moveCellOutOfSight(cell);
|
|
|
|
}
|
|
|
|
cell = _dataSource->tableCellAtIndex(this, idx);
|
|
|
|
this->_setIndexForCell(idx, cell);
|
|
|
|
this->_addCellIfNecessary(cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::insertCellAtIndex(ssize_t idx)
|
|
|
|
{
|
|
|
|
if (idx == CC_INVALID_INDEX)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long countOfItems = _dataSource->numberOfCellsInTableView(this);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (0 == countOfItems || idx > countOfItems - 1)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long newIdx = 0;
|
|
|
|
|
|
|
|
auto cell = cellAtIndex(idx);
|
|
|
|
if (cell)
|
|
|
|
{
|
|
|
|
newIdx = _cellsUsed.getIndex(cell);
|
|
|
|
// Move all cells behind the inserted position
|
|
|
|
for (long i = newIdx; i < _cellsUsed.size(); i++)
|
|
|
|
{
|
|
|
|
cell = _cellsUsed.at(i);
|
2021-12-25 10:04:45 +08:00
|
|
|
this->_setIndexForCell(cell->getIdx() + 1, cell);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// insert a new cell
|
2020-10-18 12:31:45 +08:00
|
|
|
cell = _dataSource->tableCellAtIndex(this, idx);
|
|
|
|
this->_setIndexForCell(idx, cell);
|
|
|
|
this->_addCellIfNecessary(cell);
|
|
|
|
|
|
|
|
this->_updateCellPositions();
|
|
|
|
this->_updateContentSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::removeCellAtIndex(ssize_t idx)
|
|
|
|
{
|
|
|
|
if (idx == CC_INVALID_INDEX)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
long uCountOfItems = _dataSource->numberOfCellsInTableView(this);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (0 == uCountOfItems || idx > uCountOfItems - 1)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t newIdx = 0;
|
|
|
|
|
|
|
|
TableViewCell* cell = this->cellAtIndex(idx);
|
|
|
|
if (!cell)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
newIdx = _cellsUsed.getIndex(cell);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// remove first
|
2020-10-18 12:31:45 +08:00
|
|
|
this->_moveCellOutOfSight(cell);
|
|
|
|
|
|
|
|
_indices->erase(idx);
|
|
|
|
this->_updateCellPositions();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
for (ssize_t i = _cellsUsed.size() - 1; i > newIdx; i--)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
cell = _cellsUsed.at(i);
|
2021-12-25 10:04:45 +08:00
|
|
|
this->_setIndexForCell(cell->getIdx() - 1, cell);
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TableViewCell* TableView::dequeueCell()
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
TableViewCell* cell;
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_cellsFreed.empty())
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
cell = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
cell = _cellsFreed.at(0);
|
|
|
|
cell->retain();
|
|
|
|
_cellsFreed.erase(0);
|
|
|
|
cell->autorelease();
|
|
|
|
}
|
|
|
|
return cell;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableView::_addCellIfNecessary(TableViewCell* cell)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (cell->getParent() != this->getContainer())
|
|
|
|
{
|
|
|
|
this->getContainer()->addChild(cell);
|
|
|
|
}
|
|
|
|
_cellsUsed.pushBack(cell);
|
|
|
|
_indices->insert(cell->getIdx());
|
|
|
|
_isUsedCellsDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::_updateContentSize()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
Size size = Size::ZERO;
|
2020-10-18 12:31:45 +08:00
|
|
|
ssize_t cellsCount = _dataSource->numberOfCellsInTableView(this);
|
|
|
|
|
|
|
|
if (cellsCount > 0)
|
|
|
|
{
|
|
|
|
float maxPosition = _vCellsPositions[cellsCount];
|
|
|
|
|
|
|
|
switch (this->getDirection())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case Direction::HORIZONTAL:
|
|
|
|
size = Size(maxPosition, _viewSize.height);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
size = Size(_viewSize.width, maxPosition);
|
|
|
|
break;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this->setContentSize(size);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_oldDirection != _direction)
|
|
|
|
{
|
|
|
|
if (_direction == Direction::HORIZONTAL)
|
|
|
|
{
|
|
|
|
this->setContentOffset(Vec2(0.0f, 0.0f));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this->setContentOffset(Vec2(0.0f, this->minContainerOffset().y));
|
|
|
|
}
|
|
|
|
_oldDirection = _direction;
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 TableView::_offsetFromIndex(ssize_t index)
|
|
|
|
{
|
|
|
|
Vec2 offset = this->__offsetFromIndex(index);
|
|
|
|
|
|
|
|
const Size cellSize = _dataSource->tableCellSizeForIndex(this, index);
|
|
|
|
if (_vordering == VerticalFillOrder::TOP_DOWN)
|
|
|
|
{
|
|
|
|
offset.y = this->getContainer()->getContentSize().height - offset.y - cellSize.height;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
Vec2 TableView::__offsetFromIndex(ssize_t index)
|
|
|
|
{
|
|
|
|
Vec2 offset;
|
2021-12-25 10:04:45 +08:00
|
|
|
Size cellSize;
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
switch (this->getDirection())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case Direction::HORIZONTAL:
|
|
|
|
offset.set(_vCellsPositions[index], 0.0f);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
offset.set(0.0f, _vCellsPositions[index]);
|
|
|
|
break;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
long TableView::_indexFromOffset(Vec2 offset)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
long index = 0;
|
2020-10-18 12:31:45 +08:00
|
|
|
const long maxIdx = _dataSource->numberOfCellsInTableView(this) - 1;
|
|
|
|
|
|
|
|
if (_vordering == VerticalFillOrder::TOP_DOWN)
|
|
|
|
{
|
|
|
|
offset.y = this->getContainer()->getContentSize().height - offset.y;
|
|
|
|
}
|
|
|
|
index = this->__indexFromOffset(offset);
|
|
|
|
if (index != -1)
|
|
|
|
{
|
|
|
|
index = MAX(0, index);
|
|
|
|
if (index > maxIdx)
|
|
|
|
{
|
|
|
|
index = CC_INVALID_INDEX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
|
|
|
|
long TableView::__indexFromOffset(Vec2 offset)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
long low = 0;
|
2020-10-18 12:31:45 +08:00
|
|
|
long high = _dataSource->numberOfCellsInTableView(this) - 1;
|
|
|
|
float search;
|
|
|
|
switch (this->getDirection())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case Direction::HORIZONTAL:
|
|
|
|
search = offset.x;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
search = offset.y;
|
|
|
|
break;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
while (high >= low)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
long index = low + (high - low) / 2;
|
2020-10-18 12:31:45 +08:00
|
|
|
float cellStart = _vCellsPositions[index];
|
2021-12-25 10:04:45 +08:00
|
|
|
float cellEnd = _vCellsPositions[index + 1];
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
if (search >= cellStart && search <= cellEnd)
|
|
|
|
{
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
else if (search < cellStart)
|
|
|
|
{
|
|
|
|
high = index - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
low = index + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (low <= 0)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableView::_moveCellOutOfSight(TableViewCell* cell)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_tableViewDelegate != nullptr)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
_tableViewDelegate->tableCellWillRecycle(this, cell);
|
|
|
|
}
|
|
|
|
|
|
|
|
_cellsFreed.pushBack(cell);
|
|
|
|
_cellsUsed.eraseObject(cell);
|
|
|
|
_isUsedCellsDirty = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
_indices->erase(cell->getIdx());
|
|
|
|
cell->reset();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
if (cell->getParent() == this->getContainer())
|
|
|
|
{
|
|
|
|
this->getContainer()->removeChild(cell, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableView::_setIndexForCell(ssize_t index, TableViewCell* cell)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
cell->setAnchorPoint(Vec2(0.0f, 0.0f));
|
|
|
|
cell->setPosition(this->_offsetFromIndex(index));
|
|
|
|
cell->setIdx(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TableView::_updateCellPositions()
|
|
|
|
{
|
|
|
|
long cellsCount = _dataSource->numberOfCellsInTableView(this);
|
|
|
|
_vCellsPositions.resize(cellsCount + 1, 0.0);
|
|
|
|
|
|
|
|
if (cellsCount > 0)
|
|
|
|
{
|
|
|
|
float currentPos = 0;
|
|
|
|
Size cellSize;
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < cellsCount; i++)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
_vCellsPositions[i] = currentPos;
|
2021-12-25 10:04:45 +08:00
|
|
|
cellSize = _dataSource->tableCellSizeForIndex(this, i);
|
2020-10-18 12:31:45 +08:00
|
|
|
switch (this->getDirection())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case Direction::HORIZONTAL:
|
|
|
|
currentPos += cellSize.width;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
currentPos += cellSize.height;
|
|
|
|
break;
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
_vCellsPositions[cellsCount] = currentPos; // 1 extra value allows us to get right/bottom of the last cell
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableView::scrollViewDidScroll(ScrollView* /*view*/)
|
|
|
|
{
|
2021-05-05 19:49:30 +08:00
|
|
|
if (!_dataSource)
|
|
|
|
return;
|
2020-10-18 12:31:45 +08:00
|
|
|
long countOfItems = _dataSource->numberOfCellsInTableView(this);
|
|
|
|
if (0 == countOfItems)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_isUsedCellsDirty)
|
|
|
|
{
|
|
|
|
_isUsedCellsDirty = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
std::sort(_cellsUsed.begin(), _cellsUsed.end(),
|
|
|
|
[](TableViewCell* a, TableViewCell* b) -> bool { return a->getIdx() < b->getIdx(); });
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-10-18 12:31:45 +08:00
|
|
|
ssize_t startIdx = 0, endIdx = 0, idx = 0, maxIdx = 0;
|
|
|
|
Vec2 offset = this->getContentOffset() * -1;
|
2021-12-25 10:04:45 +08:00
|
|
|
maxIdx = MAX(countOfItems - 1, 0);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
if (_vordering == VerticalFillOrder::TOP_DOWN)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
offset.y = offset.y + _viewSize.height / this->getContainer()->getScaleY();
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
startIdx = this->_indexFromOffset(offset);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (startIdx == CC_INVALID_INDEX)
|
|
|
|
{
|
|
|
|
startIdx = countOfItems - 1;
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
if (_vordering == VerticalFillOrder::TOP_DOWN)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
offset.y -= _viewSize.height / this->getContainer()->getScaleY();
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
offset.y += _viewSize.height / this->getContainer()->getScaleY();
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
offset.x += _viewSize.width / this->getContainer()->getScaleX();
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
endIdx = this->_indexFromOffset(offset);
|
2020-10-18 12:31:45 +08:00
|
|
|
if (endIdx == CC_INVALID_INDEX)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
endIdx = countOfItems - 1;
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#if 0 // For Testing.
|
2020-10-18 12:31:45 +08:00
|
|
|
Ref* pObj;
|
|
|
|
int i = 0;
|
|
|
|
CCARRAY_FOREACH(_cellsUsed, pObj)
|
|
|
|
{
|
|
|
|
TableViewCell* pCell = static_cast<TableViewCell*>(pObj);
|
|
|
|
log("cells Used index %d, value = %d", i, pCell->getIdx());
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
log("---------------------------------------");
|
|
|
|
i = 0;
|
|
|
|
CCARRAY_FOREACH(_cellsFreed, pObj)
|
|
|
|
{
|
|
|
|
TableViewCell* pCell = static_cast<TableViewCell*>(pObj);
|
|
|
|
log("cells freed index %d, value = %d", i, pCell->getIdx());
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (!_cellsUsed.empty())
|
|
|
|
{
|
|
|
|
auto cell = _cellsUsed.at(0);
|
2021-12-25 10:04:45 +08:00
|
|
|
idx = cell->getIdx();
|
|
|
|
|
|
|
|
while (idx < startIdx)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
this->_moveCellOutOfSight(cell);
|
|
|
|
if (!_cellsUsed.empty())
|
|
|
|
{
|
|
|
|
cell = _cellsUsed.at(0);
|
2021-12-25 10:04:45 +08:00
|
|
|
idx = cell->getIdx();
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!_cellsUsed.empty())
|
|
|
|
{
|
|
|
|
auto cell = _cellsUsed.back();
|
2021-12-25 10:04:45 +08:00
|
|
|
idx = cell->getIdx();
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
while (idx <= maxIdx && idx > endIdx)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
this->_moveCellOutOfSight(cell);
|
|
|
|
if (!_cellsUsed.empty())
|
|
|
|
{
|
|
|
|
cell = _cellsUsed.back();
|
2021-12-25 10:04:45 +08:00
|
|
|
idx = cell->getIdx();
|
2020-10-18 12:31:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (long i = startIdx; i <= endIdx; i++)
|
|
|
|
{
|
|
|
|
if (_indices->find(i) != _indices->end())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this->updateCellAtIndex(i);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_tableViewDelegate != nullptr)
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
_tableViewDelegate->scrollViewDidScroll(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableView::onTouchEnded(Touch* pTouch, Event* pEvent)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!this->isVisible())
|
|
|
|
{
|
2020-10-18 12:31:45 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_touchedCell)
|
|
|
|
{
|
|
|
|
Rect bb = this->getBoundingBox();
|
|
|
|
bb.origin = _parent->convertToWorldSpace(bb.origin);
|
2020-10-18 12:31:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (bb.containsPoint(pTouch->getLocation()) && _tableViewDelegate != nullptr)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
|
|
|
_tableViewDelegate->tableCellTouched(this, _touchedCell);
|
|
|
|
}
|
|
|
|
|
|
|
|
_touchedCell = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ScrollView::onTouchEnded(pTouch, pEvent);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool TableView::onTouchBegan(Touch* pTouch, Event* pEvent)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (Node* c = this; c != nullptr; c = c->getParent())
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
if (!c->isVisible())
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool touchResult = ScrollView::onTouchBegan(pTouch, pEvent);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_touches.size() == 1)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
long index;
|
|
|
|
Vec2 point;
|
|
|
|
|
|
|
|
point = this->getContainer()->convertTouchToNodeSpace(pTouch);
|
|
|
|
|
|
|
|
index = this->_indexFromOffset(point);
|
2021-12-25 10:04:45 +08:00
|
|
|
if (index == CC_INVALID_INDEX)
|
|
|
|
{
|
|
|
|
_touchedCell = nullptr;
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
else
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
_touchedCell = this->cellAtIndex(index);
|
|
|
|
}
|
2020-10-18 12:31:45 +08:00
|
|
|
|
|
|
|
if (_touchedCell && _tableViewDelegate != nullptr)
|
|
|
|
{
|
|
|
|
_tableViewDelegate->tableCellHighlight(this, _touchedCell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (_touchedCell)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_tableViewDelegate != nullptr)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
|
|
|
}
|
|
|
|
|
|
|
|
_touchedCell = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return touchResult;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableView::onTouchMoved(Touch* pTouch, Event* pEvent)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
ScrollView::onTouchMoved(pTouch, pEvent);
|
|
|
|
|
|
|
|
if (_touchedCell && isTouchMoved())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_tableViewDelegate != nullptr)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
|
|
|
}
|
|
|
|
|
|
|
|
_touchedCell = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TableView::onTouchCancelled(Touch* pTouch, Event* pEvent)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
ScrollView::onTouchCancelled(pTouch, pEvent);
|
|
|
|
|
|
|
|
if (_touchedCell)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_tableViewDelegate != nullptr)
|
2020-10-18 12:31:45 +08:00
|
|
|
{
|
|
|
|
_tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
|
|
|
|
}
|
|
|
|
|
|
|
|
_touchedCell = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|