Skip to content

Instantly share code, notes, and snippets.

@masuhajime
Last active March 25, 2016 04:38
Show Gist options
  • Save masuhajime/d30ef0e7e9041ad0bef7 to your computer and use it in GitHub Desktop.
Save masuhajime/d30ef0e7e9041ad0bef7 to your computer and use it in GitHub Desktop.
{
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);
}
@masuhajime
Copy link
Author

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