void
GameScene::onTouchesMoved(
const
std::vector<Touch*>& touches,Event *event)
{
auto winSize = Director::getInstance()->getWinSize();
if
(touches.size() > 1)
{
auto point1 = touches[0]->getLocation();
auto point2 = touches[1]->getLocation();
auto currDistance = point1.distance(point2);
auto prevDistance = touches[0]->getPrevIoUsLocation().distance(touches[1]->getPrevIoUsLocation());
auto pointVec1 = point1 - bgOrigin;
auto pointVec2 = point2 - bgOrigin;
auto relMidx = (pointVec1.x + pointVec2.x) / 2 ;
auto relMidy = (pointVec1.y + pointVec2.y) / 2 ;
auto anchorX = relMidx / bgSprite->getBoundingBox().size.width;
auto anchorY = relMidy / bgSprite->getBoundingBox().size.height;
auto absMidx = (point2.x + point1.x) / 2 ;
auto absMidy = (point2.y + point1.y) / 2 ;
if
( bgOrigin.x > 0)
{
absMidx -= bgOrigin.x;
}
if
( bgOrigin.x < -bgSprite->getBoundingBox().size.width + winSize.width )
{
absMidx += -bgSprite->getBoundingBox().size.width + winSize.width - bgOrigin.x;
}
if
( bgOrigin.y > 0 )
{
absMidy -= bgOrigin.y;
}
if
( bgOrigin.y < -bgSprite->getBoundingBox().size.height + winSize.height )
{
absMidy += -bgSprite->getBoundingBox().size.height + winSize.height - bgOrigin.y;
}
bgSprite->setAnchorPoint(Vec2(anchorX,anchorY));
bgSprite->setPosition(Vec2(absMidx,absMidy));
auto scale = bgSprite->getScale() * ( currDistance / prevDistance);
scale = MIN(4,MAX(1,scale));
bgSprite->setScale(scale);
bgOrigin = Vec2(absMidx,absMidy) - Vec2(bgSprite->getBoundingBox().size.width * anchorX,bgSprite->getBoundingBox().size.height * anchorY) ;
}
else
if
(touches.size() == 1)
{
auto touch = touches[0];
auto diff = touch->getDelta();
auto currentPos = bgSprite->getPosition();
auto pos = currentPos + diff;
auto bgSpriteCurrSize = bgSprite->getBoundingBox().size;
pos.x = MIN(pos.x,bgSpriteCurrSize.width * bgSprite->getAnchorPoint().x);
pos.x = MAX(pos.x,-bgSpriteCurrSize.width + winSize.width + bgSpriteCurrSize.width * bgSprite->getAnchorPoint().x);
pos.y = MIN(pos.y,bgSpriteCurrSize.height * bgSprite->getAnchorPoint().y);
pos.y = MAX(pos.y,-bgSpriteCurrSize.height + winSize.height + bgSpriteCurrSize.height * bgSprite->getAnchorPoint().y);
bgSprite->setPosition(pos);
if
( pos.x >= bgSpriteCurrSize.width * bgSprite->getAnchorPoint().x
|| pos.x <= -bgSpriteCurrSize.width + winSize.width + bgSpriteCurrSize.width * bgSprite->getAnchorPoint().x)
{
diff.x = 0;
}
if
( pos.y >= bgSpriteCurrSize.height * bgSprite->getAnchorPoint().y
|| pos.y <= -bgSpriteCurrSize.height + winSize.height + bgSpriteCurrSize.height * bgSprite->getAnchorPoint().y)
{
diff.y = 0;
}
bgOrigin += diff;
}
}
|