* Initialize or change the zoom level. * @param change Way to change the zoom level. * @param zoom_pt Position to keep fixed while zooming. * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out. */
| 1206 | * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out. |
| 1207 | */ |
| 1208 | void SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt) |
| 1209 | { |
| 1210 | static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away). |
| 1211 | static const int MIN_ZOOM_INDEX = 0; |
| 1212 | static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1; |
| 1213 | |
| 1214 | int new_index, cur_index, sub; |
| 1215 | Point tile; |
| 1216 | switch (change) { |
| 1217 | case ZLC_INITIALIZE: |
| 1218 | cur_index = - 1; // Definitely different from new_index. |
| 1219 | new_index = MIN_ZOOM_INDEX; |
| 1220 | tile.x = tile.y = 0; |
| 1221 | break; |
| 1222 | |
| 1223 | case ZLC_ZOOM_IN: |
| 1224 | case ZLC_ZOOM_OUT: |
| 1225 | for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) { |
| 1226 | if (this->zoom == zoomlevels[cur_index]) break; |
| 1227 | } |
| 1228 | assert(cur_index <= MAX_ZOOM_INDEX); |
| 1229 | |
| 1230 | tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub); |
| 1231 | new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX); |
| 1232 | break; |
| 1233 | |
| 1234 | default: NOT_REACHED(); |
| 1235 | } |
| 1236 | |
| 1237 | if (new_index != cur_index) { |
| 1238 | this->zoom = zoomlevels[new_index]; |
| 1239 | if (cur_index >= 0) { |
| 1240 | Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub); |
| 1241 | this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE, |
| 1242 | this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub); |
| 1243 | } else if (this->map_type == SMT_LINKSTATS) { |
| 1244 | this->overlay->SetDirty(); |
| 1245 | } |
| 1246 | this->SetWidgetDisabledState(WID_SM_ZOOM_IN, this->zoom == zoomlevels[MIN_ZOOM_INDEX]); |
| 1247 | this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]); |
| 1248 | this->SetDirty(); |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | /** |
| 1253 | * Set the link graph overlay cargo mask from the legend. |
no test coverage detected