viewport.cpp

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

Generated on Wed Dec 23 20:12:53 2009 for OpenTTD by  doxygen 1.5.6