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