2012-04-25 18:17:04 +08:00
|
|
|
#include "MutiTouchTest.h"
|
|
|
|
|
2015-04-09 08:37:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
MutiTouchTests::MutiTouchTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(MutiTouchTest);
|
|
|
|
}
|
2012-04-25 18:17:04 +08:00
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
static const Color3B* s_TouchColors[5] = {
|
2013-07-28 23:23:35 +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:
|
2014-09-11 15:39:56 +08:00
|
|
|
TouchPoint(const Vec2 &touchPoint, const Color3B &touchColor)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2014-09-11 15:39:56 +08:00
|
|
|
DrawNode* drawNode = DrawNode::create();
|
|
|
|
auto s = Director::getInstance()->getWinSize();
|
|
|
|
Color4F color(touchColor.r/255.0f, touchColor.g/255.0f, touchColor.b/255.0f, 1.0f);
|
|
|
|
drawNode->drawLine(Vec2(0, touchPoint.y), Vec2(s.width, touchPoint.y), color);
|
|
|
|
drawNode->drawLine(Vec2(touchPoint.x, 0), Vec2(touchPoint.x, s.height), color);
|
|
|
|
drawNode->drawDot(touchPoint, 3, color);
|
|
|
|
addChild(drawNode);
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
|
2014-09-11 15:39:56 +08:00
|
|
|
static TouchPoint* touchPointWithParent(Node* pParent, const Vec2 &touchPoint, const Color3B &touchColor)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2014-09-11 15:58:20 +08:00
|
|
|
auto pRet = new (std::nothrow) TouchPoint(touchPoint, touchColor);
|
2012-04-25 18:17:04 +08:00
|
|
|
pRet->setContentSize(pParent->getContentSize());
|
2014-05-15 01:07:09 +08:00
|
|
|
pRet->setAnchorPoint(Vec2(0.0f, 0.0f));
|
2012-04-25 18:17:04 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
bool MutiTouchTest::init()
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2015-04-03 14:31:03 +08:00
|
|
|
if (TestCase::init())
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-10-23 16:14:03 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
2015-04-03 14:31:03 +08:00
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(MutiTouchTest::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(MutiTouchTest::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(MutiTouchTest::onTouchesEnded, this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2014-04-09 23:31:05 +08:00
|
|
|
auto title = Label::createWithSystemFont("Please touch the screen!", "", 24);
|
2014-05-15 01:07:09 +08:00
|
|
|
title->setPosition(VisibleRect::top()+Vec2(0, -40));
|
2013-09-19 18:54:26 +08:00
|
|
|
addChild(title);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2012-04-25 18:17:04 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-18 16:03:07 +08:00
|
|
|
static Map<int, TouchPoint*> s_map;
|
2012-04-25 18:17:04 +08:00
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
void MutiTouchTest::onTouchesBegan(const std::vector<Touch*>& touches, Event *event)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
for ( auto &item: touches )
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
auto touch = item;
|
2013-08-16 16:05:27 +08:00
|
|
|
auto location = touch->getLocation();
|
2014-09-11 15:39:56 +08:00
|
|
|
auto touchPoint = TouchPoint::touchPointWithParent(this, location, *s_TouchColors[touch->getID()%5]);
|
2012-04-25 18:17:04 +08:00
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
addChild(touchPoint);
|
2013-12-18 16:03:07 +08:00
|
|
|
s_map.insert(touch->getID(), touchPoint);
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
void MutiTouchTest::onTouchesMoved(const std::vector<Touch*>& touches, Event *event)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
for( auto &item: touches)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
auto touch = item;
|
2013-12-18 16:03:07 +08:00
|
|
|
auto pTP = s_map.at(touch->getID());
|
2013-08-16 16:05:27 +08:00
|
|
|
auto location = touch->getLocation();
|
2014-09-11 15:39:56 +08:00
|
|
|
|
|
|
|
removeChild(pTP, true);
|
|
|
|
s_map.erase(touch->getID());
|
|
|
|
|
|
|
|
auto touchPointNew = TouchPoint::touchPointWithParent(this, location, *s_TouchColors[touch->getID()%5]);
|
|
|
|
addChild(touchPointNew);
|
|
|
|
s_map.insert(touch->getID(), touchPointNew);
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
void MutiTouchTest::onTouchesEnded(const std::vector<Touch*>& touches, Event *event)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
for ( auto &item: touches )
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
auto touch = item;
|
2013-12-18 16:03:07 +08:00
|
|
|
auto pTP = s_map.at(touch->getID());
|
2012-04-25 18:17:04 +08:00
|
|
|
removeChild(pTP, true);
|
2013-12-18 16:03:07 +08:00
|
|
|
s_map.erase(touch->getID());
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 14:31:03 +08:00
|
|
|
void MutiTouchTest::onTouchesCancelled(const std::vector<Touch*>& touches, Event *event)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
onTouchesEnded(touches, event);
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|