TableView updates to provide more touch input to CCTableViewDelegate

This commit is contained in:
Ed Bartley 2012-11-02 20:26:43 -04:00
parent f424da2a7e
commit 4ec9793769
2 changed files with 60 additions and 7 deletions

View File

@ -72,6 +72,7 @@ CCTableView::CCTableView()
, m_pCellsFreed(NULL)
, m_pDataSource(NULL)
, m_pTableViewDelegate(NULL)
, m_pTouchedCell(NULL)
, m_eOldDirection(kCCScrollViewDirectionNone)
{
@ -486,24 +487,69 @@ void CCTableView::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
if (!this->isVisible()) {
return;
}
if (m_pTouches->count() == 1 && !this->isTouchMoved()) {
if (m_pTouchedCell) {
m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell);
m_pTableViewDelegate->tableCellTouched(this, m_pTouchedCell);
m_pTouchedCell = NULL;
}
CCScrollView::ccTouchEnded(pTouch, pEvent);
}
bool CCTableView::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
if (!this->isVisible()) {
return false;
}
bool touchResult = CCScrollView::ccTouchBegan(pTouch, pEvent);
if(m_pTouches->count() == 1) {
unsigned int index;
CCTableViewCell *cell;
CCPoint point;
point = this->getContainer()->convertTouchToNodeSpace(pTouch);
if (m_eVordering == kCCTableViewFillTopDown) {
CCSize cellSize = m_pDataSource->cellSizeForTable(this);
point.y -= cellSize.height;
}
index = this->_indexFromOffset(point);
cell = this->_cellWithIndex(index);
if (cell) {
m_pTableViewDelegate->tableCellTouched(this, cell);
index = this->_indexFromOffset(point);
m_pTouchedCell = this->_cellWithIndex(index);
if (m_pTouchedCell) {
m_pTableViewDelegate->tableCellHighlight(this, m_pTouchedCell);
}
}
CCScrollView::ccTouchEnded(pTouch, pEvent);
else if(m_pTouchedCell) {
m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell);
m_pTouchedCell = NULL;
}
return touchResult;
}
void CCTableView::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)
{
CCScrollView::ccTouchMoved(pTouch, pEvent);
if (m_pTouchedCell && isTouchMoved()) {
m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell);
m_pTouchedCell = NULL;
}
}
void CCTableView::ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent)
{
CCScrollView::ccTouchCancelled(pTouch, pEvent);
if (m_pTouchedCell) {
m_pTableViewDelegate->tableCellUnhighlight(this, m_pTouchedCell);
m_pTouchedCell = NULL;
}
}
NS_CC_EXT_END

View File

@ -54,6 +54,8 @@ public:
* @param cell cell that is touched
*/
virtual void tableCellTouched(CCTableView* table, CCTableViewCell* cell) = 0;
virtual void tableCellHighlight(CCTableView* table, CCTableViewCell* cell){};
virtual void tableCellUnhighlight(CCTableView* table, CCTableViewCell* cell){};
};
@ -176,10 +178,15 @@ public:
virtual void scrollViewDidScroll(CCScrollView* view);
virtual void scrollViewDidZoom(CCScrollView* view) {}
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
protected:
CCTableViewCell *m_pTouchedCell;
/**
* vertical direction of cell filling
*/