2012-04-25 18:17:04 +08:00
|
|
|
#include "MutiTouchTest.h"
|
|
|
|
|
|
|
|
|
|
|
|
static ccColor3B s_TouchColors[CC_MAX_TOUCHES] = {
|
|
|
|
ccYELLOW,
|
|
|
|
ccBLUE,
|
|
|
|
ccGREEN,
|
|
|
|
ccRED,
|
|
|
|
ccMAGENTA
|
|
|
|
};
|
|
|
|
|
|
|
|
class TouchPoint : public CCNode
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TouchPoint()
|
|
|
|
{
|
|
|
|
setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTextureColor));
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void draw()
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
ccDrawColor4B(_touchColor.r, _touchColor.g, _touchColor.b, 255);
|
2012-04-25 18:17:04 +08:00
|
|
|
glLineWidth(10);
|
2013-06-15 14:03:30 +08:00
|
|
|
ccDrawLine( ccp(0, _touchPoint.y), ccp(getContentSize().width, _touchPoint.y) );
|
|
|
|
ccDrawLine( ccp(_touchPoint.x, 0), ccp(_touchPoint.x, getContentSize().height) );
|
2012-04-25 18:17:04 +08:00
|
|
|
glLineWidth(1);
|
|
|
|
ccPointSize(30);
|
2013-06-15 14:03:30 +08:00
|
|
|
ccDrawPoint(_touchPoint);
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setTouchPos(const CCPoint& pt)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchPoint = pt;
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void setTouchColor(ccColor3B color)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_touchColor = color;
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static TouchPoint* touchPointWithParent(CCNode* pParent)
|
|
|
|
{
|
|
|
|
TouchPoint* pRet = new TouchPoint();
|
|
|
|
pRet->setContentSize(pParent->getContentSize());
|
|
|
|
pRet->setAnchorPoint(ccp(0.0f, 0.0f));
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-06-15 14:03:30 +08:00
|
|
|
CCPoint _touchPoint;
|
|
|
|
ccColor3B _touchColor;
|
2012-04-25 18:17:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool MutiTouchTestLayer::init()
|
|
|
|
{
|
|
|
|
if (CCLayer::init())
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2012-04-25 18:17:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCDictionary s_dic;
|
|
|
|
|
|
|
|
void MutiTouchTestLayer::registerWithTouchDispatcher(void)
|
|
|
|
{
|
|
|
|
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutiTouchTestLayer::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
CCSetIterator iter = pTouches->begin();
|
|
|
|
for (; iter != pTouches->end(); iter++)
|
|
|
|
{
|
|
|
|
CCTouch* pTouch = (CCTouch*)(*iter);
|
|
|
|
TouchPoint* pTouchPoint = TouchPoint::touchPointWithParent(this);
|
2012-07-31 17:41:53 +08:00
|
|
|
CCPoint location = pTouch->getLocation();
|
2012-04-25 18:17:04 +08:00
|
|
|
|
|
|
|
pTouchPoint->setTouchPos(location);
|
|
|
|
pTouchPoint->setTouchColor(s_TouchColors[pTouch->getID()]);
|
|
|
|
|
|
|
|
addChild(pTouchPoint);
|
|
|
|
s_dic.setObject(pTouchPoint, pTouch->getID());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutiTouchTestLayer::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
CCSetIterator iter = pTouches->begin();
|
|
|
|
for (; iter != pTouches->end(); iter++)
|
|
|
|
{
|
|
|
|
CCTouch* pTouch = (CCTouch*)(*iter);
|
|
|
|
TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID());
|
2012-07-31 17:41:53 +08:00
|
|
|
CCPoint location = pTouch->getLocation();
|
2012-04-25 18:17:04 +08:00
|
|
|
pTP->setTouchPos(location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutiTouchTestLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
CCSetIterator iter = pTouches->begin();
|
|
|
|
for (; iter != pTouches->end(); iter++)
|
|
|
|
{
|
|
|
|
CCTouch* pTouch = (CCTouch*)(*iter);
|
|
|
|
TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID());
|
|
|
|
removeChild(pTP, true);
|
|
|
|
s_dic.removeObjectForKey(pTouch->getID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutiTouchTestLayer::ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
ccTouchesEnded(pTouches, pEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MutiTouchTestScene::runThisTest()
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
MutiTouchTestLayer* pLayer = MutiTouchTestLayer::create();
|
2012-04-25 18:17:04 +08:00
|
|
|
|
|
|
|
addChild(pLayer, 0);
|
|
|
|
|
|
|
|
CCDirector::sharedDirector()->replaceScene(this);
|
|
|
|
}
|