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;
|
|
|
|
class ArrayForObjectSorting;
|
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-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-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-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-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-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-06-20 14:15:53 +08:00
|
|
|
virtual Size tableCellSizeForIndex(TableView *table, unsigned int 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-06-20 14:15:53 +08:00
|
|
|
virtual TableViewCell* tableCellAtIndex(TableView *table, unsigned int 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-06-20 14:15:53 +08:00
|
|
|
virtual unsigned int 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-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-06-20 14:15:53 +08:00
|
|
|
static TableView* create(TableViewDataSource* dataSource, Size size, Node *container);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-07-19 07:30:19 +08:00
|
|
|
TableView();
|
|
|
|
virtual ~TableView();
|
|
|
|
|
|
|
|
bool initWithViewSize(Size size, Node* container = NULL);
|
|
|
|
|
2012-09-10 13:39:12 +08:00
|
|
|
/**
|
|
|
|
* data source
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewDataSource* getDataSource() { return _dataSource; }
|
|
|
|
void setDataSource(TableViewDataSource* source) { _dataSource = source; }
|
2012-09-10 13:39:12 +08:00
|
|
|
/**
|
|
|
|
* delegate
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
TableViewDelegate* getDelegate() { return _tableViewDelegate; }
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
void updateCellAtIndex(unsigned int idx);
|
|
|
|
/**
|
|
|
|
* Inserts a new cell at a given index
|
|
|
|
*
|
|
|
|
* @param idx location to insert
|
|
|
|
*/
|
|
|
|
void insertCellAtIndex(unsigned int idx);
|
|
|
|
/**
|
|
|
|
* Removes a cell at a given index
|
|
|
|
*
|
|
|
|
* @param idx index to find a cell
|
|
|
|
*/
|
|
|
|
void removeCellAtIndex(unsigned int idx);
|
|
|
|
/**
|
|
|
|
* 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-06-20 14:15:53 +08:00
|
|
|
TableViewCell *cellAtIndex(unsigned int 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 {}
|
|
|
|
virtual bool ccTouchBegan(Touch *pTouch, Event *pEvent) override;
|
|
|
|
virtual void ccTouchMoved(Touch *pTouch, Event *pEvent) override;
|
|
|
|
virtual void ccTouchEnded(Touch *pTouch, Event *pEvent) override;
|
|
|
|
virtual void ccTouchCancelled(Touch *pTouch, Event *pEvent) override;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
|
|
|
protected:
|
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-06-15 14:03:30 +08:00
|
|
|
std::set<unsigned int>* _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-06-20 14:15:53 +08:00
|
|
|
ArrayForObjectSorting* _cellsUsed;
|
2012-09-09 22:34:32 +08:00
|
|
|
/**
|
|
|
|
* free list of cells
|
|
|
|
*/
|
2013-06-20 14:15:53 +08:00
|
|
|
ArrayForObjectSorting* _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-07-26 22:55:41 +08:00
|
|
|
Direction _oldDirection;
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
int __indexFromOffset(Point offset);
|
|
|
|
unsigned int _indexFromOffset(Point offset);
|
|
|
|
Point __offsetFromIndex(unsigned int index);
|
|
|
|
Point _offsetFromIndex(unsigned int index);
|
2012-09-09 22:34:32 +08:00
|
|
|
|
2013-06-20 14:15:53 +08:00
|
|
|
void _moveCellOutOfSight(TableViewCell *cell);
|
|
|
|
void _setIndexForCell(unsigned int index, TableViewCell *cell);
|
|
|
|
void _addCellIfNecessary(TableViewCell * cell);
|
2013-04-10 17:13:19 +08:00
|
|
|
|
2013-04-04 22:57:43 +08:00
|
|
|
void _updateCellPositions();
|
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__ */
|