2012-04-25 18:17:04 +08:00
|
|
|
#include "MutiTouchTest.h"
|
|
|
|
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
static Color3B s_TouchColors[CC_MAX_TOUCHES] = {
|
2013-07-08 18:11:32 +08:00
|
|
|
Color3B::YELLOW,
|
|
|
|
Color3B::BLUE,
|
|
|
|
Color3B::GREEN,
|
|
|
|
Color3B::RED,
|
|
|
|
Color3B::MAGENTA
|
2012-04-25 18:17:04 +08:00
|
|
|
};
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
class TouchPoint : public Node
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
TouchPoint()
|
|
|
|
{
|
2013-07-12 06:24:23 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->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-07-12 14:11:55 +08:00
|
|
|
ccDrawLine( Point(0, _touchPoint.y), Point(getContentSize().width, _touchPoint.y) );
|
|
|
|
ccDrawLine( Point(_touchPoint.x, 0), Point(_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
|
|
|
}
|
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
void setTouchColor(Color3B color)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
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());
|
2013-07-12 14:11:55 +08:00
|
|
|
pRet->setAnchorPoint(Point(0.0f, 0.0f));
|
2012-04-25 18:17:04 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-06-20 14:17:10 +08:00
|
|
|
Point _touchPoint;
|
2013-07-05 16:49:22 +08:00
|
|
|
Color3B _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-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->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()
|
|
|
|
{
|
2013-07-23 08:25:44 +08:00
|
|
|
MutiTouchTestLayer* layer = MutiTouchTestLayer::create();
|
2012-04-25 18:17:04 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
addChild(layer, 0);
|
2012-04-25 18:17:04 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|