axmol/extensions/GUI/CCScrollView/CCTableView.h

337 lines
8.9 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2012 cocos2d-x.org
Copyright (c) 2010 Sangwoo Im
http://www.cocos2d-x.org
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.
****************************************************************************/
#ifndef __CCTABLEVIEW_H__
#define __CCTABLEVIEW_H__
#include "CCScrollView.h"
#include "CCTableViewCell.h"
#include "extensions/ExtensionExport.h"
#include <set>
#include <vector>
2015-03-26 15:47:14 +08:00
/**
* @addtogroup ui
* @{
*/
NS_CC_EXT_BEGIN
class TableView;
/**
* Sole purpose of this delegate is to single touch event in this version.
*/
class CC_EX_DLL TableViewDelegate : public ScrollViewDelegate
{
public:
/**
* Delegate to respond touch event
*
* @param table table contains the given cell
* @param cell cell that is touched
* @js NA
* @lua NA
*/
virtual void tableCellTouched(TableView* table, TableViewCell* cell) = 0;
/**
* Delegate to respond a table cell press event.
*
* @param table table contains the given cell
* @param cell cell that is pressed
* @js NA
* @lua NA
*/
virtual void tableCellHighlight(TableView* table, TableViewCell* cell);
/**
* Delegate to respond a table cell release event
*
* @param table table contains the given cell
* @param cell cell that is pressed
* @js NA
* @lua NA
*/
virtual void tableCellUnhighlight(TableView* table, TableViewCell* cell);
/**
* 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
* @js NA
* @lua NA
*/
virtual void tableCellWillRecycle(TableView* table, TableViewCell* cell);
};
/**
* Data source that governs table backend data.
*/
class CC_EX_DLL TableViewDataSource
{
public:
/**
* @js NA
* @lua NA
*/
virtual ~TableViewDataSource() {}
/**
* 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
*/
virtual Size tableCellSizeForIndex(TableView* table, ssize_t idx);
/**
* cell height for a given table.
*
* @param table table to hold the instances of Class
* @return cell size
*/
virtual Size cellSizeForTable(TableView* table);
/**
* a cell instance at a given index
*
* @param idx index to search for a cell
* @return cell found at idx
*/
2013-12-12 12:07:20 +08:00
virtual TableViewCell* tableCellAtIndex(TableView *table, ssize_t idx) = 0;
/**
* Returns number of cells in a given table view.
*
* @return number of cells
*/
virtual ssize_t numberOfCellsInTableView(TableView *table) = 0;
};
/**
* UITableView support for cocos2d-x.
*
* This is a very basic, minimal implementation to bring UITableView-like component into cocos2d world.
*/
class CC_EX_DLL TableView : public ScrollView, public ScrollViewDelegate
{
public:
enum class VerticalFillOrder
{
TOP_DOWN,
BOTTOM_UP
};
2015-10-09 16:59:11 +08:00
/** Empty constructor of TableView */
static TableView* create();
/**
2015-10-09 16:59:11 +08:00
* An initialized table view object
*
* @param dataSource data source
* @param size view size
* @return table view
* @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
*/
static TableView* create(TableViewDataSource* dataSource, Size size);
/**
* An initialized table view object
*
* @param dataSource data source;
* @param size view size
* @param container parent object for cells
* @return table view
* @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
*/
static TableView* create(TableViewDataSource* dataSource, Size size, Node *container);
/**
* @js ctor
2015-03-30 15:47:47 +08:00
* @lua new
*/
TableView();
/**
* @js NA
* @lua NA
*/
virtual ~TableView();
bool initWithViewSize(Size size, Node* container = NULL);
/**
* data source
* @js NA
* @lua NA
*/
TableViewDataSource* getDataSource() { return _dataSource; }
/**
* when this function bound to js or lua,the input params are changed
* in js:var setDataSource(var jsSource)
* in lua:local setDataSource()
* @endcode
*/
void setDataSource(TableViewDataSource* source) { _dataSource = source; }
/**
* delegate
* @js NA
* @lua NA
*/
TableViewDelegate* getDelegate() { return _tableViewDelegate; }
/**
* @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
*/
void setDelegate(TableViewDelegate* pDelegate) { _tableViewDelegate = pDelegate; }
/**
* determines how cell is ordered and filled in the view.
*/
void setVerticalFillOrder(VerticalFillOrder order);
VerticalFillOrder getVerticalFillOrder();
/**
* Updates the content of the cell at a given index.
*
* @param idx index to find a cell
*/
2013-12-12 12:07:20 +08:00
void updateCellAtIndex(ssize_t idx);
/**
* Inserts a new cell at a given index
*
* @param idx location to insert
*/
2013-12-12 12:07:20 +08:00
void insertCellAtIndex(ssize_t idx);
/**
* Removes a cell at a given index
*
* @param idx index to find a cell
*/
2013-12-12 12:07:20 +08:00
void removeCellAtIndex(ssize_t 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
*/
TableViewCell *dequeueCell();
/**
* 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-12-12 12:07:20 +08:00
TableViewCell *cellAtIndex(ssize_t idx);
// Overrides
virtual void scrollViewDidScroll(ScrollView* view) override;
virtual void scrollViewDidZoom(ScrollView* view) override {}
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;
protected:
Squashed commit of the following: commit a9572b8913f3a38b59adbd7b4017ab9848a6b2b5 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Wed May 14 10:03:44 2014 -0700 math renames `Vector2` -> `Vec2` `Vector3` -> `Vec3` `Vector4` -> `Vec4` `Matrix` -> `Mat4` commit 4e107f4bd854c26bfceb52b063d6bd9cea02d6a3 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:24:28 2014 -0700 raw version of rename Vector3 commit 1d115573ebe96a5fc815fa44fbe6417ea7dba841 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:07:14 2014 -0700 rename Vector2 after merge commit ab2ed58c129dbc30a4c0970ed94568c5d271657b Merge: 1978d2d 86fb75a Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 09:05:30 2014 -0700 Merge branch 'v3' into v3_renameMathClassName Conflicts: tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UIButtonTest/UIButtonTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UICheckBoxTest/UICheckBoxTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UISliderTest/UISliderTest_Editor.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest.cpp tests/cpp-tests/Classes/UITest/CocoStudioGUITest/UITextFieldTest/UITextFieldTest_Editor.cpp commit 1978d2d174877172ccddc083020a1bbf43ad3b39 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 08:51:45 2014 -0700 rename vector2 in tests/cpp-empty-test folder commit d4e0ff13dcce62724d2fece656543f26aa28e467 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:58:23 2014 -0700 rename vector2 in tests/cpp-tests cpp files commit be50ca2ec75e0fd32a6fcdaa15fe1ebb4cafe79f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:52:57 2014 -0700 rename vector2 in tests/cpp-tests head files commit 6daef564400d4e28c4ce20859a68e0f583fed125 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:49:48 2014 -0700 rename vector2 in extension folder commit 8f3f0f65ceea92c9e7a0d87ab54e62220c5572e2 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:47:22 2014 -0700 rename vector2 in cocos/2d cpp files commit e1f3105aae06d595661a3030f519f7cc13aefbed Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:44:39 2014 -0700 rename vector2 in cocos/2d head files commit 6708d890bfe486109120c3cd4b9fe5c078b7108f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:59 2014 -0700 rename vector2 in cocos/base folder commit d3978fa5447c31ea2f3ece5469b7e746dfba4248 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:43 2014 -0700 rename vector2 in cocos/deprecated folder commit 4bff45139363d6b9706edbbcf9f322d48b4fd019 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:40:26 2014 -0700 rename vector2 in cocos/editor-support folder commit 353d244c995f8b5d14f635c52aed8bc5e5fc1a6f Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:36:48 2014 -0700 rename vector2 in cocos/ui folder commit 758b8f4d513084b9922d7242e9b8f2c7f316de6c Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:39 2014 -0700 rename vector2 in cocos/renderer folder commit 0bd2710dd8714cecb993880bc37affd9ecb05c27 Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:32:15 2014 -0700 rename vector2 in cocos/physics folder commit b7f0581c4587348bdbc1478d5374c2325735f21d Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:25:01 2014 -0700 rename vector2 in cocos/math folder commit a8631a8e1a4e2740807ccd9be9d70de6ecaad7dd Author: Huabing.Xu <dabingnn@gmail.com> Date: Wed May 14 00:16:55 2014 -0700 rename Vector2 to Vec2 deprecate typedef Vector2
2014-05-15 01:07:09 +08:00
long __indexFromOffset(Vec2 offset);
long _indexFromOffset(Vec2 offset);
Vec2 __offsetFromIndex(ssize_t index);
Vec2 _offsetFromIndex(ssize_t index);
void _moveCellOutOfSight(TableViewCell *cell);
2013-12-12 12:07:20 +08:00
void _setIndexForCell(ssize_t index, TableViewCell *cell);
void _addCellIfNecessary(TableViewCell * cell);
void _updateCellPositions();
TableViewCell *_touchedCell;
/**
* vertical direction of cell filling
*/
VerticalFillOrder _vordering;
/**
* index set to query the indexes of the cells used.
*/
2013-12-12 12:07:20 +08:00
std::set<ssize_t>* _indices;
/**
* vector with all cell positions
*/
std::vector<float> _vCellsPositions;
//NSMutableIndexSet *indices_;
/**
* cells that are currently in the table
*/
Vector<TableViewCell*> _cellsUsed;
/**
* free list of cells
*/
Vector<TableViewCell*> _cellsFreed;
/**
* weak link to the data source object
*/
TableViewDataSource* _dataSource;
/**
* weak link to the delegate object
*/
TableViewDelegate* _tableViewDelegate;
Direction _oldDirection;
bool _isUsedCellsDirty;
public:
void _updateContentSize();
};
NS_CC_EXT_END
2015-03-26 15:47:14 +08:00
// end of ui group
/// @}
#endif /* __CCTABLEVIEW_H__ */