2012-04-25 18:17:04 +08:00
|
|
|
#include "MutiTouchTest.h"
|
|
|
|
|
|
|
|
|
|
|
|
static ccColor3B s_TouchColors[CC_MAX_TOUCHES] = {
|
|
|
|
ccYELLOW,
|
|
|
|
ccBLUE,
|
|
|
|
ccGREEN,
|
|
|
|
ccRED,
|
|
|
|
ccMAGENTA
|
|
|
|
};
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
class TouchPoint : public Node
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TouchPoint()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
setShaderProgram(ShaderCache::sharedShaderCache()->programForKey(kShader_PositionTextureColor));
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void setTouchPos(const Point& pt)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
static TouchPoint* touchPointWithParent(Node* pParent)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
|
|
|
TouchPoint* pRet = new TouchPoint();
|
|
|
|
pRet->setContentSize(pParent->getContentSize());
|
|
|
|
pRet->setAnchorPoint(ccp(0.0f, 0.0f));
|
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-06-20 14:17:10 +08:00
|
|
|
Point _touchPoint;
|
2013-06-15 14:03:30 +08:00
|
|
|
ccColor3B _touchColor;
|
2012-04-25 18:17:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
bool MutiTouchTestLayer::init()
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
if (Layer::init())
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2012-04-25 18:17:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
static Dictionary s_dic;
|
2012-04-25 18:17:04 +08:00
|
|
|
|
|
|
|
void MutiTouchTestLayer::registerWithTouchDispatcher(void)
|
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this, 0);
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
|
2013-06-22 11:02:43 +08:00
|
|
|
void MutiTouchTestLayer::ccTouchesBegan(Set *touches, Event *pEvent)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-06-22 11:02:43 +08:00
|
|
|
|
|
|
|
for ( auto &item: *touches )
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-06-22 11:02:43 +08:00
|
|
|
Touch* pTouch = (Touch*)item;
|
2012-04-25 18:17:04 +08:00
|
|
|
TouchPoint* pTouchPoint = TouchPoint::touchPointWithParent(this);
|
2013-06-20 14:17:10 +08:00
|
|
|
Point 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());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-22 11:02:43 +08:00
|
|
|
void MutiTouchTestLayer::ccTouchesMoved(Set *touches, Event *pEvent)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-06-22 11:02:43 +08:00
|
|
|
for( auto &item: *touches)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-06-22 11:02:43 +08:00
|
|
|
Touch* pTouch = (Touch*)item;
|
2012-04-25 18:17:04 +08:00
|
|
|
TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID());
|
2013-06-20 14:17:10 +08:00
|
|
|
Point location = pTouch->getLocation();
|
2012-04-25 18:17:04 +08:00
|
|
|
pTP->setTouchPos(location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 11:02:43 +08:00
|
|
|
void MutiTouchTestLayer::ccTouchesEnded(Set *touches, Event *pEvent)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-06-22 11:02:43 +08:00
|
|
|
for ( auto &item: *touches )
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-06-22 11:02:43 +08:00
|
|
|
Touch* pTouch = (Touch*)item;
|
2012-04-25 18:17:04 +08:00
|
|
|
TouchPoint* pTP = (TouchPoint*)s_dic.objectForKey(pTouch->getID());
|
|
|
|
removeChild(pTP, true);
|
|
|
|
s_dic.removeObjectForKey(pTouch->getID());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void MutiTouchTestLayer::ccTouchesCancelled(Set *pTouches, Event *pEvent)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Director::sharedDirector()->replaceScene(this);
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|