2012-04-25 18:17:04 +08:00
|
|
|
#include "MutiTouchTest.h"
|
|
|
|
|
|
|
|
|
2013-07-28 23:23:35 +08:00
|
|
|
static const Color3B* s_TouchColors[CC_MAX_TOUCHES] = {
|
|
|
|
&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
|
|
|
{
|
|
|
|
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-07-26 06:53:24 +08:00
|
|
|
void MutiTouchTestLayer::ccTouchesBegan(Set *touches, Event *event)
|
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-07-26 06:53:24 +08:00
|
|
|
Touch* touch = static_cast<Touch*>(item);
|
|
|
|
TouchPoint* touchPoint = TouchPoint::touchPointWithParent(this);
|
|
|
|
Point 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-07-26 06:53:24 +08:00
|
|
|
void MutiTouchTestLayer::ccTouchesMoved(Set *touches, Event *event)
|
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-07-26 06:53:24 +08:00
|
|
|
Touch* touch = static_cast<Touch*>(item);
|
|
|
|
TouchPoint* pTP = static_cast<TouchPoint*>(s_dic.objectForKey(touch->getID()));
|
|
|
|
Point location = touch->getLocation();
|
2012-04-25 18:17:04 +08:00
|
|
|
pTP->setTouchPos(location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
void MutiTouchTestLayer::ccTouchesEnded(Set *touches, Event *event)
|
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-07-26 06:53:24 +08:00
|
|
|
Touch* touch = static_cast<Touch*>(item);
|
|
|
|
TouchPoint* 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-07-26 06:53:24 +08:00
|
|
|
void MutiTouchTestLayer::ccTouchesCancelled(Set *touches, Event *event)
|
2012-04-25 18:17:04 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
ccTouchesEnded(touches, event);
|
2012-04-25 18:17:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|