From 4ec979376957493c0df5eb759a18edbf68026de0 Mon Sep 17 00:00:00 2001 From: Ed Bartley Date: Fri, 2 Nov 2012 20:26:43 -0400 Subject: [PATCH] TableView updates to provide more touch input to CCTableViewDelegate --- extensions/GUI/CCScrollView/CCTableView.cpp | 60 ++++++++++++++++++--- extensions/GUI/CCScrollView/CCTableView.h | 7 +++ 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/extensions/GUI/CCScrollView/CCTableView.cpp b/extensions/GUI/CCScrollView/CCTableView.cpp index 0e970d1577..b0a77ea1d6 100644 --- a/extensions/GUI/CCScrollView/CCTableView.cpp +++ b/extensions/GUI/CCScrollView/CCTableView.cpp @@ -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 diff --git a/extensions/GUI/CCScrollView/CCTableView.h b/extensions/GUI/CCScrollView/CCTableView.h index 3aa6f32a28..0faded6043 100644 --- a/extensions/GUI/CCScrollView/CCTableView.h +++ b/extensions/GUI/CCScrollView/CCTableView.h @@ -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 */