smallmap_gui.cpp

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