2012-04-25 18:17:04 +08:00
|
|
|
#include "MutiTouchTest.h"
|
|
|
|
|
|
|
|
|
2013-09-20 19:19:31 +08:00
|
|
|
static const Color3B* s_TouchColors[EventTouch::MAX_TOUCHES] = {
|
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:
|
|
|
|
TouchPoint()
|
|
|
|
{
|
2013-09-16 20:38:03 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
|
2014-03-01 08:10:48 +08:00
|
|
|
virtual void draw(Renderer *renderer, const kmMat4 &transform, bool transformUpdated)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-07-26 05:49:43 +08:00
|
|
|
DrawPrimitives::setDrawColor4B(_touchColor.r, _touchColor.g, _touchColor.b, 255);
|
2012-04-25 18:17:04 +08:00
|
|
|
glLineWidth(10);
|
2013-07-26 05:49:43 +08:00
|
|
|
DrawPrimitives::drawLine( Point(0, _touchPoint.y), Point(getContentSize().width, _touchPoint.y) );
|
|
|
|
DrawPrimitives::drawLine( Point(_touchPoint.x, 0), Point(_touchPoint.x, getContentSize().height) );
|
2012-04-25 18:17:04 +08:00
|
|
|
glLineWidth(1);
|
2013-07-26 05:49:43 +08:00
|
|
|
DrawPrimitives::setPointSize(30);
|
|
|
|
DrawPrimitives::drawPoint(_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
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto pRet = new TouchPoint();
|
2012-04-25 18:17:04 +08:00
|
|
|
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
|
|
|
{
|
2013-10-23 16:14:03 +08:00
|
|
|
auto listener = EventListenerTouchAllAtOnce::create();
|
|
|
|
listener->onTouchesBegan = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesBegan, this);
|
|
|
|
listener->onTouchesMoved = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesMoved, this);
|
|
|
|
listener->onTouchesEnded = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesEnded, this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
2014-03-26 23:33:58 +08:00
|
|
|
auto title = Label::create("Please touch the screen!", "", 24);
|
2013-09-19 18:54:26 +08:00
|
|
|
title->setPosition(VisibleRect::top()+Point(0, -40));
|
|
|
|
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
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void MutiTouchTestLayer::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 touchPoint = TouchPoint::touchPointWithParent(this);
|
|
|
|
auto location = touch->getLocation();
|
2012-04-25 18:17:04 +08:00
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
touchPoint->setTouchPos(location);
|
2013-07-28 23:23:35 +08:00
|
|
|
touchPoint->setTouchColor(*s_TouchColors[touch->getID()]);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void MutiTouchTestLayer::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();
|
2012-04-25 18:17:04 +08:00
|
|
|
pTP->setTouchPos(location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void MutiTouchTestLayer::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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void MutiTouchTestLayer::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
|
|
|
}
|
|
|
|
|
|
|
|
void MutiTouchTestScene::runThisTest()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto 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
|
|
|
}
|