axmol/extensions/GUI/CCScrollView/CCTableView.h

232 lines
6.4 KiB
C
Raw Normal View History

//
// SWTableView.h
// SWGameLib
//
// Copyright (c) 2012 cocos2d-x.org
// Copyright (c) 2010 Sangwoo Im
//
// 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.
//
//
// Created by Sangwoo Im on 6/3/10.
// Copyright 2010 Sangwoo Im. All rights reserved.
//
#ifndef __CCTABLEVIEW_H__
#define __CCTABLEVIEW_H__
#include "CCScrollView.h"
#include "CCTableViewCell.h"
#include <set>
NS_CC_EXT_BEGIN
class CCTableView;
class CCArrayForObjectSorting;
typedef enum {
CCTableViewFillTopDown,
CCTableViewFillBottomUp
} CCTableViewVerticalFillOrder;
/**
* Sole purpose of this delegate is to single touch event in this version.
*/
class CCTableViewDelegate : public CCScrollViewDelegate
{
public:
/**
* Delegate to respond touch event
*
* @param table table contains the given cell
* @param cell cell that is touched
*/
virtual void table_cellTouched(CCTableView* table, CCTableViewCell* cell) = 0;
};
/**
* Data source that governs table backend data.
*/
class CCTableViewDataSource
{
public:
/**
* cell height for a given table.
*
* @param table table to hold the instances of Class
* @return cell size
*/
virtual CCSize cellSizeForTable(CCTableView *table) = 0;
/**
* a cell instance at a given index
*
* @param idx index to search for a cell
* @return cell found at idx
*/
virtual CCTableViewCell* tableCellAtIndex(CCTableView *table, unsigned int idx) = 0;
/**
* Returns number of cells in a given table view.
*
* @return number of cells
*/
virtual unsigned int numberOfCellsInTableView(CCTableView *table) = 0;
};
/**
* UITableView counterpart for cocos2d for iphone.
*
* this is a very basic, minimal implementation to bring UITableView-like component into cocos2d world.
*
*/
class CCTableView : public CCScrollView, public CCScrollViewDelegate
{
public:
CCTableView();
virtual ~CCTableView();
/**
* vertical direction of cell filling
*/
CCTableViewVerticalFillOrder vordering_;
/**
* data source
*/
CCTableViewDataSource* getDataSource() { return dataSource_; }
void setDataSource(CCTableViewDataSource* source) { dataSource_ = source; }
/**
* delegate
*/
CCTableViewDelegate* getDelegate() { return tDelegate_; }
void setDelegate(CCTableViewDelegate* pDelegate) { tDelegate_ = pDelegate; }
/**
* determines how cell is ordered and filled in the view.
*/
void setVerticalFillOrder(CCTableViewVerticalFillOrder order);
CCTableViewVerticalFillOrder getVerticalFillOrder();
/**
* An intialized table view object
*
* @param dataSource data source
* @param size view size
* @return table view
*/
static CCTableView* viewWithDataSource(CCTableViewDataSource* dataSource, CCSize size);
/**
* An initialized table view object
*
* @param dataSource data source;
* @param size view size
* @param container parent object for cells
* @return table view
*/
static CCTableView* viewWithDataSource(CCTableViewDataSource* dataSource, CCSize size, CCNode *container);
bool initWithViewSize(CCSize size, CCNode* container = NULL);
/**
* 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
*/
CCTableViewCell *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
*/
CCTableViewCell *cellAtIndex(unsigned int idx);
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view) {}
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
protected:
/**
* index set to query the indexes of the cells used.
*/
std::set<unsigned int>* indices_;
//NSMutableIndexSet *indices_;
/**
* cells that are currently in the table
*/
CCArrayForObjectSorting *cellsUsed_;
/**
* free list of cells
*/
CCArrayForObjectSorting *cellsFreed_;
/**
* weak link to the data source object
*/
CCTableViewDataSource* dataSource_;
/**
* weak link to the delegate object
*/
CCTableViewDelegate* tDelegate_;
CCScrollViewDirection m_eOldDirection;
int __indexFromOffset(CCPoint offset);
unsigned int _indexFromOffset(CCPoint offset);
CCPoint __offsetFromIndex(unsigned int index);
CCPoint _offsetFromIndex(unsigned int index);
void _updateContentSize();
CCTableViewCell *_cellWithIndex(unsigned int cellIndex);
void _moveCellOutOfSight(CCTableViewCell *cell);
void _setIndexForCell(unsigned int index, CCTableViewCell *cell);
void _addCellIfNecessary(CCTableViewCell * cell);
};
NS_CC_EXT_END
#endif /* __CCTABLEVIEW_H__ */