smallmap_gui.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "clear_map.h"
00014 #include "industry.h"
00015 #include "station_map.h"
00016 #include "landscape.h"
00017 #include "tree_map.h"
00018 #include "viewport_func.h"
00019 #include "town.h"
00020 #include "tunnelbridge_map.h"
00021 #include "core/endian_func.hpp"
00022 #include "vehicle_base.h"
00023 #include "sound_func.h"
00024 #include "window_func.h"
00025 #include "company_base.h"
00026 #include "smallmap_gui.h"
00027 
00028 #include "table/strings.h"
00029 
00030 static int _smallmap_industry_count; 
00031 static int _smallmap_company_count;  
00032 static int _smallmap_cargo_count;    
00033 
00035 static uint8 _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
00036 
00037 static const int NUM_NO_COMPANY_ENTRIES = 4; 
00038 
00039 static const uint8 PC_ROUGH_LAND      = 0x52; 
00040 static const uint8 PC_GRASS_LAND      = 0x54; 
00041 static const uint8 PC_BARE_LAND       = 0x37; 
00042 static const uint8 PC_FIELDS          = 0x25; 
00043 static const uint8 PC_TREES           = 0x57; 
00044 static const uint8 PC_WATER           = 0xCA; 
00045 
00047 #define MK(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
00048 
00050 #define MC(height)  {0, STR_TINY_BLACK_HEIGHT, INVALID_INDUSTRYTYPE, height, INVALID_COMPANY, true, false, false}
00051 
00053 #define MO(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
00054 
00056 #define MOEND() {0, 0, INVALID_INDUSTRYTYPE, 0, OWNER_NONE, true, true, false}
00057 
00059 #define MKEND() {0, STR_NULL, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, true, false}
00060 
00065 #define MS(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, true}
00066 
00068 static LegendAndColour _legend_land_contours[] = {
00069   /* The colours for the following values are set at BuildLandLegend() based on each colour scheme. */
00070   MC(0),
00071   MC(4),
00072   MC(8),
00073   MC(12),
00074   MC(14),
00075 
00076   MS(PC_BLACK,           STR_SMALLMAP_LEGENDA_ROADS),
00077   MK(PC_GREY,            STR_SMALLMAP_LEGENDA_RAILROADS),
00078   MK(PC_LIGHT_BLUE,      STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS),
00079   MK(PC_DARK_RED,        STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
00080   MK(PC_WHITE,           STR_SMALLMAP_LEGENDA_VEHICLES),
00081   MKEND()
00082 };
00083 
00084 static const LegendAndColour _legend_vehicles[] = {
00085   MK(PC_RED,             STR_SMALLMAP_LEGENDA_TRAINS),
00086   MK(PC_YELLOW,          STR_SMALLMAP_LEGENDA_ROAD_VEHICLES),
00087   MK(PC_LIGHT_BLUE,      STR_SMALLMAP_LEGENDA_SHIPS),
00088   MK(PC_WHITE,           STR_SMALLMAP_LEGENDA_AIRCRAFT),
00089 
00090   MS(PC_BLACK,           STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
00091   MK(PC_DARK_RED,        STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
00092   MKEND()
00093 };
00094 
00095 static const LegendAndColour _legend_routes[] = {
00096   MK(PC_BLACK,           STR_SMALLMAP_LEGENDA_ROADS),
00097   MK(PC_GREY,            STR_SMALLMAP_LEGENDA_RAILROADS),
00098   MK(PC_DARK_RED,        STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
00099 
00100   MS(PC_VERY_DARK_BROWN, STR_SMALLMAP_LEGENDA_RAILROAD_STATION),
00101   MK(PC_ORANGE,          STR_SMALLMAP_LEGENDA_TRUCK_LOADING_BAY),
00102   MK(PC_YELLOW,          STR_SMALLMAP_LEGENDA_BUS_STATION),
00103   MK(PC_RED,             STR_SMALLMAP_LEGENDA_AIRPORT_HELIPORT),
00104   MK(PC_LIGHT_BLUE,      STR_SMALLMAP_LEGENDA_DOCK),
00105   MKEND()
00106 };
00107 
00108 static const LegendAndColour _legend_vegetation[] = {
00109   MK(PC_ROUGH_LAND,      STR_SMALLMAP_LEGENDA_ROUGH_LAND),
00110   MK(PC_GRASS_LAND,      STR_SMALLMAP_LEGENDA_GRASS_LAND),
00111   MK(PC_BARE_LAND,       STR_SMALLMAP_LEGENDA_BARE_LAND),
00112   MK(PC_FIELDS,          STR_SMALLMAP_LEGENDA_FIELDS),
00113   MK(PC_TREES,           STR_SMALLMAP_LEGENDA_TREES),
00114   MK(PC_GREEN,           STR_SMALLMAP_LEGENDA_FOREST),
00115 
00116   MS(PC_GREY,            STR_SMALLMAP_LEGENDA_ROCKS),
00117   MK(PC_ORANGE,          STR_SMALLMAP_LEGENDA_DESERT),
00118   MK(PC_LIGHT_BLUE,      STR_SMALLMAP_LEGENDA_SNOW),
00119   MK(PC_BLACK,           STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
00120   MK(PC_DARK_RED,        STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
00121   MKEND()
00122 };
00123 
00124 static LegendAndColour _legend_land_owners[NUM_NO_COMPANY_ENTRIES + MAX_COMPANIES + 1] = {
00125   MO(PC_WATER,           STR_SMALLMAP_LEGENDA_WATER),
00126   MO(0x00,               STR_SMALLMAP_LEGENDA_NO_OWNER), // This colour will vary depending on settings.
00127   MO(PC_DARK_RED,        STR_SMALLMAP_LEGENDA_TOWNS),
00128   MO(PC_DARK_GREY,       STR_SMALLMAP_LEGENDA_INDUSTRIES),
00129   /* The legend will be terminated the first time it is used. */
00130   MOEND(),
00131 };
00132 
00133 #undef MK
00134 #undef MC
00135 #undef MS
00136 #undef MO
00137 #undef MOEND
00138 #undef MKEND
00139 
00141 static LegendAndColour _legend_linkstats[NUM_CARGO + lengthof(_linkstat_colours_in_legenda) + 1];
00146 static LegendAndColour _legend_from_industries[NUM_INDUSTRYTYPES + 1];
00148 static uint _industry_to_list_pos[NUM_INDUSTRYTYPES];
00150 static bool _smallmap_show_heightmap = false;
00152 static uint _company_to_list_pos[MAX_COMPANIES];
00153 
00157 void BuildIndustriesLegend()
00158 {
00159   uint j = 0;
00160 
00161   /* Add each name */
00162   for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
00163     IndustryType ind = _sorted_industry_types[i];
00164     const IndustrySpec *indsp = GetIndustrySpec(ind);
00165     if (indsp->enabled) {
00166       _legend_from_industries[j].legend = indsp->name;
00167       _legend_from_industries[j].colour = indsp->map_colour;
00168       _legend_from_industries[j].type = ind;
00169       _legend_from_industries[j].show_on_map = true;
00170       _legend_from_industries[j].col_break = false;
00171       _legend_from_industries[j].end = false;
00172 
00173       /* Store widget number for this industry type. */
00174       _industry_to_list_pos[ind] = j;
00175       j++;
00176     }
00177   }
00178   /* Terminate the list */
00179   _legend_from_industries[j].end = true;
00180 
00181   /* Store number of enabled industries */
00182   _smallmap_industry_count = j;
00183 }
00184 
00188 void BuildLinkStatsLegend()
00189 {
00190   /* Clear the legend */
00191   memset(_legend_linkstats, 0, sizeof(_legend_linkstats));
00192 
00193   uint i = 0;
00194   for (; i < _sorted_cargo_specs_size; ++i) {
00195     const CargoSpec *cs = _sorted_cargo_specs[i];
00196 
00197     _legend_linkstats[i].legend = cs->name;
00198     _legend_linkstats[i].colour = cs->legend_colour;
00199     _legend_linkstats[i].type = cs->Index();
00200     _legend_linkstats[i].show_on_map = true;
00201   }
00202 
00203   _legend_linkstats[i].col_break = true;
00204   _smallmap_cargo_count = i;
00205 
00206   for (; i < _smallmap_cargo_count + lengthof(_linkstat_colours_in_legenda); ++i) {
00207     _legend_linkstats[i].legend = STR_EMPTY;
00208     _legend_linkstats[i].colour = LinkGraphOverlay::LINK_COLOURS[_linkstat_colours_in_legenda[i - _smallmap_cargo_count]];
00209     _legend_linkstats[i].show_on_map = true;
00210   }
00211 
00212   _legend_linkstats[_smallmap_cargo_count].legend = STR_LINKGRAPH_LEGEND_UNUSED;
00213   _legend_linkstats[i - 1].legend = STR_LINKGRAPH_LEGEND_OVERLOADED;
00214   _legend_linkstats[(_smallmap_cargo_count + i - 1) / 2].legend = STR_LINKGRAPH_LEGEND_SATURATED;
00215   _legend_linkstats[i].end = true;
00216 }
00217 
00218 static const LegendAndColour * const _legend_table[] = {
00219   _legend_land_contours,
00220   _legend_vehicles,
00221   _legend_from_industries,
00222   _legend_linkstats,
00223   _legend_routes,
00224   _legend_vegetation,
00225   _legend_land_owners,
00226 };
00227 
00228 #define MKCOLOUR(x)         TO_LE32X(x)
00229 
00230 #define MKCOLOUR_XXXX(x)    (MKCOLOUR(0x01010101) * (uint)(x))
00231 #define MKCOLOUR_X0X0(x)    (MKCOLOUR(0x01000100) * (uint)(x))
00232 #define MKCOLOUR_0X0X(x)    (MKCOLOUR(0x00010001) * (uint)(x))
00233 #define MKCOLOUR_0XX0(x)    (MKCOLOUR(0x00010100) * (uint)(x))
00234 #define MKCOLOUR_X00X(x)    (MKCOLOUR(0x01000001) * (uint)(x))
00235 
00236 #define MKCOLOUR_XYXY(x, y) (MKCOLOUR_X0X0(x) | MKCOLOUR_0X0X(y))
00237 #define MKCOLOUR_XYYX(x, y) (MKCOLOUR_X00X(x) | MKCOLOUR_0XX0(y))
00238 
00239 #define MKCOLOUR_0000       MKCOLOUR_XXXX(0x00)
00240 #define MKCOLOUR_0FF0       MKCOLOUR_0XX0(0xFF)
00241 #define MKCOLOUR_F00F       MKCOLOUR_X00X(0xFF)
00242 #define MKCOLOUR_FFFF       MKCOLOUR_XXXX(0xFF)
00243 
00245 static const uint32 _green_map_heights[] = {
00246   MKCOLOUR_XXXX(0x5A),
00247   MKCOLOUR_XYXY(0x5A, 0x5B),
00248   MKCOLOUR_XXXX(0x5B),
00249   MKCOLOUR_XYXY(0x5B, 0x5C),
00250   MKCOLOUR_XXXX(0x5C),
00251   MKCOLOUR_XYXY(0x5C, 0x5D),
00252   MKCOLOUR_XXXX(0x5D),
00253   MKCOLOUR_XYXY(0x5D, 0x5E),
00254   MKCOLOUR_XXXX(0x5E),
00255   MKCOLOUR_XYXY(0x5E, 0x5F),
00256   MKCOLOUR_XXXX(0x5F),
00257   MKCOLOUR_XYXY(0x5F, 0x1F),
00258   MKCOLOUR_XXXX(0x1F),
00259   MKCOLOUR_XYXY(0x1F, 0x27),
00260   MKCOLOUR_XXXX(0x27),
00261   MKCOLOUR_XXXX(0x27),
00262 };
00263 assert_compile(lengthof(_green_map_heights) == MAX_TILE_HEIGHT + 1);
00264 
00266 static const uint32 _dark_green_map_heights[] = {
00267   MKCOLOUR_XXXX(0x60),
00268   MKCOLOUR_XYXY(0x60, 0x61),
00269   MKCOLOUR_XXXX(0x61),
00270   MKCOLOUR_XYXY(0x61, 0x62),
00271   MKCOLOUR_XXXX(0x62),
00272   MKCOLOUR_XYXY(0x62, 0x63),
00273   MKCOLOUR_XXXX(0x63),
00274   MKCOLOUR_XYXY(0x63, 0x64),
00275   MKCOLOUR_XXXX(0x64),
00276   MKCOLOUR_XYXY(0x64, 0x65),
00277   MKCOLOUR_XXXX(0x65),
00278   MKCOLOUR_XYXY(0x65, 0x66),
00279   MKCOLOUR_XXXX(0x66),
00280   MKCOLOUR_XYXY(0x66, 0x67),
00281   MKCOLOUR_XXXX(0x67),
00282   MKCOLOUR_XXXX(0x67),
00283 };
00284 assert_compile(lengthof(_dark_green_map_heights) == MAX_TILE_HEIGHT + 1);
00285 
00287 static const uint32 _violet_map_heights[] = {
00288   MKCOLOUR_XXXX(0x80),
00289   MKCOLOUR_XYXY(0x80, 0x81),
00290   MKCOLOUR_XXXX(0x81),
00291   MKCOLOUR_XYXY(0x81, 0x82),
00292   MKCOLOUR_XXXX(0x82),
00293   MKCOLOUR_XYXY(0x82, 0x83),
00294   MKCOLOUR_XXXX(0x83),
00295   MKCOLOUR_XYXY(0x83, 0x84),
00296   MKCOLOUR_XXXX(0x84),
00297   MKCOLOUR_XYXY(0x84, 0x85),
00298   MKCOLOUR_XXXX(0x85),
00299   MKCOLOUR_XYXY(0x85, 0x86),
00300   MKCOLOUR_XXXX(0x86),
00301   MKCOLOUR_XYXY(0x86, 0x87),
00302   MKCOLOUR_XXXX(0x87),
00303   MKCOLOUR_XXXX(0x87),
00304 };
00305 assert_compile(lengthof(_violet_map_heights) == MAX_TILE_HEIGHT + 1);
00306 
00308 struct SmallMapColourScheme {
00309   const uint32 *height_colours; 
00310   uint32 default_colour;   
00311 };
00312 
00314 static const SmallMapColourScheme _heightmap_schemes[] = {
00315   {_green_map_heights,      MKCOLOUR_XXXX(0x54)}, 
00316   {_dark_green_map_heights, MKCOLOUR_XXXX(0x62)}, 
00317   {_violet_map_heights,     MKCOLOUR_XXXX(0x82)}, 
00318 };
00319 
00323 void BuildLandLegend()
00324 {
00325   for (LegendAndColour *lc = _legend_land_contours; lc->legend == STR_TINY_BLACK_HEIGHT; lc++) {
00326     lc->colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[lc->height];
00327   }
00328 }
00329 
00333 void BuildOwnerLegend()
00334 {
00335   _legend_land_owners[1].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].default_colour;
00336 
00337   int i = NUM_NO_COMPANY_ENTRIES;
00338   const Company *c;
00339   FOR_ALL_COMPANIES(c) {
00340     _legend_land_owners[i].colour = _colour_gradient[c->colour][5];
00341     _legend_land_owners[i].company = c->index;
00342     _legend_land_owners[i].show_on_map = true;
00343     _legend_land_owners[i].col_break = false;
00344     _legend_land_owners[i].end = false;
00345     _company_to_list_pos[c->index] = i;
00346     i++;
00347   }
00348 
00349   /* Terminate the list */
00350   _legend_land_owners[i].end = true;
00351 
00352   /* Store maximum amount of owner legend entries. */
00353   _smallmap_company_count = i;
00354 }
00355 
00356 struct AndOr {
00357   uint32 mor;
00358   uint32 mand;
00359 };
00360 
00361 static inline uint32 ApplyMask(uint32 colour, const AndOr *mask)
00362 {
00363   return (colour & mask->mand) | mask->mor;
00364 }
00365 
00366 
00368 static const AndOr _smallmap_contours_andor[] = {
00369   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_CLEAR
00370   {MKCOLOUR_0XX0(PC_GREY      ), MKCOLOUR_F00F}, // MP_RAILWAY
00371   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F}, // MP_ROAD
00372   {MKCOLOUR_0XX0(PC_DARK_RED  ), MKCOLOUR_F00F}, // MP_HOUSE
00373   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_TREES
00374   {MKCOLOUR_XXXX(PC_LIGHT_BLUE), MKCOLOUR_0000}, // MP_STATION
00375   {MKCOLOUR_XXXX(PC_WATER     ), MKCOLOUR_0000}, // MP_WATER
00376   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_VOID
00377   {MKCOLOUR_XXXX(PC_DARK_RED  ), MKCOLOUR_0000}, // MP_INDUSTRY
00378   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
00379   {MKCOLOUR_0XX0(PC_DARK_RED  ), MKCOLOUR_F00F}, // MP_OBJECT
00380   {MKCOLOUR_0XX0(PC_GREY      ), MKCOLOUR_F00F},
00381 };
00382 
00384 static const AndOr _smallmap_vehicles_andor[] = {
00385   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_CLEAR
00386   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F}, // MP_RAILWAY
00387   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F}, // MP_ROAD
00388   {MKCOLOUR_0XX0(PC_DARK_RED  ), MKCOLOUR_F00F}, // MP_HOUSE
00389   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_TREES
00390   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F}, // MP_STATION
00391   {MKCOLOUR_XXXX(PC_WATER     ), MKCOLOUR_0000}, // MP_WATER
00392   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_VOID
00393   {MKCOLOUR_XXXX(PC_DARK_RED  ), MKCOLOUR_0000}, // MP_INDUSTRY
00394   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
00395   {MKCOLOUR_0XX0(PC_DARK_RED  ), MKCOLOUR_F00F}, // MP_OBJECT
00396   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F},
00397 };
00398 
00400 static const byte _tiletype_importance[] = {
00401   2, // MP_CLEAR
00402   8, // MP_RAILWAY
00403   7, // MP_ROAD
00404   5, // MP_HOUSE
00405   2, // MP_TREES
00406   9, // MP_STATION
00407   2, // MP_WATER
00408   1, // MP_VOID
00409   6, // MP_INDUSTRY
00410   8, // MP_TUNNELBRIDGE
00411   2, // MP_OBJECT
00412   0,
00413 };
00414 
00415 
00416 static inline TileType GetEffectiveTileType(TileIndex tile)
00417 {
00418   TileType t = GetTileType(tile);
00419 
00420   if (t == MP_TUNNELBRIDGE) {
00421     TransportType tt = GetTunnelBridgeTransportType(tile);
00422 
00423     switch (tt) {
00424       case TRANSPORT_RAIL: t = MP_RAILWAY; break;
00425       case TRANSPORT_ROAD: t = MP_ROAD;    break;
00426       default:             t = MP_WATER;   break;
00427     }
00428   }
00429   return t;
00430 }
00431 
00438 static inline uint32 GetSmallMapContoursPixels(TileIndex tile, TileType t)
00439 {
00440   const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00441   return ApplyMask(cs->height_colours[TileHeight(tile)], &_smallmap_contours_andor[t]);
00442 }
00443 
00451 static inline uint32 GetSmallMapVehiclesPixels(TileIndex tile, TileType t)
00452 {
00453   const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00454   return ApplyMask(cs->default_colour, &_smallmap_vehicles_andor[t]);
00455 }
00456 
00464 static inline uint32 GetSmallMapIndustriesPixels(TileIndex tile, TileType t)
00465 {
00466   if (t == MP_INDUSTRY) {
00467     /* If industry is allowed to be seen, use its colour on the map */
00468     if (_legend_from_industries[_industry_to_list_pos[Industry::GetByTile(tile)->type]].show_on_map) {
00469       return GetIndustrySpec(Industry::GetByTile(tile)->type)->map_colour * 0x01010101;
00470     } else {
00471       /* Otherwise, return the colour which will make it disappear */
00472       t = (IsTileOnWater(tile) ? MP_WATER : MP_CLEAR);
00473     }
00474   }
00475 
00476   const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00477   return ApplyMask(_smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour, &_smallmap_vehicles_andor[t]);
00478 }
00479 
00487 static inline uint32 GetSmallMapRoutesPixels(TileIndex tile, TileType t)
00488 {
00489   if (t == MP_STATION) {
00490     switch (GetStationType(tile)) {
00491       case STATION_RAIL:    return MKCOLOUR_XXXX(PC_VERY_DARK_BROWN);
00492       case STATION_AIRPORT: return MKCOLOUR_XXXX(PC_RED);
00493       case STATION_TRUCK:   return MKCOLOUR_XXXX(PC_ORANGE);
00494       case STATION_BUS:     return MKCOLOUR_XXXX(PC_YELLOW);
00495       case STATION_DOCK:    return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
00496       default:              return MKCOLOUR_FFFF;
00497     }
00498   } else if (t == MP_RAILWAY) {
00499     AndOr andor = {
00500       MKCOLOUR_0XX0(GetRailTypeInfo(GetRailType(tile))->map_colour),
00501       _smallmap_contours_andor[t].mand
00502     };
00503 
00504     const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00505     return ApplyMask(cs->default_colour, &andor);
00506   }
00507 
00508   /* Ground colour */
00509   const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00510   return ApplyMask(cs->default_colour, &_smallmap_contours_andor[t]);
00511 }
00512 
00520 static inline uint32 GetSmallMapLinkStatsPixels(TileIndex tile, TileType t)
00521 {
00522   return _smallmap_show_heightmap ? GetSmallMapContoursPixels(tile, t) : GetSmallMapRoutesPixels(tile, t);
00523 }
00524 
00525 static const uint32 _vegetation_clear_bits[] = {
00526   MKCOLOUR_XXXX(PC_GRASS_LAND), 
00527   MKCOLOUR_XXXX(PC_ROUGH_LAND), 
00528   MKCOLOUR_XXXX(PC_GREY),       
00529   MKCOLOUR_XXXX(PC_FIELDS),     
00530   MKCOLOUR_XXXX(PC_LIGHT_BLUE), 
00531   MKCOLOUR_XXXX(PC_ORANGE),     
00532   MKCOLOUR_XXXX(PC_GRASS_LAND), 
00533   MKCOLOUR_XXXX(PC_GRASS_LAND), 
00534 };
00535 
00543 static inline uint32 GetSmallMapVegetationPixels(TileIndex tile, TileType t)
00544 {
00545   switch (t) {
00546     case MP_CLEAR:
00547       return (IsClearGround(tile, CLEAR_GRASS) && GetClearDensity(tile) < 3) ? MKCOLOUR_XXXX(PC_BARE_LAND) : _vegetation_clear_bits[GetClearGround(tile)];
00548 
00549     case MP_INDUSTRY:
00550       return IsTileForestIndustry(tile) ? MKCOLOUR_XXXX(PC_GREEN) : MKCOLOUR_XXXX(PC_DARK_RED);
00551 
00552     case MP_TREES:
00553       if (GetTreeGround(tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(tile) == TREE_GROUND_ROUGH_SNOW) {
00554         return (_settings_game.game_creation.landscape == LT_ARCTIC) ? MKCOLOUR_XYYX(PC_LIGHT_BLUE, PC_TREES) : MKCOLOUR_XYYX(PC_ORANGE, PC_TREES);
00555       }
00556       return MKCOLOUR_XYYX(PC_GRASS_LAND, PC_TREES);
00557 
00558     default:
00559       return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND), &_smallmap_vehicles_andor[t]);
00560   }
00561 }
00562 
00570 static inline uint32 GetSmallMapOwnerPixels(TileIndex tile, TileType t)
00571 {
00572   Owner o;
00573 
00574   switch (t) {
00575     case MP_INDUSTRY: return MKCOLOUR_XXXX(PC_DARK_GREY);
00576     case MP_HOUSE:    return MKCOLOUR_XXXX(PC_DARK_RED);
00577     default:          o = GetTileOwner(tile); break;
00578     /* FIXME: For MP_ROAD there are multiple owners.
00579      * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
00580      * even if there are no ROADTYPE_ROAD bits on the tile.
00581      */
00582   }
00583 
00584   if ((o < MAX_COMPANIES && !_legend_land_owners[_company_to_list_pos[o]].show_on_map) || o == OWNER_NONE || o == OWNER_WATER) {
00585     if (t == MP_WATER) return MKCOLOUR_XXXX(PC_WATER);
00586     const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00587     return _smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour;
00588   } else if (o == OWNER_TOWN) {
00589     return MKCOLOUR_XXXX(PC_DARK_RED);
00590   }
00591 
00592   return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
00593 }
00594 
00596 static const byte _vehicle_type_colours[6] = {
00597   PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
00598 };
00599 
00600 inline Point SmallMapWindow::SmallmapRemapCoords(int x, int y) const
00601 {
00602   Point pt;
00603   pt.x = (y - x) * 2;
00604   pt.y = y + x;
00605   return pt;
00606 }
00607 
00614 inline Point SmallMapWindow::RemapTile(int tile_x, int tile_y) const
00615 {
00616   int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
00617   int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
00618 
00619   if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
00620 
00621   /* For negative offsets, round towards -inf. */
00622   if (x_offset < 0) x_offset -= this->zoom - 1;
00623   if (y_offset < 0) y_offset -= this->zoom - 1;
00624 
00625   return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
00626 }
00627 
00638 inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) const
00639 {
00640   if (add_sub) px += this->subscroll;  // Total horizontal offset.
00641 
00642   /* For each two rows down, add a x and a y tile, and
00643    * For each four pixels to the right, move a tile to the right. */
00644   Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
00645   px &= 3;
00646 
00647   if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
00648     if (px < 2) {
00649       pt.x += this->zoom;
00650       px += 2;
00651     } else {
00652       pt.y += this->zoom;
00653       px -= 2;
00654     }
00655   }
00656 
00657   *sub = px;
00658   return pt;
00659 }
00660 
00670 Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
00671 {
00672   assert(x >= 0 && y >= 0);
00673 
00674   int new_sub;
00675   Point tile_xy = PixelToTile(x, y, &new_sub, false);
00676   tx -= tile_xy.x;
00677   ty -= tile_xy.y;
00678 
00679   Point scroll;
00680   if (new_sub == 0) {
00681     *sub = 0;
00682     scroll.x = (tx + this->zoom) * TILE_SIZE;
00683     scroll.y = (ty - this->zoom) * TILE_SIZE;
00684   } else {
00685     *sub = 4 - new_sub;
00686     scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
00687     scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
00688   }
00689   return scroll;
00690 }
00691 
00698 void SmallMapWindow::SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
00699 {
00700   static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
00701   static const int MIN_ZOOM_INDEX = 0;
00702   static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
00703 
00704   int new_index, cur_index, sub;
00705   Point tile;
00706   switch (change) {
00707     case ZLC_INITIALIZE:
00708       cur_index = - 1; // Definitely different from new_index.
00709       new_index = MIN_ZOOM_INDEX;
00710       break;
00711 
00712     case ZLC_ZOOM_IN:
00713     case ZLC_ZOOM_OUT:
00714       for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
00715         if (this->zoom == zoomlevels[cur_index]) break;
00716       }
00717       assert(cur_index <= MAX_ZOOM_INDEX);
00718 
00719       tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
00720       new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
00721       break;
00722 
00723     default: NOT_REACHED();
00724   }
00725 
00726   if (new_index != cur_index) {
00727     this->zoom = zoomlevels[new_index];
00728     if (cur_index >= 0) {
00729       Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
00730       this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
00731           this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
00732     } else if (this->map_type == SMT_LINKSTATS) {
00733       this->overlay->RebuildCache();
00734     }
00735     this->SetWidgetDisabledState(WID_SM_ZOOM_IN,  this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
00736     this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
00737     this->SetDirty();
00738   }
00739 }
00740 
00746 inline uint32 SmallMapWindow::GetTileColours(const TileArea &ta) const
00747 {
00748   int importance = 0;
00749   TileIndex tile = INVALID_TILE; // Position of the most important tile.
00750   TileType et = MP_VOID;         // Effective tile type at that position.
00751 
00752   TILE_AREA_LOOP(ti, ta) {
00753     TileType ttype = GetEffectiveTileType(ti);
00754     if (_tiletype_importance[ttype] > importance) {
00755       importance = _tiletype_importance[ttype];
00756       tile = ti;
00757       et = ttype;
00758     }
00759   }
00760 
00761   switch (this->map_type) {
00762     case SMT_CONTOUR:
00763       return GetSmallMapContoursPixels(tile, et);
00764 
00765     case SMT_VEHICLES:
00766       return GetSmallMapVehiclesPixels(tile, et);
00767 
00768     case SMT_INDUSTRY:
00769       return GetSmallMapIndustriesPixels(tile, et);
00770 
00771     case SMT_LINKSTATS:
00772       return GetSmallMapLinkStatsPixels(tile, et);
00773 
00774     case SMT_ROUTES:
00775       return GetSmallMapRoutesPixels(tile, et);
00776 
00777     case SMT_VEGETATION:
00778       return GetSmallMapVegetationPixels(tile, et);
00779 
00780     case SMT_OWNER:
00781       return GetSmallMapOwnerPixels(tile, et);
00782 
00783     default: NOT_REACHED();
00784   }
00785 }
00786 
00801 void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
00802 {
00803   void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
00804   uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
00805 
00806   do {
00807     /* Check if the tile (xc,yc) is within the map range */
00808     if (xc >= MapMaxX() || yc >= MapMaxY()) continue;
00809 
00810     /* Check if the dst pointer points to a pixel inside the screen buffer */
00811     if (dst < _screen.dst_ptr) continue;
00812     if (dst >= dst_ptr_abs_end) continue;
00813 
00814     /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
00815     TileArea ta;
00816     if (min_xy == 1 && (xc == 0 || yc == 0)) {
00817       if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
00818 
00819       ta = TileArea(TileXY(max(min_xy, xc), max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
00820     } else {
00821       ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
00822     }
00823     ta.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
00824 
00825     uint32 val = this->GetTileColours(ta);
00826     uint8 *val8 = (uint8 *)&val;
00827     int idx = max(0, -start_pos);
00828     for (int pos = max(0, start_pos); pos < end_pos; pos++) {
00829       blitter->SetPixel(dst, idx, 0, val8[idx]);
00830       idx++;
00831     }
00832   /* Switch to next tile in the column */
00833   } while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
00834 }
00835 
00841 void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
00842 {
00843   const Vehicle *v;
00844   FOR_ALL_VEHICLES(v) {
00845     if (v->type == VEH_EFFECT) continue;
00846     if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
00847 
00848     /* Remap into flat coordinates. */
00849     Point pt = this->RemapTile(v->x_pos / TILE_SIZE, v->y_pos / TILE_SIZE);
00850 
00851     int y = pt.y - dpi->top;
00852     if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
00853 
00854     bool skip = false; // Default is to draw both pixels.
00855     int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
00856     if (x < 0) {
00857       /* if x+1 is 0, that means we're on the very left edge,
00858        * and should thus only draw a single pixel */
00859       if (++x != 0) continue;
00860       skip = true;
00861     } else if (x >= dpi->width - 1) {
00862       /* Check if we're at the very right edge, and if so draw only a single pixel */
00863       if (x != dpi->width - 1) continue;
00864       skip = true;
00865     }
00866 
00867     /* Calculate pointer to pixel and the colour */
00868     byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
00869 
00870     /* And draw either one or two pixels depending on clipping */
00871     blitter->SetPixel(dpi->dst_ptr, x, y, colour);
00872     if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
00873   }
00874 }
00875 
00880 void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
00881 {
00882   const Town *t;
00883   FOR_ALL_TOWNS(t) {
00884     /* Remap the town coordinate */
00885     Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
00886     int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
00887     int y = pt.y;
00888 
00889     /* Check if the town sign is within bounds */
00890     if (x + t->cache.sign.width_small > dpi->left &&
00891         x < dpi->left + dpi->width &&
00892         y + FONT_HEIGHT_SMALL > dpi->top &&
00893         y < dpi->top + dpi->height) {
00894       /* And draw it. */
00895       SetDParam(0, t->index);
00896       DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
00897     }
00898   }
00899 }
00900 
00904 void SmallMapWindow::DrawMapIndicators() const
00905 {
00906   /* Find main viewport. */
00907   const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
00908 
00909   Point tile = InverseRemapCoords(vp->virtual_left, vp->virtual_top);
00910   Point tl = this->RemapTile(tile.x >> 4, tile.y >> 4);
00911   tl.x -= this->subscroll;
00912 
00913   tile = InverseRemapCoords(vp->virtual_left + vp->virtual_width, vp->virtual_top + vp->virtual_height);
00914   Point br = this->RemapTile(tile.x >> 4, tile.y >> 4);
00915   br.x -= this->subscroll;
00916 
00917   SmallMapWindow::DrawVertMapIndicator(tl.x, tl.y, br.y);
00918   SmallMapWindow::DrawVertMapIndicator(br.x, tl.y, br.y);
00919 
00920   SmallMapWindow::DrawHorizMapIndicator(tl.x, br.x, tl.y);
00921   SmallMapWindow::DrawHorizMapIndicator(tl.x, br.x, br.y);
00922 }
00923 
00935 void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
00936 {
00937   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00938   DrawPixelInfo *old_dpi;
00939 
00940   old_dpi = _cur_dpi;
00941   _cur_dpi = dpi;
00942 
00943   /* Clear it */
00944   GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
00945 
00946   /* Which tile is displayed at (dpi->left, dpi->top)? */
00947   int dx;
00948   Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
00949   int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
00950   int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
00951 
00952   void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
00953   int x = - dx - 4;
00954   int y = 0;
00955 
00956   for (;;) {
00957     /* Distance from left edge */
00958     if (x >= -3) {
00959       if (x >= dpi->width) break; // Exit the loop.
00960 
00961       int end_pos = min(dpi->width, x + 4);
00962       int reps = (dpi->height - y + 1) / 2; // Number of lines.
00963       if (reps > 0) {
00964         this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
00965       }
00966     }
00967 
00968     if (y == 0) {
00969       tile_y += this->zoom;
00970       y++;
00971       ptr = blitter->MoveTo(ptr, 0, 1);
00972     } else {
00973       tile_x -= this->zoom;
00974       y--;
00975       ptr = blitter->MoveTo(ptr, 0, -1);
00976     }
00977     ptr = blitter->MoveTo(ptr, 2, 0);
00978     x += 2;
00979   }
00980 
00981   /* Draw vehicles */
00982   if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
00983 
00984   /* Draw link stat overlay */
00985   if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
00986 
00987   /* Draw town names */
00988   if (this->show_towns) this->DrawTowns(dpi);
00989 
00990   /* Draw map indicators */
00991   this->DrawMapIndicators();
00992 
00993   _cur_dpi = old_dpi;
00994 }
00995 
00999 void SmallMapWindow::SetupWidgetData()
01000 {
01001   StringID legend_tooltip;
01002   StringID enable_all_tooltip;
01003   StringID disable_all_tooltip;
01004   int plane;
01005   switch (this->map_type) {
01006     case SMT_INDUSTRY:
01007       legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
01008       enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
01009       disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
01010       plane = 0;
01011       break;
01012 
01013     case SMT_OWNER:
01014       legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
01015       enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
01016       disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
01017       plane = 0;
01018       break;
01019 
01020     case SMT_LINKSTATS:
01021       legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
01022       enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
01023       disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
01024       plane = 0;
01025       break;
01026 
01027     default:
01028       legend_tooltip = STR_NULL;
01029       enable_all_tooltip = STR_NULL;
01030       disable_all_tooltip = STR_NULL;
01031       plane = 1;
01032       break;
01033   }
01034 
01035   this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
01036   this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
01037   this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
01038   this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
01039 }
01040 
01041 SmallMapWindow::SmallMapWindow(const WindowDesc *desc, int window_number) :
01042     Window(),
01043     refresh(FORCE_REFRESH_PERIOD)
01044 {
01045   this->overlay = new LinkGraphOverlay(this, WID_SM_MAP);
01046   this->InitNested(desc, window_number);
01047   this->LowerWidget(this->map_type + WID_SM_CONTOUR);
01048 
01049   BuildLandLegend();
01050   this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
01051 
01052   this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
01053 
01054   this->SetupWidgetData();
01055 
01056   this->SetZoomLevel(ZLC_INITIALIZE, NULL);
01057   this->SmallMapCenterOnCurrentPos();
01058 }
01059 
01060 void SmallMapWindow::SetStringParameters(int widget) const
01061 {
01062   switch (widget) {
01063     case WID_SM_CAPTION:
01064       SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
01065       break;
01066   }
01067 }
01068 
01069 void SmallMapWindow::OnInit()
01070 {
01071   uint min_width = 0;
01072   this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
01073   this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
01074   for (uint i = 0; i < lengthof(_legend_table); i++) {
01075     uint height = 0;
01076     uint num_columns = 1;
01077     for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
01078       StringID str;
01079       if (i == SMT_INDUSTRY) {
01080         SetDParam(0, tbl->legend);
01081         SetDParam(1, IndustryPool::MAX_SIZE);
01082         str = STR_SMALLMAP_INDUSTRY;
01083       } else if (i == SMT_LINKSTATS) {
01084         SetDParam(0, tbl->legend);
01085         str = STR_SMALLMAP_LINKSTATS;
01086       } else if (i == SMT_OWNER) {
01087         if (tbl->company != INVALID_COMPANY) {
01088           if (!Company::IsValidID(tbl->company)) {
01089             /* Rebuild the owner legend. */
01090             BuildOwnerLegend();
01091             this->OnInit();
01092             return;
01093           }
01094           /* Non-fixed legend entries for the owner view. */
01095           SetDParam(0, tbl->company);
01096           str = STR_SMALLMAP_COMPANY;
01097         } else {
01098           str = tbl->legend;
01099         }
01100       } else {
01101         if (tbl->col_break) {
01102           this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
01103           height = 0;
01104           num_columns++;
01105         }
01106         height++;
01107         str = tbl->legend;
01108       }
01109       min_width = max(GetStringBoundingBox(str).width, min_width);
01110     }
01111     this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
01112     this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
01113   }
01114 
01115   /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
01116   this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
01117 }
01118 
01119 void SmallMapWindow::OnPaint()
01120 {
01121   if (this->map_type == SMT_OWNER) {
01122     for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
01123       if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
01124         /* Rebuild the owner legend. */
01125         BuildOwnerLegend();
01126         this->InvalidateData(1);
01127         break;
01128       }
01129     }
01130   }
01131 
01132   this->DrawWidgets();
01133 }
01134 
01135 void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
01136 {
01137   switch (widget) {
01138     case WID_SM_MAP: {
01139       DrawPixelInfo new_dpi;
01140       if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
01141       this->DrawSmallMap(&new_dpi);
01142       break;
01143     }
01144 
01145     case WID_SM_LEGEND: {
01146       uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
01147       uint number_of_rows = this->GetNumberRowsLegend(columns);
01148       bool rtl = _current_text_dir == TD_RTL;
01149       uint y_org = r.top + WD_FRAMERECT_TOP;
01150       uint x = rtl ? r.right - this->column_width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT;
01151       uint y = y_org;
01152       uint i = 0; // Row counter for industry legend.
01153       uint row_height = FONT_HEIGHT_SMALL;
01154 
01155       uint text_left  = rtl ? 0 : LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT;
01156       uint text_right = this->column_width - 1 - (rtl ? LEGEND_BLOB_WIDTH + WD_FRAMERECT_RIGHT : 0);
01157       uint blob_left  = rtl ? this->column_width - 1 - LEGEND_BLOB_WIDTH : 0;
01158       uint blob_right = rtl ? this->column_width - 1 : LEGEND_BLOB_WIDTH;
01159 
01160       StringID string = STR_NULL;
01161       switch (this->map_type) {
01162         case SMT_INDUSTRY:
01163           string = STR_SMALLMAP_INDUSTRY;
01164           break;
01165         case SMT_LINKSTATS:
01166           string = STR_SMALLMAP_LINKSTATS;
01167           break;
01168         case SMT_OWNER:
01169           string = STR_SMALLMAP_COMPANY;
01170           break;
01171         default:
01172           break;
01173       }
01174 
01175       for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
01176         if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
01177           /* Column break needed, continue at top, COLUMN_WIDTH pixels
01178            * (one "row") to the right. */
01179           x += rtl ? -(int)this->column_width : this->column_width;
01180           y = y_org;
01181           i = 1;
01182         }
01183 
01184         switch (this->map_type) {
01185           case SMT_INDUSTRY:
01186             /* Industry name must be formatted, since it's not in tiny font in the specs.
01187              * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
01188             SetDParam(0, tbl->legend);
01189             SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
01190             /* FALL THROUGH */
01191           case SMT_LINKSTATS:
01192             SetDParam(0, tbl->legend);
01193             /* FALL_THROUGH */
01194           case SMT_OWNER:
01195             if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
01196               if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
01197               if (!tbl->show_on_map) {
01198                 /* Simply draw the string, not the black border of the legend colour.
01199                  * This will enforce the idea of the disabled item */
01200                 DrawString(x + text_left, x + text_right, y, string, TC_GREY);
01201               } else {
01202                 DrawString(x + text_left, x + text_right, y, string, TC_BLACK);
01203                 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK); // Outer border of the legend colour
01204               }
01205               break;
01206             }
01207             /* FALL_THROUGH */
01208           default:
01209             if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
01210             /* Anything that is not an industry or a company is using normal process */
01211             GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK);
01212             DrawString(x + text_left, x + text_right, y, tbl->legend);
01213             break;
01214         }
01215 
01216         GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, tbl->colour); // Legend colour
01217 
01218         y += row_height;
01219       }
01220     }
01221   }
01222 }
01223 
01232 inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
01233 {
01234   /* reserve one column for link colours */
01235   uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
01236   uint num_rows_others = CeilDiv(max(_smallmap_industry_count,_smallmap_company_count), columns);
01237   return max(this->min_number_of_fixed_rows, max(num_rows_linkstats, num_rows_others));
01238 }
01239 
01251 void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
01252 {
01253   if (_ctrl_pressed) {
01254     /* Disable all, except the clicked one */
01255     bool changes = false;
01256     for (int i = begin_legend_item; i != end_legend_item; i++) {
01257       bool new_state = (i == click_pos);
01258       if (legend[i].show_on_map != new_state) {
01259         changes = true;
01260         legend[i].show_on_map = new_state;
01261       }
01262     }
01263     if (!changes) {
01264       /* Nothing changed? Then show all (again). */
01265       for (int i = begin_legend_item; i != end_legend_item; i++) {
01266         legend[i].show_on_map = true;
01267       }
01268     }
01269   } else {
01270     legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
01271   }
01272   this->SetOverlayCargoMask();
01273 }
01274 
01278 void SmallMapWindow::SetOverlayCargoMask()
01279 {
01280   if (this->map_type == SMT_LINKSTATS) {
01281     uint32 cargo_mask = 0;
01282     for (int i = 0; i != _smallmap_cargo_count; ++i) {
01283       if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
01284     }
01285     this->overlay->SetCargoMask(cargo_mask);
01286     this->overlay->RebuildCache();
01287   }
01288 }
01289 
01294 void SmallMapWindow::SwitchMapType(SmallMapType map_type)
01295 {
01296   this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
01297   this->map_type = map_type;
01298   this->LowerWidget(this->map_type + WID_SM_CONTOUR);
01299 
01300   this->SetupWidgetData();
01301 
01302   if (map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
01303   this->SetDirty();
01304 }
01305 
01306 void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
01307 {
01308   /* User clicked something, notify the industry chain window to stop sending newly selected industries. */
01309   InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
01310 
01311   switch (widget) {
01312     case WID_SM_MAP: { // Map window
01313       /*
01314        * XXX: scrolling with the left mouse button is done by subsequently
01315        * clicking with the left mouse button; clicking once centers the
01316        * large map at the selected point. So by unclicking the left mouse
01317        * button here, it gets reclicked during the next inputloop, which
01318        * would make it look like the mouse is being dragged, while it is
01319        * actually being (virtually) clicked every inputloop.
01320        */
01321       _left_button_clicked = false;
01322 
01323       const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01324       Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
01325       int sub;
01326       pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
01327       pt = RemapCoords(this->scroll_x + pt.x * TILE_SIZE + this->zoom * (TILE_SIZE - sub * TILE_SIZE / 4),
01328           this->scroll_y + pt.y * TILE_SIZE + sub * this->zoom * TILE_SIZE / 4, 0);
01329 
01330       w->viewport->follow_vehicle = INVALID_VEHICLE;
01331       w->viewport->dest_scrollpos_x = pt.x - (w->viewport->virtual_width  >> 1);
01332       w->viewport->dest_scrollpos_y = pt.y - (w->viewport->virtual_height >> 1);
01333 
01334       this->SetDirty();
01335       break;
01336     }
01337 
01338     case WID_SM_ZOOM_IN:
01339     case WID_SM_ZOOM_OUT: {
01340       const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01341       Point pt = {wid->current_x / 2, wid->current_y / 2};
01342       this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
01343       SndPlayFx(SND_15_BEEP);
01344       break;
01345     }
01346 
01347     case WID_SM_CONTOUR:    // Show land contours
01348     case WID_SM_VEHICLES:   // Show vehicles
01349     case WID_SM_INDUSTRIES: // Show industries
01350     case WID_SM_LINKSTATS:  // Show route map
01351     case WID_SM_ROUTES:     // Show transport routes
01352     case WID_SM_VEGETATION: // Show vegetation
01353     case WID_SM_OWNERS:     // Show land owners
01354       this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
01355       SndPlayFx(SND_15_BEEP);
01356       break;
01357 
01358     case WID_SM_CENTERMAP: // Center the smallmap again
01359       this->SmallMapCenterOnCurrentPos();
01360       this->HandleButtonClick(WID_SM_CENTERMAP);
01361       SndPlayFx(SND_15_BEEP);
01362       break;
01363 
01364     case WID_SM_TOGGLETOWNNAME: // Toggle town names
01365       this->show_towns = !this->show_towns;
01366       this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
01367 
01368       this->SetDirty();
01369       SndPlayFx(SND_15_BEEP);
01370       break;
01371 
01372     case WID_SM_LEGEND: // Legend
01373       if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
01374         /* If click on label, find right type and enable/disable it */
01375         const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND); // Label panel
01376         uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
01377         uint columns = this->GetNumberColumnsLegend(wi->current_x);
01378         uint number_of_rows = this->GetNumberRowsLegend(columns);
01379         if (line >= number_of_rows) break;
01380 
01381         bool rtl = _current_text_dir == TD_RTL;
01382         int x = pt.x - wi->pos_x;
01383         if (rtl) x = wi->current_x - x;
01384         uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
01385 
01386         /* Check if click is on label*/
01387         int click_pos = (column * number_of_rows) + line;
01388         if (this->map_type == SMT_INDUSTRY) {
01389           if (click_pos < _smallmap_industry_count) {
01390             this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
01391           }
01392         } else if (this->map_type == SMT_LINKSTATS) {
01393           if (click_pos < _smallmap_cargo_count) {
01394             this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
01395           }
01396         } else if (this->map_type == SMT_OWNER) {
01397           if (click_pos < _smallmap_company_count) {
01398             this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
01399           }
01400         }
01401         this->SetDirty();
01402       }
01403       break;
01404 
01405     case WID_SM_ENABLE_ALL:
01406       /* FALL THROUGH */
01407     case WID_SM_DISABLE_ALL: {
01408       LegendAndColour *tbl = NULL;
01409       switch (this->map_type) {
01410         case SMT_INDUSTRY:
01411           tbl = _legend_from_industries;
01412           break;
01413         case SMT_OWNER:
01414           tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
01415           break;
01416         case SMT_LINKSTATS:
01417           tbl = _legend_linkstats;
01418           break;
01419         default:
01420           NOT_REACHED();
01421       }
01422       for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
01423         tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
01424       }
01425       this->SetOverlayCargoMask();
01426       this->SetDirty();
01427       break;
01428     }
01429 
01430     case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
01431       _smallmap_show_heightmap = !_smallmap_show_heightmap;
01432       this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
01433       this->SetDirty();
01434       break;
01435   }
01436 }
01437 
01445 void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
01446 {
01447   if (!gui_scope) return;
01448   switch (data) {
01449     case 1:
01450       /* The owner legend has already been rebuilt. */
01451       this->ReInit();
01452       break;
01453 
01454     case 0: {
01455       extern uint64 _displayed_industries;
01456       if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
01457 
01458       for (int i = 0; i != _smallmap_industry_count; i++) {
01459         _legend_from_industries[i].show_on_map = HasBit(_displayed_industries, _legend_from_industries[i].type);
01460       }
01461       break;
01462     }
01463 
01464     default: NOT_REACHED();
01465   }
01466   this->SetDirty();
01467 }
01468 
01469 bool SmallMapWindow::OnRightClick(Point pt, int widget)
01470 {
01471   if (widget != WID_SM_MAP || _scrolling_viewport) return false;
01472 
01473   _scrolling_viewport = true;
01474   return true;
01475 }
01476 
01477 void SmallMapWindow::OnMouseWheel(int wheel)
01478 {
01479   if (_settings_client.gui.scrollwheel_scrolling == 0) {
01480     const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01481     int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
01482     int cursor_y = _cursor.pos.y - this->top  - wid->pos_y;
01483     if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
01484       Point pt = {cursor_x, cursor_y};
01485       this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
01486     }
01487   }
01488 }
01489 
01490 void SmallMapWindow::OnTick()
01491 {
01492   /* Update the window every now and then */
01493   if (--this->refresh != 0) return;
01494 
01495   if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
01496   this->refresh = FORCE_REFRESH_PERIOD;
01497   this->SetDirty();
01498 }
01499 
01507 void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
01508 {
01509   const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01510   Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
01511   hv.x *= this->zoom;
01512   hv.y *= this->zoom;
01513 
01514   if (sx < -hv.x) {
01515     sx = -hv.x;
01516     sub = 0;
01517   }
01518   if (sx > (int)(MapMaxX() * TILE_SIZE) - hv.x) {
01519     sx = MapMaxX() * TILE_SIZE - hv.x;
01520     sub = 0;
01521   }
01522   if (sy < -hv.y) {
01523     sy = -hv.y;
01524     sub = 0;
01525   }
01526   if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
01527     sy = MapMaxY() * TILE_SIZE - hv.y;
01528     sub = 0;
01529   }
01530 
01531   this->scroll_x = sx;
01532   this->scroll_y = sy;
01533   this->subscroll = sub;
01534   if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
01535 }
01536 
01537 void SmallMapWindow::OnScroll(Point delta)
01538 {
01539   _cursor.fix_at = true;
01540 
01541   /* While tile is at (delta.x, delta.y)? */
01542   int sub;
01543   Point pt = this->PixelToTile(delta.x, delta.y, &sub);
01544   this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
01545 
01546   this->SetDirty();
01547 }
01548 
01549 void SmallMapWindow::SmallMapCenterOnCurrentPos()
01550 {
01551   const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
01552   Point pt = InverseRemapCoords(vp->virtual_left + vp->virtual_width  / 2, vp->virtual_top  + vp->virtual_height / 2);
01553 
01554   int sub;
01555   const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01556   Point sxy = this->ComputeScroll(pt.x / TILE_SIZE, pt.y / TILE_SIZE, max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
01557   this->SetNewScroll(sxy.x, sxy.y, sub);
01558   this->SetDirty();
01559 }
01560 
01566 Point SmallMapWindow::GetStationMiddle(const Station *st) const
01567 {
01568   int x = (st->rect.right + st->rect.left + 1) / 2;
01569   int y = (st->rect.bottom + st->rect.top + 1) / 2;
01570   Point ret = this->RemapTile(x, y);
01571 
01572   /* Same magic 3 as in DrawVehicles; that's where I got it from.
01573    * No idea what it is, but without it the result looks bad.
01574    */
01575   ret.x -= 3 + this->subscroll;
01576   return ret;
01577 }
01578 
01579 SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
01580 bool SmallMapWindow::show_towns = true;
01581 
01590 class NWidgetSmallmapDisplay : public NWidgetContainer {
01591   const SmallMapWindow *smallmap_window; 
01592 public:
01593   NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL)
01594   {
01595     this->smallmap_window = NULL;
01596   }
01597 
01598   virtual void SetupSmallestSize(Window *w, bool init_array)
01599   {
01600     NWidgetBase *display = this->head;
01601     NWidgetBase *bar = display->next;
01602 
01603     display->SetupSmallestSize(w, init_array);
01604     bar->SetupSmallestSize(w, init_array);
01605 
01606     this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
01607     this->smallest_x = max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
01608     this->smallest_y = display->smallest_y + max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
01609     this->fill_x = max(display->fill_x, bar->fill_x);
01610     this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : min(display->fill_y, bar->fill_y);
01611     this->resize_x = max(display->resize_x, bar->resize_x);
01612     this->resize_y = min(display->resize_y, bar->resize_y);
01613   }
01614 
01615   virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01616   {
01617     this->pos_x = x;
01618     this->pos_y = y;
01619     this->current_x = given_width;
01620     this->current_y = given_height;
01621 
01622     NWidgetBase *display = this->head;
01623     NWidgetBase *bar = display->next;
01624 
01625     if (sizing == ST_SMALLEST) {
01626       this->smallest_x = given_width;
01627       this->smallest_y = given_height;
01628       /* Make display and bar exactly equal to their minimal size. */
01629       display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl);
01630       bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
01631     }
01632 
01633     uint bar_height = max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
01634     uint display_height = given_height - bar_height;
01635     display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl);
01636     bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl);
01637   }
01638 
01639   virtual NWidgetCore *GetWidgetFromPos(int x, int y)
01640   {
01641     if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
01642     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01643       NWidgetCore *widget = child_wid->GetWidgetFromPos(x, y);
01644       if (widget != NULL) return widget;
01645     }
01646     return NULL;
01647   }
01648 
01649   virtual void Draw(const Window *w)
01650   {
01651     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) child_wid->Draw(w);
01652   }
01653 };
01654 
01656 static const NWidgetPart _nested_smallmap_display[] = {
01657   NWidget(WWT_PANEL, COLOUR_BROWN, WID_SM_MAP_BORDER),
01658     NWidget(WWT_INSET, COLOUR_BROWN, WID_SM_MAP), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
01659   EndContainer(),
01660 };
01661 
01663 static const NWidgetPart _nested_smallmap_bar[] = {
01664   NWidget(WWT_PANEL, COLOUR_BROWN),
01665     NWidget(NWID_HORIZONTAL),
01666       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SM_LEGEND), SetResize(1, 1),
01667       NWidget(NWID_VERTICAL),
01668         /* Top button row. */
01669         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01670           NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_IN),
01671               SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN), SetFill(1, 1),
01672           NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_CENTERMAP),
01673               SetDataTip(SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER), SetFill(1, 1),
01674           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_BLANK),
01675               SetDataTip(SPR_DOT_SMALL, STR_NULL), SetFill(1, 1),
01676           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_CONTOUR),
01677               SetDataTip(SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP), SetFill(1, 1),
01678           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEHICLES),
01679               SetDataTip(SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP), SetFill(1, 1),
01680           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_INDUSTRIES),
01681               SetDataTip(SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP), SetFill(1, 1),
01682         EndContainer(),
01683         /* Bottom button row. */
01684         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01685           NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_OUT),
01686               SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT), SetFill(1, 1),
01687           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_TOGGLETOWNNAME),
01688               SetDataTip(SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF), SetFill(1, 1),
01689           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_LINKSTATS),
01690               SetDataTip(SPR_IMG_GRAPHS, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP), SetFill(1, 1),
01691           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_ROUTES),
01692               SetDataTip(SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON), SetFill(1, 1),
01693           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEGETATION),
01694               SetDataTip(SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP), SetFill(1, 1),
01695           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_OWNERS),
01696               SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP), SetFill(1, 1),
01697         EndContainer(),
01698         NWidget(NWID_SPACER), SetResize(0, 1),
01699       EndContainer(),
01700     EndContainer(),
01701   EndContainer(),
01702 };
01703 
01704 static NWidgetBase *SmallMapDisplay(int *biggest_index)
01705 {
01706   NWidgetContainer *map_display = new NWidgetSmallmapDisplay;
01707 
01708   MakeNWidgets(_nested_smallmap_display, lengthof(_nested_smallmap_display), biggest_index, map_display);
01709   MakeNWidgets(_nested_smallmap_bar, lengthof(_nested_smallmap_bar), biggest_index, map_display);
01710   return map_display;
01711 }
01712 
01713 
01714 static const NWidgetPart _nested_smallmap_widgets[] = {
01715   NWidget(NWID_HORIZONTAL),
01716     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01717     NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SM_CAPTION), SetDataTip(STR_SMALLMAP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01718     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01719     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01720   EndContainer(),
01721   NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
01722   /* Bottom button row and resize box. */
01723   NWidget(NWID_HORIZONTAL),
01724     NWidget(WWT_PANEL, COLOUR_BROWN),
01725       NWidget(NWID_HORIZONTAL),
01726         NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SELECT_BUTTONS),
01727           NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01728             NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
01729             NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
01730             NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
01731           EndContainer(),
01732           NWidget(NWID_SPACER), SetFill(1, 1),
01733         EndContainer(),
01734         NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01735       EndContainer(),
01736     EndContainer(),
01737     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01738   EndContainer(),
01739 };
01740 
01741 static const WindowDesc _smallmap_desc(
01742   WDP_AUTO, 484, 314,
01743   WC_SMALLMAP, WC_NONE,
01744   WDF_UNCLICK_BUTTONS,
01745   _nested_smallmap_widgets, lengthof(_nested_smallmap_widgets)
01746 );
01747 
01751 void ShowSmallMap()
01752 {
01753   AllocateWindowDescFront<SmallMapWindow>(&_smallmap_desc, 0);
01754 }
01755 
01764 bool ScrollMainWindowTo(int x, int y, int z, bool instant)
01765 {
01766   bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
01767 
01768   /* If a user scrolls to a tile (via what way what so ever) and already is on
01769    * that tile (e.g.: pressed twice), move the smallmap to that location,
01770    * so you directly see where you are on the smallmap. */
01771 
01772   if (res) return res;
01773 
01774   SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WC_SMALLMAP, 0));
01775   if (w != NULL) w->SmallMapCenterOnCurrentPos();
01776 
01777   return res;
01778 }