Last active
March 25, 2016 04:38
-
-
Save masuhajime/d30ef0e7e9041ad0bef7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
auto listener = larme::eventlistener::EventListenerTouchAction::create(); | |
listener->onTouchesActionZoom = [this](float distance) { | |
this->cameraZoom += distance / 1000.0f; | |
// ここで distanceのmaxを設定したほうがいいかも・ | |
}; | |
listener->onTouchesActionPan = [this](Vec2 distance) { | |
this->cameraPosition -= distance / this->cameraZoom; | |
}; | |
this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this); | |
this->setCameraMask((unsigned short)cocos2d::CameraFlag::USER2, true); | |
} | |
void LayerUsagi::update(float delta) { | |
// カメラの設定 | |
auto size = Director::getInstance()->getVisibleSize(); | |
if (cameraZoom < 0.5f) { | |
cameraZoom = 0.5f; | |
} else if (1.5f < cameraZoom) { | |
cameraZoom = 1.5f; | |
} | |
Vec2 zoomOffset = Vec2::ZERO; | |
// 謎の式だがこれでおおよそ上手く動作する | |
zoomOffset -= (1 - cameraZoom)/2 * Vec2(size.width, size.height); | |
zoomOffset *= 1 / cameraZoom; | |
camera->setPosition(cameraPosition + zoomOffset); | |
camera->initOrthographic(size.width / cameraZoom, size.height / cameraZoom, -1, 1); | |
} |
cameraZoom = min(1.5f, max(0.5f, cameraZoom)); // 0.5 ~ 1.5 の間に収める
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
zoomOffset
はViewOriginの左下(0,0) を基点とする zoom/out 分の画面移動を中央に移動させるためのoffsetzoomOffset -= (1 - cameraZoom)/2 * Vec2(size.width, size.height);
// これで zoomを中央へと移動させる (余白/2ぶんだけ移動する)zoomOffset *= 1 / cameraZoom;
// ここでzoom分の拡大縮小分をoffsetにかける事によってzoomOffsetをzoom倍させる(?)// cameraZoomが0にならないように気をつける