2012-04-25 18:17:04 +08:00
|
|
|
#include "MutiTouchTest.h"
|
|
|
|
|
|
|
|
|
2013-09-12 14:26:39 +08:00
|
|
|
static const Color3B* s_TouchColors[TouchEvent::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-07-25 17:48:22 +08:00
|
|
|
setShaderProgram(ShaderCache::getInstance()->programForKey(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void draw()
|
|
|
|
{
|
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
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2013-09-03 18:22:03 +08:00
|
|
|
|
|
|
|
// Register Touch Event
|
|
|
|
// auto listener = TouchEventListener::create(Touch::DispatchMode::ALL_AT_ONCE);
|
|
|
|
//
|
|
|
|
// listener->onTouchesBegan = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesBegan, this);
|
|
|
|
// listener->onTouchesMoved = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesMoved, this);
|
|
|
|
// listener->onTouchesEnded = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesEnded, this);
|
|
|
|
// listener->onTouchesCancelled = CC_CALLBACK_2(MutiTouchTestLayer::onTouchesCancelled, this);
|
|
|
|
//
|
|
|
|
// EventDispatcher::getInstance()->registerEventListenerWithSceneGraphPriority(listener, this);
|
|
|
|
|
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
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
//void MutiTouchTestLayer::registerWithTouchDispatcher(void)
|
|
|
|
//{
|
|
|
|
// Director::getInstance()->getTouchDispatcher()->addStandardDelegate(this, 0);
|
|
|
|
//}
|
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-06-22 11:02:43 +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);
|
|
|
|
s_dic.setObject(touchPoint, touch->getID());
|
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-08-16 16:05:27 +08:00
|
|
|
auto pTP = static_cast<TouchPoint*>(s_dic.objectForKey(touch->getID()));
|
|
|
|
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-08-16 16:05:27 +08:00
|
|
|
auto pTP = static_cast<TouchPoint*>(s_dic.objectForKey(touch->getID()));
|
2012-04-25 18:17:04 +08:00
|
|
|
removeChild(pTP, true);
|
2013-07-26 06:53:24 +08:00
|
|
|
s_dic.removeObjectForKey(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
|
|
|
}
|