* Initialize viewport of the window for use. * @param w Window to use/display the viewport in * @param x Offset of left edge of viewport with respect to left edge window \a w * @param y Offset of top edge of viewport with respect to top edge window \a w * @param width Width of the viewport * @param height Height of the viewport * @param focus Either the tile index or vehicle ID to focus. *
| 216 | * @param zoom Zoomlevel to display |
| 217 | */ |
| 218 | void InitializeWindowViewport(Window *w, int x, int y, |
| 219 | int width, int height, std::variant<TileIndex, VehicleID> focus, ZoomLevel zoom) |
| 220 | { |
| 221 | assert(w->viewport == nullptr); |
| 222 | |
| 223 | auto vp = std::make_unique<ViewportData>(); |
| 224 | |
| 225 | vp->left = x + w->left; |
| 226 | vp->top = y + w->top; |
| 227 | vp->width = width; |
| 228 | vp->height = height; |
| 229 | |
| 230 | vp->zoom = Clamp(zoom, _settings_client.gui.zoom_min, _settings_client.gui.zoom_max); |
| 231 | |
| 232 | vp->virtual_width = ScaleByZoom(width, zoom); |
| 233 | vp->virtual_height = ScaleByZoom(height, zoom); |
| 234 | |
| 235 | Point pt; |
| 236 | |
| 237 | if (std::holds_alternative<VehicleID>(focus)) { |
| 238 | const Vehicle *veh; |
| 239 | |
| 240 | vp->follow_vehicle = std::get<VehicleID>(focus); |
| 241 | veh = Vehicle::Get(vp->follow_vehicle); |
| 242 | pt = MapXYZToViewport(*vp, veh->x_pos, veh->y_pos, veh->z_pos); |
| 243 | } else { |
| 244 | TileIndex tile = std::get<TileIndex>(focus); |
| 245 | if (tile == INVALID_TILE) { |
| 246 | /* No tile? Use center of main viewport. */ |
| 247 | const Window *mw = GetMainWindow(); |
| 248 | |
| 249 | /* center on same place as main window (zoom is maximum, no adjustment needed) */ |
| 250 | pt.x = mw->viewport->scrollpos_x + mw->viewport->virtual_width / 2; |
| 251 | pt.x -= vp->virtual_width / 2; |
| 252 | pt.y = mw->viewport->scrollpos_y + mw->viewport->virtual_height / 2; |
| 253 | pt.y -= vp->virtual_height / 2; |
| 254 | } else { |
| 255 | x = TileX(tile) * TILE_SIZE; |
| 256 | y = TileY(tile) * TILE_SIZE; |
| 257 | pt = MapXYZToViewport(*vp, x, y, GetSlopePixelZ(x, y)); |
| 258 | } |
| 259 | vp->follow_vehicle = VehicleID::Invalid(); |
| 260 | } |
| 261 | |
| 262 | vp->scrollpos_x = pt.x; |
| 263 | vp->scrollpos_y = pt.y; |
| 264 | vp->dest_scrollpos_x = pt.x; |
| 265 | vp->dest_scrollpos_y = pt.y; |
| 266 | |
| 267 | vp->overlay = nullptr; |
| 268 | |
| 269 | vp->virtual_left = 0; |
| 270 | vp->virtual_top = 0; |
| 271 | |
| 272 | w->viewport = std::move(vp); |
| 273 | } |
| 274 | |
| 275 | static Point _vp_move_offs; |
no test coverage detected