CCTableViewSmooth.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /****************************************************************************
  2. Copyright (c) 2012 cocos2d-x.org
  3. Copyright (c) 2010 Sangwoo Im
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "CCTableViewSmooth.h"
  22. #include "CCTableViewCell.h"
  23. NS_CC_EXT_BEGIN
  24. TableViewSmooth* TableViewSmooth::create()
  25. {
  26. return TableViewSmooth::create(nullptr, Size::ZERO);
  27. }
  28. TableViewSmooth* TableViewSmooth::create(TableViewDataSourceSmooth* dataSource, Size size)
  29. {
  30. return TableViewSmooth::create(dataSource, size, nullptr);
  31. }
  32. TableViewSmooth* TableViewSmooth::create(TableViewDataSourceSmooth* dataSource, Size size, Node *container)
  33. {
  34. TableViewSmooth *table = new (std::nothrow) TableViewSmooth();
  35. table->initWithViewSize(size, container);
  36. table->autorelease();
  37. table->setDataSource(dataSource);
  38. table->_updateCellPositions();
  39. table->_updateContentSize();
  40. return table;
  41. }
  42. bool TableViewSmooth::initWithViewSize(Size size, Node* container/* = nullptr*/)
  43. {
  44. if (ScrollViewSmooth::initWithViewSize(size,container))
  45. {
  46. CC_SAFE_DELETE(_indices);
  47. _indices = new (std::nothrow) std::set<ssize_t>();
  48. _vordering = VerticalFillOrder::BOTTOM_UP;
  49. this->setDirection(Direction::VERTICAL);
  50. ScrollViewSmooth::setDelegate(this);
  51. return true;
  52. }
  53. return false;
  54. }
  55. TableViewSmooth::TableViewSmooth()
  56. : _touchedCell(nullptr)
  57. , _indices(nullptr)
  58. , _dataSource(nullptr)
  59. , _tableViewDelegate(nullptr)
  60. , _oldDirection(Direction::NONE)
  61. , _isUsedCellsDirty(false)
  62. {
  63. }
  64. TableViewSmooth::~TableViewSmooth()
  65. {
  66. CC_SAFE_DELETE(_indices);
  67. }
  68. void TableViewSmooth::setVerticalFillOrder(VerticalFillOrder fillOrder)
  69. {
  70. if (_vordering != fillOrder)
  71. {
  72. _vordering = fillOrder;
  73. if (!_cellsUsed.empty())
  74. {
  75. this->reloadData();
  76. }
  77. }
  78. }
  79. TableViewSmooth::VerticalFillOrder TableViewSmooth::getVerticalFillOrder()
  80. {
  81. return _vordering;
  82. }
  83. void TableViewSmooth::reloadData()
  84. {
  85. _oldDirection = Direction::NONE;
  86. for(const auto &cell : _cellsUsed) {
  87. if(_tableViewDelegate != nullptr) {
  88. _tableViewDelegate->tableCellWillRecycle(this, cell);
  89. }
  90. _cellsFreed.pushBack(cell);
  91. cell->reset();
  92. if (cell->getParent() == this->getContainer())
  93. {
  94. this->getContainer()->removeChild(cell, true);
  95. }
  96. }
  97. _indices->clear();
  98. _cellsUsed.clear();
  99. this->_updateCellPositions();
  100. this->_updateContentSize();
  101. if (_dataSource->numberOfCellsInTableView(this) > 0)
  102. {
  103. this->scrollViewDidScroll(this);
  104. }
  105. }
  106. TableViewCell *TableViewSmooth::cellAtIndex(ssize_t idx)
  107. {
  108. if (_indices->find(idx) != _indices->end())
  109. {
  110. for (const auto& cell : _cellsUsed)
  111. {
  112. if (cell->getIdx() == idx)
  113. {
  114. return cell;
  115. }
  116. }
  117. }
  118. return nullptr;
  119. }
  120. void TableViewSmooth::updateCellAtIndex(ssize_t idx)
  121. {
  122. if (idx == CC_INVALID_INDEX)
  123. {
  124. return;
  125. }
  126. long countOfItems = _dataSource->numberOfCellsInTableView(this);
  127. if (0 == countOfItems || idx > countOfItems-1)
  128. {
  129. return;
  130. }
  131. TableViewCell* cell = this->cellAtIndex(idx);
  132. if (cell)
  133. {
  134. this->_moveCellOutOfSight(cell);
  135. }
  136. cell = _dataSource->tableCellAtIndex(this, idx);
  137. this->_setIndexForCell(idx, cell);
  138. this->_addCellIfNecessary(cell);
  139. }
  140. void TableViewSmooth::insertCellAtIndex(ssize_t idx)
  141. {
  142. if (idx == CC_INVALID_INDEX)
  143. {
  144. return;
  145. }
  146. long countOfItems = _dataSource->numberOfCellsInTableView(this);
  147. if (0 == countOfItems || idx > countOfItems-1)
  148. {
  149. return;
  150. }
  151. long newIdx = 0;
  152. auto cell = cellAtIndex(idx);
  153. if (cell)
  154. {
  155. newIdx = _cellsUsed.getIndex(cell);
  156. // Move all cells behind the inserted position
  157. for (long i = newIdx; i < _cellsUsed.size(); i++)
  158. {
  159. cell = _cellsUsed.at(i);
  160. this->_setIndexForCell(cell->getIdx()+1, cell);
  161. }
  162. }
  163. //insert a new cell
  164. cell = _dataSource->tableCellAtIndex(this, idx);
  165. this->_setIndexForCell(idx, cell);
  166. this->_addCellIfNecessary(cell);
  167. this->_updateCellPositions();
  168. this->_updateContentSize();
  169. }
  170. void TableViewSmooth::removeCellAtIndex(ssize_t idx)
  171. {
  172. if (idx == CC_INVALID_INDEX)
  173. {
  174. return;
  175. }
  176. long uCountOfItems = _dataSource->numberOfCellsInTableView(this);
  177. if (0 == uCountOfItems || idx > uCountOfItems-1)
  178. {
  179. return;
  180. }
  181. ssize_t newIdx = 0;
  182. TableViewCell* cell = this->cellAtIndex(idx);
  183. if (!cell)
  184. {
  185. return;
  186. }
  187. newIdx = _cellsUsed.getIndex(cell);
  188. //remove first
  189. this->_moveCellOutOfSight(cell);
  190. _indices->erase(idx);
  191. this->_updateCellPositions();
  192. for (ssize_t i = _cellsUsed.size()-1; i > newIdx; i--)
  193. {
  194. cell = _cellsUsed.at(i);
  195. this->_setIndexForCell(cell->getIdx()-1, cell);
  196. }
  197. }
  198. TableViewCell *TableViewSmooth::dequeueCell()
  199. {
  200. TableViewCell *cell;
  201. if (_cellsFreed.empty()) {
  202. cell = nullptr;
  203. } else {
  204. cell = _cellsFreed.at(0);
  205. cell->retain();
  206. _cellsFreed.erase(0);
  207. cell->autorelease();
  208. }
  209. return cell;
  210. }
  211. void TableViewSmooth::_addCellIfNecessary(TableViewCell * cell)
  212. {
  213. if (cell->getParent() != this->getContainer())
  214. {
  215. this->getContainer()->addChild(cell);
  216. }
  217. _cellsUsed.pushBack(cell);
  218. _indices->insert(cell->getIdx());
  219. _isUsedCellsDirty = true;
  220. }
  221. void TableViewSmooth::_updateContentSize()
  222. {
  223. Size size = Size::ZERO;
  224. ssize_t cellsCount = _dataSource->numberOfCellsInTableView(this);
  225. if (cellsCount > 0)
  226. {
  227. float maxPosition = _vCellsPositions[cellsCount];
  228. switch (this->getDirection())
  229. {
  230. case Direction::HORIZONTAL:
  231. size = Size(maxPosition, _viewSize.height);
  232. break;
  233. default:
  234. size = Size(_viewSize.width, maxPosition);
  235. break;
  236. }
  237. }
  238. this->setContentSize(size);
  239. if (_oldDirection != _direction)
  240. {
  241. if (_direction == Direction::HORIZONTAL)
  242. {
  243. this->setContentOffset(Vec2(0,0));
  244. }
  245. else
  246. {
  247. this->setContentOffset(Vec2(0,this->minContainerOffset().y));
  248. }
  249. _oldDirection = _direction;
  250. }
  251. }
  252. Vec2 TableViewSmooth::_offsetFromIndex(ssize_t index)
  253. {
  254. Vec2 offset = this->__offsetFromIndex(index);
  255. const Size cellSize = _dataSource->tableCellSizeForIndex(this, index);
  256. if (_vordering == VerticalFillOrder::TOP_DOWN)
  257. {
  258. offset.y = this->getContainer()->getContentSize().height - offset.y - cellSize.height;
  259. }
  260. return offset;
  261. }
  262. Vec2 TableViewSmooth::__offsetFromIndex(ssize_t index)
  263. {
  264. Vec2 offset;
  265. Size cellSize;
  266. switch (this->getDirection())
  267. {
  268. case Direction::HORIZONTAL:
  269. offset.set(_vCellsPositions[index], 0.0f);
  270. break;
  271. default:
  272. offset.set(0.0f, _vCellsPositions[index]);
  273. break;
  274. }
  275. return offset;
  276. }
  277. long TableViewSmooth::_indexFromOffset(Vec2 offset)
  278. {
  279. long index = 0;
  280. const long maxIdx = _dataSource->numberOfCellsInTableView(this) - 1;
  281. if (_vordering == VerticalFillOrder::TOP_DOWN)
  282. {
  283. offset.y = this->getContainer()->getContentSize().height - offset.y;
  284. }
  285. index = this->__indexFromOffset(offset);
  286. if (index != -1)
  287. {
  288. index = MAX(0, index);
  289. if (index > maxIdx)
  290. {
  291. index = CC_INVALID_INDEX;
  292. }
  293. }
  294. return index;
  295. }
  296. long TableViewSmooth::__indexFromOffset(Vec2 offset)
  297. {
  298. long low = 0;
  299. long high = _dataSource->numberOfCellsInTableView(this) - 1;
  300. float search;
  301. switch (this->getDirection())
  302. {
  303. case Direction::HORIZONTAL:
  304. search = offset.x;
  305. break;
  306. default:
  307. search = offset.y;
  308. break;
  309. }
  310. while (high >= low)
  311. {
  312. long index = low + (high - low) / 2;
  313. float cellStart = _vCellsPositions[index];
  314. float cellEnd = _vCellsPositions[index + 1];
  315. if (search >= cellStart && search <= cellEnd)
  316. {
  317. return index;
  318. }
  319. else if (search < cellStart)
  320. {
  321. high = index - 1;
  322. }
  323. else
  324. {
  325. low = index + 1;
  326. }
  327. }
  328. if (low <= 0) {
  329. return 0;
  330. }
  331. return -1;
  332. }
  333. void TableViewSmooth::_moveCellOutOfSight(TableViewCell *cell)
  334. {
  335. if(_tableViewDelegate != nullptr) {
  336. _tableViewDelegate->tableCellWillRecycle(this, cell);
  337. }
  338. _cellsFreed.pushBack(cell);
  339. _cellsUsed.eraseObject(cell);
  340. _isUsedCellsDirty = true;
  341. _indices->erase(cell->getIdx());
  342. cell->reset();
  343. if (cell->getParent() == this->getContainer())
  344. {
  345. this->getContainer()->removeChild(cell, true);;
  346. }
  347. }
  348. void TableViewSmooth::_setIndexForCell(ssize_t index, TableViewCell *cell)
  349. {
  350. cell->setAnchorPoint(Vec2(0.0f, 0.0f));
  351. cell->setPosition(this->_offsetFromIndex(index));
  352. cell->setIdx(index);
  353. }
  354. void TableViewSmooth::_updateCellPositions()
  355. {
  356. long cellsCount = _dataSource->numberOfCellsInTableView(this);
  357. _vCellsPositions.resize(cellsCount + 1, 0.0);
  358. if (cellsCount > 0)
  359. {
  360. float currentPos = 0;
  361. Size cellSize;
  362. for (int i=0; i < cellsCount; i++)
  363. {
  364. _vCellsPositions[i] = currentPos;
  365. cellSize = _dataSource->tableCellSizeForIndex(this, i);
  366. switch (this->getDirection())
  367. {
  368. case Direction::HORIZONTAL:
  369. currentPos += cellSize.width;
  370. break;
  371. default:
  372. currentPos += cellSize.height;
  373. break;
  374. }
  375. }
  376. _vCellsPositions[cellsCount] = currentPos;//1 extra value allows us to get right/bottom of the last cell
  377. }
  378. }
  379. void TableViewSmooth::scrollViewDidScroll(ScrollViewSmooth* view)
  380. {
  381. long countOfItems = _dataSource->numberOfCellsInTableView(this);
  382. if (0 == countOfItems)
  383. {
  384. return;
  385. }
  386. if (_isUsedCellsDirty)
  387. {
  388. _isUsedCellsDirty = false;
  389. std::sort(_cellsUsed.begin(), _cellsUsed.end(), [](TableViewCell *a, TableViewCell *b) -> bool{
  390. return a->getIdx() < b->getIdx();
  391. });
  392. }
  393. if(_tableViewDelegate != nullptr) {
  394. _tableViewDelegate->scrollViewDidScroll(this);
  395. }
  396. ssize_t startIdx = 0, endIdx = 0, idx = 0, maxIdx = 0;
  397. Vec2 offset = this->getContentOffset() * -1;
  398. maxIdx = MAX(countOfItems-1, 0);
  399. if (_vordering == VerticalFillOrder::TOP_DOWN)
  400. {
  401. offset.y = offset.y + _viewSize.height/this->getContainer()->getScaleY();
  402. }
  403. startIdx = this->_indexFromOffset(offset);
  404. if (startIdx == CC_INVALID_INDEX)
  405. {
  406. startIdx = countOfItems - 1;
  407. }
  408. if (_vordering == VerticalFillOrder::TOP_DOWN)
  409. {
  410. offset.y -= _viewSize.height/this->getContainer()->getScaleY();
  411. }
  412. else
  413. {
  414. offset.y += _viewSize.height/this->getContainer()->getScaleY();
  415. }
  416. offset.x += _viewSize.width/this->getContainer()->getScaleX();
  417. endIdx = this->_indexFromOffset(offset);
  418. if (endIdx == CC_INVALID_INDEX)
  419. {
  420. endIdx = countOfItems - 1;
  421. }
  422. #if 0 // For Testing.
  423. Ref* pObj;
  424. int i = 0;
  425. CCARRAY_FOREACH(_cellsUsed, pObj)
  426. {
  427. TableViewCell* pCell = static_cast<TableViewCell*>(pObj);
  428. log("cells Used index %d, value = %d", i, pCell->getIdx());
  429. i++;
  430. }
  431. log("---------------------------------------");
  432. i = 0;
  433. CCARRAY_FOREACH(_cellsFreed, pObj)
  434. {
  435. TableViewCell* pCell = static_cast<TableViewCell*>(pObj);
  436. log("cells freed index %d, value = %d", i, pCell->getIdx());
  437. i++;
  438. }
  439. log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  440. #endif
  441. if (!_cellsUsed.empty())
  442. {
  443. auto cell = _cellsUsed.at(0);
  444. idx = cell->getIdx();
  445. while(idx < startIdx)
  446. {
  447. this->_moveCellOutOfSight(cell);
  448. if (!_cellsUsed.empty())
  449. {
  450. cell = _cellsUsed.at(0);
  451. idx = cell->getIdx();
  452. }
  453. else
  454. {
  455. break;
  456. }
  457. }
  458. }
  459. if (!_cellsUsed.empty())
  460. {
  461. auto cell = _cellsUsed.back();
  462. idx = cell->getIdx();
  463. while(idx <= maxIdx && idx > endIdx)
  464. {
  465. this->_moveCellOutOfSight(cell);
  466. if (!_cellsUsed.empty())
  467. {
  468. cell = _cellsUsed.back();
  469. idx = cell->getIdx();
  470. }
  471. else
  472. {
  473. break;
  474. }
  475. }
  476. }
  477. for (long i = startIdx; i <= endIdx; i++)
  478. {
  479. if (_indices->find(i) != _indices->end())
  480. {
  481. continue;
  482. }
  483. this->updateCellAtIndex(i);
  484. }
  485. }
  486. void TableViewSmooth::onTouchEnded(Touch *pTouch, Event *pEvent)
  487. {
  488. if (!this->isVisible()) {
  489. return;
  490. }
  491. if (_touchedCell){
  492. Rect bb = this->getBoundingBox();
  493. bb.origin = _parent->convertToWorldSpace(bb.origin);
  494. if (bb.containsPoint(pTouch->getLocation()) && _tableViewDelegate != nullptr)
  495. {
  496. _tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
  497. _tableViewDelegate->tableCellTouched(this, _touchedCell);
  498. }
  499. _touchedCell = nullptr;
  500. }
  501. ScrollViewSmooth::onTouchEnded(pTouch, pEvent);
  502. }
  503. bool TableViewSmooth::onTouchBegan(Touch *pTouch, Event *pEvent)
  504. {
  505. for (Node *c = this; c != nullptr; c = c->getParent())
  506. {
  507. if (!c->isVisible())
  508. {
  509. return false;
  510. }
  511. }
  512. bool touchResult = ScrollViewSmooth::onTouchBegan(pTouch, pEvent);
  513. if(_touches.size() == 1)
  514. {
  515. long index;
  516. Vec2 point;
  517. point = this->getContainer()->convertTouchToNodeSpace(pTouch);
  518. index = this->_indexFromOffset(point);
  519. if (index == CC_INVALID_INDEX)
  520. {
  521. _touchedCell = nullptr;
  522. }
  523. else
  524. {
  525. _touchedCell = this->cellAtIndex(index);
  526. }
  527. if (_touchedCell && _tableViewDelegate != nullptr)
  528. {
  529. _tableViewDelegate->tableCellHighlight(this, _touchedCell);
  530. }
  531. }
  532. else if (_touchedCell)
  533. {
  534. if(_tableViewDelegate != nullptr)
  535. {
  536. _tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
  537. }
  538. _touchedCell = nullptr;
  539. }
  540. return touchResult;
  541. }
  542. void TableViewSmooth::onTouchMoved(Touch *pTouch, Event *pEvent)
  543. {
  544. ScrollViewSmooth::onTouchMoved(pTouch, pEvent);
  545. if (_touchedCell && isTouchMoved())
  546. {
  547. if(_tableViewDelegate != nullptr)
  548. {
  549. _tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
  550. }
  551. _touchedCell = nullptr;
  552. }
  553. }
  554. void TableViewSmooth::onTouchCancelled(Touch *pTouch, Event *pEvent)
  555. {
  556. ScrollViewSmooth::onTouchCancelled(pTouch, pEvent);
  557. if (_touchedCell)
  558. {
  559. if(_tableViewDelegate != nullptr)
  560. {
  561. _tableViewDelegate->tableCellUnhighlight(this, _touchedCell);
  562. }
  563. _touchedCell = nullptr;
  564. }
  565. }
  566. NS_CC_EXT_END