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 IndustryType _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
00154 static bool _smallmap_industry_highlight_state;
00156 static uint _company_to_list_pos[MAX_COMPANIES];
00157 
00161 void BuildIndustriesLegend()
00162 {
00163   uint j = 0;
00164 
00165   /* Add each name */
00166   for (uint8 i = 0; i < NUM_INDUSTRYTYPES; i++) {
00167     IndustryType ind = _sorted_industry_types[i];
00168     const IndustrySpec *indsp = GetIndustrySpec(ind);
00169     if (indsp->enabled) {
00170       _legend_from_industries[j].legend = indsp->name;
00171       _legend_from_industries[j].colour = indsp->map_colour;
00172       _legend_from_industries[j].type = ind;
00173       _legend_from_industries[j].show_on_map = true;
00174       _legend_from_industries[j].col_break = false;
00175       _legend_from_industries[j].end = false;
00176 
00177       /* Store widget number for this industry type. */
00178       _industry_to_list_pos[ind] = j;
00179       j++;
00180     }
00181   }
00182   /* Terminate the list */
00183   _legend_from_industries[j].end = true;
00184 
00185   /* Store number of enabled industries */
00186   _smallmap_industry_count = j;
00187 }
00188 
00192 void BuildLinkStatsLegend()
00193 {
00194   /* Clear the legend */
00195   memset(_legend_linkstats, 0, sizeof(_legend_linkstats));
00196 
00197   uint i = 0;
00198   for (; i < _sorted_cargo_specs_size; ++i) {
00199     const CargoSpec *cs = _sorted_cargo_specs[i];
00200 
00201     _legend_linkstats[i].legend = cs->name;
00202     _legend_linkstats[i].colour = cs->legend_colour;
00203     _legend_linkstats[i].type = cs->Index();
00204     _legend_linkstats[i].show_on_map = true;
00205   }
00206 
00207   _legend_linkstats[i].col_break = true;
00208   _smallmap_cargo_count = i;
00209 
00210   for (; i < _smallmap_cargo_count + lengthof(_linkstat_colours_in_legenda); ++i) {
00211     _legend_linkstats[i].legend = STR_EMPTY;
00212     _legend_linkstats[i].colour = LinkGraphOverlay::LINK_COLOURS[_linkstat_colours_in_legenda[i - _smallmap_cargo_count]];
00213     _legend_linkstats[i].show_on_map = true;
00214   }
00215 
00216   _legend_linkstats[_smallmap_cargo_count].legend = STR_LINKGRAPH_LEGEND_UNUSED;
00217   _legend_linkstats[i - 1].legend = STR_LINKGRAPH_LEGEND_OVERLOADED;
00218   _legend_linkstats[(_smallmap_cargo_count + i - 1) / 2].legend = STR_LINKGRAPH_LEGEND_SATURATED;
00219   _legend_linkstats[i].end = true;
00220 }
00221 
00222 static const LegendAndColour * const _legend_table[] = {
00223   _legend_land_contours,
00224   _legend_vehicles,
00225   _legend_from_industries,
00226   _legend_linkstats,
00227   _legend_routes,
00228   _legend_vegetation,
00229   _legend_land_owners,
00230 };
00231 
00232 #define MKCOLOUR(x)         TO_LE32X(x)
00233 
00234 #define MKCOLOUR_XXXX(x)    (MKCOLOUR(0x01010101) * (uint)(x))
00235 #define MKCOLOUR_X0X0(x)    (MKCOLOUR(0x01000100) * (uint)(x))
00236 #define MKCOLOUR_0X0X(x)    (MKCOLOUR(0x00010001) * (uint)(x))
00237 #define MKCOLOUR_0XX0(x)    (MKCOLOUR(0x00010100) * (uint)(x))
00238 #define MKCOLOUR_X00X(x)    (MKCOLOUR(0x01000001) * (uint)(x))
00239 
00240 #define MKCOLOUR_XYXY(x, y) (MKCOLOUR_X0X0(x) | MKCOLOUR_0X0X(y))
00241 #define MKCOLOUR_XYYX(x, y) (MKCOLOUR_X00X(x) | MKCOLOUR_0XX0(y))
00242 
00243 #define MKCOLOUR_0000       MKCOLOUR_XXXX(0x00)
00244 #define MKCOLOUR_0FF0       MKCOLOUR_0XX0(0xFF)
00245 #define MKCOLOUR_F00F       MKCOLOUR_X00X(0xFF)
00246 #define MKCOLOUR_FFFF       MKCOLOUR_XXXX(0xFF)
00247 
00249 static const uint32 _green_map_heights[] = {
00250   MKCOLOUR_XXXX(0x5A),
00251   MKCOLOUR_XYXY(0x5A, 0x5B),
00252   MKCOLOUR_XXXX(0x5B),
00253   MKCOLOUR_XYXY(0x5B, 0x5C),
00254   MKCOLOUR_XXXX(0x5C),
00255   MKCOLOUR_XYXY(0x5C, 0x5D),
00256   MKCOLOUR_XXXX(0x5D),
00257   MKCOLOUR_XYXY(0x5D, 0x5E),
00258   MKCOLOUR_XXXX(0x5E),
00259   MKCOLOUR_XYXY(0x5E, 0x5F),
00260   MKCOLOUR_XXXX(0x5F),
00261   MKCOLOUR_XYXY(0x5F, 0x1F),
00262   MKCOLOUR_XXXX(0x1F),
00263   MKCOLOUR_XYXY(0x1F, 0x27),
00264   MKCOLOUR_XXXX(0x27),
00265   MKCOLOUR_XXXX(0x27),
00266 };
00267 assert_compile(lengthof(_green_map_heights) == MAX_TILE_HEIGHT + 1);
00268 
00270 static const uint32 _dark_green_map_heights[] = {
00271   MKCOLOUR_XXXX(0x60),
00272   MKCOLOUR_XYXY(0x60, 0x61),
00273   MKCOLOUR_XXXX(0x61),
00274   MKCOLOUR_XYXY(0x61, 0x62),
00275   MKCOLOUR_XXXX(0x62),
00276   MKCOLOUR_XYXY(0x62, 0x63),
00277   MKCOLOUR_XXXX(0x63),
00278   MKCOLOUR_XYXY(0x63, 0x64),
00279   MKCOLOUR_XXXX(0x64),
00280   MKCOLOUR_XYXY(0x64, 0x65),
00281   MKCOLOUR_XXXX(0x65),
00282   MKCOLOUR_XYXY(0x65, 0x66),
00283   MKCOLOUR_XXXX(0x66),
00284   MKCOLOUR_XYXY(0x66, 0x67),
00285   MKCOLOUR_XXXX(0x67),
00286   MKCOLOUR_XXXX(0x67),
00287 };
00288 assert_compile(lengthof(_dark_green_map_heights) == MAX_TILE_HEIGHT + 1);
00289 
00291 static const uint32 _violet_map_heights[] = {
00292   MKCOLOUR_XXXX(0x80),
00293   MKCOLOUR_XYXY(0x80, 0x81),
00294   MKCOLOUR_XXXX(0x81),
00295   MKCOLOUR_XYXY(0x81, 0x82),
00296   MKCOLOUR_XXXX(0x82),
00297   MKCOLOUR_XYXY(0x82, 0x83),
00298   MKCOLOUR_XXXX(0x83),
00299   MKCOLOUR_XYXY(0x83, 0x84),
00300   MKCOLOUR_XXXX(0x84),
00301   MKCOLOUR_XYXY(0x84, 0x85),
00302   MKCOLOUR_XXXX(0x85),
00303   MKCOLOUR_XYXY(0x85, 0x86),
00304   MKCOLOUR_XXXX(0x86),
00305   MKCOLOUR_XYXY(0x86, 0x87),
00306   MKCOLOUR_XXXX(0x87),
00307   MKCOLOUR_XXXX(0x87),
00308 };
00309 assert_compile(lengthof(_violet_map_heights) == MAX_TILE_HEIGHT + 1);
00310 
00312 struct SmallMapColourScheme {
00313   const uint32 *height_colours; 
00314   uint32 default_colour;   
00315 };
00316 
00318 static const SmallMapColourScheme _heightmap_schemes[] = {
00319   {_green_map_heights,      MKCOLOUR_XXXX(0x54)}, 
00320   {_dark_green_map_heights, MKCOLOUR_XXXX(0x62)}, 
00321   {_violet_map_heights,     MKCOLOUR_XXXX(0x82)}, 
00322 };
00323 
00327 void BuildLandLegend()
00328 {
00329   for (LegendAndColour *lc = _legend_land_contours; lc->legend == STR_TINY_BLACK_HEIGHT; lc++) {
00330     lc->colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[lc->height];
00331   }
00332 }
00333 
00337 void BuildOwnerLegend()
00338 {
00339   _legend_land_owners[1].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].default_colour;
00340 
00341   int i = NUM_NO_COMPANY_ENTRIES;
00342   const Company *c;
00343   FOR_ALL_COMPANIES(c) {
00344     _legend_land_owners[i].colour = _colour_gradient[c->colour][5];
00345     _legend_land_owners[i].company = c->index;
00346     _legend_land_owners[i].show_on_map = true;
00347     _legend_land_owners[i].col_break = false;
00348     _legend_land_owners[i].end = false;
00349     _company_to_list_pos[c->index] = i;
00350     i++;
00351   }
00352 
00353   /* Terminate the list */
00354   _legend_land_owners[i].end = true;
00355 
00356   /* Store maximum amount of owner legend entries. */
00357   _smallmap_company_count = i;
00358 }
00359 
00360 struct AndOr {
00361   uint32 mor;
00362   uint32 mand;
00363 };
00364 
00365 static inline uint32 ApplyMask(uint32 colour, const AndOr *mask)
00366 {
00367   return (colour & mask->mand) | mask->mor;
00368 }
00369 
00370 
00372 static const AndOr _smallmap_contours_andor[] = {
00373   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_CLEAR
00374   {MKCOLOUR_0XX0(PC_GREY      ), MKCOLOUR_F00F}, // MP_RAILWAY
00375   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F}, // MP_ROAD
00376   {MKCOLOUR_0XX0(PC_DARK_RED  ), MKCOLOUR_F00F}, // MP_HOUSE
00377   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_TREES
00378   {MKCOLOUR_XXXX(PC_LIGHT_BLUE), MKCOLOUR_0000}, // MP_STATION
00379   {MKCOLOUR_XXXX(PC_WATER     ), MKCOLOUR_0000}, // MP_WATER
00380   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_VOID
00381   {MKCOLOUR_XXXX(PC_DARK_RED  ), MKCOLOUR_0000}, // MP_INDUSTRY
00382   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
00383   {MKCOLOUR_0XX0(PC_DARK_RED  ), MKCOLOUR_F00F}, // MP_OBJECT
00384   {MKCOLOUR_0XX0(PC_GREY      ), MKCOLOUR_F00F},
00385 };
00386 
00388 static const AndOr _smallmap_vehicles_andor[] = {
00389   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_CLEAR
00390   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F}, // MP_RAILWAY
00391   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F}, // MP_ROAD
00392   {MKCOLOUR_0XX0(PC_DARK_RED  ), MKCOLOUR_F00F}, // MP_HOUSE
00393   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_TREES
00394   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F}, // MP_STATION
00395   {MKCOLOUR_XXXX(PC_WATER     ), MKCOLOUR_0000}, // MP_WATER
00396   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_VOID
00397   {MKCOLOUR_XXXX(PC_DARK_RED  ), MKCOLOUR_0000}, // MP_INDUSTRY
00398   {MKCOLOUR_0000               , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
00399   {MKCOLOUR_0XX0(PC_DARK_RED  ), MKCOLOUR_F00F}, // MP_OBJECT
00400   {MKCOLOUR_0XX0(PC_BLACK     ), MKCOLOUR_F00F},
00401 };
00402 
00404 static const byte _tiletype_importance[] = {
00405   2, // MP_CLEAR
00406   8, // MP_RAILWAY
00407   7, // MP_ROAD
00408   5, // MP_HOUSE
00409   2, // MP_TREES
00410   9, // MP_STATION
00411   2, // MP_WATER
00412   1, // MP_VOID
00413   6, // MP_INDUSTRY
00414   8, // MP_TUNNELBRIDGE
00415   2, // MP_OBJECT
00416   0,
00417 };
00418 
00419 
00420 static inline TileType GetEffectiveTileType(TileIndex tile)
00421 {
00422   TileType t = GetTileType(tile);
00423 
00424   if (t == MP_TUNNELBRIDGE) {
00425     TransportType tt = GetTunnelBridgeTransportType(tile);
00426 
00427     switch (tt) {
00428       case TRANSPORT_RAIL: t = MP_RAILWAY; break;
00429       case TRANSPORT_ROAD: t = MP_ROAD;    break;
00430       default:             t = MP_WATER;   break;
00431     }
00432   }
00433   return t;
00434 }
00435 
00442 static inline uint32 GetSmallMapContoursPixels(TileIndex tile, TileType t)
00443 {
00444   const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00445   return ApplyMask(cs->height_colours[TileHeight(tile)], &_smallmap_contours_andor[t]);
00446 }
00447 
00455 static inline uint32 GetSmallMapVehiclesPixels(TileIndex tile, TileType t)
00456 {
00457   const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00458   return ApplyMask(cs->default_colour, &_smallmap_vehicles_andor[t]);
00459 }
00460 
00468 static inline uint32 GetSmallMapIndustriesPixels(TileIndex tile, TileType t)
00469 {
00470   if (t == MP_INDUSTRY) {
00471     /* If industry is allowed to be seen, use its colour on the map */
00472     IndustryType type = Industry::GetByTile(tile)->type;
00473     if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map &&
00474         (_smallmap_industry_highlight_state || type != _smallmap_industry_highlight)) {
00475       return (type == _smallmap_industry_highlight ? PC_WHITE : GetIndustrySpec(Industry::GetByTile(tile)->type)->map_colour) * 0x01010101;
00476     } else {
00477       /* Otherwise, return the colour which will make it disappear */
00478       t = (IsTileOnWater(tile) ? MP_WATER : MP_CLEAR);
00479     }
00480   }
00481 
00482   const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00483   return ApplyMask(_smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour, &_smallmap_vehicles_andor[t]);
00484 }
00485 
00493 static inline uint32 GetSmallMapRoutesPixels(TileIndex tile, TileType t)
00494 {
00495   if (t == MP_STATION) {
00496     switch (GetStationType(tile)) {
00497       case STATION_RAIL:    return MKCOLOUR_XXXX(PC_VERY_DARK_BROWN);
00498       case STATION_AIRPORT: return MKCOLOUR_XXXX(PC_RED);
00499       case STATION_TRUCK:   return MKCOLOUR_XXXX(PC_ORANGE);
00500       case STATION_BUS:     return MKCOLOUR_XXXX(PC_YELLOW);
00501       case STATION_DOCK:    return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
00502       default:              return MKCOLOUR_FFFF;
00503     }
00504   } else if (t == MP_RAILWAY) {
00505     AndOr andor = {
00506       MKCOLOUR_0XX0(GetRailTypeInfo(GetRailType(tile))->map_colour),
00507       _smallmap_contours_andor[t].mand
00508     };
00509 
00510     const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00511     return ApplyMask(cs->default_colour, &andor);
00512   }
00513 
00514   /* Ground colour */
00515   const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00516   return ApplyMask(cs->default_colour, &_smallmap_contours_andor[t]);
00517 }
00518 
00526 static inline uint32 GetSmallMapLinkStatsPixels(TileIndex tile, TileType t)
00527 {
00528   return _smallmap_show_heightmap ? GetSmallMapContoursPixels(tile, t) : GetSmallMapRoutesPixels(tile, t);
00529 }
00530 
00531 static const uint32 _vegetation_clear_bits[] = {
00532   MKCOLOUR_XXXX(PC_GRASS_LAND), 
00533   MKCOLOUR_XXXX(PC_ROUGH_LAND), 
00534   MKCOLOUR_XXXX(PC_GREY),       
00535   MKCOLOUR_XXXX(PC_FIELDS),     
00536   MKCOLOUR_XXXX(PC_LIGHT_BLUE), 
00537   MKCOLOUR_XXXX(PC_ORANGE),     
00538   MKCOLOUR_XXXX(PC_GRASS_LAND), 
00539   MKCOLOUR_XXXX(PC_GRASS_LAND), 
00540 };
00541 
00549 static inline uint32 GetSmallMapVegetationPixels(TileIndex tile, TileType t)
00550 {
00551   switch (t) {
00552     case MP_CLEAR:
00553       return (IsClearGround(tile, CLEAR_GRASS) && GetClearDensity(tile) < 3) ? MKCOLOUR_XXXX(PC_BARE_LAND) : _vegetation_clear_bits[GetClearGround(tile)];
00554 
00555     case MP_INDUSTRY:
00556       return IsTileForestIndustry(tile) ? MKCOLOUR_XXXX(PC_GREEN) : MKCOLOUR_XXXX(PC_DARK_RED);
00557 
00558     case MP_TREES:
00559       if (GetTreeGround(tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(tile) == TREE_GROUND_ROUGH_SNOW) {
00560         return (_settings_game.game_creation.landscape == LT_ARCTIC) ? MKCOLOUR_XYYX(PC_LIGHT_BLUE, PC_TREES) : MKCOLOUR_XYYX(PC_ORANGE, PC_TREES);
00561       }
00562       return MKCOLOUR_XYYX(PC_GRASS_LAND, PC_TREES);
00563 
00564     default:
00565       return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND), &_smallmap_vehicles_andor[t]);
00566   }
00567 }
00568 
00576 static inline uint32 GetSmallMapOwnerPixels(TileIndex tile, TileType t)
00577 {
00578   Owner o;
00579 
00580   switch (t) {
00581     case MP_INDUSTRY: return MKCOLOUR_XXXX(PC_DARK_GREY);
00582     case MP_HOUSE:    return MKCOLOUR_XXXX(PC_DARK_RED);
00583     default:          o = GetTileOwner(tile); break;
00584     /* FIXME: For MP_ROAD there are multiple owners.
00585      * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
00586      * even if there are no ROADTYPE_ROAD bits on the tile.
00587      */
00588   }
00589 
00590   if ((o < MAX_COMPANIES && !_legend_land_owners[_company_to_list_pos[o]].show_on_map) || o == OWNER_NONE || o == OWNER_WATER) {
00591     if (t == MP_WATER) return MKCOLOUR_XXXX(PC_WATER);
00592     const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
00593     return _smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour;
00594   } else if (o == OWNER_TOWN) {
00595     return MKCOLOUR_XXXX(PC_DARK_RED);
00596   }
00597 
00598   return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
00599 }
00600 
00602 static const byte _vehicle_type_colours[6] = {
00603   PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
00604 };
00605 
00606 inline Point SmallMapWindow::SmallmapRemapCoords(int x, int y) const
00607 {
00608   Point pt;
00609   pt.x = (y - x) * 2;
00610   pt.y = y + x;
00611   return pt;
00612 }
00613 
00620 inline Point SmallMapWindow::RemapTile(int tile_x, int tile_y) const
00621 {
00622   int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
00623   int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
00624 
00625   if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
00626 
00627   /* For negative offsets, round towards -inf. */
00628   if (x_offset < 0) x_offset -= this->zoom - 1;
00629   if (y_offset < 0) y_offset -= this->zoom - 1;
00630 
00631   return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
00632 }
00633 
00644 inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) const
00645 {
00646   if (add_sub) px += this->subscroll;  // Total horizontal offset.
00647 
00648   /* For each two rows down, add a x and a y tile, and
00649    * For each four pixels to the right, move a tile to the right. */
00650   Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
00651   px &= 3;
00652 
00653   if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
00654     if (px < 2) {
00655       pt.x += this->zoom;
00656       px += 2;
00657     } else {
00658       pt.y += this->zoom;
00659       px -= 2;
00660     }
00661   }
00662 
00663   *sub = px;
00664   return pt;
00665 }
00666 
00676 Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
00677 {
00678   assert(x >= 0 && y >= 0);
00679 
00680   int new_sub;
00681   Point tile_xy = PixelToTile(x, y, &new_sub, false);
00682   tx -= tile_xy.x;
00683   ty -= tile_xy.y;
00684 
00685   Point scroll;
00686   if (new_sub == 0) {
00687     *sub = 0;
00688     scroll.x = (tx + this->zoom) * TILE_SIZE;
00689     scroll.y = (ty - this->zoom) * TILE_SIZE;
00690   } else {
00691     *sub = 4 - new_sub;
00692     scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
00693     scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
00694   }
00695   return scroll;
00696 }
00697 
00704 void SmallMapWindow::SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
00705 {
00706   static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
00707   static const int MIN_ZOOM_INDEX = 0;
00708   static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
00709 
00710   int new_index, cur_index, sub;
00711   Point tile;
00712   switch (change) {
00713     case ZLC_INITIALIZE:
00714       cur_index = - 1; // Definitely different from new_index.
00715       new_index = MIN_ZOOM_INDEX;
00716       break;
00717 
00718     case ZLC_ZOOM_IN:
00719     case ZLC_ZOOM_OUT:
00720       for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
00721         if (this->zoom == zoomlevels[cur_index]) break;
00722       }
00723       assert(cur_index <= MAX_ZOOM_INDEX);
00724 
00725       tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
00726       new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
00727       break;
00728 
00729     default: NOT_REACHED();
00730   }
00731 
00732   if (new_index != cur_index) {
00733     this->zoom = zoomlevels[new_index];
00734     if (cur_index >= 0) {
00735       Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
00736       this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
00737           this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
00738     } else if (this->map_type == SMT_LINKSTATS) {
00739       this->overlay->RebuildCache();
00740     }
00741     this->SetWidgetDisabledState(WID_SM_ZOOM_IN,  this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
00742     this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
00743     this->SetDirty();
00744   }
00745 }
00746 
00752 inline uint32 SmallMapWindow::GetTileColours(const TileArea &ta) const
00753 {
00754   int importance = 0;
00755   TileIndex tile = INVALID_TILE; // Position of the most important tile.
00756   TileType et = MP_VOID;         // Effective tile type at that position.
00757 
00758   TILE_AREA_LOOP(ti, ta) {
00759     TileType ttype = GetEffectiveTileType(ti);
00760     if (_tiletype_importance[ttype] > importance) {
00761       importance = _tiletype_importance[ttype];
00762       tile = ti;
00763       et = ttype;
00764     }
00765   }
00766 
00767   switch (this->map_type) {
00768     case SMT_CONTOUR:
00769       return GetSmallMapContoursPixels(tile, et);
00770 
00771     case SMT_VEHICLES:
00772       return GetSmallMapVehiclesPixels(tile, et);
00773 
00774     case SMT_INDUSTRY:
00775       return GetSmallMapIndustriesPixels(tile, et);
00776 
00777     case SMT_LINKSTATS:
00778       return GetSmallMapLinkStatsPixels(tile, et);
00779 
00780     case SMT_ROUTES:
00781       return GetSmallMapRoutesPixels(tile, et);
00782 
00783     case SMT_VEGETATION:
00784       return GetSmallMapVegetationPixels(tile, et);
00785 
00786     case SMT_OWNER:
00787       return GetSmallMapOwnerPixels(tile, et);
00788 
00789     default: NOT_REACHED();
00790   }
00791 }
00792 
00806 void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
00807 {
00808   void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
00809   uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
00810 
00811   do {
00812     /* Check if the tile (xc,yc) is within the map range */
00813     if (xc >= MapMaxX() || yc >= MapMaxY()) continue;
00814 
00815     /* Check if the dst pointer points to a pixel inside the screen buffer */
00816     if (dst < _screen.dst_ptr) continue;
00817     if (dst >= dst_ptr_abs_end) continue;
00818 
00819     /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
00820     TileArea ta;
00821     if (min_xy == 1 && (xc == 0 || yc == 0)) {
00822       if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
00823 
00824       ta = TileArea(TileXY(max(min_xy, xc), max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
00825     } else {
00826       ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
00827     }
00828     ta.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
00829 
00830     uint32 val = this->GetTileColours(ta);
00831     uint8 *val8 = (uint8 *)&val;
00832     int idx = max(0, -start_pos);
00833     for (int pos = max(0, start_pos); pos < end_pos; pos++) {
00834       blitter->SetPixel(dst, idx, 0, val8[idx]);
00835       idx++;
00836     }
00837   /* Switch to next tile in the column */
00838   } while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
00839 }
00840 
00846 void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
00847 {
00848   const Vehicle *v;
00849   FOR_ALL_VEHICLES(v) {
00850     if (v->type == VEH_EFFECT) continue;
00851     if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
00852 
00853     /* Remap into flat coordinates. */
00854     Point pt = this->RemapTile(v->x_pos / TILE_SIZE, v->y_pos / TILE_SIZE);
00855 
00856     int y = pt.y - dpi->top;
00857     if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
00858 
00859     bool skip = false; // Default is to draw both pixels.
00860     int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
00861     if (x < 0) {
00862       /* if x+1 is 0, that means we're on the very left edge,
00863        * and should thus only draw a single pixel */
00864       if (++x != 0) continue;
00865       skip = true;
00866     } else if (x >= dpi->width - 1) {
00867       /* Check if we're at the very right edge, and if so draw only a single pixel */
00868       if (x != dpi->width - 1) continue;
00869       skip = true;
00870     }
00871 
00872     /* Calculate pointer to pixel and the colour */
00873     byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
00874 
00875     /* And draw either one or two pixels depending on clipping */
00876     blitter->SetPixel(dpi->dst_ptr, x, y, colour);
00877     if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
00878   }
00879 }
00880 
00885 void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
00886 {
00887   const Town *t;
00888   FOR_ALL_TOWNS(t) {
00889     /* Remap the town coordinate */
00890     Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
00891     int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
00892     int y = pt.y;
00893 
00894     /* Check if the town sign is within bounds */
00895     if (x + t->cache.sign.width_small > dpi->left &&
00896         x < dpi->left + dpi->width &&
00897         y + FONT_HEIGHT_SMALL > dpi->top &&
00898         y < dpi->top + dpi->height) {
00899       /* And draw it. */
00900       SetDParam(0, t->index);
00901       DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
00902     }
00903   }
00904 }
00905 
00909 void SmallMapWindow::DrawMapIndicators() const
00910 {
00911   /* Find main viewport. */
00912   const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
00913 
00914   Point tile = InverseRemapCoords(vp->virtual_left, vp->virtual_top);
00915   Point tl = this->RemapTile(tile.x >> 4, tile.y >> 4);
00916   tl.x -= this->subscroll;
00917 
00918   tile = InverseRemapCoords(vp->virtual_left + vp->virtual_width, vp->virtual_top + vp->virtual_height);
00919   Point br = this->RemapTile(tile.x >> 4, tile.y >> 4);
00920   br.x -= this->subscroll;
00921 
00922   SmallMapWindow::DrawVertMapIndicator(tl.x, tl.y, br.y);
00923   SmallMapWindow::DrawVertMapIndicator(br.x, tl.y, br.y);
00924 
00925   SmallMapWindow::DrawHorizMapIndicator(tl.x, br.x, tl.y);
00926   SmallMapWindow::DrawHorizMapIndicator(tl.x, br.x, br.y);
00927 }
00928 
00940 void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
00941 {
00942   Blitter *blitter = BlitterFactoryBase::GetCurrentBlitter();
00943   DrawPixelInfo *old_dpi;
00944 
00945   old_dpi = _cur_dpi;
00946   _cur_dpi = dpi;
00947 
00948   /* Clear it */
00949   GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
00950 
00951   /* Which tile is displayed at (dpi->left, dpi->top)? */
00952   int dx;
00953   Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
00954   int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
00955   int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
00956 
00957   void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
00958   int x = - dx - 4;
00959   int y = 0;
00960 
00961   for (;;) {
00962     /* Distance from left edge */
00963     if (x >= -3) {
00964       if (x >= dpi->width) break; // Exit the loop.
00965 
00966       int end_pos = min(dpi->width, x + 4);
00967       int reps = (dpi->height - y + 1) / 2; // Number of lines.
00968       if (reps > 0) {
00969         this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
00970       }
00971     }
00972 
00973     if (y == 0) {
00974       tile_y += this->zoom;
00975       y++;
00976       ptr = blitter->MoveTo(ptr, 0, 1);
00977     } else {
00978       tile_x -= this->zoom;
00979       y--;
00980       ptr = blitter->MoveTo(ptr, 0, -1);
00981     }
00982     ptr = blitter->MoveTo(ptr, 2, 0);
00983     x += 2;
00984   }
00985 
00986   /* Draw vehicles */
00987   if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
00988 
00989   /* Draw link stat overlay */
00990   if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
00991 
00992   /* Draw town names */
00993   if (this->show_towns) this->DrawTowns(dpi);
00994 
00995   /* Draw map indicators */
00996   this->DrawMapIndicators();
00997 
00998   _cur_dpi = old_dpi;
00999 }
01000 
01004 void SmallMapWindow::SetupWidgetData()
01005 {
01006   StringID legend_tooltip;
01007   StringID enable_all_tooltip;
01008   StringID disable_all_tooltip;
01009   int plane;
01010   switch (this->map_type) {
01011     case SMT_INDUSTRY:
01012       legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
01013       enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
01014       disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
01015       plane = 0;
01016       break;
01017 
01018     case SMT_OWNER:
01019       legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
01020       enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
01021       disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
01022       plane = 0;
01023       break;
01024 
01025     case SMT_LINKSTATS:
01026       legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
01027       enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
01028       disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
01029       plane = 0;
01030       break;
01031 
01032     default:
01033       legend_tooltip = STR_NULL;
01034       enable_all_tooltip = STR_NULL;
01035       disable_all_tooltip = STR_NULL;
01036       plane = 1;
01037       break;
01038   }
01039 
01040   this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
01041   this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
01042   this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
01043   this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
01044 }
01045 
01046 SmallMapWindow::SmallMapWindow(const WindowDesc *desc, int window_number) : Window(), refresh(FORCE_REFRESH_PERIOD)
01047 {
01048   _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
01049   this->overlay = new LinkGraphOverlay(this, WID_SM_MAP);
01050   this->InitNested(desc, window_number);
01051   this->LowerWidget(this->map_type + WID_SM_CONTOUR);
01052 
01053   BuildLandLegend();
01054   this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
01055 
01056   this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
01057 
01058   this->SetupWidgetData();
01059 
01060   this->SetZoomLevel(ZLC_INITIALIZE, NULL);
01061   this->SmallMapCenterOnCurrentPos();
01062 }
01063 
01064 void SmallMapWindow::SetStringParameters(int widget) const
01065 {
01066   switch (widget) {
01067     case WID_SM_CAPTION:
01068       SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
01069       break;
01070   }
01071 }
01072 
01073 void SmallMapWindow::OnInit()
01074 {
01075   uint min_width = 0;
01076   this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
01077   this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
01078   for (uint i = 0; i < lengthof(_legend_table); i++) {
01079     uint height = 0;
01080     uint num_columns = 1;
01081     for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
01082       StringID str;
01083       if (i == SMT_INDUSTRY) {
01084         SetDParam(0, tbl->legend);
01085         SetDParam(1, IndustryPool::MAX_SIZE);
01086         str = STR_SMALLMAP_INDUSTRY;
01087       } else if (i == SMT_LINKSTATS) {
01088         SetDParam(0, tbl->legend);
01089         str = STR_SMALLMAP_LINKSTATS;
01090       } else if (i == SMT_OWNER) {
01091         if (tbl->company != INVALID_COMPANY) {
01092           if (!Company::IsValidID(tbl->company)) {
01093             /* Rebuild the owner legend. */
01094             BuildOwnerLegend();
01095             this->OnInit();
01096             return;
01097           }
01098           /* Non-fixed legend entries for the owner view. */
01099           SetDParam(0, tbl->company);
01100           str = STR_SMALLMAP_COMPANY;
01101         } else {
01102           str = tbl->legend;
01103         }
01104       } else {
01105         if (tbl->col_break) {
01106           this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
01107           height = 0;
01108           num_columns++;
01109         }
01110         height++;
01111         str = tbl->legend;
01112       }
01113       min_width = max(GetStringBoundingBox(str).width, min_width);
01114     }
01115     this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
01116     this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
01117   }
01118 
01119   /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
01120   this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
01121 }
01122 
01123 void SmallMapWindow::OnPaint()
01124 {
01125   if (this->map_type == SMT_OWNER) {
01126     for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
01127       if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
01128         /* Rebuild the owner legend. */
01129         BuildOwnerLegend();
01130         this->InvalidateData(1);
01131         break;
01132       }
01133     }
01134   }
01135 
01136   this->DrawWidgets();
01137 }
01138 
01139 void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
01140 {
01141   switch (widget) {
01142     case WID_SM_MAP: {
01143       DrawPixelInfo new_dpi;
01144       if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
01145       this->DrawSmallMap(&new_dpi);
01146       break;
01147     }
01148 
01149     case WID_SM_LEGEND: {
01150       uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
01151       uint number_of_rows = this->GetNumberRowsLegend(columns);
01152       bool rtl = _current_text_dir == TD_RTL;
01153       uint y_org = r.top + WD_FRAMERECT_TOP;
01154       uint x = rtl ? r.right - this->column_width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT;
01155       uint y = y_org;
01156       uint i = 0; // Row counter for industry legend.
01157       uint row_height = FONT_HEIGHT_SMALL;
01158 
01159       uint text_left  = rtl ? 0 : LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT;
01160       uint text_right = this->column_width - 1 - (rtl ? LEGEND_BLOB_WIDTH + WD_FRAMERECT_RIGHT : 0);
01161       uint blob_left  = rtl ? this->column_width - 1 - LEGEND_BLOB_WIDTH : 0;
01162       uint blob_right = rtl ? this->column_width - 1 : LEGEND_BLOB_WIDTH;
01163 
01164       StringID string = STR_NULL;
01165       switch (this->map_type) {
01166         case SMT_INDUSTRY:
01167           string = STR_SMALLMAP_INDUSTRY;
01168           break;
01169         case SMT_LINKSTATS:
01170           string = STR_SMALLMAP_LINKSTATS;
01171           break;
01172         case SMT_OWNER:
01173           string = STR_SMALLMAP_COMPANY;
01174           break;
01175         default:
01176           break;
01177       }
01178 
01179       for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
01180         if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
01181           /* Column break needed, continue at top, COLUMN_WIDTH pixels
01182            * (one "row") to the right. */
01183           x += rtl ? -(int)this->column_width : this->column_width;
01184           y = y_org;
01185           i = 1;
01186         }
01187 
01188         uint8 legend_colour = tbl->colour;
01189 
01190         switch (this->map_type) {
01191           case SMT_INDUSTRY:
01192             /* Industry name must be formatted, since it's not in tiny font in the specs.
01193              * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
01194             SetDParam(0, tbl->legend);
01195             SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
01196             if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
01197               legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
01198             }
01199             /* FALL THROUGH */
01200           case SMT_LINKSTATS:
01201             SetDParam(0, tbl->legend);
01202             /* FALL_THROUGH */
01203           case SMT_OWNER:
01204             if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
01205               if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
01206               if (!tbl->show_on_map) {
01207                 /* Simply draw the string, not the black border of the legend colour.
01208                  * This will enforce the idea of the disabled item */
01209                 DrawString(x + text_left, x + text_right, y, string, TC_GREY);
01210               } else {
01211                 DrawString(x + text_left, x + text_right, y, string, TC_BLACK);
01212                 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK); // Outer border of the legend colour
01213               }
01214               break;
01215             }
01216             /* FALL_THROUGH */
01217           default:
01218             if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
01219             /* Anything that is not an industry or a company is using normal process */
01220             GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK);
01221             DrawString(x + text_left, x + text_right, y, tbl->legend);
01222             break;
01223         }
01224         GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, legend_colour); // Legend colour
01225 
01226         y += row_height;
01227       }
01228     }
01229   }
01230 }
01231 
01240 inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
01241 {
01242   /* Reserve one column for link colours */
01243   uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
01244   uint num_rows_others = CeilDiv(max(_smallmap_industry_count,_smallmap_company_count), columns);
01245   return max(this->min_number_of_fixed_rows, max(num_rows_linkstats, num_rows_others));
01246 }
01247 
01259 void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
01260 {
01261   if (_ctrl_pressed) {
01262     /* Disable all, except the clicked one */
01263     bool changes = false;
01264     for (int i = begin_legend_item; i != end_legend_item; i++) {
01265       bool new_state = (i == click_pos);
01266       if (legend[i].show_on_map != new_state) {
01267         changes = true;
01268         legend[i].show_on_map = new_state;
01269       }
01270     }
01271     if (!changes) {
01272       /* Nothing changed? Then show all (again). */
01273       for (int i = begin_legend_item; i != end_legend_item; i++) {
01274         legend[i].show_on_map = true;
01275       }
01276     }
01277   } else {
01278     legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
01279   }
01280   this->SetOverlayCargoMask();
01281 }
01282 
01286 void SmallMapWindow::SetOverlayCargoMask()
01287 {
01288   if (this->map_type == SMT_LINKSTATS) {
01289     uint32 cargo_mask = 0;
01290     for (int i = 0; i != _smallmap_cargo_count; ++i) {
01291       if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
01292     }
01293     this->overlay->SetCargoMask(cargo_mask);
01294     this->overlay->RebuildCache();
01295   }
01296 }
01297 
01303 int SmallMapWindow::GetPositionOnLegend(Point pt)
01304 {
01305   const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
01306   uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
01307   uint columns = this->GetNumberColumnsLegend(wi->current_x);
01308   uint number_of_rows = this->GetNumberRowsLegend(columns);
01309   if (line >= number_of_rows) return -1;
01310 
01311   bool rtl = _current_text_dir == TD_RTL;
01312   int x = pt.x - wi->pos_x;
01313   if (rtl) x = wi->current_x - x;
01314   uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
01315 
01316   return (column * number_of_rows) + line;
01317 }
01318 
01319 void SmallMapWindow::OnMouseOver(Point pt, int widget)
01320 {
01321   IndustryType new_highlight = INVALID_INDUSTRYTYPE;
01322   if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
01323     int industry_pos = GetPositionOnLegend(pt);
01324     if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
01325       new_highlight = _legend_from_industries[industry_pos].type;
01326     }
01327   }
01328   if (new_highlight != _smallmap_industry_highlight) {
01329     _smallmap_industry_highlight = new_highlight;
01330     this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
01331     _smallmap_industry_highlight_state = true;
01332     this->SetDirty();
01333   }
01334 }
01335 
01340 void SmallMapWindow::SwitchMapType(SmallMapType map_type)
01341 {
01342   this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
01343   this->map_type = map_type;
01344   this->LowerWidget(this->map_type + WID_SM_CONTOUR);
01345 
01346   this->SetupWidgetData();
01347 
01348   if (map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
01349   this->SetDirty();
01350 }
01351 
01352 void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
01353 {
01354   /* User clicked something, notify the industry chain window to stop sending newly selected industries. */
01355   InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
01356 
01357   switch (widget) {
01358     case WID_SM_MAP: { // Map window
01359       /*
01360        * XXX: scrolling with the left mouse button is done by subsequently
01361        * clicking with the left mouse button; clicking once centers the
01362        * large map at the selected point. So by unclicking the left mouse
01363        * button here, it gets reclicked during the next inputloop, which
01364        * would make it look like the mouse is being dragged, while it is
01365        * actually being (virtually) clicked every inputloop.
01366        */
01367       _left_button_clicked = false;
01368 
01369       const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01370       Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
01371       int sub;
01372       pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
01373       pt = RemapCoords(this->scroll_x + pt.x * TILE_SIZE + this->zoom * (TILE_SIZE - sub * TILE_SIZE / 4),
01374           this->scroll_y + pt.y * TILE_SIZE + sub * this->zoom * TILE_SIZE / 4, 0);
01375 
01376       w->viewport->follow_vehicle = INVALID_VEHICLE;
01377       w->viewport->dest_scrollpos_x = pt.x - (w->viewport->virtual_width  >> 1);
01378       w->viewport->dest_scrollpos_y = pt.y - (w->viewport->virtual_height >> 1);
01379 
01380       this->SetDirty();
01381       break;
01382     }
01383 
01384     case WID_SM_ZOOM_IN:
01385     case WID_SM_ZOOM_OUT: {
01386       const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01387       Point pt = {wid->current_x / 2, wid->current_y / 2};
01388       this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
01389       if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
01390       break;
01391     }
01392 
01393     case WID_SM_CONTOUR:    // Show land contours
01394     case WID_SM_VEHICLES:   // Show vehicles
01395     case WID_SM_INDUSTRIES: // Show industries
01396     case WID_SM_LINKSTATS:  // Show route map
01397     case WID_SM_ROUTES:     // Show transport routes
01398     case WID_SM_VEGETATION: // Show vegetation
01399     case WID_SM_OWNERS:     // Show land owners
01400       this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
01401       if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
01402       break;
01403 
01404     case WID_SM_CENTERMAP: // Center the smallmap again
01405       this->SmallMapCenterOnCurrentPos();
01406       this->HandleButtonClick(WID_SM_CENTERMAP);
01407       if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
01408       break;
01409 
01410     case WID_SM_TOGGLETOWNNAME: // Toggle town names
01411       this->show_towns = !this->show_towns;
01412       this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
01413 
01414       this->SetDirty();
01415       if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
01416       break;
01417 
01418     case WID_SM_LEGEND: // Legend
01419       if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
01420         int click_pos = this->GetPositionOnLegend(pt);
01421         /* If industry type small map*/
01422         if (this->map_type == SMT_INDUSTRY) {
01423           /* If click on industries label, find right industry type and enable/disable it. */
01424           if (click_pos >= 0 && click_pos < _smallmap_industry_count) {
01425             this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
01426           }
01427         } else if (this->map_type == SMT_LINKSTATS) {
01428           if (click_pos < _smallmap_cargo_count) {
01429             this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
01430           }
01431         } else if (this->map_type == SMT_OWNER) {
01432           if (click_pos < _smallmap_company_count) {
01433             this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
01434           }
01435         }
01436         this->SetDirty();
01437       }
01438       break;
01439 
01440     case WID_SM_ENABLE_ALL:
01441       /* FALL THROUGH */
01442     case WID_SM_DISABLE_ALL: {
01443       LegendAndColour *tbl = NULL;
01444       switch (this->map_type) {
01445         case SMT_INDUSTRY:
01446           tbl = _legend_from_industries;
01447           break;
01448         case SMT_OWNER:
01449           tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
01450           break;
01451         case SMT_LINKSTATS:
01452           tbl = _legend_linkstats;
01453           break;
01454         default:
01455           NOT_REACHED();
01456       }
01457       for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
01458         tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
01459       }
01460       this->SetOverlayCargoMask();
01461       this->SetDirty();
01462       break;
01463     }
01464 
01465     case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
01466       _smallmap_show_heightmap = !_smallmap_show_heightmap;
01467       this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
01468       this->SetDirty();
01469       break;
01470   }
01471 }
01472 
01480 void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
01481 {
01482   if (!gui_scope) return;
01483   switch (data) {
01484     case 1:
01485       /* The owner legend has already been rebuilt. */
01486       this->ReInit();
01487       break;
01488 
01489     case 0: {
01490       extern uint64 _displayed_industries;
01491       if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
01492 
01493       for (int i = 0; i != _smallmap_industry_count; i++) {
01494         _legend_from_industries[i].show_on_map = HasBit(_displayed_industries, _legend_from_industries[i].type);
01495       }
01496       break;
01497     }
01498 
01499     default: NOT_REACHED();
01500   }
01501   this->SetDirty();
01502 }
01503 
01504 bool SmallMapWindow::OnRightClick(Point pt, int widget)
01505 {
01506   if (widget != WID_SM_MAP || _scrolling_viewport) return false;
01507 
01508   _scrolling_viewport = true;
01509   return true;
01510 }
01511 
01512 void SmallMapWindow::OnMouseWheel(int wheel)
01513 {
01514   if (_settings_client.gui.scrollwheel_scrolling == 0) {
01515     const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01516     int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
01517     int cursor_y = _cursor.pos.y - this->top  - wid->pos_y;
01518     if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
01519       Point pt = {cursor_x, cursor_y};
01520       this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
01521     }
01522   }
01523 }
01524 
01525 void SmallMapWindow::OnTick()
01526 {
01527   /* Update the window every now and then */
01528   if (--this->refresh != 0) return;
01529 
01530   if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
01531   _smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
01532 
01533   this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
01534   this->SetDirty();
01535 }
01536 
01544 void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
01545 {
01546   const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01547   Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
01548   hv.x *= this->zoom;
01549   hv.y *= this->zoom;
01550 
01551   if (sx < -hv.x) {
01552     sx = -hv.x;
01553     sub = 0;
01554   }
01555   if (sx > (int)(MapMaxX() * TILE_SIZE) - hv.x) {
01556     sx = MapMaxX() * TILE_SIZE - hv.x;
01557     sub = 0;
01558   }
01559   if (sy < -hv.y) {
01560     sy = -hv.y;
01561     sub = 0;
01562   }
01563   if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
01564     sy = MapMaxY() * TILE_SIZE - hv.y;
01565     sub = 0;
01566   }
01567 
01568   this->scroll_x = sx;
01569   this->scroll_y = sy;
01570   this->subscroll = sub;
01571   if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
01572 }
01573 
01574 void SmallMapWindow::OnScroll(Point delta)
01575 {
01576   _cursor.fix_at = true;
01577 
01578   /* While tile is at (delta.x, delta.y)? */
01579   int sub;
01580   Point pt = this->PixelToTile(delta.x, delta.y, &sub);
01581   this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
01582 
01583   this->SetDirty();
01584 }
01585 
01586 void SmallMapWindow::SmallMapCenterOnCurrentPos()
01587 {
01588   const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
01589   Point pt = InverseRemapCoords(vp->virtual_left + vp->virtual_width  / 2, vp->virtual_top  + vp->virtual_height / 2);
01590 
01591   int sub;
01592   const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
01593   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);
01594   this->SetNewScroll(sxy.x, sxy.y, sub);
01595   this->SetDirty();
01596 }
01597 
01603 Point SmallMapWindow::GetStationMiddle(const Station *st) const
01604 {
01605   int x = (st->rect.right + st->rect.left + 1) / 2;
01606   int y = (st->rect.bottom + st->rect.top + 1) / 2;
01607   Point ret = this->RemapTile(x, y);
01608 
01609   /* Same magic 3 as in DrawVehicles; that's where I got it from.
01610    * No idea what it is, but without it the result looks bad.
01611    */
01612   ret.x -= 3 + this->subscroll;
01613   return ret;
01614 }
01615 
01616 SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
01617 bool SmallMapWindow::show_towns = true;
01618 
01627 class NWidgetSmallmapDisplay : public NWidgetContainer {
01628   const SmallMapWindow *smallmap_window; 
01629 public:
01630   NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL)
01631   {
01632     this->smallmap_window = NULL;
01633   }
01634 
01635   virtual void SetupSmallestSize(Window *w, bool init_array)
01636   {
01637     NWidgetBase *display = this->head;
01638     NWidgetBase *bar = display->next;
01639 
01640     display->SetupSmallestSize(w, init_array);
01641     bar->SetupSmallestSize(w, init_array);
01642 
01643     this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
01644     this->smallest_x = max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
01645     this->smallest_y = display->smallest_y + max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
01646     this->fill_x = max(display->fill_x, bar->fill_x);
01647     this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : min(display->fill_y, bar->fill_y);
01648     this->resize_x = max(display->resize_x, bar->resize_x);
01649     this->resize_y = min(display->resize_y, bar->resize_y);
01650   }
01651 
01652   virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
01653   {
01654     this->pos_x = x;
01655     this->pos_y = y;
01656     this->current_x = given_width;
01657     this->current_y = given_height;
01658 
01659     NWidgetBase *display = this->head;
01660     NWidgetBase *bar = display->next;
01661 
01662     if (sizing == ST_SMALLEST) {
01663       this->smallest_x = given_width;
01664       this->smallest_y = given_height;
01665       /* Make display and bar exactly equal to their minimal size. */
01666       display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl);
01667       bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
01668     }
01669 
01670     uint bar_height = max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
01671     uint display_height = given_height - bar_height;
01672     display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl);
01673     bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl);
01674   }
01675 
01676   virtual NWidgetCore *GetWidgetFromPos(int x, int y)
01677   {
01678     if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
01679     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
01680       NWidgetCore *widget = child_wid->GetWidgetFromPos(x, y);
01681       if (widget != NULL) return widget;
01682     }
01683     return NULL;
01684   }
01685 
01686   virtual void Draw(const Window *w)
01687   {
01688     for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) child_wid->Draw(w);
01689   }
01690 };
01691 
01693 static const NWidgetPart _nested_smallmap_display[] = {
01694   NWidget(WWT_PANEL, COLOUR_BROWN, WID_SM_MAP_BORDER),
01695     NWidget(WWT_INSET, COLOUR_BROWN, WID_SM_MAP), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
01696   EndContainer(),
01697 };
01698 
01700 static const NWidgetPart _nested_smallmap_bar[] = {
01701   NWidget(WWT_PANEL, COLOUR_BROWN),
01702     NWidget(NWID_HORIZONTAL),
01703       NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SM_LEGEND), SetResize(1, 1),
01704       NWidget(NWID_VERTICAL),
01705         /* Top button row. */
01706         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01707           NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_IN),
01708               SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN), SetFill(1, 1),
01709           NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_CENTERMAP),
01710               SetDataTip(SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER), SetFill(1, 1),
01711           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_BLANK),
01712               SetDataTip(SPR_DOT_SMALL, STR_NULL), SetFill(1, 1),
01713           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_CONTOUR),
01714               SetDataTip(SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP), SetFill(1, 1),
01715           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEHICLES),
01716               SetDataTip(SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP), SetFill(1, 1),
01717           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_INDUSTRIES),
01718               SetDataTip(SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP), SetFill(1, 1),
01719         EndContainer(),
01720         /* Bottom button row. */
01721         NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01722           NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_OUT),
01723               SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT), SetFill(1, 1),
01724           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_TOGGLETOWNNAME),
01725               SetDataTip(SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF), SetFill(1, 1),
01726           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_LINKSTATS),
01727               SetDataTip(SPR_IMG_GRAPHS, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP), SetFill(1, 1),
01728           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_ROUTES),
01729               SetDataTip(SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON), SetFill(1, 1),
01730           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEGETATION),
01731               SetDataTip(SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP), SetFill(1, 1),
01732           NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_OWNERS),
01733               SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP), SetFill(1, 1),
01734         EndContainer(),
01735         NWidget(NWID_SPACER), SetResize(0, 1),
01736       EndContainer(),
01737     EndContainer(),
01738   EndContainer(),
01739 };
01740 
01741 static NWidgetBase *SmallMapDisplay(int *biggest_index)
01742 {
01743   NWidgetContainer *map_display = new NWidgetSmallmapDisplay;
01744 
01745   MakeNWidgets(_nested_smallmap_display, lengthof(_nested_smallmap_display), biggest_index, map_display);
01746   MakeNWidgets(_nested_smallmap_bar, lengthof(_nested_smallmap_bar), biggest_index, map_display);
01747   return map_display;
01748 }
01749 
01750 
01751 static const NWidgetPart _nested_smallmap_widgets[] = {
01752   NWidget(NWID_HORIZONTAL),
01753     NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
01754     NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SM_CAPTION), SetDataTip(STR_SMALLMAP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
01755     NWidget(WWT_SHADEBOX, COLOUR_BROWN),
01756     NWidget(WWT_STICKYBOX, COLOUR_BROWN),
01757   EndContainer(),
01758   NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
01759   /* Bottom button row and resize box. */
01760   NWidget(NWID_HORIZONTAL),
01761     NWidget(WWT_PANEL, COLOUR_BROWN),
01762       NWidget(NWID_HORIZONTAL),
01763         NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SELECT_BUTTONS),
01764           NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
01765             NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
01766             NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
01767             NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
01768           EndContainer(),
01769           NWidget(NWID_SPACER), SetFill(1, 1),
01770         EndContainer(),
01771         NWidget(NWID_SPACER), SetFill(1, 0), SetResize(1, 0),
01772       EndContainer(),
01773     EndContainer(),
01774     NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
01775   EndContainer(),
01776 };
01777 
01778 static const WindowDesc _smallmap_desc(
01779   WDP_AUTO, 484, 314,
01780   WC_SMALLMAP, WC_NONE,
01781   0,
01782   _nested_smallmap_widgets, lengthof(_nested_smallmap_widgets)
01783 );
01784 
01788 void ShowSmallMap()
01789 {
01790   AllocateWindowDescFront<SmallMapWindow>(&_smallmap_desc, 0);
01791 }
01792 
01801 bool ScrollMainWindowTo(int x, int y, int z, bool instant)
01802 {
01803   bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
01804 
01805   /* If a user scrolls to a tile (via what way what so ever) and already is on
01806    * that tile (e.g.: pressed twice), move the smallmap to that location,
01807    * so you directly see where you are on the smallmap. */
01808 
01809   if (res) return res;
01810 
01811   SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WC_SMALLMAP, 0));
01812   if (w != NULL) w->SmallMapCenterOnCurrentPos();
01813 
01814   return res;
01815 }