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
|
|
|
|
|
|
|
#ifndef __CCTABLEVIEW_H__
|
|
|
|
#define __CCTABLEVIEW_H__
|
|
|
|
|
|
|
|
#include "CCScrollView.h"
|
|
|
|
#include "CCTableViewCell.h"
|
|
|
|
|
|
|
|
#include <set>
|
2013-04-04 22:57:43 +08:00
|
|
|
#include <vector>
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
class TableView;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sole purpose of this delegate is to single touch event in this version.
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
class TableViewDelegate : public ScrollViewDelegate
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Delegate to respond touch event
|
|
|
|
*
|
|
|
|
* @param table table contains the given cell
|
|
|
|
* @param cell cell that is touched
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-09-09 22:34:32 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void tableCellTouched(TableView* table, TableViewCell* cell) = 0;
|
2012-12-05 04:46:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate to respond a table cell press event.
|
|
|
|
*
|
|
|
|
* @param table table contains the given cell
|
|
|
|
* @param cell cell that is pressed
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-12-05 04:46:27 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void tableCellHighlight(TableView* table, TableViewCell* cell){};
|
2012-12-05 04:46:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delegate to respond a table cell release event
|
|
|
|
*
|
|
|
|
* @param table table contains the given cell
|
|
|
|
* @param cell cell that is pressed
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-12-05 04:46:27 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void tableCellUnhighlight(TableView* table, TableViewCell* cell){};
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-01-24 03:04:49 +08:00
|
|
|
/**
|
|
|
|
* Delegate called when the cell is about to be recycled. Immediately
|
|
|
|
* after this call the cell will be removed from the scene graph and
|
|
|
|
* recycled.
|
|
|
|
*
|
|
|
|
* @param table table contains the given cell
|
|
|
|
* @param cell cell that is pressed
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2013-01-24 03:04:49 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual void tableCellWillRecycle(TableView* table, TableViewCell* cell){};
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Data source that governs table backend data.
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
class TableViewDataSource
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual ~TableViewDataSource() {}
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
/**
|
|
|
|
* cell size for a given index
|
|
|
|
*
|
|
|
|
* @param idx the index of a cell to get a size
|
|
|
|
* @return size of a cell at given index
|
|
|
|
*/
|
2013-11-18 15:55:58 +08:00
|
|
|
virtual Size tableCellSizeForIndex(TableView *table, long idx) {
|
2013-04-04 22:57:43 +08:00
|
|
|
return cellSizeForTable(table);
|
|
|
|
};
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* cell height for a given table.
|
|
|
|
*
|
|
|
|
* @param table table to hold the instances of Class
|
|
|
|
* @return cell size
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
virtual Size cellSizeForTable(TableView *table) {
|
2013-07-12 14:47:36 +08:00
|
|
|
return Size::ZERO;
|
2013-04-04 22:57:43 +08:00
|
|
|
};
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* a cell instance at a given index
|
|
|
|
*
|
|
|
|
* @param idx index to search for a cell
|
|
|
|
* @return cell found at idx
|
|
|
|
*/
|
2013-11-18 15:55:58 +08:00
|
|
|
virtual TableViewCell* tableCellAtIndex(TableView *table, long idx) = 0;
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* Returns number of cells in a given table view.
|
2013-04-10 17:13:19 +08:00
|
|
|
*
|
2012-09-09 22:34:32 +08:00
|
|
|
* @return number of cells
|
|
|
|
*/
|
2013-11-18 15:55:58 +08:00
|
|
|
virtual long numberOfCellsInTableView(TableView *table) = 0;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2013-08-09 02:16:52 +08:00
|
|
|
* UITableView support for cocos2d-x.
|
2013-04-10 17:13:19 +08:00
|
|
|
*
|
2013-08-09 02:16:52 +08:00
|
|
|
* This is a very basic, minimal implementation to bring UITableView-like component into cocos2d world.
|
2012-09-09 22:34:32 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
class TableView : public ScrollView, public ScrollViewDelegate
|
2012-09-09 22:34:32 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-07-26 22:55:41 +08:00
|
|
|
|
|
|
|
enum class VerticalFillOrder
|
|
|
|
{
|
|
|
|
TOP_DOWN,
|
|
|
|
BOTTOM_UP
|
|
|
|
};
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* An intialized table view object
|
|
|
|
*
|
|
|
|
* @param dataSource data source
|
|
|
|
* @param size view size
|
|
|
|
* @return table view
|
2013-09-13 11:41:20 +08:00
|
|
|
* @code
|
|
|
|
* when this function bound to js or lua,the input params are changed
|
|
|
|
* in js:var create(var jsObject,var size)
|
|
|
|
* in lua:local create(var size)
|
|
|
|
* in lua:
|
|
|
|
* @endcode
|
2012-09-09 22:34:32 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
static TableView* create(TableViewDataSource* dataSource, Size size);
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* An initialized table view object
|
|
|
|
*
|
|
|
|
* @param dataSource data source;
|
|
|
|
* @param size view size
|
|
|
|
* @param container parent object for cells
|
|
|
|
* @return table view
|
2013-09-13 11:41:20 +08:00
|
|
|
* @code
|
|
|
|
* when this function bound to js or lua,the input params are changed
|
|
|
|
* in js:var create(var jsObject,var size,var container)
|
|
|
|
* in lua:local create(var size, var container)
|
|
|
|
* in lua:
|
|
|
|
* @endcode
|
2012-09-09 22:34:32 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
static TableView* create(TableViewDataSource* dataSource, Size size, Node *container);
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-07-19 07:30:19 +08:00
|
|
|
TableView();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-19 07:30:19 +08:00
|
|
|
virtual ~TableView();
|
|
|
|
|
|
|
|
bool initWithViewSize(Size size, Node* container = NULL);
|
|
|
|
|
2012-09-10 13:39:12 +08:00
|
|
|
/**
|
|
|
|
* data source
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-09-10 13:39:12 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewDataSource* getDataSource() { return _dataSource; }
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* when this function bound to js or lua,the input params are changed
|
|
|
|
* in js:var setDataSource(var jsSource)
|
|
|
|
* in lua:local setDataSource()
|
|
|
|
* @endcode
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
void setDataSource(TableViewDataSource* source) { _dataSource = source; }
|
2012-09-10 13:39:12 +08:00
|
|
|
/**
|
|
|
|
* delegate
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-09-10 13:39:12 +08:00
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewDelegate* getDelegate() { return _tableViewDelegate; }
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @code
|
|
|
|
* when this function bound to js or lua,the input params are changed
|
|
|
|
* in js:var setDelegate(var jsDelegate)
|
|
|
|
* in lua:local setDelegate()
|
|
|
|
* @endcode
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
void setDelegate(TableViewDelegate* pDelegate) { _tableViewDelegate = pDelegate; }
|
2012-09-10 13:39:12 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* determines how cell is ordered and filled in the view.
|
|
|
|
*/
|
2013-07-26 22:55:41 +08:00
|
|
|
void setVerticalFillOrder(VerticalFillOrder order);
|
|
|
|
VerticalFillOrder getVerticalFillOrder();
|
2012-09-10 13:39:12 +08:00
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* Updates the content of the cell at a given index.
|
|
|
|
*
|
|
|
|
* @param idx index to find a cell
|
|
|
|
*/
|
2013-11-18 15:55:58 +08:00
|
|
|
void updateCellAtIndex(long idx);
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* Inserts a new cell at a given index
|
|
|
|
*
|
|
|
|
* @param idx location to insert
|
|
|
|
*/
|
2013-11-18 15:55:58 +08:00
|
|
|
void insertCellAtIndex(long idx);
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* Removes a cell at a given index
|
|
|
|
*
|
|
|
|
* @param idx index to find a cell
|
|
|
|
*/
|
2013-11-18 15:55:58 +08:00
|
|
|
void removeCellAtIndex(long idx);
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* reloads data from data source. the view will be refreshed.
|
|
|
|
*/
|
|
|
|
void reloadData();
|
|
|
|
/**
|
|
|
|
* Dequeues a free cell if available. nil if not.
|
|
|
|
*
|
|
|
|
* @return free cell
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell *dequeueCell();
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an existing cell at a given index. Returns nil if a cell is nonexistent at the moment of query.
|
|
|
|
*
|
|
|
|
* @param idx index
|
|
|
|
* @return a cell at a given index
|
|
|
|
*/
|
2013-11-18 15:55:58 +08:00
|
|
|
TableViewCell *cellAtIndex(long idx);
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
// Overrides
|
|
|
|
virtual void scrollViewDidScroll(ScrollView* view) override;
|
|
|
|
virtual void scrollViewDidZoom(ScrollView* view) override {}
|
2013-09-03 18:22:03 +08:00
|
|
|
virtual bool onTouchBegan(Touch *pTouch, Event *pEvent) override;
|
|
|
|
virtual void onTouchMoved(Touch *pTouch, Event *pEvent) override;
|
|
|
|
virtual void onTouchEnded(Touch *pTouch, Event *pEvent) override;
|
|
|
|
virtual void onTouchCancelled(Touch *pTouch, Event *pEvent) override;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
protected:
|
2013-12-07 14:17:47 +08:00
|
|
|
long __indexFromOffset(Point offset);
|
|
|
|
long _indexFromOffset(Point offset);
|
|
|
|
Point __offsetFromIndex(long index);
|
|
|
|
Point _offsetFromIndex(long index);
|
|
|
|
|
|
|
|
void _moveCellOutOfSight(TableViewCell *cell);
|
|
|
|
void _setIndexForCell(long index, TableViewCell *cell);
|
|
|
|
void _addCellIfNecessary(TableViewCell * cell);
|
|
|
|
|
|
|
|
void _updateCellPositions();
|
|
|
|
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewCell *_touchedCell;
|
2012-09-10 13:39:12 +08:00
|
|
|
/**
|
|
|
|
* vertical direction of cell filling
|
|
|
|
*/
|
2013-07-26 22:55:41 +08:00
|
|
|
VerticalFillOrder _vordering;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* index set to query the indexes of the cells used.
|
|
|
|
*/
|
2013-11-18 15:55:58 +08:00
|
|
|
std::set<long>* _indices;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
/**
|
|
|
|
* vector with all cell positions
|
|
|
|
*/
|
2013-06-15 14:03:30 +08:00
|
|
|
std::vector<float> _vCellsPositions;
|
2012-09-09 22:34:32 +08:00
|
|
|
//NSMutableIndexSet *indices_;
|
|
|
|
/**
|
|
|
|
* cells that are currently in the table
|
|
|
|
*/
|
2013-12-07 14:17:47 +08:00
|
|
|
Vector<TableViewCell*> _cellsUsed;
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* free list of cells
|
|
|
|
*/
|
2013-12-07 14:17:47 +08:00
|
|
|
Vector<TableViewCell*> _cellsFreed;
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* weak link to the data source object
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewDataSource* _dataSource;
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* weak link to the delegate object
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewDelegate* _tableViewDelegate;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
Direction _oldDirection;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-12-07 14:17:47 +08:00
|
|
|
bool _isUsedCellsDirty;
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-02-21 10:06:22 +08:00
|
|
|
public:
|
|
|
|
void _updateContentSize();
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_EXT_END
|
|
|
|
|
|
|
|
#endif /* __CCTABLEVIEW_H__ */
|