void RenderTextureSave::onTouchesMoved(const std::vector<Touch*>& touches, Event* event) { auto touch = touches[0]; auto start = touch->getLocation(); auto end = touch->getPreviousLocation(); // begin drawing to the render texture _target->begin(); // for extra points, we'll draw this smoothly from the last position and vary the sprite's // scale/rotation/offset float distance = start.getDistance(end); if (distance > 1) { int d = (int)distance; for (int i = 0; i < d; i++) { float difx = end.x - start.x; float dify = end.y - start.y; float delta = (float)i / distance; _brush->setPosition(Point(start.x + (difx * delta), start.y + (dify * delta))); _brush->setRotation(rand() % 360); float r = (float)(rand() % 50 / 50.f) + 0.25f; _brush->setScale(r); /*_brush->setColor(Color3B(CCRANDOM_0_1() * 127 + 128, 255, 255));*/ // Use CCRANDOM_0_1() will cause error when loading libtests.so on android, I don't know why. _brush->setColor(Color3B(rand() % 127 + 128, 255, 255)); // Call visit to draw the brush, don't call draw.. _brush->visit(); } } // finish drawing and return context back to the screen _target->end(); }
void Camera3DTestDemo::onTouchesMoved(const std::vector<Touch*>& touches, cocos2d::Event *event) { if(touches.size()==1) { auto touch = touches[0]; auto location = touch->getLocation(); Point newPos = touch->getPreviousLocation()-location; if(_cameraType==CameraType::Free || _cameraType==CameraType::FirstPerson) { Vec3 cameraDir; Vec3 cameraRightDir; _camera->getNodeToWorldTransform().getForwardVector(&cameraDir); cameraDir.normalize(); cameraDir.y=0; _camera->getNodeToWorldTransform().getRightVector(&cameraRightDir); cameraRightDir.normalize(); cameraRightDir.y=0; Vec3 cameraPos= _camera->getPosition3D(); cameraPos+=cameraDir*newPos.y*0.1f; cameraPos+=cameraRightDir*newPos.x*0.1f; _camera->setPosition3D(cameraPos); if(_sprite3D && _cameraType==CameraType::FirstPerson) { _sprite3D->setPosition3D(Vec3(_camera->getPositionX(),0,_camera->getPositionZ())); _targetPos=_sprite3D->getPosition3D(); } } } }
void BillBoardTest::onTouchesMoved(const std::vector<Touch*>& touches, Event* event) { if(touches.size()==1) { auto touch = touches[0]; auto location = touch->getLocation(); auto PreviousLocation = touch->getPreviousLocation(); Point newPos = PreviousLocation - location; Vec3 cameraDir; Vec3 cameraRightDir; _camera->getNodeToWorldTransform().getForwardVector(&cameraDir); cameraDir.normalize(); cameraDir.y=0; _camera->getNodeToWorldTransform().getRightVector(&cameraRightDir); cameraRightDir.normalize(); cameraRightDir.y=0; Vec3 cameraPos= _camera->getPosition3D(); cameraPos+=cameraDir*newPos.y*0.5; cameraPos+=cameraRightDir*newPos.x*0.5; _camera->setPosition3D(cameraPos); } }
// returns the delta position between the current location and the previous location in OpenGL coordinates CCPoint CCTouch::getDelta() const { return ccpSub(getLocation(), getPreviousLocation()); }
// returns the delta position between the current location and the previous location in OpenGL coordinates Vec2 Touch::getDelta() const { return getLocation() - getPreviousLocation(); }
// returns the delta position between the current location and the previous location in OpenGL coordinates Vec2 EventMouse::getDelta() const { CCASSERT(0, "Mouse::getDelta() returns garbage value. Use touch listener instead"); return getLocation() - getPreviousLocation(); }
// returns the delta position between the current location and the previous location in OpenGL coordinates Vec2 EventMouse::getDelta() const { return getLocation() - getPreviousLocation(); }
bool SwipeRecognizer::renewTouch(cocos2d::Touch *touch) { if (Recognizer::renewTouch(touch)) { if (touch->getID() == _currentTouch->getID()) { _currentTouch = touch; } if (_touches.size() == 1) { cocos2d::Vec2 current = _currentTouch->getLocation(); cocos2d::Vec2 prev = (_swipeBegin)?_currentTouch->getPreviousLocation():_currentTouch->getStartLocation(); _gesture.firstTouch = _currentTouch; _gesture.firstTouch.prevPoint = prev; _gesture.midpoint = _gesture.firstTouch.point; _gesture.delta = current - prev; if (!_swipeBegin && _gesture.delta.length() > _threshold) { _gesture.cleanup(); if (_sendThreshold) { _gesture.delta = current - prev; } else { _gesture.delta = current - _currentTouch->getPreviousLocation(); } _gesture.firstTouch = _currentTouch; _gesture.firstTouch.prevPoint = prev; _gesture.midpoint = _gesture.firstTouch.point; _swipeBegin = true; _event = Event::Began; if (_callback) { _callback(this, _event, _gesture); } } if (_swipeBegin /* && _gesture.delta.length() > 0.01f */) { auto t = Time::now(); float tm = (float)1000000LL / (float)((t - _lastTime).toMicroseconds()); if (tm > 80) { tm = 80; } float velX = _velocityX.step(_gesture.delta.x * tm); float velY = _velocityY.step(_gesture.delta.y * tm); _gesture.velocity = cocos2d::Vec2(velX, velY); _event = Event::Activated; if (_callback) { _callback(this, _event, _gesture); } _lastTime = t; } } else if (_touches.size() == 2) { if (touch == _currentTouch) { auto firstTouch = _touches.at(0); auto secondTouch = _touches.at(1); cocos2d::Vec2 currentFirst = firstTouch->getLocation(); cocos2d::Vec2 prevFirst = (_swipeBegin)?firstTouch->getPreviousLocation():firstTouch->getStartLocation(); cocos2d::Vec2 currentSecond = secondTouch->getLocation(); cocos2d::Vec2 prevSecond = (_swipeBegin)?secondTouch->getPreviousLocation():secondTouch->getStartLocation(); _gesture.firstTouch = firstTouch; _gesture.firstTouch.prevPoint = prevFirst; _gesture.secondTouch = secondTouch; _gesture.secondTouch.prevPoint = prevSecond; _gesture.midpoint = _gesture.secondTouch.point.getMidpoint(_gesture.firstTouch.point); _gesture.delta = currentFirst.getMidpoint(currentSecond) - prevFirst.getMidpoint(prevSecond); if (!_swipeBegin && _gesture.delta.length() > _threshold) { _gesture.cleanup(); _gesture.firstTouch = firstTouch; _gesture.firstTouch.prevPoint = prevFirst; _gesture.secondTouch = secondTouch; _gesture.secondTouch.prevPoint = prevSecond; _gesture.midpoint = _gesture.secondTouch.point.getMidpoint(_gesture.firstTouch.point); _swipeBegin = true; _event = Event::Began; if (_callback) { _callback(this, _event, _gesture); } } if (_swipeBegin /* && _gesture.delta.length() > 0.01f */ ) { auto t = Time::now(); float tm = (float)1000000LL / (float)((t - _lastTime).toMicroseconds()); if (tm > 80) { tm = 80; } float velX = _velocityX.step(_gesture.delta.x * tm); float velY = _velocityY.step(_gesture.delta.y * tm); _gesture.velocity = cocos2d::Vec2(velX, velY); _event = Event::Activated; if (_callback) { _callback(this, _event, _gesture); } _lastTime = t; } } } if (touch == _currentTouch) { } return true; } else { return false; } }