# viewport.cpp

Go to the documentation of this file.

    00001 /* $Id: viewport.cpp 15593 2009-02-27 22:26:06Z yexo $ */
    00002
    00021 #include "stdafx.h"
    00022 #include "openttd.h"
    00023 #include "landscape.h"
    00024 #include "viewport_func.h"
    00025 #include "station_base.h"
    00026 #include "town.h"
    00027 #include "signs_base.h"
    00028 #include "signs_func.h"
    00029 #include "variables.h"
    00030 #include "vehicle_base.h"
    00031 #include "vehicle_gui.h"
    00032 #include "blitter/factory.hpp"
    00033 #include "transparency.h"
    00034 #include "strings_func.h"
    00035 #include "zoom_func.h"
    00036 #include "vehicle_func.h"
    00037 #include "company_func.h"
    00038 #include "station_func.h"
    00039 #include "window_func.h"
    00040 #include "tilehighlight_func.h"
    00041 #include "window_gui.h"
    00042
    00043 #include "table/sprites.h"
    00044 #include "table/strings.h"
    00045
    00046 PlaceProc *_place_proc;
    00047 Point _tile_fract_coords;
    00048 ZoomLevel _saved_scrollpos_zoom;
    00049
    00050 struct StringSpriteToDraw {
    00051   StringID string;
    00052   uint16 colour;
    00053   int32 x;
    00054   int32 y;
    00055   uint64 params[2];
    00056   uint16 width;
    00057 };
    00058
    00059 struct TileSpriteToDraw {
    00060   SpriteID image;
    00061   SpriteID pal;
    00062   const SubSprite *sub;
    00063   int32 x;
    00064   int32 y;
    00065   byte z;
    00066 };
    00067
    00068 struct ChildScreenSpriteToDraw {
    00069   SpriteID image;
    00070   SpriteID pal;
    00071   const SubSprite *sub;
    00072   int32 x;
    00073   int32 y;
    00074   int next;
    00075 };
    00076
    00078 struct ParentSpriteToDraw {
    00079   SpriteID image;
    00080   SpriteID pal;
    00081   const SubSprite *sub;
    00082
    00083   int32 x;
    00084   int32 y;
    00085
    00086   int32 left;
    00087   int32 top;
    00088
    00089   int32 xmin;
    00090   int32 xmax;
    00091   int32 ymin;
    00092   int32 ymax;
    00093   int zmin;
    00094   int zmax;
    00095
    00096   int first_child;
    00097   bool comparison_done;
    00098 };
    00099
    00101 enum FoundationPart {
    00102   FOUNDATION_PART_NONE     = 0xFF,
    00103   FOUNDATION_PART_NORMAL   = 0,
    00104   FOUNDATION_PART_HALFTILE = 1,
    00105   FOUNDATION_PART_END
    00106 };
    00107
    00108 typedef SmallVector<TileSpriteToDraw, 64> TileSpriteToDrawVector;
    00109 typedef SmallVector<StringSpriteToDraw, 4> StringSpriteToDrawVector;
    00110 typedef SmallVector<ParentSpriteToDraw, 64> ParentSpriteToDrawVector;
    00111 typedef SmallVector<ParentSpriteToDraw*, 64> ParentSpriteToSortVector;
    00112 typedef SmallVector<ChildScreenSpriteToDraw, 16> ChildScreenSpriteToDrawVector;
    00113
    00115 struct ViewportDrawer {
    00116   DrawPixelInfo dpi;
    00117
    00118   StringSpriteToDrawVector string_sprites_to_draw;
    00119   TileSpriteToDrawVector tile_sprites_to_draw;
    00120   ParentSpriteToDrawVector parent_sprites_to_draw;
    00121   ParentSpriteToSortVector parent_sprites_to_sort;
    00122   ChildScreenSpriteToDrawVector child_screen_sprites_to_draw;
    00123
    00124   int *last_child;
    00125
    00126   byte combine_sprites;
    00127
    00128   int foundation[FOUNDATION_PART_END];
    00129   FoundationPart foundation_part;
    00130   int *last_foundation_child[FOUNDATION_PART_END];
    00131   Point foundation_offset[FOUNDATION_PART_END];
    00132 };
    00133
    00134 static ViewportDrawer _vd;
    00135
    00136 TileHighlightData _thd;
    00137 static TileInfo *_cur_ti;
    00138 bool _draw_bounding_boxes = false;
    00139
    00140 static Point MapXYZToViewport(const ViewPort *vp, uint x, uint y, uint z)
    00141 {
    00142   Point p = RemapCoords(x, y, z);
    00143   p.x -= vp->virtual_width / 2;
    00144   p.y -= vp->virtual_height / 2;
    00145   return p;
    00146 }
    00147
    00148 void DeleteWindowViewport(Window *w)
    00149 {
    00150   free(w->viewport);
    00151   w->viewport = NULL;
    00152 }
    00153
    00166 void InitializeWindowViewport(Window *w, int x, int y,
    00167   int width, int height, uint32 follow_flags, ZoomLevel zoom)
    00168 {
    00169   assert(w->viewport == NULL);
    00170
    00171   ViewportData *vp = CallocT<ViewportData>(1);
    00172
    00173   vp->left = x + w->left;
    00174   vp->top = y + w->top;
    00175   vp->width = width;
    00176   vp->height = height;
    00177
    00178   vp->zoom = zoom;
    00179
    00180   vp->virtual_width = ScaleByZoom(width, zoom);
    00181   vp->virtual_height = ScaleByZoom(height, zoom);
    00182
    00183   Point pt;
    00184
    00185   if (follow_flags & 0x80000000) {
    00186     const Vehicle *veh;
    00187
    00188     vp->follow_vehicle = (VehicleID)(follow_flags & 0xFFFF);
    00189     veh = GetVehicle(vp->follow_vehicle);
    00190     pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
    00191   } else {
    00192     uint x = TileX(follow_flags) * TILE_SIZE;
    00193     uint y = TileY(follow_flags) * TILE_SIZE;
    00194
    00195     vp->follow_vehicle = INVALID_VEHICLE;
    00196     pt = MapXYZToViewport(vp, x, y, GetSlopeZ(x, y));
    00197   }
    00198
    00199   vp->scrollpos_x = pt.x;
    00200   vp->scrollpos_y = pt.y;
    00201   vp->dest_scrollpos_x = pt.x;
    00202   vp->dest_scrollpos_y = pt.y;
    00203
    00204   w->viewport = vp;
    00205   vp->virtual_left = 0;//pt.x;
    00206   vp->virtual_top = 0;//pt.y;
    00207 }
    00208
    00209 static Point _vp_move_offs;
    00210
    00211 static void DoSetViewportPosition(const Window *w, int left, int top, int width, int height)
    00212 {
    00213   FOR_ALL_WINDOWS_FROM_BACK_FROM(w, w) {
    00214     if (left + width > w->left &&
    00215         w->left + w->width > left &&
    00216         top + height > w->top &&
    00217         w->top + w->height > top) {
    00218
    00219       if (left < w->left) {
    00220         DoSetViewportPosition(w, left, top, w->left - left, height);
    00221         DoSetViewportPosition(w, left + (w->left - left), top, width - (w->left - left), height);
    00222         return;
    00223       }
    00224
    00225       if (left + width > w->left + w->width) {
    00226         DoSetViewportPosition(w, left, top, (w->left + w->width - left), height);
    00227         DoSetViewportPosition(w, left + (w->left + w->width - left), top, width - (w->left + w->width - left) , height);
    00228         return;
    00229       }
    00230
    00231       if (top < w->top) {
    00232         DoSetViewportPosition(w, left, top, width, (w->top - top));
    00233         DoSetViewportPosition(w, left, top + (w->top - top), width, height - (w->top - top));
    00234         return;
    00235       }
    00236
    00237       if (top + height > w->top + w->height) {
    00238         DoSetViewportPosition(w, left, top, width, (w->top + w->height - top));
    00239         DoSetViewportPosition(w, left, top + (w->top + w->height - top), width , height - (w->top + w->height - top));
    00240         return;
    00241       }
    00242
    00243       return;
    00244     }
    00245   }
    00246
    00247   {
    00248     int xo = _vp_move_offs.x;
    00249     int yo = _vp_move_offs.y;
    00250
    00251     if (abs(xo) >= width || abs(yo) >= height) {
    00252       /* fully_outside */
    00253       RedrawScreenRect(left, top, left + width, top + height);
    00254       return;
    00255     }
    00256
    00257     GfxScroll(left, top, width, height, xo, yo);
    00258
    00259     if (xo > 0) {
    00260       RedrawScreenRect(left, top, xo + left, top + height);
    00261       left += xo;
    00262       width -= xo;
    00263     } else if (xo < 0) {
    00264       RedrawScreenRect(left + width + xo, top, left + width, top + height);
    00265       width += xo;
    00266     }
    00267
    00268     if (yo > 0) {
    00269       RedrawScreenRect(left, top, width + left, top + yo);
    00270     } else if (yo < 0) {
    00271       RedrawScreenRect(left, top + height + yo, width + left, top + height);
    00272     }
    00273   }
    00274 }
    00275
    00276 static void SetViewportPosition(Window *w, int x, int y)
    00277 {
    00278   ViewPort *vp = w->viewport;
    00279   int old_left = vp->virtual_left;
    00280   int old_top = vp->virtual_top;
    00281   int i;
    00282   int left, top, width, height;
    00283
    00284   vp->virtual_left = x;
    00285   vp->virtual_top = y;
    00286
    00287   /* viewport is bound to its left top corner, so it must be rounded down (UnScaleByZoomLower)
    00288    * else glitch described in FS#1412 will happen (offset by 1 pixel with zoom level > NORMAL)
    00289    */
    00290   old_left = UnScaleByZoomLower(old_left, vp->zoom);
    00291   old_top = UnScaleByZoomLower(old_top, vp->zoom);
    00292   x = UnScaleByZoomLower(x, vp->zoom);
    00293   y = UnScaleByZoomLower(y, vp->zoom);
    00294
    00295   old_left -= x;
    00296   old_top -= y;
    00297
    00298   if (old_top == 0 && old_left == 0) return;
    00299
    00300   _vp_move_offs.x = old_left;
    00301   _vp_move_offs.y = old_top;
    00302
    00303   left = vp->left;
    00304   top = vp->top;
    00305   width = vp->width;
    00306   height = vp->height;
    00307
    00308   if (left < 0) {
    00309     width += left;
    00310     left = 0;
    00311   }
    00312
    00313   i = left + width - _screen.width;
    00314   if (i >= 0) width -= i;
    00315
    00316   if (width > 0) {
    00317     if (top < 0) {
    00318       height += top;
    00319       top = 0;
    00320     }
    00321
    00322     i = top + height - _screen.height;
    00323     if (i >= 0) height -= i;
    00324
    00325     if (height > 0) DoSetViewportPosition(w->z_front, left, top, width, height);
    00326   }
    00327 }
    00328
    00337 ViewPort *IsPtInWindowViewport(const Window *w, int x, int y)
    00338 {
    00339   ViewPort *vp = w->viewport;
    00340
    00341   if (vp != NULL &&
    00342       IsInsideMM(x, vp->left, vp->left + vp->width) &&
    00343       IsInsideMM(y, vp->top, vp->top + vp->height))
    00344     return vp;
    00345
    00346   return NULL;
    00347 }
    00348
    00355 static Point TranslateXYToTileCoord(const ViewPort *vp, int x, int y)
    00356 {
    00357   Point pt;
    00358   int a, b;
    00359   uint z;
    00360
    00361   if ( (uint)(x -= vp->left) >= (uint)vp->width ||
    00362         (uint)(y -= vp->top) >= (uint)vp->height) {
    00363         Point pt = {-1, -1};
    00364         return pt;
    00365   }
    00366
    00367   x = (ScaleByZoom(x, vp->zoom) + vp->virtual_left) >> 2;
    00368   y = (ScaleByZoom(y, vp->zoom) + vp->virtual_top) >> 1;
    00369
    00370   a = y - x;
    00371   b = y + x;
    00372
    00373   /* we need to move variables in to the valid range, as the
    00374    * GetTileZoomCenterWindow() function can call here with invalid x and/or y,
    00375    * when the user tries to zoom out along the sides of the map */
    00376   a = Clamp(a, -4 * TILE_SIZE, (int)(MapMaxX() * TILE_SIZE) - 1);
    00377   b = Clamp(b, -4 * TILE_SIZE, (int)(MapMaxY() * TILE_SIZE) - 1);
    00378
    00379   /* (a, b) is the X/Y-world coordinate that belongs to (x,y) if the landscape would be completely flat on height 0.
    00380    * Now find the Z-world coordinate by fix point iteration.
    00381    * This is a bit tricky because the tile height is non-continuous at foundations.
    00382    * The clicked point should be approached from the back, otherwise there are regions that are not clickable.
    00383    * (FOUNDATION_HALFTILE_LOWER on SLOPE_STEEP_S hides north halftile completely)
    00384    * So give it a z-malus of 4 in the first iterations.
    00385    */
    00386   z = 0;
    00387
    00388   int min_coord = _settings_game.construction.freeform_edges ? TILE_SIZE : 0;
    00389
    00390   for (int i = 0; i < 5; i++) z = GetSlopeZ(Clamp(a + (int)max(z, 4u) - 4, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + (int)max(z, 4u) - 4, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
    00391   for (uint malus = 3; malus > 0; malus--) z = GetSlopeZ(Clamp(a + (int)max(z, malus) - (int)malus, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + (int)max(z, malus) - (int)malus, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
    00392   for (int i = 0; i < 5; i++) z = GetSlopeZ(Clamp(a + (int)z, min_coord, MapMaxX() * TILE_SIZE - 1), Clamp(b + (int)z, min_coord, MapMaxY() * TILE_SIZE - 1)) / 2;
    00393
    00394   pt.x = Clamp(a + (int)z, min_coord, MapMaxX() * TILE_SIZE - 1);
    00395   pt.y = Clamp(b + (int)z, min_coord, MapMaxY() * TILE_SIZE - 1);
    00396
    00397   return pt;
    00398 }
    00399
    00400 /* When used for zooming, check area below current coordinates (x,y)
    00401  * and return the tile of the zoomed out/in position (zoom_x, zoom_y)
    00402  * when you just want the tile, make x = zoom_x and y = zoom_y */
    00403 static Point GetTileFromScreenXY(int x, int y, int zoom_x, int zoom_y)
    00404 {
    00405   Window *w;
    00406   ViewPort *vp;
    00407   Point pt;
    00408
    00409   if ( (w = FindWindowFromPt(x, y)) != NULL &&
    00410        (vp = IsPtInWindowViewport(w, x, y)) != NULL)
    00411         return TranslateXYToTileCoord(vp, zoom_x, zoom_y);
    00412
    00413   pt.y = pt.x = -1;
    00414   return pt;
    00415 }
    00416
    00417 Point GetTileBelowCursor()
    00418 {
    00419   return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, _cursor.pos.x, _cursor.pos.y);
    00420 }
    00421
    00422
    00423 Point GetTileZoomCenterWindow(bool in, Window * w)
    00424 {
    00425   int x, y;
    00426   ViewPort *vp = w->viewport;
    00427
    00428   if (in) {
    00429     x = ((_cursor.pos.x - vp->left) >> 1) + (vp->width >> 2);
    00430     y = ((_cursor.pos.y - vp->top) >> 1) + (vp->height >> 2);
    00431   } else {
    00432     x = vp->width - (_cursor.pos.x - vp->left);
    00433     y = vp->height - (_cursor.pos.y - vp->top);
    00434   }
    00435   /* Get the tile below the cursor and center on the zoomed-out center */
    00436   return GetTileFromScreenXY(_cursor.pos.x, _cursor.pos.y, x + vp->left, y + vp->top);
    00437 }
    00438
    00445 void HandleZoomMessage(Window *w, const ViewPort *vp, byte widget_zoom_in, byte widget_zoom_out)
    00446 {
    00447   w->SetWidgetDisabledState(widget_zoom_in, vp->zoom == ZOOM_LVL_MIN);
    00448   w->InvalidateWidget(widget_zoom_in);
    00449
    00450   w->SetWidgetDisabledState(widget_zoom_out, vp->zoom == ZOOM_LVL_MAX);
    00451   w->InvalidateWidget(widget_zoom_out);
    00452 }
    00453
    00465 void DrawGroundSpriteAt(SpriteID image, SpriteID pal, int32 x, int32 y, byte z, const SubSprite *sub)
    00466 {
    00467   assert((image & SPRITE_MASK) < MAX_SPRITES);
    00468
    00469   TileSpriteToDraw *ts = _vd.tile_sprites_to_draw.Append();
    00470   ts->image = image;
    00471   ts->pal = pal;
    00472   ts->sub = sub;
    00473   ts->x = x;
    00474   ts->y = y;
    00475   ts->z = z;
    00476 }
    00477
    00490 static void AddChildSpriteToFoundation(SpriteID image, SpriteID pal, const SubSprite *sub, FoundationPart foundation_part, int extra_offs_x, int extra_offs_y)
    00491 {
    00492   assert(IsInsideMM(foundation_part, 0, FOUNDATION_PART_END));
    00493   assert(_vd.foundation[foundation_part] != -1);
    00494   Point offs = _vd.foundation_offset[foundation_part];
    00495
    00496   /* Change the active ChildSprite list to the one of the foundation */
    00497   int *old_child = _vd.last_child;
    00498   _vd.last_child = _vd.last_foundation_child[foundation_part];
    00499
    00500   AddChildSpriteScreen(image, pal, offs.x + extra_offs_x, offs.y + extra_offs_y, false, sub);
    00501
    00502   /* Switch back to last ChildSprite list */
    00503   _vd.last_child = old_child;
    00504 }
    00505
    00514 void DrawGroundSprite(SpriteID image, SpriteID pal, const SubSprite *sub)
    00515 {
    00516   /* Switch to first foundation part, if no foundation was drawn */
    00517   if (_vd.foundation_part == FOUNDATION_PART_NONE) _vd.foundation_part = FOUNDATION_PART_NORMAL;
    00518
    00519   if (_vd.foundation[_vd.foundation_part] != -1) {
    00520     AddChildSpriteToFoundation(image, pal, sub, _vd.foundation_part, 0, 0);
    00521   } else {
    00522     DrawGroundSpriteAt(image, pal, _cur_ti->x, _cur_ti->y, _cur_ti->z, sub);
    00523   }
    00524 }
    00525
    00526
    00534 void OffsetGroundSprite(int x, int y)
    00535 {
    00536   /* Switch to next foundation part */
    00537   switch (_vd.foundation_part) {
    00538     case FOUNDATION_PART_NONE:
    00539       _vd.foundation_part = FOUNDATION_PART_NORMAL;
    00540       break;
    00541     case FOUNDATION_PART_NORMAL:
    00542       _vd.foundation_part = FOUNDATION_PART_HALFTILE;
    00543       break;
    00544     default: NOT_REACHED();
    00545   }
    00546
    00547   /* _vd.last_child == NULL if foundation sprite was clipped by the viewport bounds */
    00548   if (_vd.last_child != NULL) _vd.foundation[_vd.foundation_part] = _vd.parent_sprites_to_draw.Length() - 1;
    00549
    00550   _vd.foundation_offset[_vd.foundation_part].x = x;
    00551   _vd.foundation_offset[_vd.foundation_part].y = y;
    00552   _vd.last_foundation_child[_vd.foundation_part] = _vd.last_child;
    00553 }
    00554
    00566 static void AddCombinedSprite(SpriteID image, SpriteID pal, int x, int y, byte z, const SubSprite *sub)
    00567 {
    00568   Point pt = RemapCoords(x, y, z);
    00569   const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
    00570
    00571   if (pt.x + spr->x_offs >= _vd.dpi.left + _vd.dpi.width ||
    00572       pt.x + spr->x_offs + spr->width <= _vd.dpi.left ||
    00573       pt.y + spr->y_offs >= _vd.dpi.top + _vd.dpi.height ||
    00574       pt.y + spr->y_offs + spr->height <= _vd.dpi.top)
    00575     return;
    00576
    00577   const ParentSpriteToDraw *pstd = _vd.parent_sprites_to_draw.End() - 1;
    00578   AddChildSpriteScreen(image, pal, pt.x - pstd->left, pt.y - pstd->top, false, sub);
    00579 }
    00580
    00605 void AddSortableSpriteToDraw(SpriteID image, SpriteID pal, int x, int y, int w, int h, int dz, int z, bool transparent, int bb_offset_x, int bb_offset_y, int bb_offset_z, const SubSprite *sub)
    00606 {
    00607   int32 left, right, top, bottom;
    00608
    00609   assert((image & SPRITE_MASK) < MAX_SPRITES);
    00610
    00611   /* make the sprites transparent with the right palette */
    00612   if (transparent) {
    00613     SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
    00614     pal = PALETTE_TO_TRANSPARENT;
    00615   }
    00616
    00617   if (_vd.combine_sprites == 2) {
    00618     AddCombinedSprite(image, pal, x, y, z, sub);
    00619     return;
    00620   }
    00621
    00622   _vd.last_child = NULL;
    00623
    00624   Point pt = RemapCoords(x, y, z);
    00625   int tmp_left, tmp_top, tmp_x = pt.x, tmp_y = pt.y;
    00626
    00627   /* Compute screen extents of sprite */
    00628   if (image == SPR_EMPTY_BOUNDING_BOX) {
    00629     left = tmp_left = RemapCoords(x + w          , y + bb_offset_y, z + bb_offset_z).x;
    00630     right           = RemapCoords(x + bb_offset_x, y + h          , z + bb_offset_z).x + 1;
    00631     top  = tmp_top  = RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz         ).y;
    00632     bottom          = RemapCoords(x + w          , y + h          , z + bb_offset_z).y + 1;
    00633   } else {
    00634     const Sprite *spr = GetSprite(image & SPRITE_MASK, ST_NORMAL);
    00635     left = tmp_left = (pt.x += spr->x_offs);
    00636     right           = (pt.x +  spr->width );
    00637     top  = tmp_top  = (pt.y += spr->y_offs);
    00638     bottom          = (pt.y +  spr->height);
    00639   }
    00640
    00641   if (_draw_bounding_boxes && (image != SPR_EMPTY_BOUNDING_BOX)) {
    00642     /* Compute maximal extents of sprite and it's bounding box */
    00643     left   = min(left  , RemapCoords(x + w          , y + bb_offset_y, z + bb_offset_z).x);
    00644     right  = max(right , RemapCoords(x + bb_offset_x, y + h          , z + bb_offset_z).x + 1);
    00645     top    = min(top   , RemapCoords(x + bb_offset_x, y + bb_offset_y, z + dz         ).y);
    00646     bottom = max(bottom, RemapCoords(x + w          , y + h          , z + bb_offset_z).y + 1);
    00647   }
    00648
    00649   /* Do not add the sprite to the viewport, if it is outside */
    00650   if (left   >= _vd.dpi.left + _vd.dpi.width ||
    00651       right  <= _vd.dpi.left                 ||
    00652       top    >= _vd.dpi.top + _vd.dpi.height ||
    00653       bottom <= _vd.dpi.top) {
    00654     return;
    00655   }
    00656
    00657   ParentSpriteToDraw *ps = _vd.parent_sprites_to_draw.Append();
    00658   ps->x = tmp_x;
    00659   ps->y = tmp_y;
    00660
    00661   ps->left = tmp_left;
    00662   ps->top  = tmp_top;
    00663
    00664   ps->image = image;
    00665   ps->pal = pal;
    00666   ps->sub = sub;
    00667   ps->xmin = x + bb_offset_x;
    00668   ps->xmax = x + max(bb_offset_x, w) - 1;
    00669
    00670   ps->ymin = y + bb_offset_y;
    00671   ps->ymax = y + max(bb_offset_y, h) - 1;
    00672
    00673   ps->zmin = z + bb_offset_z;
    00674   ps->zmax = z + max(bb_offset_z, dz) - 1;
    00675
    00676   ps->comparison_done = false;
    00677   ps->first_child = -1;
    00678
    00679   _vd.last_child = &ps->first_child;
    00680
    00681   if (_vd.combine_sprites == 1) _vd.combine_sprites = 2;
    00682 }
    00683
    00684 void StartSpriteCombine()
    00685 {
    00686   _vd.combine_sprites = 1;
    00687 }
    00688
    00689 void EndSpriteCombine()
    00690 {
    00691   _vd.combine_sprites = 0;
    00692 }
    00693
    00704 void AddChildSpriteScreen(SpriteID image, SpriteID pal, int x, int y, bool transparent, const SubSprite *sub)
    00705 {
    00706   assert((image & SPRITE_MASK) < MAX_SPRITES);
    00707
    00708   /* If the ParentSprite was clipped by the viewport bounds, do not draw the ChildSprites either */
    00709   if (_vd.last_child == NULL) return;
    00710
    00711   /* make the sprites transparent with the right palette */
    00712   if (transparent) {
    00713     SetBit(image, PALETTE_MODIFIER_TRANSPARENT);
    00714     pal = PALETTE_TO_TRANSPARENT;
    00715   }
    00716
    00717   *_vd.last_child = _vd.child_screen_sprites_to_draw.Length();
    00718
    00719   ChildScreenSpriteToDraw *cs = _vd.child_screen_sprites_to_draw.Append();
    00720   cs->image = image;
    00721   cs->pal = pal;
    00722   cs->sub = sub;
    00723   cs->x = x;
    00724   cs->y = y;
    00725   cs->next = -1;
    00726
    00727   /* Append the sprite to the active ChildSprite list.
    00728    * If the active ParentSprite is a foundation, update last_foundation_child as well.
    00729    * Note: ChildSprites of foundations are NOT sequential in the vector, as selection sprites are added at last. */
    00730   if (_vd.last_foundation_child[0] == _vd.last_child) _vd.last_foundation_child[0] = &cs->next;
    00731   if (_vd.last_foundation_child[1] == _vd.last_child) _vd.last_foundation_child[1] = &cs->next;
    00732   _vd.last_child = &cs->next;
    00733 }
    00734
    00735 /* Returns a StringSpriteToDraw */
    00736 void AddStringToDraw(int x, int y, StringID string, uint64 params_1, uint64 params_2, uint16 colour, uint16 width)
    00737 {
    00738   StringSpriteToDraw *ss = _vd.string_sprites_to_draw.Append();
    00739   ss->string = string;
    00740   ss->x = x;
    00741   ss->y = y;
    00742   ss->params[0] = params_1;
    00743   ss->params[1] = params_2;
    00744   ss->width = width;
    00745   ss->colour = colour;
    00746 }
    00747
    00748
    00760 static void DrawSelectionSprite(SpriteID image, SpriteID pal, const TileInfo *ti, int z_offset, FoundationPart foundation_part)
    00761 {
    00762   /* FIXME: This is not totally valid for some autorail highlights, that extent over the edges of the tile. */
    00763   if (_vd.foundation[foundation_part] == -1) {
    00764     /* draw on real ground */
    00765     DrawGroundSpriteAt(image, pal, ti->x, ti->y, ti->z + z_offset);
    00766   } else {
    00767     /* draw on top of foundation */
    00768     AddChildSpriteToFoundation(image, pal, NULL, foundation_part, 0, -z_offset);
    00769   }
    00770 }
    00771
    00778 static void DrawTileSelectionRect(const TileInfo *ti, SpriteID pal)
    00779 {
    00780   if (!IsValidTile(ti->tile)) return;
    00781
    00782   SpriteID sel;
    00783   if (IsHalftileSlope(ti->tileh)) {
    00784     Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
    00785     SpriteID sel2 = SPR_HALFTILE_SELECTION_FLAT + halftile_corner;
    00786     DrawSelectionSprite(sel2, pal, ti, 7 + TILE_HEIGHT, FOUNDATION_PART_HALFTILE);
    00787
    00788     Corner opposite_corner = OppositeCorner(halftile_corner);
    00789     if (IsSteepSlope(ti->tileh)) {
    00790       sel = SPR_HALFTILE_SELECTION_DOWN;
    00791     } else {
    00792       sel = ((ti->tileh & SlopeWithOneCornerRaised(opposite_corner)) != 0 ? SPR_HALFTILE_SELECTION_UP : SPR_HALFTILE_SELECTION_FLAT);
    00793     }
    00794     sel += opposite_corner;
    00795   } else {
    00796     sel = SPR_SELECT_TILE + _tileh_to_sprite[ti->tileh];
    00797   }
    00798   DrawSelectionSprite(sel, pal, ti, 7, FOUNDATION_PART_NORMAL);
    00799 }
    00800
    00801 static bool IsPartOfAutoLine(int px, int py)
    00802 {
    00803   px -= _thd.selstart.x;
    00804   py -= _thd.selstart.y;
    00805
    00806   if ((_thd.drawstyle & ~HT_DIR_MASK) != HT_LINE) return false;
    00807
    00808   switch (_thd.drawstyle & HT_DIR_MASK) {
    00809     case HT_DIR_X:  return py == 0; // x direction
    00810     case HT_DIR_Y:  return px == 0; // y direction
    00811     case HT_DIR_HU: return px == -py || px == -py - 16; // horizontal upper
    00812     case HT_DIR_HL: return px == -py || px == -py + 16; // horizontal lower
    00813     case HT_DIR_VL: return px == py || px == py + 16; // vertival left
    00814     case HT_DIR_VR: return px == py || px == py - 16; // vertical right
    00815     default:
    00816       NOT_REACHED();
    00817   }
    00818 }
    00819
    00820 // [direction][side]
    00821 static const HighLightStyle _autorail_type[6][2] = {
    00822   { HT_DIR_X,  HT_DIR_X },
    00823   { HT_DIR_Y,  HT_DIR_Y },
    00824   { HT_DIR_HU, HT_DIR_HL },
    00825   { HT_DIR_HL, HT_DIR_HU },
    00826   { HT_DIR_VL, HT_DIR_VR },
    00827   { HT_DIR_VR, HT_DIR_VL }
    00828 };
    00829
    00830 #include "table/autorail.h"
    00831
    00838 static void DrawAutorailSelection(const TileInfo *ti, uint autorail_type)
    00839 {
    00840   SpriteID image;
    00841   SpriteID pal;
    00842   int offset;
    00843
    00844   FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
    00845   Slope autorail_tileh = RemoveHalftileSlope(ti->tileh);
    00846   if (IsHalftileSlope(ti->tileh)) {
    00847     static const uint _lower_rail[4] = { 5U, 2U, 4U, 3U };
    00848     Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
    00849     if (autorail_type != _lower_rail[halftile_corner]) {
    00850       foundation_part = FOUNDATION_PART_HALFTILE;
    00851       /* Here we draw the highlights of the "three-corners-raised"-slope. That looks ok to me. */
    00852       autorail_tileh = SlopeWithThreeCornersRaised(OppositeCorner(halftile_corner));
    00853     }
    00854   }
    00855
    00856   offset = _AutorailTilehSprite[autorail_tileh][autorail_type];
    00857   if (offset >= 0) {
    00858     image = SPR_AUTORAIL_BASE + offset;
    00859     pal = PAL_NONE;
    00860   } else {
    00861     image = SPR_AUTORAIL_BASE - offset;
    00862     pal = PALETTE_SEL_TILE_RED;
    00863   }
    00864
    00865   DrawSelectionSprite(image, _thd.make_square_red ? PALETTE_SEL_TILE_RED : pal, ti, 7, foundation_part);
    00866 }
    00867
    00872 static void DrawTileSelection(const TileInfo *ti)
    00873 {
    00874   /* Draw a red error square? */
    00875   bool is_redsq = _thd.redsq == ti->tile;
    00876   if (is_redsq) DrawTileSelectionRect(ti, PALETTE_TILE_RED_PULSATING);
    00877
    00878   /* no selection active? */
    00879   if (_thd.drawstyle == 0) return;
    00880
    00881   /* Inside the inner area? */
    00882   if (IsInsideBS(ti->x, _thd.pos.x, _thd.size.x) &&
    00883       IsInsideBS(ti->y, _thd.pos.y, _thd.size.y)) {
    00884     if (_thd.drawstyle & HT_RECT) {
    00885       if (!is_redsq) DrawTileSelectionRect(ti, _thd.make_square_red ? PALETTE_SEL_TILE_RED : PAL_NONE);
    00886     } else if (_thd.drawstyle & HT_POINT) {
    00887       /* Figure out the Z coordinate for the single dot. */
    00888       byte z = 0;
    00889       FoundationPart foundation_part = FOUNDATION_PART_NORMAL;
    00890       if (ti->tileh & SLOPE_N) {
    00891         z += TILE_HEIGHT;
    00892         if (RemoveHalftileSlope(ti->tileh) == SLOPE_STEEP_N) z += TILE_HEIGHT;
    00893       }
    00894       if (IsHalftileSlope(ti->tileh)) {
    00895         Corner halftile_corner = GetHalftileSlopeCorner(ti->tileh);
    00896         if ((halftile_corner == CORNER_W) || (halftile_corner == CORNER_E)) z += TILE_HEIGHT;
    00897         if (halftile_corner != CORNER_S) {
    00898           foundation_part = FOUNDATION_PART_HALFTILE;
    00899           if (IsSteepSlope(ti->tileh)) z -= TILE_HEIGHT;
    00900         }
    00901       }
    00902       DrawSelectionSprite(_cur_dpi->zoom <= ZOOM_LVL_DETAIL ? SPR_DOT : SPR_DOT_SMALL, PAL_NONE, ti, z, foundation_part);
    00903     } else if (_thd.drawstyle & HT_RAIL /*&& _thd.place_mode == VHM_RAIL*/) {
    00904       /* autorail highlight piece under cursor */
    00905       uint type = _thd.drawstyle & 0xF;
    00906       assert(type <= 5);
    00907       DrawAutorailSelection(ti, _autorail_type[type][0]);
    00908     } else if (IsPartOfAutoLine(ti->x, ti->y)) {
    00909       /* autorail highlighting long line */
    00910       int dir = _thd.drawstyle & ~0xF0;
    00911       uint side;
    00912
    00913       if (dir < 2) {
    00914         side = 0;
    00915       } else {
    00916         TileIndex start = TileVirtXY(_thd.selstart.x, _thd.selstart.y);
    00917         side = Delta(Delta(TileX(start), TileX(ti->tile)), Delta(TileY(start), TileY(ti->tile)));
    00918       }
    00919
    00920       DrawAutorailSelection(ti, _autorail_type[dir][side]);
    00921     }
    00922     return;
    00923   }
    00924
    00925   /* Check if it's inside the outer area? */
    00926   if (!is_redsq && _thd.outersize.x &&
    00927       _thd.size.x < _thd.size.x + _thd.outersize.x &&
    00928       IsInsideBS(ti->x, _thd.pos.x + _thd.offs.x, _thd.size.x + _thd.outersize.x) &&
    00929       IsInsideBS(ti->y, _thd.pos.y + _thd.offs.y, _thd.size.y + _thd.outersize.y)) {
    00930     /* Draw a blue rect. */
    00931     DrawTileSelectionRect(ti, PALETTE_SEL_TILE_BLUE);
    00932     return;
    00933   }
    00934 }
    00935
    00936 static void ViewportAddLandscape()
    00937 {
    00938   int x, y, width, height;
    00939   TileInfo ti;
    00940   bool direction;
    00941
    00942   _cur_ti = &ti;
    00943
    00944   /* Transform into tile coordinates and round to closest full tile */
    00945   x = ((_vd.dpi.top >> 1) - (_vd.dpi.left >> 2)) & ~0xF;
    00946   y = ((_vd.dpi.top >> 1) + (_vd.dpi.left >> 2) - 0x10) & ~0xF;
    00947
    00948   /* determine size of area */
    00949   {
    00950     Point pt = RemapCoords(x, y, 241);
    00951     width = (_vd.dpi.left + _vd.dpi.width - pt.x + 95) >> 6;
    00952     height = (_vd.dpi.top + _vd.dpi.height - pt.y) >> 5 << 1;
    00953   }
    00954
    00955   assert(width > 0);
    00956   assert(height > 0);
    00957
    00958   direction = false;
    00959
    00960   do {
    00961     int width_cur = width;
    00962     int x_cur = x;
    00963     int y_cur = y;
    00964
    00965     do {
    00966       TileType tt = MP_VOID;
    00967
    00968       ti.x = x_cur;
    00969       ti.y = y_cur;
    00970
    00971       ti.z = 0;
    00972
    00973       ti.tileh = SLOPE_FLAT;
    00974       ti.tile = INVALID_TILE;
    00975
    00976       if (0 <= x_cur && x_cur < (int)MapMaxX() * TILE_SIZE &&
    00977           0 <= y_cur && y_cur < (int)MapMaxY() * TILE_SIZE) {
    00978         TileIndex tile = TileVirtXY(x_cur, y_cur);
    00979
    00980         if (!_settings_game.construction.freeform_edges || (TileX(tile) != 0 && TileY(tile) != 0)) {
    00981           if (x_cur == ((int)MapMaxX() - 1) * TILE_SIZE || y_cur == ((int)MapMaxY() - 1) * TILE_SIZE) {
    00982             uint maxh = max<uint>(TileHeight(tile), 1);
    00983             for (uint h = 0; h < maxh; h++) {
    00984               DrawGroundSpriteAt(SPR_SHADOW_CELL, PAL_NONE, ti.x, ti.y, h * TILE_HEIGHT);
    00985             }
    00986           }
    00987
    00988           ti.tile = tile;
    00989           ti.tileh = GetTileSlope(tile, &ti.z);
    00990           tt = GetTileType(tile);
    00991         }
    00992       }
    00993
    00994       _vd.foundation_part = FOUNDATION_PART_NONE;
    00995       _vd.foundation[0] = -1;
    00996       _vd.foundation[1] = -1;
    00997       _vd.last_foundation_child[0] = NULL;
    00998       _vd.last_foundation_child[1] = NULL;
    00999
    01000       _tile_type_procs[tt]->draw_tile_proc(&ti);
    01001
    01002       if ((x_cur == (int)MapMaxX() * TILE_SIZE && IsInsideMM(y_cur, 0, MapMaxY() * TILE_SIZE + 1)) ||
    01003         (y_cur == (int)MapMaxY() * TILE_SIZE && IsInsideMM(x_cur, 0, MapMaxX() * TILE_SIZE + 1))) {
    01004         TileIndex tile = TileVirtXY(x_cur, y_cur);
    01005         ti.tile = tile;
    01006         ti.tileh = GetTileSlope(tile, &ti.z);
    01007         tt = GetTileType(tile);
    01008       }
    01009       if (ti.tile != INVALID_TILE) DrawTileSelection(&ti);
    01010
    01011       y_cur += 0x10;
    01012       x_cur -= 0x10;
    01013     } while (--width_cur);
    01014
    01015     if ((direction ^= 1) != 0) {
    01016       y += 0x10;
    01017     } else {
    01018       x += 0x10;
    01019     }
    01020   } while (--height);
    01021 }
    01022
    01023
    01024 static void ViewportAddTownNames(DrawPixelInfo *dpi)
    01025 {
    01026   Town *t;
    01027   int left, top, right, bottom;
    01028
    01029   if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES) || _game_mode == GM_MENU)
    01030     return;
    01031
    01032   left = dpi->left;
    01033   top = dpi->top;
    01034   right = left + dpi->width;
    01035   bottom = top + dpi->height;
    01036
    01037   switch (dpi->zoom) {
    01038     case ZOOM_LVL_NORMAL:
    01039       FOR_ALL_TOWNS(t) {
    01040         if (bottom > t->sign.top &&
    01041             top    < t->sign.top + 12 &&
    01042             right  > t->sign.left &&
    01043             left   < t->sign.left + t->sign.width_1) {
    01044           AddStringToDraw(t->sign.left + 1, t->sign.top + 1,
    01045             _settings_client.gui.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL,
    01046             t->index, t->population);
    01047         }
    01048       }
    01049       break;
    01050
    01051     case ZOOM_LVL_OUT_2X:
    01052       right += 2;
    01053       bottom += 2;
    01054
    01055       FOR_ALL_TOWNS(t) {
    01056         if (bottom > t->sign.top &&
    01057             top    < t->sign.top + 24 &&
    01058             right  > t->sign.left &&
    01059             left   < t->sign.left + t->sign.width_1 * 2) {
    01060           AddStringToDraw(t->sign.left + 1, t->sign.top + 1,
    01061             _settings_client.gui.population_in_label ? STR_TOWN_LABEL_POP : STR_TOWN_LABEL,
    01062             t->index, t->population);
    01063         }
    01064       }
    01065       break;
    01066
    01067     case ZOOM_LVL_OUT_4X:
    01068     case ZOOM_LVL_OUT_8X:
    01069       right += ScaleByZoom(1, dpi->zoom);
    01070       bottom += ScaleByZoom(1, dpi->zoom) + 1;
    01071
    01072       FOR_ALL_TOWNS(t) {
    01073         if (bottom > t->sign.top &&
    01074             top    < t->sign.top + ScaleByZoom(12, dpi->zoom) &&
    01075             right  > t->sign.left &&
    01076             left   < t->sign.left + ScaleByZoom(t->sign.width_2, dpi->zoom)) {
    01077           AddStringToDraw(t->sign.left + 5, t->sign.top + 1, STR_TOWN_LABEL_TINY_BLACK, t->index, 0);
    01078           AddStringToDraw(t->sign.left + 1, t->sign.top - 3, STR_TOWN_LABEL_TINY_WHITE, t->index, 0);
    01079         }
    01080       }
    01081       break;
    01082
    01083     default: NOT_REACHED();
    01084   }
    01085 }
    01086
    01087
    01088 static void AddStation(const Station *st, StringID str, uint16 width)
    01089 {
    01090   AddStringToDraw(st->sign.left + 1, st->sign.top + 1, str, st->index, st->facilities, (st->owner == OWNER_NONE || st->facilities == 0) ? 0xE : _company_colours[st->owner], width);
    01091 }
    01092
    01093
    01094 static void ViewportAddStationNames(DrawPixelInfo *dpi)
    01095 {
    01096   int left, top, right, bottom;
    01097   const Station *st;
    01098
    01099   if (!HasBit(_display_opt, DO_SHOW_STATION_NAMES) || _game_mode == GM_MENU)
    01100     return;
    01101
    01102   left = dpi->left;
    01103   top = dpi->top;
    01104   right = left + dpi->width;
    01105   bottom = top + dpi->height;
    01106
    01107   switch (dpi->zoom) {
    01108     case ZOOM_LVL_NORMAL:
    01109       FOR_ALL_STATIONS(st) {
    01110         if (bottom > st->sign.top &&
    01111             top    < st->sign.top + 12 &&
    01112             right  > st->sign.left &&
    01113             left   < st->sign.left + st->sign.width_1) {
    01114           AddStation(st, STR_305C_0, st->sign.width_1);
    01115         }
    01116       }
    01117       break;
    01118
    01119     case ZOOM_LVL_OUT_2X:
    01120       right += 2;
    01121       bottom += 2;
    01122       FOR_ALL_STATIONS(st) {
    01123         if (bottom > st->sign.top &&
    01124             top    < st->sign.top + 24 &&
    01125             right  > st->sign.left &&
    01126             left   < st->sign.left + st->sign.width_1 * 2) {
    01127           AddStation(st, STR_305C_0, st->sign.width_1);
    01128         }
    01129       }
    01130       break;
    01131
    01132     case ZOOM_LVL_OUT_4X:
    01133     case ZOOM_LVL_OUT_8X:
    01134       right += ScaleByZoom(1, dpi->zoom);
    01135       bottom += ScaleByZoom(1, dpi->zoom) + 1;
    01136
    01137       FOR_ALL_STATIONS(st) {
    01138         if (bottom > st->sign.top &&
    01139             top    < st->sign.top + ScaleByZoom(12, dpi->zoom) &&
    01140             right  > st->sign.left &&
    01141             left   < st->sign.left + ScaleByZoom(st->sign.width_2, dpi->zoom)) {
    01142           AddStation(st, STR_STATION_SIGN_TINY, st->sign.width_2 | 0x8000);
    01143         }
    01144       }
    01145       break;
    01146
    01147     default: NOT_REACHED();
    01148   }
    01149 }
    01150
    01151
    01152 static void AddSign(const Sign *si, StringID str, uint16 width)
    01153 {
    01154   AddStringToDraw(si->sign.left + 1, si->sign.top + 1, str, si->index, 0, (si->owner == OWNER_NONE) ? 14 : _company_colours[si->owner], width);
    01155 }
    01156
    01157
    01158 static void ViewportAddSigns(DrawPixelInfo *dpi)
    01159 {
    01160   const Sign *si;
    01161   int left, top, right, bottom;
    01162
    01163   /* Signs are turned off or are invisible */
    01164   if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS)) return;
    01165
    01166   left = dpi->left;
    01167   top = dpi->top;
    01168   right = left + dpi->width;
    01169   bottom = top + dpi->height;
    01170
    01171   switch (dpi->zoom) {
    01172     case ZOOM_LVL_NORMAL:
    01173       FOR_ALL_SIGNS(si) {
    01174         if (bottom > si->sign.top &&
    01175             top    < si->sign.top + 12 &&
    01176             right  > si->sign.left &&
    01177             left   < si->sign.left + si->sign.width_1) {
    01178           AddSign(si, STR_2806, si->sign.width_1);
    01179         }
    01180       }
    01181       break;
    01182
    01183     case ZOOM_LVL_OUT_2X:
    01184       right += 2;
    01185       bottom += 2;
    01186       FOR_ALL_SIGNS(si) {
    01187         if (bottom > si->sign.top &&
    01188             top    < si->sign.top + 24 &&
    01189             right  > si->sign.left &&
    01190             left   < si->sign.left + si->sign.width_1 * 2) {
    01191           AddSign(si, STR_2806, si->sign.width_1);
    01192         }
    01193       }
    01194       break;
    01195
    01196     case ZOOM_LVL_OUT_4X:
    01197     case ZOOM_LVL_OUT_8X:
    01198       right += ScaleByZoom(1, dpi->zoom);
    01199       bottom += ScaleByZoom(1, dpi->zoom) + 1;
    01200
    01201       FOR_ALL_SIGNS(si) {
    01202         if (bottom > si->sign.top &&
    01203             top    < si->sign.top + ScaleByZoom(12, dpi->zoom) &&
    01204             right  > si->sign.left &&
    01205             left   < si->sign.left + ScaleByZoom(si->sign.width_2, dpi->zoom)) {
    01206           AddSign(si, IsTransparencySet(TO_SIGNS) ? STR_2002_WHITE : STR_2002, si->sign.width_2 | 0x8000);
    01207         }
    01208       }
    01209       break;
    01210
    01211     default: NOT_REACHED();
    01212   }
    01213 }
    01214
    01215
    01216 static void AddWaypoint(const Waypoint *wp, StringID str, uint16 width)
    01217 {
    01218   AddStringToDraw(wp->sign.left + 1, wp->sign.top + 1, str, wp->index, 0, (wp->deleted ? 0xE : _company_colours[wp->owner]), width);
    01219 }
    01220
    01221
    01222 static void ViewportAddWaypoints(DrawPixelInfo *dpi)
    01223 {
    01224   const Waypoint *wp;
    01225   int left, top, right, bottom;
    01226
    01227   if (!HasBit(_display_opt, DO_WAYPOINTS))
    01228     return;
    01229
    01230   left = dpi->left;
    01231   top = dpi->top;
    01232   right = left + dpi->width;
    01233   bottom = top + dpi->height;
    01234
    01235   switch (dpi->zoom) {
    01236     case ZOOM_LVL_NORMAL:
    01237       FOR_ALL_WAYPOINTS(wp) {
    01238         if (bottom > wp->sign.top &&
    01239             top    < wp->sign.top + 12 &&
    01240             right  > wp->sign.left &&
    01241             left   < wp->sign.left + wp->sign.width_1) {
    01242           AddWaypoint(wp, STR_WAYPOINT_VIEWPORT, wp->sign.width_1);
    01243         }
    01244       }
    01245       break;
    01246
    01247     case ZOOM_LVL_OUT_2X:
    01248       right += 2;
    01249       bottom += 2;
    01250       FOR_ALL_WAYPOINTS(wp) {
    01251         if (bottom > wp->sign.top &&
    01252             top    < wp->sign.top + 24 &&
    01253             right  > wp->sign.left &&
    01254             left   < wp->sign.left + wp->sign.width_1 * 2) {
    01255           AddWaypoint(wp, STR_WAYPOINT_VIEWPORT, wp->sign.width_1);
    01256         }
    01257       }
    01258       break;
    01259
    01260     case ZOOM_LVL_OUT_4X:
    01261     case ZOOM_LVL_OUT_8X:
    01262       right += ScaleByZoom(1, dpi->zoom);
    01263       bottom += ScaleByZoom(1, dpi->zoom) + 1;
    01264
    01265       FOR_ALL_WAYPOINTS(wp) {
    01266         if (bottom > wp->sign.top &&
    01267             top    < wp->sign.top + ScaleByZoom(12, dpi->zoom) &&
    01268             right  > wp->sign.left &&
    01269             left   < wp->sign.left + ScaleByZoom(wp->sign.width_2, dpi->zoom)) {
    01270           AddWaypoint(wp, STR_WAYPOINT_VIEWPORT_TINY, wp->sign.width_2 | 0x8000);
    01271         }
    01272       }
    01273       break;
    01274
    01275     default: NOT_REACHED();
    01276   }
    01277 }
    01278
    01279 void UpdateViewportSignPos(ViewportSign *sign, int left, int top, StringID str)
    01280 {
    01281   char buffer[256];
    01282   uint w;
    01283
    01284   sign->top = top;
    01285
    01286   GetString(buffer, str, lastof(buffer));
    01287   w = GetStringBoundingBox(buffer).width + 3;
    01288   sign->width_1 = w;
    01289   sign->left = left - w / 2;
    01290
    01291   /* zoomed out version */
    01292   _cur_fontsize = FS_SMALL;
    01293   w = GetStringBoundingBox(buffer).width + 3;
    01294   _cur_fontsize = FS_NORMAL;
    01295   sign->width_2 = w;
    01296 }
    01297
    01298
    01299 static void ViewportDrawTileSprites(const TileSpriteToDrawVector *tstdv)
    01300 {
    01301   const TileSpriteToDraw *tsend = tstdv->End();
    01302   for (const TileSpriteToDraw *ts = tstdv->Begin(); ts != tsend; ++ts) {
    01303     Point pt = RemapCoords(ts->x, ts->y, ts->z);
    01304     DrawSprite(ts->image, ts->pal, pt.x, pt.y, ts->sub);
    01305   }
    01306 }
    01307
    01309 static void ViewportSortParentSprites(ParentSpriteToSortVector *psdv)
    01310 {
    01311   ParentSpriteToDraw **psdvend = psdv->End();
    01312   ParentSpriteToDraw **psd = psdv->Begin();
    01313   while (psd != psdvend) {
    01314     ParentSpriteToDraw *ps = *psd;
    01315
    01316     if (ps->comparison_done) {
    01317       psd++;
    01318       continue;
    01319     }
    01320
    01321     ps->comparison_done = true;
    01322
    01323     for (ParentSpriteToDraw **psd2 = psd + 1; psd2 != psdvend; psd2++) {
    01324       ParentSpriteToDraw *ps2 = *psd2;
    01325
    01326       if (ps2->comparison_done) continue;
    01327
    01328       /* Decide which comparator to use, based on whether the bounding
    01329        * boxes overlap
    01330        */
    01331       if (ps->xmax >= ps2->xmin && ps->xmin <= ps2->xmax && // overlap in X?
    01332           ps->ymax >= ps2->ymin && ps->ymin <= ps2->ymax && // overlap in Y?
    01333           ps->zmax >= ps2->zmin && ps->zmin <= ps2->zmax) { // overlap in Z?
    01334         /* Use X+Y+Z as the sorting order, so sprites closer to the bottom of
    01335          * the screen and with higher Z elevation, are drawn in front.
    01336          * Here X,Y,Z are the coordinates of the "center of mass" of the sprite,
    01337          * i.e. X=(left+right)/2, etc.
    01338          * However, since we only care about order, don't actually divide / 2
    01339          */
    01340         if (ps->xmin + ps->xmax + ps->ymin + ps->ymax + ps->zmin + ps->zmax <=
    01341             ps2->xmin + ps2->xmax + ps2->ymin + ps2->ymax + ps2->zmin + ps2->zmax) {
    01342           continue;
    01343         }
    01344       } else {
    01345         /* We only change the order, if it is definite.
    01346          * I.e. every single order of X, Y, Z says ps2 is behind ps or they overlap.
    01347          * That is: If one partial order says ps behind ps2, do not change the order.
    01348          */
    01349         if (ps->xmax < ps2->xmin ||
    01350             ps->ymax < ps2->ymin ||
    01351             ps->zmax < ps2->zmin) {
    01352           continue;
    01353         }
    01354       }
    01355
    01356       /* Move ps2 in front of ps */
    01357       ParentSpriteToDraw *temp = ps2;
    01358       for (ParentSpriteToDraw **psd3 = psd2; psd3 > psd; psd3--) {
    01359         *psd3 = *(psd3 - 1);
    01360       }
    01361       *psd = temp;
    01362     }
    01363   }
    01364 }
    01365
    01366 static void ViewportDrawParentSprites(const ParentSpriteToSortVector *psd, const ChildScreenSpriteToDrawVector *csstdv)
    01367 {
    01368   const ParentSpriteToDraw * const *psd_end = psd->End();
    01369   for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
    01370     const ParentSpriteToDraw *ps = *it;
    01371     if (ps->image != SPR_EMPTY_BOUNDING_BOX) DrawSprite(ps->image, ps->pal, ps->x, ps->y, ps->sub);
    01372
    01373     int child_idx = ps->first_child;
    01374     while (child_idx >= 0) {
    01375       const ChildScreenSpriteToDraw *cs = csstdv->Get(child_idx);
    01376       child_idx = cs->next;
    01377       DrawSprite(cs->image, cs->pal, ps->left + cs->x, ps->top + cs->y, cs->sub);
    01378     }
    01379   }
    01380 }
    01381
    01386 static void ViewportDrawBoundingBoxes(const ParentSpriteToSortVector *psd)
    01387 {
    01388   const ParentSpriteToDraw * const *psd_end = psd->End();
    01389   for (const ParentSpriteToDraw * const *it = psd->Begin(); it != psd_end; it++) {
    01390     const ParentSpriteToDraw *ps = *it;
    01391     Point pt1 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmax + 1); // top front corner
    01392     Point pt2 = RemapCoords(ps->xmin    , ps->ymax + 1, ps->zmax + 1); // top left corner
    01393     Point pt3 = RemapCoords(ps->xmax + 1, ps->ymin    , ps->zmax + 1); // top right corner
    01394     Point pt4 = RemapCoords(ps->xmax + 1, ps->ymax + 1, ps->zmin    ); // bottom front corner
    01395
    01396     DrawBox(        pt1.x,         pt1.y,
    01397             pt2.x - pt1.x, pt2.y - pt1.y,
    01398             pt3.x - pt1.x, pt3.y - pt1.y,
    01399             pt4.x - pt1.x, pt4.y - pt1.y);
    01400   }
    01401 }
    01402
    01403 static void ViewportDrawStrings(DrawPixelInfo *dpi, const StringSpriteToDrawVector *sstdv)
    01404 {
    01405   DrawPixelInfo dp;
    01406   ZoomLevel zoom;
    01407
    01408   _cur_dpi = &dp;
    01409   dp = *dpi;
    01410
    01411   zoom = dp.zoom;
    01412   dp.zoom = ZOOM_LVL_NORMAL;
    01413
    01414   dp.left   = UnScaleByZoom(dp.left,   zoom);
    01415   dp.top    = UnScaleByZoom(dp.top,    zoom);
    01416   dp.width  = UnScaleByZoom(dp.width,  zoom);
    01417   dp.height = UnScaleByZoom(dp.height, zoom);
    01418
    01419   const StringSpriteToDraw *ssend = sstdv->End();
    01420   for (const StringSpriteToDraw *ss = sstdv->Begin(); ss != ssend; ++ss) {
    01421     TextColour colour;
    01422
    01423     if (ss->width != 0) {
    01424       /* Do not draw signs nor station names if they are set invisible */
    01425       if (IsInvisibilitySet(TO_SIGNS) && ss->string != STR_2806) continue;
    01426
    01427       int x = UnScaleByZoom(ss->x, zoom) - 1;
    01428       int y = UnScaleByZoom(ss->y, zoom) - 1;
    01429       int bottom = y + 11;
    01430       int w = ss->width;
    01431
    01432       if (w & 0x8000) {
    01433         w &= ~0x8000;
    01434         y--;
    01435         bottom -= 6;
    01436         w -= 3;
    01437       }
    01438
    01439     /* Draw the rectangle if 'tranparent station signs' is off,
    01440      * or if we are drawing a general text sign (STR_2806) */
    01441       if (!IsTransparencySet(TO_SIGNS) || ss->string == STR_2806) {
    01442         DrawFrameRect(
    01443           x, y, x + w, bottom, (Colours)ss->colour,
    01444           IsTransparencySet(TO_SIGNS) ? FR_TRANSPARENT : FR_NONE
    01445         );
    01446       }
    01447     }
    01448
    01449     SetDParam(0, ss->params[0]);
    01450     SetDParam(1, ss->params[1]);
    01451     /* if we didn't draw a rectangle, or if transparant building is on,
    01452      * draw the text in the colour the rectangle would have */
    01453     if (IsTransparencySet(TO_SIGNS) && ss->string != STR_2806 && ss->width != 0) {
    01454       /* Real colours need the IS_PALETTE_COLOUR flag
    01455        * otherwise colours from _string_colourmap are assumed. */
    01456       colour = (TextColour)_colour_gradient[ss->colour][6] | IS_PALETTE_COLOUR;
    01457     } else {
    01458       colour = TC_BLACK;
    01459     }
    01460     DrawString(
    01461       UnScaleByZoom(ss->x, zoom), UnScaleByZoom(ss->y, zoom) - (ss->width & 0x8000 ? 2 : 0),
    01462       ss->string, colour
    01463     );
    01464   }
    01465 }
    01466
    01467 void ViewportDoDraw(const ViewPort *vp, int left, int top, int right, int bottom)
    01468 {
    01469   DrawPixelInfo *old_dpi = _cur_dpi;
    01470   _cur_dpi = &_vd.dpi;
    01471
    01472   _vd.dpi.zoom = vp->zoom;
    01473   int mask = ScaleByZoom(-1, vp->zoom);
    01474
    01475   _vd.combine_sprites = 0;
    01476
    01477   _vd.dpi.width = (right - left) & mask;
    01478   _vd.dpi.height = (bottom - top) & mask;
    01479   _vd.dpi.left = left & mask;
    01480   _vd.dpi.top = top & mask;
    01481   _vd.dpi.pitch = old_dpi->pitch;
    01482   _vd.last_child = NULL;
    01483
    01484   int x = UnScaleByZoom(_vd.dpi.left - (vp->virtual_left & mask), vp->zoom) + vp->left;
    01485   int y = UnScaleByZoom(_vd.dpi.top - (vp->virtual_top & mask), vp->zoom) + vp->top;
    01486
    01487   _vd.dpi.dst_ptr = BlitterFactoryBase::GetCurrentBlitter()->MoveTo(old_dpi->dst_ptr, x - old_dpi->left, y - old_dpi->top);
    01488
    01489   ViewportAddLandscape();
    01490   ViewportAddVehicles(&_vd.dpi);
    01491
    01492   ViewportAddTownNames(&_vd.dpi);
    01493   ViewportAddStationNames(&_vd.dpi);
    01494   ViewportAddSigns(&_vd.dpi);
    01495   ViewportAddWaypoints(&_vd.dpi);
    01496
    01497   DrawTextEffects(&_vd.dpi);
    01498
    01499   if (_vd.tile_sprites_to_draw.Length() != 0) ViewportDrawTileSprites(&_vd.tile_sprites_to_draw);
    01500
    01501   ParentSpriteToDraw *psd_end = _vd.parent_sprites_to_draw.End();
    01502   for (ParentSpriteToDraw *it = _vd.parent_sprites_to_draw.Begin(); it != psd_end; it++) {
    01503     *_vd.parent_sprites_to_sort.Append() = it;
    01504   }
    01505
    01506   ViewportSortParentSprites(&_vd.parent_sprites_to_sort);
    01507   ViewportDrawParentSprites(&_vd.parent_sprites_to_sort, &_vd.child_screen_sprites_to_draw);
    01508
    01509   if (_draw_bounding_boxes) ViewportDrawBoundingBoxes(&_vd.parent_sprites_to_sort);
    01510
    01511   if (_vd.string_sprites_to_draw.Length() != 0) ViewportDrawStrings(&_vd.dpi, &_vd.string_sprites_to_draw);
    01512
    01513   _cur_dpi = old_dpi;
    01514
    01515   _vd.string_sprites_to_draw.Clear();
    01516   _vd.tile_sprites_to_draw.Clear();
    01517   _vd.parent_sprites_to_draw.Clear();
    01518   _vd.parent_sprites_to_sort.Clear();
    01519   _vd.child_screen_sprites_to_draw.Clear();
    01520 }
    01521
    01524 static void ViewportDrawChk(const ViewPort *vp, int left, int top, int right, int bottom)
    01525 {
    01526   if (ScaleByZoom(bottom - top, vp->zoom) * ScaleByZoom(right - left, vp->zoom) > 180000) {
    01527     if ((bottom - top) > (right - left)) {
    01528       int t = (top + bottom) >> 1;
    01529       ViewportDrawChk(vp, left, top, right, t);
    01530       ViewportDrawChk(vp, left, t, right, bottom);
    01531     } else {
    01532       int t = (left + right) >> 1;
    01533       ViewportDrawChk(vp, left, top, t, bottom);
    01534       ViewportDrawChk(vp, t, top, right, bottom);
    01535     }
    01536   } else {
    01537     ViewportDoDraw(vp,
    01538       ScaleByZoom(left - vp->left, vp->zoom) + vp->virtual_left,
    01539       ScaleByZoom(top - vp->top, vp->zoom) + vp->virtual_top,
    01540       ScaleByZoom(right - vp->left, vp->zoom) + vp->virtual_left,
    01541       ScaleByZoom(bottom - vp->top, vp->zoom) + vp->virtual_top
    01542     );
    01543   }
    01544 }
    01545
    01546 static inline void ViewportDraw(const ViewPort *vp, int left, int top, int right, int bottom)
    01547 {
    01548   if (right <= vp->left || bottom <= vp->top) return;
    01549
    01550   if (left >= vp->left + vp->width) return;
    01551
    01552   if (left < vp->left) left = vp->left;
    01553   if (right > vp->left + vp->width) right = vp->left + vp->width;
    01554
    01555   if (top >= vp->top + vp->height) return;
    01556
    01557   if (top < vp->top) top = vp->top;
    01558   if (bottom > vp->top + vp->height) bottom = vp->top + vp->height;
    01559
    01560   ViewportDrawChk(vp, left, top, right, bottom);
    01561 }
    01562
    01566 void Window::DrawViewport() const
    01567 {
    01568   DrawPixelInfo *dpi = _cur_dpi;
    01569
    01570   dpi->left += this->left;
    01571   dpi->top += this->top;
    01572
    01573   ViewportDraw(this->viewport, dpi->left, dpi->top, dpi->left + dpi->width, dpi->top + dpi->height);
    01574
    01575   dpi->left -= this->left;
    01576   dpi->top -= this->top;
    01577 }
    01578
    01579 static inline void ClampViewportToMap(const ViewPort *vp, int &x, int &y)
    01580 {
    01581   /* Centre of the viewport is hot spot */
    01582   x += vp->virtual_width / 2;
    01583   y += vp->virtual_height / 2;
    01584
    01585   /* Convert viewport coordinates to map coordinates
    01586    * Calculation is scaled by 4 to avoid rounding errors */
    01587   int vx = -x + y * 2;
    01588   int vy =  x + y * 2;
    01589
    01590   /* clamp to size of map */
    01591   vx = Clamp(vx, 0, MapMaxX() * TILE_SIZE * 4);
    01592   vy = Clamp(vy, 0, MapMaxY() * TILE_SIZE * 4);
    01593
    01594   /* Convert map coordinates to viewport coordinates */
    01595   x = (-vx + vy) / 2;
    01596   y = ( vx + vy) / 4;
    01597
    01598   /* Remove centreing */
    01599   x -= vp->virtual_width / 2;
    01600   y -= vp->virtual_height / 2;
    01601 }
    01602
    01603 void UpdateViewportPosition(Window *w)
    01604 {
    01605   const ViewPort *vp = w->viewport;
    01606
    01607   if (w->viewport->follow_vehicle != INVALID_VEHICLE) {
    01608     const Vehicle *veh = GetVehicle(w->viewport->follow_vehicle);
    01609     Point pt = MapXYZToViewport(vp, veh->x_pos, veh->y_pos, veh->z_pos);
    01610
    01611     w->viewport->scrollpos_x = pt.x;
    01612     w->viewport->scrollpos_y = pt.y;
    01613     SetViewportPosition(w, pt.x, pt.y);
    01614   } else {
    01615     /* Ensure the destination location is within the map */
    01616     ClampViewportToMap(vp, w->viewport->dest_scrollpos_x, w->viewport->dest_scrollpos_y);
    01617
    01618     int delta_x = w->viewport->dest_scrollpos_x - w->viewport->scrollpos_x;
    01619     int delta_y = w->viewport->dest_scrollpos_y - w->viewport->scrollpos_y;
    01620
    01621     if (delta_x != 0 || delta_y != 0) {
    01622       if (_settings_client.gui.smooth_scroll) {
    01623         int max_scroll = ScaleByMapSize1D(512);
    01624         /* Not at our desired positon yet... */
    01625         w->viewport->scrollpos_x += Clamp(delta_x / 4, -max_scroll, max_scroll);
    01626         w->viewport->scrollpos_y += Clamp(delta_y / 4, -max_scroll, max_scroll);
    01627       } else {
    01628         w->viewport->scrollpos_x = w->viewport->dest_scrollpos_x;
    01629         w->viewport->scrollpos_y = w->viewport->dest_scrollpos_y;
    01630       }
    01631     }
    01632
    01633     ClampViewportToMap(vp, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
    01634
    01635     SetViewportPosition(w, w->viewport->scrollpos_x, w->viewport->scrollpos_y);
    01636   }
    01637 }
    01638
    01648 static void MarkViewportDirty(const ViewPort *vp, int left, int top, int right, int bottom)
    01649 {
    01650   right -= vp->virtual_left;
    01651   if (right <= 0) return;
    01652
    01653   bottom -= vp->virtual_top;
    01654   if (bottom <= 0) return;
    01655
    01656   left = max(0, left - vp->virtual_left);
    01657
    01658   if (left >= vp->virtual_width) return;
    01659
    01660   top = max(0, top - vp->virtual_top);
    01661
    01662   if (top >= vp->virtual_height) return;
    01663
    01664   SetDirtyBlocks(
    01665     UnScaleByZoomLower(left, vp->zoom) + vp->left,
    01666     UnScaleByZoomLower(top, vp->zoom) + vp->top,
    01667     UnScaleByZoom(right, vp->zoom) + vp->left + 1,
    01668     UnScaleByZoom(bottom, vp->zoom) + vp->top + 1
    01669   );
    01670 }
    01671
    01680 void MarkAllViewportsDirty(int left, int top, int right, int bottom)
    01681 {
    01682   Window *w;
    01683   FOR_ALL_WINDOWS_FROM_BACK(w) {
    01684     ViewPort *vp = w->viewport;
    01685     if (vp != NULL) {
    01686       assert(vp->width != 0);
    01687       MarkViewportDirty(vp, left, top, right, bottom);
    01688     }
    01689   }
    01690 }
    01691
    01692 void MarkTileDirtyByTile(TileIndex tile)
    01693 {
    01694   Point pt = RemapCoords(TileX(tile) * TILE_SIZE, TileY(tile) * TILE_SIZE, GetTileZ(tile));
    01695   MarkAllViewportsDirty(
    01696     pt.x - 31,
    01697     pt.y - 122,
    01698     pt.x - 31 + 67,
    01699     pt.y - 122 + 154
    01700   );
    01701 }
    01702
    01703 void MarkTileDirty(int x, int y)
    01704 {
    01705   uint z = 0;
    01706   Point pt;
    01707
    01708   if (IsInsideMM(x, 0, MapSizeX() * TILE_SIZE) &&
    01709       IsInsideMM(y, 0, MapSizeY() * TILE_SIZE))
    01710     z = GetTileZ(TileVirtXY(x, y));
    01711   pt = RemapCoords(x, y, z);
    01712
    01713   MarkAllViewportsDirty(
    01714     pt.x - 31,
    01715     pt.y - 122,
    01716     pt.x - 31 + 67,
    01717     pt.y - 122 + 154
    01718   );
    01719 }
    01720
    01729 static void SetSelectionTilesDirty()
    01730 {
    01731   int y_size, x_size;
    01732   int x = _thd.pos.x;
    01733   int y = _thd.pos.y;
    01734
    01735   x_size = _thd.size.x;
    01736   y_size = _thd.size.y;
    01737
    01738   if (_thd.outersize.x) {
    01739     x_size += _thd.outersize.x;
    01740     x += _thd.offs.x;
    01741     y_size += _thd.outersize.y;
    01742     y += _thd.offs.y;
    01743   }
    01744
    01745   assert(x_size > 0);
    01746   assert(y_size > 0);
    01747
    01748   x_size += x;
    01749   y_size += y;
    01750
    01751   do {
    01752     int y_bk = y;
    01753     do {
    01754       MarkTileDirty(x, y);
    01755     } while ( (y += TILE_SIZE) != y_size);
    01756     y = y_bk;
    01757   } while ( (x += TILE_SIZE) != x_size);
    01758 }
    01759
    01760
    01761 void SetSelectionRed(bool b)
    01762 {
    01763   _thd.make_square_red = b;
    01764   SetSelectionTilesDirty();
    01765 }
    01766
    01767
    01768 static bool CheckClickOnTown(const ViewPort *vp, int x, int y)
    01769 {
    01770   const Town *t;
    01771
    01772   if (!HasBit(_display_opt, DO_SHOW_TOWN_NAMES)) return false;
    01773
    01774   switch (vp->zoom) {
    01775     case ZOOM_LVL_NORMAL:
    01776       x = x - vp->left + vp->virtual_left;
    01777       y = y - vp->top  + vp->virtual_top;
    01778       FOR_ALL_TOWNS(t) {
    01779         if (y >= t->sign.top &&
    01780             y < t->sign.top + 12 &&
    01781             x >= t->sign.left &&
    01782             x < t->sign.left + t->sign.width_1) {
    01783           ShowTownViewWindow(t->index);
    01784           return true;
    01785         }
    01786       }
    01787       break;
    01788
    01789     case ZOOM_LVL_OUT_2X:
    01790       x = (x - vp->left + 1) * 2 + vp->virtual_left;
    01791       y = (y - vp->top  + 1) * 2 + vp->virtual_top;
    01792       FOR_ALL_TOWNS(t) {
    01793         if (y >= t->sign.top &&
    01794             y < t->sign.top + 24 &&
    01795             x >= t->sign.left &&
    01796             x < t->sign.left + t->sign.width_1 * 2) {
    01797           ShowTownViewWindow(t->index);
    01798           return true;
    01799         }
    01800       }
    01801       break;
    01802
    01803     case ZOOM_LVL_OUT_4X:
    01804     case ZOOM_LVL_OUT_8X:
    01805       x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left;
    01806       y = ScaleByZoom(y - vp->top  + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top;
    01807
    01808       FOR_ALL_TOWNS(t) {
    01809         if (y >= t->sign.top &&
    01810             y < t->sign.top + ScaleByZoom(12, vp->zoom) &&
    01811             x >= t->sign.left &&
    01812             x < t->sign.left + ScaleByZoom(t->sign.width_2, vp->zoom)) {
    01813           ShowTownViewWindow(t->index);
    01814           return true;
    01815         }
    01816       }
    01817       break;
    01818
    01819     default: NOT_REACHED();
    01820   }
    01821
    01822   return false;
    01823 }
    01824
    01825
    01826 static bool CheckClickOnStation(const ViewPort *vp, int x, int y)
    01827 {
    01828   const Station *st;
    01829
    01830   if (!HasBit(_display_opt, DO_SHOW_STATION_NAMES) || IsInvisibilitySet(TO_SIGNS)) return false;
    01831
    01832   switch (vp->zoom) {
    01833     case ZOOM_LVL_NORMAL:
    01834       x = x - vp->left + vp->virtual_left;
    01835       y = y - vp->top  + vp->virtual_top;
    01836       FOR_ALL_STATIONS(st) {
    01837         if (y >= st->sign.top &&
    01838             y < st->sign.top + 12 &&
    01839             x >= st->sign.left &&
    01840             x < st->sign.left + st->sign.width_1) {
    01841           ShowStationViewWindow(st->index);
    01842           return true;
    01843         }
    01844       }
    01845       break;
    01846
    01847     case ZOOM_LVL_OUT_2X:
    01848       x = (x - vp->left + 1) * 2 + vp->virtual_left;
    01849       y = (y - vp->top  + 1) * 2 + vp->virtual_top;
    01850       FOR_ALL_STATIONS(st) {
    01851         if (y >= st->sign.top &&
    01852             y < st->sign.top + 24 &&
    01853             x >= st->sign.left &&
    01854             x < st->sign.left + st->sign.width_1 * 2) {
    01855           ShowStationViewWindow(st->index);
    01856           return true;
    01857         }
    01858       }
    01859       break;
    01860
    01861     case ZOOM_LVL_OUT_4X:
    01862     case ZOOM_LVL_OUT_8X:
    01863       x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left;
    01864       y = ScaleByZoom(y - vp->top  + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top;
    01865
    01866       FOR_ALL_STATIONS(st) {
    01867         if (y >= st->sign.top &&
    01868             y < st->sign.top + ScaleByZoom(12, vp->zoom) &&
    01869             x >= st->sign.left &&
    01870             x < st->sign.left + ScaleByZoom(st->sign.width_2, vp->zoom)) {
    01871           ShowStationViewWindow(st->index);
    01872           return true;
    01873         }
    01874       }
    01875       break;
    01876
    01877     default: NOT_REACHED();
    01878   }
    01879
    01880   return false;
    01881 }
    01882
    01883
    01884 static bool CheckClickOnSign(const ViewPort *vp, int x, int y)
    01885 {
    01886   const Sign *si;
    01887
    01888   /* Signs are turned off, or they are transparent and invisibility is ON, or company is a spectator */
    01889   if (!HasBit(_display_opt, DO_SHOW_SIGNS) || IsInvisibilitySet(TO_SIGNS) || _current_company == COMPANY_SPECTATOR) return false;
    01890
    01891   switch (vp->zoom) {
    01892     case ZOOM_LVL_NORMAL:
    01893       x = x - vp->left + vp->virtual_left;
    01894       y = y - vp->top  + vp->virtual_top;
    01895       FOR_ALL_SIGNS(si) {
    01896         if (y >= si->sign.top &&
    01897             y <  si->sign.top + 12 &&
    01898             x >= si->sign.left &&
    01899             x <  si->sign.left + si->sign.width_1) {
    01900           HandleClickOnSign(si);
    01901           return true;
    01902         }
    01903       }
    01904       break;
    01905
    01906     case ZOOM_LVL_OUT_2X:
    01907       x = (x - vp->left + 1) * 2 + vp->virtual_left;
    01908       y = (y - vp->top  + 1) * 2 + vp->virtual_top;
    01909       FOR_ALL_SIGNS(si) {
    01910         if (y >= si->sign.top &&
    01911             y <  si->sign.top + 24 &&
    01912             x >= si->sign.left &&
    01913             x <  si->sign.left + si->sign.width_1 * 2) {
    01914           HandleClickOnSign(si);
    01915           return true;
    01916         }
    01917       }
    01918       break;
    01919
    01920     case ZOOM_LVL_OUT_4X:
    01921     case ZOOM_LVL_OUT_8X:
    01922       x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left;
    01923       y = ScaleByZoom(y - vp->top  + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top;
    01924
    01925       FOR_ALL_SIGNS(si) {
    01926         if (y >= si->sign.top &&
    01927             y <  si->sign.top + ScaleByZoom(12, vp->zoom) &&
    01928             x >= si->sign.left &&
    01929             x <  si->sign.left + ScaleByZoom(si->sign.width_2, vp->zoom)) {
    01930           HandleClickOnSign(si);
    01931           return true;
    01932         }
    01933       }
    01934       break;
    01935
    01936     default: NOT_REACHED();
    01937   }
    01938
    01939   return false;
    01940 }
    01941
    01942
    01943 static bool CheckClickOnWaypoint(const ViewPort *vp, int x, int y)
    01944 {
    01945   const Waypoint *wp;
    01946
    01947   if (!HasBit(_display_opt, DO_WAYPOINTS) || IsInvisibilitySet(TO_SIGNS)) return false;
    01948
    01949   switch (vp->zoom) {
    01950     case ZOOM_LVL_NORMAL:
    01951       x = x - vp->left + vp->virtual_left;
    01952       y = y - vp->top  + vp->virtual_top;
    01953       FOR_ALL_WAYPOINTS(wp) {
    01954         if (y >= wp->sign.top &&
    01955             y < wp->sign.top + 12 &&
    01956             x >= wp->sign.left &&
    01957             x < wp->sign.left + wp->sign.width_1) {
    01958           ShowWaypointWindow(wp);
    01959           return true;
    01960         }
    01961       }
    01962       break;
    01963
    01964     case ZOOM_LVL_OUT_2X:
    01965       x = (x - vp->left + 1) * 2 + vp->virtual_left;
    01966       y = (y - vp->top  + 1) * 2 + vp->virtual_top;
    01967       FOR_ALL_WAYPOINTS(wp) {
    01968         if (y >= wp->sign.top &&
    01969             y < wp->sign.top + 24 &&
    01970             x >= wp->sign.left &&
    01971             x < wp->sign.left + wp->sign.width_1 * 2) {
    01972           ShowWaypointWindow(wp);
    01973           return true;
    01974         }
    01975       }
    01976       break;
    01977
    01978     case ZOOM_LVL_OUT_4X:
    01979     case ZOOM_LVL_OUT_8X:
    01980       x = ScaleByZoom(x - vp->left + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_left;
    01981       y = ScaleByZoom(y - vp->top  + ScaleByZoom(1, vp->zoom) - 1, vp->zoom) + vp->virtual_top;
    01982
    01983       FOR_ALL_WAYPOINTS(wp) {
    01984         if (y >= wp->sign.top &&
    01985             y < wp->sign.top + ScaleByZoom(12, vp->zoom) &&
    01986             x >= wp->sign.left &&
    01987             x < wp->sign.left + ScaleByZoom(wp->sign.width_2, vp->zoom)) {
    01988           ShowWaypointWindow(wp);
    01989           return true;
    01990         }
    01991       }
    01992       break;
    01993
    01994     default: NOT_REACHED();
    01995   }
    01996
    01997   return false;
    01998 }
    01999
    02000
    02001 static bool CheckClickOnLandscape(const ViewPort *vp, int x, int y)
    02002 {
    02003   Point pt = TranslateXYToTileCoord(vp, x, y);
    02004
    02005   if (pt.x != -1) return ClickTile(TileVirtXY(pt.x, pt.y));
    02006   return true;
    02007 }
    02008
    02009
    02010 bool HandleViewportClicked(const ViewPort *vp, int x, int y)
    02011 {
    02012   const Vehicle *v;
    02013
    02014   if (CheckClickOnTown(vp, x, y)) return true;
    02015   if (CheckClickOnStation(vp, x, y)) return true;
    02016   if (CheckClickOnSign(vp, x, y)) return true;
    02017   if (CheckClickOnWaypoint(vp, x, y)) return true;
    02018   CheckClickOnLandscape(vp, x, y);
    02019
    02020   v = CheckClickOnVehicle(vp, x, y);
    02021   if (v != NULL) {
    02022     DEBUG(misc, 2, "Vehicle %d (index %d) at %p", v->unitnumber, v->index, v);
    02023     if (IsCompanyBuildableVehicleType(v)) ShowVehicleViewWindow(v->First());
    02024     return true;
    02025   }
    02026   return CheckClickOnLandscape(vp, x, y);
    02027 }
    02028
    02029 Vehicle *CheckMouseOverVehicle()
    02030 {
    02031   const Window *w;
    02032   const ViewPort *vp;
    02033
    02034   int x = _cursor.pos.x;
    02035   int y = _cursor.pos.y;
    02036
    02037   w = FindWindowFromPt(x, y);
    02038   if (w == NULL) return NULL;
    02039
    02040   vp = IsPtInWindowViewport(w, x, y);
    02041   return (vp != NULL) ? CheckClickOnVehicle(vp, x, y) : NULL;
    02042 }
    02043
    02044
    02045
    02046 void PlaceObject()
    02047 {
    02048   Point pt;
    02049   Window *w;
    02050
    02051   pt = GetTileBelowCursor();
    02052   if (pt.x == -1) return;
    02053
    02054   if (_thd.place_mode == VHM_POINT) {
    02055     pt.x += 8;
    02056     pt.y += 8;
    02057   }
    02058
    02059   _tile_fract_coords.x = pt.x & 0xF;
    02060   _tile_fract_coords.y = pt.y & 0xF;
    02061
    02062   w = GetCallbackWnd();
    02063   if (w != NULL) w->OnPlaceObject(pt, TileVirtXY(pt.x, pt.y));
    02064 }
    02065
    02066
    02067 /* scrolls the viewport in a window to a given location */
    02068 bool ScrollWindowTo(int x , int y, Window *w, bool instant)
    02069 {
    02070   /* The slope cannot be acquired outside of the map, so make sure we are always within the map. */
    02071   Point pt = MapXYZToViewport(w->viewport, x, y, GetSlopeZ(Clamp(x, 0, MapSizeX() * TILE_SIZE - 1), Clamp(y, 0, MapSizeY() * TILE_SIZE - 1)));
    02072   w->viewport->follow_vehicle = INVALID_VEHICLE;
    02073
    02074   if (w->viewport->dest_scrollpos_x == pt.x && w->viewport->dest_scrollpos_y == pt.y)
    02075     return false;
    02076
    02077   if (instant) {
    02078     w->viewport->scrollpos_x = pt.x;
    02079     w->viewport->scrollpos_y = pt.y;
    02080   }
    02081
    02082   w->viewport->dest_scrollpos_x = pt.x;
    02083   w->viewport->dest_scrollpos_y = pt.y;
    02084   return true;
    02085 }
    02086
    02087 bool ScrollMainWindowToTile(TileIndex tile, bool instant)
    02088 {
    02089   return ScrollMainWindowTo(TileX(tile) * TILE_SIZE + TILE_SIZE / 2, TileY(tile) * TILE_SIZE + TILE_SIZE / 2, instant);
    02090 }
    02091
    02092 void SetRedErrorSquare(TileIndex tile)
    02093 {
    02094   TileIndex old;
    02095
    02096   old = _thd.redsq;
    02097   _thd.redsq = tile;
    02098
    02099   if (tile != old) {
    02100     if (tile != INVALID_TILE) MarkTileDirtyByTile(tile);
    02101     if (old  != INVALID_TILE) MarkTileDirtyByTile(old);
    02102   }
    02103 }
    02104
    02105 void SetTileSelectSize(int w, int h)
    02106 {
    02107   _thd.new_size.x = w * TILE_SIZE;
    02108   _thd.new_size.y = h * TILE_SIZE;
    02109   _thd.new_outersize.x = 0;
    02110   _thd.new_outersize.y = 0;
    02111 }
    02112
    02113 void SetTileSelectBigSize(int ox, int oy, int sx, int sy)
    02114 {
    02115   _thd.offs.x = ox * TILE_SIZE;
    02116   _thd.offs.y = oy * TILE_SIZE;
    02117   _thd.new_outersize.x = sx * TILE_SIZE;
    02118   _thd.new_outersize.y = sy * TILE_SIZE;
    02119 }
    02120
    02122 static HighLightStyle GetAutorailHT(int x, int y)
    02123 {
    02124   return HT_RAIL | _autorail_piece[x & 0xF][y & 0xF];
    02125 }
    02126
    02134 void UpdateTileSelection()
    02135 {
    02136   int x1;
    02137   int y1;
    02138
    02139   _thd.new_drawstyle = 0;
    02140
    02141   if (_thd.place_mode == VHM_SPECIAL) {
    02142     x1 = _thd.selend.x;
    02143     y1 = _thd.selend.y;
    02144     if (x1 != -1) {
    02145       int x2 = _thd.selstart.x & ~0xF;
    02146       int y2 = _thd.selstart.y & ~0xF;
    02147       x1 &= ~0xF;
    02148       y1 &= ~0xF;
    02149
    02150       if (x1 >= x2) Swap(x1, x2);
    02151       if (y1 >= y2) Swap(y1, y2);
    02152       _thd.new_pos.x = x1;
    02153       _thd.new_pos.y = y1;
    02154       _thd.new_size.x = x2 - x1 + TILE_SIZE;
    02155       _thd.new_size.y = y2 - y1 + TILE_SIZE;
    02156       _thd.new_drawstyle = _thd.next_drawstyle;
    02157     }
    02158   } else if (_thd.place_mode != VHM_NONE) {
    02159     Point pt = GetTileBelowCursor();
    02160     x1 = pt.x;
    02161     y1 = pt.y;
    02162     if (x1 != -1) {
    02163       switch (_thd.place_mode) {
    02164         case VHM_RECT:
    02165           _thd.new_drawstyle = HT_RECT;
    02166           break;
    02167         case VHM_POINT:
    02168           _thd.new_drawstyle = HT_POINT;
    02169           x1 += 8;
    02170           y1 += 8;
    02171           break;
    02172         case VHM_RAIL:
    02173           _thd.new_drawstyle = GetAutorailHT(pt.x, pt.y); // draw one highlighted tile
    02174           break;
    02175         default:
    02176           NOT_REACHED();
    02177           break;
    02178       }
    02179       _thd.new_pos.x = x1 & ~0xF;
    02180       _thd.new_pos.y = y1 & ~0xF;
    02181     }
    02182   }
    02183
    02184   /* redraw selection */
    02185   if (_thd.drawstyle != _thd.new_drawstyle ||
    02186       _thd.pos.x != _thd.new_pos.x || _thd.pos.y != _thd.new_pos.y ||
    02187       _thd.size.x != _thd.new_size.x || _thd.size.y != _thd.new_size.y ||
    02188       _thd.outersize.x != _thd.new_outersize.x ||
    02189       _thd.outersize.y != _thd.new_outersize.y) {
    02190     /* clear the old selection? */
    02191     if (_thd.drawstyle) SetSelectionTilesDirty();
    02192
    02193     _thd.drawstyle = _thd.new_drawstyle;
    02194     _thd.pos = _thd.new_pos;
    02195     _thd.size = _thd.new_size;
    02196     _thd.outersize = _thd.new_outersize;
    02197     _thd.dirty = 0xff;
    02198
    02199     /* draw the new selection? */
    02200     if (_thd.new_drawstyle) SetSelectionTilesDirty();
    02201   }
    02202 }
    02203
    02209 static inline void ShowMeasurementTooltips(StringID str, uint paramcount, const uint64 params[])
    02210 {
    02211   if (!_settings_client.gui.measure_tooltip) return;
    02212   GuiShowTooltips(str, paramcount, params, true);
    02213 }
    02214
    02216 void VpStartPlaceSizing(TileIndex tile, ViewportPlaceMethod method, ViewportDragDropSelectionProcess process)
    02217 {
    02218   _thd.select_method = method;
    02219   _thd.select_proc   = process;
    02220   _thd.selend.x = TileX(tile) * TILE_SIZE;
    02221   _thd.selstart.x = TileX(tile) * TILE_SIZE;
    02222   _thd.selend.y = TileY(tile) * TILE_SIZE;
    02223   _thd.selstart.y = TileY(tile) * TILE_SIZE;
    02224
    02225   /* Needed so several things (road, autoroad, bridges, ...) are placed correctly.
    02226    * In effect, placement starts from the centre of a tile
    02227    */
    02228   if (method == VPM_X_OR_Y || method == VPM_FIX_X || method == VPM_FIX_Y) {
    02229     _thd.selend.x += TILE_SIZE / 2;
    02230     _thd.selend.y += TILE_SIZE / 2;
    02231     _thd.selstart.x += TILE_SIZE / 2;
    02232     _thd.selstart.y += TILE_SIZE / 2;
    02233   }
    02234
    02235   if (_thd.place_mode == VHM_RECT) {
    02236     _thd.place_mode = VHM_SPECIAL;
    02237     _thd.next_drawstyle = HT_RECT;
    02238   } else if (_thd.place_mode == VHM_RAIL) { // autorail one piece
    02239     _thd.place_mode = VHM_SPECIAL;
    02240     _thd.next_drawstyle = _thd.drawstyle;
    02241   } else {
    02242     _thd.place_mode = VHM_SPECIAL;
    02243     _thd.next_drawstyle = HT_POINT;
    02244   }
    02245   _special_mouse_mode = WSM_SIZING;
    02246 }
    02247
    02248 void VpSetPlaceSizingLimit(int limit)
    02249 {
    02250   _thd.sizelimit = limit;
    02251 }
    02252
    02257 void VpSetPresizeRange(TileIndex from, TileIndex to)
    02258 {
    02259   uint64 distance = DistanceManhattan(from, to) + 1;
    02260
    02261   _thd.selend.x = TileX(to) * TILE_SIZE;
    02262   _thd.selend.y = TileY(to) * TILE_SIZE;
    02263   _thd.selstart.x = TileX(from) * TILE_SIZE;
    02264   _thd.selstart.y = TileY(from) * TILE_SIZE;
    02265   _thd.next_drawstyle = HT_RECT;
    02266
    02267   /* show measurement only if there is any length to speak of */
    02268   if (distance > 1) ShowMeasurementTooltips(STR_MEASURE_LENGTH, 1, &distance);
    02269 }
    02270
    02271 static void VpStartPreSizing()
    02272 {
    02273   _thd.selend.x = -1;
    02274   _special_mouse_mode = WSM_PRESIZE;
    02275 }
    02276
    02279 static HighLightStyle Check2x1AutoRail(int mode)
    02280 {
    02281   int fxpy = _tile_fract_coords.x + _tile_fract_coords.y;
    02282   int sxpy = (_thd.selend.x & 0xF) + (_thd.selend.y & 0xF);
    02283   int fxmy = _tile_fract_coords.x - _tile_fract_coords.y;
    02284   int sxmy = (_thd.selend.x & 0xF) - (_thd.selend.y & 0xF);
    02285
    02286   switch (mode) {
    02287     default: NOT_REACHED();
    02288     case 0: // end piece is lower right
    02289       if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; }
    02290       if (fxmy < -3 && sxmy > 3) {/* DoRailroadTrack(0); */return HT_DIR_VR; }
    02291       return HT_DIR_Y;
    02292
    02293     case 1:
    02294       if (fxmy > 3 && sxmy < -3) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_VL; }
    02295       if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; }
    02296       return HT_DIR_Y;
    02297
    02298     case 2:
    02299       if (fxmy > 3 && sxmy < -3) { /*DoRailroadTrack(3);*/ return HT_DIR_VL; }
    02300       if (fxpy >= 20 && sxpy <= 12) { /*SwapSelection(); DoRailroadTrack(0); */return HT_DIR_HL; }
    02301       return HT_DIR_X;
    02302
    02303     case 3:
    02304       if (fxmy < -3 && sxmy > 3) { /*SwapSelection(); DoRailroadTrack(3);*/ return HT_DIR_VR; }
    02305       if (fxpy <= 12 && sxpy >= 20) { /*DoRailroadTrack(0); */return HT_DIR_HU; }
    02306       return HT_DIR_X;
    02307   }
    02308 }
    02309
    02321 static bool SwapDirection(HighLightStyle style, TileIndex start_tile, TileIndex end_tile)
    02322 {
    02323   uint start_x = TileX(start_tile);
    02324   uint start_y = TileY(start_tile);
    02325   uint end_x = TileX(end_tile);
    02326   uint end_y = TileY(end_tile);
    02327
    02328   switch (style & HT_DRAG_MASK) {
    02329     case HT_RAIL:
    02330     case HT_LINE: return (end_x > start_x || (end_x == start_x && end_y > start_y));
    02331
    02332     case HT_RECT:
    02333     case HT_POINT: return (end_x != start_x && end_y < start_y);
    02334     default: NOT_REACHED();
    02335   }
    02336
    02337   return false;
    02338 }
    02339
    02354 static int CalcHeightdiff(HighLightStyle style, uint distance, TileIndex start_tile, TileIndex end_tile)
    02355 {
    02356   bool swap = SwapDirection(style, start_tile, end_tile);
    02357   byte style_t;
    02358   uint h0, h1, ht; // start heigth, end height, and temp variable
    02359
    02360   if (start_tile == end_tile) return 0;
    02361   if (swap) Swap(start_tile, end_tile);
    02362
    02363   switch (style & HT_DRAG_MASK) {
    02364     case HT_RECT: {
    02365       static const TileIndexDiffC heightdiff_area_by_dir[] = {
    02366         /* Start */ {1, 0}, /* Dragging east */ {0, 0}, /* Dragging south */
    02367         /* End   */ {0, 1}, /* Dragging east */ {1, 1}  /* Dragging south */
    02368       };
    02369
    02370       /* In the case of an area we can determine whether we were dragging south or
    02371        * east by checking the X-coordinates of the tiles */
    02372       style_t = (byte)(TileX(end_tile) > TileX(start_tile));
    02373       start_tile = TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_area_by_dir[style_t]));
    02374       end_tile   = TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_area_by_dir[2 + style_t]));
    02375     }
    02376     /* Fallthrough */
    02377     case HT_POINT:
    02378       h0 = TileHeight(start_tile);
    02379       h1 = TileHeight(end_tile);
    02380       break;
    02381     default: { /* All other types, this is mostly only line/autorail */
    02382       static const HighLightStyle flip_style_direction[] = {
    02383         HT_DIR_X, HT_DIR_Y, HT_DIR_HL, HT_DIR_HU, HT_DIR_VR, HT_DIR_VL
    02384       };
    02385       static const TileIndexDiffC heightdiff_line_by_dir[] = {
    02386         /* Start */ {1, 0}, {1, 1}, /* HT_DIR_X  */ {0, 1}, {1, 1}, /* HT_DIR_Y  */
    02387         /* Start */ {1, 0}, {0, 0}, /* HT_DIR_HU */ {1, 0}, {1, 1}, /* HT_DIR_HL */
    02388         /* Start */ {1, 0}, {1, 1}, /* HT_DIR_VL */ {0, 1}, {1, 1}, /* HT_DIR_VR */
    02389
    02390         /* Start */ {0, 1}, {0, 0}, /* HT_DIR_X  */ {1, 0}, {0, 0}, /* HT_DIR_Y  */
    02391         /* End   */ {0, 1}, {0, 0}, /* HT_DIR_HU */ {1, 1}, {0, 1}, /* HT_DIR_HL */
    02392         /* End   */ {1, 0}, {0, 0}, /* HT_DIR_VL */ {0, 0}, {0, 1}, /* HT_DIR_VR */
    02393       };
    02394
    02395       distance %= 2; // we're only interested if the distance is even or uneven
    02396       style &= HT_DIR_MASK;
    02397
    02398       /* To handle autorail, we do some magic to be able to use a lookup table.
    02399        * Firstly if we drag the other way around, we switch start&end, and if needed
    02400        * also flip the drag-position. Eg if it was on the left, and the distance is even
    02401        * that means the end, which is now the start is on the right */
    02402       if (swap && distance == 0) style = flip_style_direction[style];
    02403
    02404       /* Use lookup table for start-tile based on HighLightStyle direction */
    02405       style_t = style * 2;
    02406       assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
    02407       h0 = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t])));
    02408       ht = TileHeight(TILE_ADD(start_tile, ToTileIndexDiff(heightdiff_line_by_dir[style_t + 1])));
    02409       h0 = max(h0, ht);
    02410
    02411       /* Use lookup table for end-tile based on HighLightStyle direction
    02412        * flip around side (lower/upper, left/right) based on distance */
    02413       if (distance == 0) style_t = flip_style_direction[style] * 2;
    02414       assert(style_t < lengthof(heightdiff_line_by_dir) - 13);
    02415       h1 = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t])));
    02416       ht = TileHeight(TILE_ADD(end_tile, ToTileIndexDiff(heightdiff_line_by_dir[12 + style_t + 1])));
    02417       h1 = max(h1, ht);
    02418     } break;
    02419   }
    02420
    02421   if (swap) Swap(h0, h1);
    02422   /* Minimap shows height in intervals of 50 meters, let's do the same */
    02423   return (int)(h1 - h0) * 50;
    02424 }
    02425
    02426 static const StringID measure_strings_length[] = {STR_NULL, STR_MEASURE_LENGTH, STR_MEASURE_LENGTH_HEIGHTDIFF};
    02427
    02429 static void CalcRaildirsDrawstyle(TileHighlightData *thd, int x, int y, int method)
    02430 {
    02431   HighLightStyle b;
    02432   uint w, h;
    02433
    02434   int dx = thd->selstart.x - (thd->selend.x & ~0xF);
    02435   int dy = thd->selstart.y - (thd->selend.y & ~0xF);
    02436   w = abs(dx) + 16;
    02437   h = abs(dy) + 16;
    02438
    02439   if (TileVirtXY(thd->selstart.x, thd->selstart.y) == TileVirtXY(x, y)) { // check if we're only within one tile
    02440     if (method == VPM_RAILDIRS) {
    02441       b = GetAutorailHT(x, y);
    02442     } else { // rect for autosignals on one tile
    02443       b = HT_RECT;
    02444     }
    02445   } else if (h == 16) { // Is this in X direction?
    02446     if (dx == 16) { // 2x1 special handling
    02447       b = (Check2x1AutoRail(3)) | HT_LINE;
    02448     } else if (dx == -16) {
    02449       b = (Check2x1AutoRail(2)) | HT_LINE;
    02450     } else {
    02451       b = HT_LINE | HT_DIR_X;
    02452     }
    02453     y = thd->selstart.y;
    02454   } else if (w == 16) { // Or Y direction?
    02455     if (dy == 16) { // 2x1 special handling
    02456       b = (Check2x1AutoRail(1)) | HT_LINE;
    02457     } else if (dy == -16) { // 2x1 other direction
    02458       b = (Check2x1AutoRail(0)) | HT_LINE;
    02459     } else {
    02460       b = HT_LINE | HT_DIR_Y;
    02461     }
    02462     x = thd->selstart.x;
    02463   } else if (w > h * 2) { // still count as x dir?
    02464     b = HT_LINE | HT_DIR_X;
    02465     y = thd->selstart.y;
    02466   } else if (h > w * 2) { // still count as y dir?
    02467     b = HT_LINE | HT_DIR_Y;
    02468     x = thd->selstart.x;
    02469   } else { // complicated direction
    02470     int d = w - h;
    02471     thd->selend.x = thd->selend.x & ~0xF;
    02472     thd->selend.y = thd->selend.y & ~0xF;
    02473
    02474     // four cases.
    02475     if (x > thd->selstart.x) {
    02476       if (y > thd->selstart.y) {
    02477         // south
    02478         if (d == 0) {
    02479           b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
    02480         } else if (d >= 0) {
    02481           x = thd->selstart.x + h;
    02482           b = HT_LINE | HT_DIR_VL;
    02483           // return px == py || px == py + 16;
    02484         } else {
    02485           y = thd->selstart.y + w;
    02486           b = HT_LINE | HT_DIR_VR;
    02487         } // return px == py || px == py - 16;
    02488       } else {
    02489         // west
    02490         if (d == 0) {
    02491           b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
    02492         } else if (d >= 0) {
    02493           x = thd->selstart.x + h;
    02494           b = HT_LINE | HT_DIR_HL;
    02495         } else {
    02496           y = thd->selstart.y - w;
    02497           b = HT_LINE | HT_DIR_HU;
    02498         }
    02499       }
    02500     } else {
    02501       if (y > thd->selstart.y) {
    02502         // east
    02503         if (d == 0) {
    02504           b = (x & 0xF) + (y & 0xF) >= 0x10 ? HT_LINE | HT_DIR_HL : HT_LINE | HT_DIR_HU;
    02505         } else if (d >= 0) {
    02506           x = thd->selstart.x - h;
    02507           b = HT_LINE | HT_DIR_HU;
    02508           // return px == -py || px == -py - 16;
    02509         } else {
    02510           y = thd->selstart.y + w;
    02511           b = HT_LINE | HT_DIR_HL;
    02512         } // return px == -py || px == -py + 16;
    02513       } else {
    02514         // north
    02515         if (d == 0) {
    02516           b = (x & 0xF) > (y & 0xF) ? HT_LINE | HT_DIR_VL : HT_LINE | HT_DIR_VR;
    02517         } else if (d >= 0) {
    02518           x = thd->selstart.x - h;
    02519           b = HT_LINE | HT_DIR_VR;
    02520           // return px == py || px == py - 16;
    02521         } else {
    02522           y = thd->selstart.y - w;
    02523           b = HT_LINE | HT_DIR_VL;
    02524         } //return px == py || px == py + 16;
    02525       }
    02526     }
    02527   }
    02528
    02529   if (_settings_client.gui.measure_tooltip) {
    02530     TileIndex t0 = TileVirtXY(thd->selstart.x, thd->selstart.y);
    02531     TileIndex t1 = TileVirtXY(x, y);
    02532     uint distance = DistanceManhattan(t0, t1) + 1;
    02533     byte index = 0;
    02534     uint64 params[2];
    02535
    02536     if (distance != 1) {
    02537       int heightdiff = CalcHeightdiff(b, distance, t0, t1);
    02538       /* If we are showing a tooltip for horizontal or vertical drags,
    02539        * 2 tiles have a length of 1. To bias towards the ceiling we add
    02540        * one before division. It feels more natural to count 3 lengths as 2 */
    02541       if ((b & HT_DIR_MASK) != HT_DIR_X && (b & HT_DIR_MASK) != HT_DIR_Y) {
    02542         distance = (distance + 1) / 2;
    02543       }
    02544
    02545       params[index++] = distance;
    02546       if (heightdiff != 0) params[index++] = heightdiff;
    02547     }
    02548
    02549     ShowMeasurementTooltips(measure_strings_length[index], index, params);
    02550   }
    02551
    02552   thd->selend.x = x;
    02553   thd->selend.y = y;
    02554   thd->next_drawstyle = b;
    02555 }
    02556
    02563 void VpSelectTilesWithMethod(int x, int y, ViewportPlaceMethod method)
    02564 {
    02565   int sx, sy;
    02566   HighLightStyle style;
    02567
    02568   if (x == -1) {
    02569     _thd.selend.x = -1;
    02570     return;
    02571   }
    02572
    02573   /* Special handling of drag in any (8-way) direction */
    02574   if (method == VPM_RAILDIRS || method == VPM_SIGNALDIRS) {
    02575     _thd.selend.x = x;
    02576     _thd.selend.y = y;
    02577     CalcRaildirsDrawstyle(&_thd, x, y, method);
    02578     return;
    02579   }
    02580
    02581   /* Needed so level-land is placed correctly */
    02582   if (_thd.next_drawstyle == HT_POINT) {
    02583     x += TILE_SIZE / 2;
    02584     y += TILE_SIZE / 2;
    02585   }
    02586
    02587   sx = _thd.selstart.x;
    02588   sy = _thd.selstart.y;
    02589
    02590   switch (method) {
    02591     case VPM_X_OR_Y: /* drag in X or Y direction */
    02592       if (abs(sy - y) < abs(sx - x)) {
    02593         y = sy;
    02594         style = HT_DIR_X;
    02595       } else {
    02596         x = sx;
    02597         style = HT_DIR_Y;
    02598       }
    02599       goto calc_heightdiff_single_direction;
    02600     case VPM_FIX_X: /* drag in Y direction */
    02601       x = sx;
    02602       style = HT_DIR_Y;
    02603       goto calc_heightdiff_single_direction;
    02604     case VPM_FIX_Y: /* drag in X direction */
    02605       y = sy;
    02606       style = HT_DIR_X;
    02607
    02608 calc_heightdiff_single_direction:;
    02609       if (_settings_client.gui.measure_tooltip) {
    02610         TileIndex t0 = TileVirtXY(sx, sy);
    02611         TileIndex t1 = TileVirtXY(x, y);
    02612         uint distance = DistanceManhattan(t0, t1) + 1;
    02613         byte index = 0;
    02614         uint64 params[2];
    02615
    02616         if (distance != 1) {
    02617           /* With current code passing a HT_LINE style to calculate the height
    02618            * difference is enough. However if/when a point-tool is created
    02619            * with this method, function should be called with new_style (below)
    02620            * instead of HT_LINE | style case HT_POINT is handled specially
    02621            * new_style := (_thd.next_drawstyle & HT_RECT) ? HT_LINE | style : _thd.next_drawstyle; */
    02622           int heightdiff = CalcHeightdiff(HT_LINE | style, 0, t0, t1);
    02623
    02624           params[index++] = distance;
    02625           if (heightdiff != 0) params[index++] = heightdiff;
    02626         }
    02627
    02628         ShowMeasurementTooltips(measure_strings_length[index], index, params);
    02629       } break;
    02630
    02631     case VPM_X_AND_Y_LIMITED: { /* drag an X by Y constrained rect area */
    02632       int limit = (_thd.sizelimit - 1) * TILE_SIZE;
    02633       x = sx + Clamp(x - sx, -limit, limit);
    02634       y = sy + Clamp(y - sy, -limit, limit);
    02635       } /* Fallthrough */
    02636     case VPM_X_AND_Y: { /* drag an X by Y area */
    02637       if (_settings_client.gui.measure_tooltip) {
    02638         static const StringID measure_strings_area[] = {
    02639           STR_NULL, STR_NULL, STR_MEASURE_AREA, STR_MEASURE_AREA_HEIGHTDIFF
    02640         };
    02641
    02642         TileIndex t0 = TileVirtXY(sx, sy);
    02643         TileIndex t1 = TileVirtXY(x, y);
    02644         uint dx = Delta(TileX(t0), TileX(t1)) + 1;
    02645         uint dy = Delta(TileY(t0), TileY(t1)) + 1;
    02646         byte index = 0;
    02647         uint64 params[3];
    02648
    02649         /* If dragging an area (eg dynamite tool) and it is actually a single
    02650          * row/column, change the type to 'line' to get proper calculation for height */
    02651         style = (HighLightStyle)_thd.next_drawstyle;
    02652         if (style & HT_RECT) {
    02653           if (dx == 1) {
    02654             style = HT_LINE | HT_DIR_Y;
    02655           } else if (dy == 1) {
    02656             style = HT_LINE | HT_DIR_X;
    02657           }
    02658         }
    02659
    02660         if (dx != 1 || dy != 1) {
    02661           int heightdiff = CalcHeightdiff(style, 0, t0, t1);
    02662
    02663           params[index++] = dx;
    02664           params[index++] = dy;
    02665           if (heightdiff != 0) params[index++] = heightdiff;
    02666         }
    02667
    02668         ShowMeasurementTooltips(measure_strings_area[index], index, params);
    02669       }
    02670     break;
    02671
    02672     }
    02673     default: NOT_REACHED();
    02674   }
    02675
    02676   _thd.selend.x = x;
    02677   _thd.selend.y = y;
    02678 }
    02679
    02684 bool VpHandlePlaceSizingDrag()
    02685 {
    02686   if (_special_mouse_mode != WSM_SIZING) return true;
    02687
    02688   /* stop drag mode if the window has been closed */
    02689   Window *w = FindWindowById(_thd.window_class, _thd.window_number);
    02690   if (w == NULL) {
    02691     ResetObjectToPlace();
    02692     return false;
    02693   }
    02694
    02695   /* while dragging execute the drag procedure of the corresponding window (mostly VpSelectTilesWithMethod() ) */
    02696   if (_left_button_down) {
    02697     w->OnPlaceDrag(_thd.select_method, _thd.select_proc, GetTileBelowCursor());
    02698     return false;
    02699   }
    02700
    02701   /* mouse button released..
    02702    * keep the selected tool, but reset it to the original mode. */
    02703   _special_mouse_mode = WSM_NONE;
    02704   if (_thd.next_drawstyle == HT_RECT) {
    02705     _thd.place_mode = VHM_RECT;
    02706   } else if (_thd.select_method == VPM_SIGNALDIRS) { // some might call this a hack... -- Dominik
    02707     _thd.place_mode = VHM_RECT;
    02708   } else if (_thd.next_drawstyle & HT_LINE) {
    02709     _thd.place_mode = VHM_RAIL;
    02710   } else if (_thd.next_drawstyle & HT_RAIL) {
    02711     _thd.place_mode = VHM_RAIL;
    02712   } else {
    02713     _thd.place_mode = VHM_POINT;
    02714   }
    02715   SetTileSelectSize(1, 1);
    02716
    02717   w->OnPlaceMouseUp(_thd.select_method, _thd.select_proc, _thd.selend, TileVirtXY(_thd.selstart.x, _thd.selstart.y), TileVirtXY(_thd.selend.x, _thd.selend.y));
    02718
    02719   return false;
    02720 }
    02721
    02722 void SetObjectToPlaceWnd(CursorID icon, SpriteID pal, ViewportHighlightMode mode, Window *w)
    02723 {
    02724   SetObjectToPlace(icon, pal, mode, w->window_class, w->window_number);
    02725 }
    02726
    02727 #include "table/animcursors.h"
    02728
    02729 void SetObjectToPlace(CursorID icon, SpriteID pal, ViewportHighlightMode mode, WindowClass window_class, WindowNumber window_num)
    02730 {
    02731   /* undo clicking on button and drag & drop */
    02732   if (_thd.place_mode != VHM_NONE || _special_mouse_mode == WSM_DRAGDROP) {
    02733     Window *w = FindWindowById(_thd.window_class, _thd.window_number);
    02734     if (w != NULL) {
    02735       /* Call the abort function, but set the window class to something
    02736        * that will never be used to avoid infinite loops. Setting it to
    02737        * the 'next' window class must not be done because recursion into
    02738        * this function might in some cases reset the newly set object to
    02739        * place or not properly reset the original selection. */
    02740       _thd.window_class = WC_INVALID;
    02741       w->OnPlaceObjectAbort();
    02742     }
    02743   }
    02744
    02745   SetTileSelectSize(1, 1);
    02746
    02747   _thd.make_square_red = false;
    02748
    02749   if (mode == VHM_DRAG) { // VHM_DRAG is for dragdropping trains in the depot window
    02750     mode = VHM_NONE;
    02751     _special_mouse_mode = WSM_DRAGDROP;
    02752   } else {
    02753     _special_mouse_mode = WSM_NONE;
    02754   }
    02755
    02756   _thd.place_mode = mode;
    02757   _thd.window_class = window_class;
    02758   _thd.window_number = window_num;
    02759
    02760   if (mode == VHM_SPECIAL) // special tools, like tunnels or docks start with presizing mode
    02761     VpStartPreSizing();
    02762
    02763   if ((int)icon < 0) {
    02764     SetAnimatedMouseCursor(_animcursors[~icon]);
    02765   } else {
    02766     SetMouseCursor(icon, pal);
    02767   }
    02768
    02769 }
    02770
    02771 void ResetObjectToPlace()
    02772 {
    02773   SetObjectToPlace(SPR_CURSOR_MOUSE, PAL_NONE, VHM_NONE, WC_MAIN_WINDOW, 0);
    02774 }