Move getMin/MaxZoom to TransformState

This commit is contained in:
John Firebaugh
2015-06-25 13:35:07 -07:00
parent 874aa69efb
commit e87a36444f
6 changed files with 50 additions and 52 deletions

View File

@@ -186,11 +186,11 @@ void Map::resetZoom() {
}
double Map::getMinZoom() const {
return data->transform.getMinZoom();
return data->transform.currentState().getMinZoom();
}
double Map::getMaxZoom() const {
return data->transform.getMaxZoom();
return data->transform.currentState().getMaxZoom();
}
@@ -279,7 +279,7 @@ uint32_t Map::addPointAnnotation(const LatLng& point, const std::string& symbol)
AnnotationIDs Map::addPointAnnotations(const AnnotationSegment& points,
const std::vector<std::string>& symbols) {
AnnotationsProperties properties = { { "symbols", symbols } };
auto result = data->annotationManager.addPointAnnotations(points, properties, data->transform.getMaxZoom());
auto result = data->annotationManager.addPointAnnotations(points, properties, getMaxZoom());
context->invoke(&MapContext::updateAnnotationTiles, result.first);
return result.second;
}
@@ -291,7 +291,7 @@ uint32_t Map::addShapeAnnotation(const AnnotationSegments& shape,
AnnotationIDs Map::addShapeAnnotations(const std::vector<AnnotationSegments>& shapes,
const std::vector<StyleProperties>& styleProperties) {
auto result = data->annotationManager.addShapeAnnotations(shapes, styleProperties, {{}}, data->transform.getMaxZoom());
auto result = data->annotationManager.addShapeAnnotations(shapes, styleProperties, {{}}, getMaxZoom());
context->invoke(&MapContext::updateAnnotationTiles, result.first);
return result.second;
}
@@ -301,12 +301,12 @@ void Map::removeAnnotation(uint32_t annotation) {
}
void Map::removeAnnotations(const std::vector<uint32_t>& annotations) {
auto result = data->annotationManager.removeAnnotations(annotations, data->transform.getMaxZoom());
auto result = data->annotationManager.removeAnnotations(annotations, getMaxZoom());
context->invoke(&MapContext::updateAnnotationTiles, result);
}
std::vector<uint32_t> Map::getAnnotationsInBounds(const LatLngBounds& bounds, const AnnotationType& type) {
return data->annotationManager.getAnnotationsInBounds(bounds, data->transform.getMaxZoom(), type);
return data->annotationManager.getAnnotationsInBounds(bounds, getMaxZoom(), type);
}
LatLngBounds Map::getBoundsForAnnotations(const std::vector<uint32_t>& annotations) {

View File

@@ -468,7 +468,7 @@ bool Source::update(MapData& data,
if (info.type != SourceType::Raster && cache.getSize() == 0) {
size_t conservativeCacheSize = ((float)transformState.getWidth() / util::tileSize) *
((float)transformState.getHeight() / util::tileSize) *
(data.transform.getMaxZoom() - data.transform.getMinZoom() + 1) *
(transformState.getMaxZoom() - transformState.getMinZoom() + 1) *
0.5;
cache.setSize(conservativeCacheSize);
}

View File

@@ -47,7 +47,7 @@ bool Transform::resize(const uint16_t w, const uint16_t h, const float ratio,
state.pixelRatio = ratio;
state.framebuffer[0] = fb_w;
state.framebuffer[1] = fb_h;
constrain(state.scale, state.y);
state.constrain(state.scale, state.y);
view.notifyMapChange(MapChangeRegionDidChange);
@@ -79,7 +79,7 @@ void Transform::_moveBy(const double dx, const double dy, const Duration duratio
double x = state.x + std::cos(state.angle) * dx + std::sin( state.angle) * dy;
double y = state.y + std::cos(state.angle) * dy + std::sin(-state.angle) * dx;
constrain(state.scale, y);
state.constrain(state.scale, y);
if (duration == Duration::zero()) {
state.x = x;
@@ -156,10 +156,10 @@ void Transform::scaleBy(const double ds, const double cx, const double cy, const
// clamp scale to min/max values
double new_scale = state.scale * ds;
if (new_scale < min_scale) {
new_scale = min_scale;
} else if (new_scale > max_scale) {
new_scale = max_scale;
if (new_scale < state.min_scale) {
new_scale = state.min_scale;
} else if (new_scale > state.max_scale) {
new_scale = state.max_scale;
}
_setScale(new_scale, cx, cy, duration);
@@ -198,26 +198,14 @@ double Transform::getScale() const {
return state.scale;
}
double Transform::getMinZoom() const {
double test_scale = state.scale;
double test_y = state.y;
constrain(test_scale, test_y);
return std::log2(std::fmin(min_scale, test_scale));
}
double Transform::getMaxZoom() const {
return std::log2(max_scale);
}
void Transform::_setScale(double new_scale, double cx, double cy, const Duration duration) {
// This is only called internally, so we don't need a lock here.
// Ensure that we don't zoom in further than the maximum allowed.
if (new_scale < min_scale) {
new_scale = min_scale;
} else if (new_scale > max_scale) {
new_scale = max_scale;
if (new_scale < state.min_scale) {
new_scale = state.min_scale;
} else if (new_scale > state.max_scale) {
new_scale = state.max_scale;
}
// Zoom in on the center if we don't have click or gesture anchor coordinates.
@@ -255,7 +243,7 @@ void Transform::_setScaleXY(const double new_scale, const double xn, const doubl
double x = xn;
double y = yn;
constrain(scale, y);
state.constrain(scale, y);
if (duration == Duration::zero()) {
state.scale = scale;
@@ -293,19 +281,6 @@ void Transform::_setScaleXY(const double new_scale, const double xn, const doubl
duration);
}
#pragma mark - Constraints
void Transform::constrain(double& scale, double& y) const {
// Constrain minimum zoom to avoid zooming out far enough to show off-world areas.
if (scale < (state.height / util::tileSize)) scale = (state.height / util::tileSize);
// Constrain min/max vertical pan to avoid showing off-world areas.
double max_y = ((scale * util::tileSize) - state.height) / 2;
if (y > max_y) y = max_y;
if (y < -max_y) y = -max_y;
}
#pragma mark - Angle
void Transform::rotateBy(const double start_x, const double start_y, const double end_x,

View File

@@ -36,8 +36,6 @@ public:
void setZoom(double zoom, Duration = Duration::zero());
double getZoom() const;
double getScale() const;
double getMinZoom() const;
double getMaxZoom() const;
// Angle
void rotateBy(double sx, double sy, double ex, double ey, Duration = Duration::zero());
@@ -64,18 +62,12 @@ private:
void _setScaleXY(double new_scale, double xn, double yn, Duration = Duration::zero());
void _setAngle(double angle, Duration = Duration::zero());
void constrain(double& scale, double& y) const;
View &view;
mutable std::recursive_mutex mtx;
TransformState state;
// Limit the amount of zooming possible on the map.
const double min_scale = std::pow(2, 0);
const double max_scale = std::pow(2, 18);
void startTransition(std::function<Update(double)> frame,
std::function<void()> finish,
Duration);

View File

@@ -152,6 +152,18 @@ double TransformState::getScale() const {
return scale;
}
double TransformState::getMinZoom() const {
double test_scale = scale;
double test_y = y;
constrain(test_scale, test_y);
return std::log2(std::fmin(min_scale, test_scale));
}
double TransformState::getMaxZoom() const {
return std::log2(max_scale);
}
#pragma mark - Rotation
@@ -256,6 +268,18 @@ bool TransformState::isChanging() const {
#pragma mark - (private helper functions)
void TransformState::constrain(double& scale_, double& y_) const {
// Constrain minimum zoom to avoid zooming out far enough to show off-world areas.
if (scale_ < height / util::tileSize) {
scale_ = height / util::tileSize;
}
// Constrain min/max vertical pan to avoid showing off-world areas.
double max_y = ((scale_ * util::tileSize) - height) / 2;
if (y_ > max_y) y_ = max_y;
if (y_ < -max_y) y_ = -max_y;
}
double TransformState::pixel_x() const {
const double center = (width - scale * util::tileSize) / 2;

View File

@@ -47,6 +47,8 @@ public:
int32_t getIntegerZoom() const;
double getZoomFraction() const;
double getScale() const;
double getMinZoom() const;
double getMaxZoom() const;
// Rotation
float getAngle() const;
@@ -59,10 +61,15 @@ public:
bool isChanging() const;
private:
void constrain(double& scale, double& y) const;
// Limit the amount of zooming possible on the map.
double min_scale = std::pow(2, 0);
double max_scale = std::pow(2, 18);
double pixel_x() const;
double pixel_y() const;
private:
// logical dimensions
uint16_t width = 0, height = 0;