(svn r27707) -Fix: Make the result of NewGRF's CARGO_NAME string code reliably print...
[openttd.git] / src / smallmap_gui.cpp
blobf4bcdece18c628604e860208bbbfcf898d021ea7
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
10 /** @file smallmap_gui.cpp GUI that shows a small map of the world with metadata like owner or height. */
12 #include "stdafx.h"
13 #include "clear_map.h"
14 #include "industry.h"
15 #include "station_map.h"
16 #include "landscape.h"
17 #include "tree_map.h"
18 #include "viewport_func.h"
19 #include "town.h"
20 #include "tunnelbridge_map.h"
21 #include "core/endian_func.hpp"
22 #include "vehicle_base.h"
23 #include "sound_func.h"
24 #include "window_func.h"
25 #include "company_base.h"
27 #include "smallmap_gui.h"
29 #include "table/strings.h"
31 #include <bitset>
33 #include "safeguards.h"
35 static int _smallmap_industry_count; ///< Number of used industries
36 static int _smallmap_company_count; ///< Number of entries in the owner legend.
37 static int _smallmap_cargo_count; ///< Number of cargos in the link stats legend.
39 /** Link stat colours shown in legenda. */
40 static uint8 _linkstat_colours_in_legenda[] = {0, 1, 3, 5, 7, 9, 11};
42 static const int NUM_NO_COMPANY_ENTRIES = 4; ///< Number of entries in the owner legend that are not companies.
44 static const uint8 PC_ROUGH_LAND = 0x52; ///< Dark green palette colour for rough land.
45 static const uint8 PC_GRASS_LAND = 0x54; ///< Dark green palette colour for grass land.
46 static const uint8 PC_BARE_LAND = 0x37; ///< Brown palette colour for bare land.
47 static const uint8 PC_FIELDS = 0x25; ///< Light brown palette colour for fields.
48 static const uint8 PC_TREES = 0x57; ///< Green palette colour for trees.
49 static const uint8 PC_WATER = 0xCA; ///< Dark blue palette colour for water.
51 /** Macro for ordinary entry of LegendAndColour */
52 #define MK(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
54 /** Macro for a height legend entry with configurable colour. */
55 #define MC(col_break) {0, STR_TINY_BLACK_HEIGHT, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, col_break}
57 /** Macro for non-company owned property entry of LegendAndColour */
58 #define MO(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, false}
60 /** Macro used for forcing a rebuild of the owner legend the first time it is used. */
61 #define MOEND() {0, 0, INVALID_INDUSTRYTYPE, 0, OWNER_NONE, true, true, false}
63 /** Macro for end of list marker in arrays of LegendAndColour */
64 #define MKEND() {0, STR_NULL, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, true, false}
66 /**
67 * Macro for break marker in arrays of LegendAndColour.
68 * It will have valid data, though
70 #define MS(a, b) {a, b, INVALID_INDUSTRYTYPE, 0, INVALID_COMPANY, true, false, true}
72 /** Legend text giving the colours to look for on the minimap */
73 static LegendAndColour _legend_land_contours[] = {
74 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
75 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
76 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_STATIONS_AIRPORTS_DOCKS),
77 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
78 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_VEHICLES),
80 /* Placeholders for the colours and heights of the legend.
81 * The following values are set at BuildLandLegend() based
82 * on each colour scheme and the maximum map height. */
83 MC(true),
84 MC(false),
85 MC(false),
86 MC(false),
87 MC(false),
88 MC(false),
89 MC(true),
90 MC(false),
91 MC(false),
92 MC(false),
93 MC(false),
94 MC(false),
95 MKEND()
98 static const LegendAndColour _legend_vehicles[] = {
99 MK(PC_RED, STR_SMALLMAP_LEGENDA_TRAINS),
100 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_ROAD_VEHICLES),
101 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SHIPS),
102 MK(PC_WHITE, STR_SMALLMAP_LEGENDA_AIRCRAFT),
104 MS(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
105 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
106 MKEND()
109 static const LegendAndColour _legend_routes[] = {
110 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_ROADS),
111 MK(PC_GREY, STR_SMALLMAP_LEGENDA_RAILROADS),
112 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
114 MS(PC_VERY_DARK_BROWN, STR_SMALLMAP_LEGENDA_RAILROAD_STATION),
115 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_TRUCK_LOADING_BAY),
116 MK(PC_YELLOW, STR_SMALLMAP_LEGENDA_BUS_STATION),
117 MK(PC_RED, STR_SMALLMAP_LEGENDA_AIRPORT_HELIPORT),
118 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_DOCK),
119 MKEND()
122 static const LegendAndColour _legend_vegetation[] = {
123 MK(PC_ROUGH_LAND, STR_SMALLMAP_LEGENDA_ROUGH_LAND),
124 MK(PC_GRASS_LAND, STR_SMALLMAP_LEGENDA_GRASS_LAND),
125 MK(PC_BARE_LAND, STR_SMALLMAP_LEGENDA_BARE_LAND),
126 MK(PC_FIELDS, STR_SMALLMAP_LEGENDA_FIELDS),
127 MK(PC_TREES, STR_SMALLMAP_LEGENDA_TREES),
128 MK(PC_GREEN, STR_SMALLMAP_LEGENDA_FOREST),
130 MS(PC_GREY, STR_SMALLMAP_LEGENDA_ROCKS),
131 MK(PC_ORANGE, STR_SMALLMAP_LEGENDA_DESERT),
132 MK(PC_LIGHT_BLUE, STR_SMALLMAP_LEGENDA_SNOW),
133 MK(PC_BLACK, STR_SMALLMAP_LEGENDA_TRANSPORT_ROUTES),
134 MK(PC_DARK_RED, STR_SMALLMAP_LEGENDA_BUILDINGS_INDUSTRIES),
135 MKEND()
138 static LegendAndColour _legend_land_owners[NUM_NO_COMPANY_ENTRIES + MAX_COMPANIES + 1] = {
139 MO(PC_WATER, STR_SMALLMAP_LEGENDA_WATER),
140 MO(0x00, STR_SMALLMAP_LEGENDA_NO_OWNER), // This colour will vary depending on settings.
141 MO(PC_DARK_RED, STR_SMALLMAP_LEGENDA_TOWNS),
142 MO(PC_DARK_GREY, STR_SMALLMAP_LEGENDA_INDUSTRIES),
143 /* The legend will be terminated the first time it is used. */
144 MOEND(),
147 #undef MK
148 #undef MC
149 #undef MS
150 #undef MO
151 #undef MOEND
152 #undef MKEND
154 /** Legend entries for the link stats view. */
155 static LegendAndColour _legend_linkstats[NUM_CARGO + lengthof(_linkstat_colours_in_legenda) + 1];
157 * Allow room for all industries, plus a terminator entry
158 * This is required in order to have the industry slots all filled up
160 static LegendAndColour _legend_from_industries[NUM_INDUSTRYTYPES + 1];
161 /** For connecting industry type to position in industries list(small map legend) */
162 static uint _industry_to_list_pos[NUM_INDUSTRYTYPES];
163 /** Show heightmap in industry and owner mode of smallmap window. */
164 static bool _smallmap_show_heightmap = false;
165 /** Highlight a specific industry type */
166 static IndustryType _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
167 /** State of highlight blinking */
168 static bool _smallmap_industry_highlight_state;
169 /** For connecting company ID to position in owner list (small map legend) */
170 static uint _company_to_list_pos[MAX_COMPANIES];
173 * Fills an array for the industries legends.
175 void BuildIndustriesLegend()
177 uint j = 0;
179 /* Add each name */
180 for (uint i = 0; i < NUM_INDUSTRYTYPES; i++) {
181 IndustryType ind = _sorted_industry_types[i];
182 const IndustrySpec *indsp = GetIndustrySpec(ind);
183 if (indsp->enabled) {
184 _legend_from_industries[j].legend = indsp->name;
185 _legend_from_industries[j].colour = indsp->map_colour;
186 _legend_from_industries[j].type = ind;
187 _legend_from_industries[j].show_on_map = true;
188 _legend_from_industries[j].col_break = false;
189 _legend_from_industries[j].end = false;
191 /* Store widget number for this industry type. */
192 _industry_to_list_pos[ind] = j;
193 j++;
196 /* Terminate the list */
197 _legend_from_industries[j].end = true;
199 /* Store number of enabled industries */
200 _smallmap_industry_count = j;
204 * Populate legend table for the link stat view.
206 void BuildLinkStatsLegend()
208 /* Clear the legend */
209 memset(_legend_linkstats, 0, sizeof(_legend_linkstats));
211 uint i = 0;
212 for (; i < _sorted_cargo_specs_size; ++i) {
213 const CargoSpec *cs = _sorted_cargo_specs[i];
215 _legend_linkstats[i].legend = cs->name;
216 _legend_linkstats[i].colour = cs->legend_colour;
217 _legend_linkstats[i].type = cs->Index();
218 _legend_linkstats[i].show_on_map = true;
221 _legend_linkstats[i].col_break = true;
222 _smallmap_cargo_count = i;
224 for (; i < _smallmap_cargo_count + lengthof(_linkstat_colours_in_legenda); ++i) {
225 _legend_linkstats[i].legend = STR_EMPTY;
226 _legend_linkstats[i].colour = LinkGraphOverlay::LINK_COLOURS[_linkstat_colours_in_legenda[i - _smallmap_cargo_count]];
227 _legend_linkstats[i].show_on_map = true;
230 _legend_linkstats[_smallmap_cargo_count].legend = STR_LINKGRAPH_LEGEND_UNUSED;
231 _legend_linkstats[i - 1].legend = STR_LINKGRAPH_LEGEND_OVERLOADED;
232 _legend_linkstats[(_smallmap_cargo_count + i - 1) / 2].legend = STR_LINKGRAPH_LEGEND_SATURATED;
233 _legend_linkstats[i].end = true;
236 static const LegendAndColour * const _legend_table[] = {
237 _legend_land_contours,
238 _legend_vehicles,
239 _legend_from_industries,
240 _legend_linkstats,
241 _legend_routes,
242 _legend_vegetation,
243 _legend_land_owners,
246 #define MKCOLOUR(x) TO_LE32X(x)
248 #define MKCOLOUR_XXXX(x) (MKCOLOUR(0x01010101) * (uint)(x))
249 #define MKCOLOUR_X0X0(x) (MKCOLOUR(0x01000100) * (uint)(x))
250 #define MKCOLOUR_0X0X(x) (MKCOLOUR(0x00010001) * (uint)(x))
251 #define MKCOLOUR_0XX0(x) (MKCOLOUR(0x00010100) * (uint)(x))
252 #define MKCOLOUR_X00X(x) (MKCOLOUR(0x01000001) * (uint)(x))
254 #define MKCOLOUR_XYXY(x, y) (MKCOLOUR_X0X0(x) | MKCOLOUR_0X0X(y))
255 #define MKCOLOUR_XYYX(x, y) (MKCOLOUR_X00X(x) | MKCOLOUR_0XX0(y))
257 #define MKCOLOUR_0000 MKCOLOUR_XXXX(0x00)
258 #define MKCOLOUR_0FF0 MKCOLOUR_0XX0(0xFF)
259 #define MKCOLOUR_F00F MKCOLOUR_X00X(0xFF)
260 #define MKCOLOUR_FFFF MKCOLOUR_XXXX(0xFF)
262 #include "table/heightmap_colours.h"
264 /** Colour scheme of the smallmap. */
265 struct SmallMapColourScheme {
266 uint32 *height_colours; ///< Cached colours for each level in a map.
267 const uint32 *height_colours_base; ///< Base table for determining the colours
268 size_t colour_count; ///< The number of colours.
269 uint32 default_colour; ///< Default colour of the land.
272 /** Available colour schemes for height maps. */
273 static SmallMapColourScheme _heightmap_schemes[] = {
274 {NULL, _green_map_heights, lengthof(_green_map_heights), MKCOLOUR_XXXX(0x54)}, ///< Green colour scheme.
275 {NULL, _dark_green_map_heights, lengthof(_dark_green_map_heights), MKCOLOUR_XXXX(0x62)}, ///< Dark green colour scheme.
276 {NULL, _violet_map_heights, lengthof(_violet_map_heights), MKCOLOUR_XXXX(0x82)}, ///< Violet colour scheme.
280 * (Re)build the colour tables for the legends.
282 void BuildLandLegend()
284 /* The smallmap window has never been initialized, so no need to change the legend. */
285 if (_heightmap_schemes[0].height_colours == NULL) return;
288 * The general idea of this function is to fill the legend with an appropriate evenly spaced
289 * selection of height levels. All entries with STR_TINY_BLACK_HEIGHT are reserved for this.
290 * At the moment there are twelve of these.
292 * The table below defines up to which height level a particular delta in the legend should be
293 * used. One could opt for just dividing the maximum height and use that as delta, but that
294 * creates many "ugly" legend labels, e.g. once every 950 meter. As a result, this table will
295 * reduce the number of deltas to 7: every 100m, 200m, 300m, 500m, 750m, 1000m and 1250m. The
296 * deltas are closer together at the lower numbers because going from 12 entries to just 4, as
297 * would happen when replacing 200m and 300m by 250m, would mean the legend would be short and
298 * that might not be considered appropriate.
300 * The current method yields at least 7 legend entries and at most 12. It can be increased to
301 * 8 by adding a 150m and 400m option, but especially 150m creates ugly heights.
303 * It tries to evenly space the legend items over the two columns that are there for the legend.
306 /* Table for delta; if max_height is less than the first column, use the second column as value. */
307 uint deltas[][2] = { { 24, 2 }, { 48, 4 }, { 72, 6 }, { 120, 10 }, { 180, 15 }, { 240, 20 }, { MAX_TILE_HEIGHT + 1, 25 }};
308 uint i = 0;
309 for (; _settings_game.construction.max_heightlevel >= deltas[i][0]; i++) {
310 /* Nothing to do here. */
312 uint delta = deltas[i][1];
314 int total_entries = (_settings_game.construction.max_heightlevel / delta) + 1;
315 int rows = CeilDiv(total_entries, 2);
316 int j = 0;
318 for (i = 0; i < lengthof(_legend_land_contours) - 1 && j < total_entries; i++) {
319 if (_legend_land_contours[i].legend != STR_TINY_BLACK_HEIGHT) continue;
321 _legend_land_contours[i].col_break = j % rows == 0;
322 _legend_land_contours[i].end = false;
323 _legend_land_contours[i].height = j * delta;
324 _legend_land_contours[i].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].height_colours[j * delta];
325 j++;
327 _legend_land_contours[i].end = true;
331 * Completes the array for the owned property legend.
333 void BuildOwnerLegend()
335 _legend_land_owners[1].colour = _heightmap_schemes[_settings_client.gui.smallmap_land_colour].default_colour;
337 int i = NUM_NO_COMPANY_ENTRIES;
338 const Company *c;
339 FOR_ALL_COMPANIES(c) {
340 _legend_land_owners[i].colour = _colour_gradient[c->colour][5];
341 _legend_land_owners[i].company = c->index;
342 _legend_land_owners[i].show_on_map = true;
343 _legend_land_owners[i].col_break = false;
344 _legend_land_owners[i].end = false;
345 _company_to_list_pos[c->index] = i;
346 i++;
349 /* Terminate the list */
350 _legend_land_owners[i].end = true;
352 /* Store maximum amount of owner legend entries. */
353 _smallmap_company_count = i;
356 struct AndOr {
357 uint32 mor;
358 uint32 mand;
361 static inline uint32 ApplyMask(uint32 colour, const AndOr *mask)
363 return (colour & mask->mand) | mask->mor;
367 /** Colour masks for "Contour" and "Routes" modes. */
368 static const AndOr _smallmap_contours_andor[] = {
369 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_CLEAR
370 {MKCOLOUR_0XX0(PC_GREY ), MKCOLOUR_F00F}, // MP_RAILWAY
371 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_ROAD
372 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_HOUSE
373 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TREES
374 {MKCOLOUR_XXXX(PC_LIGHT_BLUE), MKCOLOUR_0000}, // MP_STATION
375 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // MP_WATER
376 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_VOID
377 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // MP_INDUSTRY
378 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
379 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_OBJECT
380 {MKCOLOUR_0XX0(PC_GREY ), MKCOLOUR_F00F},
383 /** Colour masks for "Vehicles", "Industry", and "Vegetation" modes. */
384 static const AndOr _smallmap_vehicles_andor[] = {
385 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_CLEAR
386 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_RAILWAY
387 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_ROAD
388 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_HOUSE
389 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TREES
390 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F}, // MP_STATION
391 {MKCOLOUR_XXXX(PC_WATER ), MKCOLOUR_0000}, // MP_WATER
392 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_VOID
393 {MKCOLOUR_XXXX(PC_DARK_RED ), MKCOLOUR_0000}, // MP_INDUSTRY
394 {MKCOLOUR_0000 , MKCOLOUR_FFFF}, // MP_TUNNELBRIDGE
395 {MKCOLOUR_0XX0(PC_DARK_RED ), MKCOLOUR_F00F}, // MP_OBJECT
396 {MKCOLOUR_0XX0(PC_BLACK ), MKCOLOUR_F00F},
399 /** Mapping of tile type to importance of the tile (higher number means more interesting to show). */
400 static const byte _tiletype_importance[] = {
401 2, // MP_CLEAR
402 8, // MP_RAILWAY
403 7, // MP_ROAD
404 5, // MP_HOUSE
405 2, // MP_TREES
406 9, // MP_STATION
407 2, // MP_WATER
408 1, // MP_VOID
409 6, // MP_INDUSTRY
410 8, // MP_TUNNELBRIDGE
411 2, // MP_OBJECT
417 * Return the colour a tile would be displayed with in the small map in mode "Contour".
418 * @param tile The tile of which we would like to get the colour.
419 * @param t Effective tile type of the tile (see #GetTileColours).
420 * @return The colour of tile in the small map in mode "Contour"
422 static inline uint32 GetSmallMapContoursPixels(TileIndex tile, TileType t)
424 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
425 return ApplyMask(cs->height_colours[TileHeight(tile)], &_smallmap_contours_andor[t]);
429 * Return the colour a tile would be displayed with in the small map in mode "Vehicles".
431 * @param tile The tile of which we would like to get the colour.
432 * @param t Effective tile type of the tile (see #GetTileColours).
433 * @return The colour of tile in the small map in mode "Vehicles"
435 static inline uint32 GetSmallMapVehiclesPixels(TileIndex tile, TileType t)
437 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
438 return ApplyMask(cs->default_colour, &_smallmap_vehicles_andor[t]);
442 * Return the colour a tile would be displayed with in the small map in mode "Industries".
444 * @param tile The tile of which we would like to get the colour.
445 * @param t Effective tile type of the tile (see #GetTileColours).
446 * @return The colour of tile in the small map in mode "Industries"
448 static inline uint32 GetSmallMapIndustriesPixels(TileIndex tile, TileType t)
450 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
451 return ApplyMask(_smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour, &_smallmap_vehicles_andor[t]);
455 * Return the colour a tile would be displayed with in the small map in mode "Routes".
457 * @param tile The tile of which we would like to get the colour.
458 * @param t Effective tile type of the tile (see #GetTileColours).
459 * @return The colour of tile in the small map in mode "Routes"
461 static inline uint32 GetSmallMapRoutesPixels(TileIndex tile, TileType t)
463 if (t == MP_STATION) {
464 switch (GetStationType(tile)) {
465 case STATION_RAIL: return MKCOLOUR_XXXX(PC_VERY_DARK_BROWN);
466 case STATION_AIRPORT: return MKCOLOUR_XXXX(PC_RED);
467 case STATION_TRUCK: return MKCOLOUR_XXXX(PC_ORANGE);
468 case STATION_BUS: return MKCOLOUR_XXXX(PC_YELLOW);
469 case STATION_DOCK: return MKCOLOUR_XXXX(PC_LIGHT_BLUE);
470 default: return MKCOLOUR_FFFF;
472 } else if (t == MP_RAILWAY) {
473 AndOr andor = {
474 MKCOLOUR_0XX0(GetRailTypeInfo(GetRailType(tile))->map_colour),
475 _smallmap_contours_andor[t].mand
478 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
479 return ApplyMask(cs->default_colour, &andor);
482 /* Ground colour */
483 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
484 return ApplyMask(cs->default_colour, &_smallmap_contours_andor[t]);
488 * Return the colour a tile would be displayed with in the small map in mode "link stats".
490 * @param tile The tile of which we would like to get the colour.
491 * @param t Effective tile type of the tile (see #GetTileColours).
492 * @return The colour of tile in the small map in mode "link stats"
494 static inline uint32 GetSmallMapLinkStatsPixels(TileIndex tile, TileType t)
496 return _smallmap_show_heightmap ? GetSmallMapContoursPixels(tile, t) : GetSmallMapRoutesPixels(tile, t);
499 static const uint32 _vegetation_clear_bits[] = {
500 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< full grass
501 MKCOLOUR_XXXX(PC_ROUGH_LAND), ///< rough land
502 MKCOLOUR_XXXX(PC_GREY), ///< rocks
503 MKCOLOUR_XXXX(PC_FIELDS), ///< fields
504 MKCOLOUR_XXXX(PC_LIGHT_BLUE), ///< snow
505 MKCOLOUR_XXXX(PC_ORANGE), ///< desert
506 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< unused
507 MKCOLOUR_XXXX(PC_GRASS_LAND), ///< unused
511 * Return the colour a tile would be displayed with in the smallmap in mode "Vegetation".
513 * @param tile The tile of which we would like to get the colour.
514 * @param t Effective tile type of the tile (see #GetTileColours).
515 * @return The colour of tile in the smallmap in mode "Vegetation"
517 static inline uint32 GetSmallMapVegetationPixels(TileIndex tile, TileType t)
519 switch (t) {
520 case MP_CLEAR:
521 return (IsClearGround(tile, CLEAR_GRASS) && GetClearDensity(tile) < 3) ? MKCOLOUR_XXXX(PC_BARE_LAND) : _vegetation_clear_bits[GetClearGround(tile)];
523 case MP_INDUSTRY:
524 return IsTileForestIndustry(tile) ? MKCOLOUR_XXXX(PC_GREEN) : MKCOLOUR_XXXX(PC_DARK_RED);
526 case MP_TREES:
527 if (GetTreeGround(tile) == TREE_GROUND_SNOW_DESERT || GetTreeGround(tile) == TREE_GROUND_ROUGH_SNOW) {
528 return (_settings_game.game_creation.landscape == LT_ARCTIC) ? MKCOLOUR_XYYX(PC_LIGHT_BLUE, PC_TREES) : MKCOLOUR_XYYX(PC_ORANGE, PC_TREES);
530 return MKCOLOUR_XYYX(PC_GRASS_LAND, PC_TREES);
532 default:
533 return ApplyMask(MKCOLOUR_XXXX(PC_GRASS_LAND), &_smallmap_vehicles_andor[t]);
538 * Return the colour a tile would be displayed with in the small map in mode "Owner".
540 * @param tile The tile of which we would like to get the colour.
541 * @param t Effective tile type of the tile (see #GetTileColours).
542 * @return The colour of tile in the small map in mode "Owner"
544 static inline uint32 GetSmallMapOwnerPixels(TileIndex tile, TileType t)
546 Owner o;
548 switch (t) {
549 case MP_INDUSTRY: return MKCOLOUR_XXXX(PC_DARK_GREY);
550 case MP_HOUSE: return MKCOLOUR_XXXX(PC_DARK_RED);
551 default: o = GetTileOwner(tile); break;
552 /* FIXME: For MP_ROAD there are multiple owners.
553 * GetTileOwner returns the rail owner (level crossing) resp. the owner of ROADTYPE_ROAD (normal road),
554 * even if there are no ROADTYPE_ROAD bits on the tile.
558 if ((o < MAX_COMPANIES && !_legend_land_owners[_company_to_list_pos[o]].show_on_map) || o == OWNER_NONE || o == OWNER_WATER) {
559 if (t == MP_WATER) return MKCOLOUR_XXXX(PC_WATER);
560 const SmallMapColourScheme *cs = &_heightmap_schemes[_settings_client.gui.smallmap_land_colour];
561 return _smallmap_show_heightmap ? cs->height_colours[TileHeight(tile)] : cs->default_colour;
562 } else if (o == OWNER_TOWN) {
563 return MKCOLOUR_XXXX(PC_DARK_RED);
566 return MKCOLOUR_XXXX(_legend_land_owners[_company_to_list_pos[o]].colour);
569 /** Vehicle colours in #SMT_VEHICLES mode. Indexed by #VehicleTypeByte. */
570 static const byte _vehicle_type_colours[6] = {
571 PC_RED, PC_YELLOW, PC_LIGHT_BLUE, PC_WHITE, PC_BLACK, PC_RED
575 inline Point SmallMapWindow::SmallmapRemapCoords(int x, int y) const
577 Point pt;
578 pt.x = (y - x) * 2;
579 pt.y = y + x;
580 return pt;
584 * Remap tile to location on this smallmap.
585 * @param tile_x X coordinate of the tile.
586 * @param tile_y Y coordinate of the tile.
587 * @return Position to draw on.
589 inline Point SmallMapWindow::RemapTile(int tile_x, int tile_y) const
591 int x_offset = tile_x - this->scroll_x / (int)TILE_SIZE;
592 int y_offset = tile_y - this->scroll_y / (int)TILE_SIZE;
594 if (this->zoom == 1) return SmallmapRemapCoords(x_offset, y_offset);
596 /* For negative offsets, round towards -inf. */
597 if (x_offset < 0) x_offset -= this->zoom - 1;
598 if (y_offset < 0) y_offset -= this->zoom - 1;
600 return SmallmapRemapCoords(x_offset / this->zoom, y_offset / this->zoom);
604 * Determine the tile relative to the base tile of the smallmap, and the pixel position at
605 * that tile for a point in the smallmap.
606 * @param px Horizontal coordinate of the pixel.
607 * @param py Vertical coordinate of the pixel.
608 * @param sub[out] Pixel position at the tile (0..3).
609 * @param add_sub Add current #subscroll to the position.
610 * @return Tile being displayed at the given position relative to #scroll_x and #scroll_y.
611 * @note The #subscroll offset is already accounted for.
613 inline Point SmallMapWindow::PixelToTile(int px, int py, int *sub, bool add_sub) const
615 if (add_sub) px += this->subscroll; // Total horizontal offset.
617 /* For each two rows down, add a x and a y tile, and
618 * For each four pixels to the right, move a tile to the right. */
619 Point pt = {((py >> 1) - (px >> 2)) * this->zoom, ((py >> 1) + (px >> 2)) * this->zoom};
620 px &= 3;
622 if (py & 1) { // Odd number of rows, handle the 2 pixel shift.
623 if (px < 2) {
624 pt.x += this->zoom;
625 px += 2;
626 } else {
627 pt.y += this->zoom;
628 px -= 2;
632 *sub = px;
633 return pt;
637 * Compute base parameters of the smallmap such that tile (\a tx, \a ty) starts at pixel (\a x, \a y).
638 * @param tx Tile x coordinate.
639 * @param ty Tile y coordinate.
640 * @param x Non-negative horizontal position in the display where the tile starts.
641 * @param y Non-negative vertical position in the display where the tile starts.
642 * @param sub [out] Value of #subscroll needed.
643 * @return #scroll_x, #scroll_y values.
645 Point SmallMapWindow::ComputeScroll(int tx, int ty, int x, int y, int *sub)
647 assert(x >= 0 && y >= 0);
649 int new_sub;
650 Point tile_xy = PixelToTile(x, y, &new_sub, false);
651 tx -= tile_xy.x;
652 ty -= tile_xy.y;
654 Point scroll;
655 if (new_sub == 0) {
656 *sub = 0;
657 scroll.x = (tx + this->zoom) * TILE_SIZE;
658 scroll.y = (ty - this->zoom) * TILE_SIZE;
659 } else {
660 *sub = 4 - new_sub;
661 scroll.x = (tx + 2 * this->zoom) * TILE_SIZE;
662 scroll.y = (ty - 2 * this->zoom) * TILE_SIZE;
664 return scroll;
668 * Initialize or change the zoom level.
669 * @param change Way to change the zoom level.
670 * @param zoom_pt Position to keep fixed while zooming.
671 * @pre \c *zoom_pt should contain a point in the smallmap display when zooming in or out.
673 void SmallMapWindow::SetZoomLevel(ZoomLevelChange change, const Point *zoom_pt)
675 static const int zoomlevels[] = {1, 2, 4, 6, 8}; // Available zoom levels. Bigger number means more zoom-out (further away).
676 static const int MIN_ZOOM_INDEX = 0;
677 static const int MAX_ZOOM_INDEX = lengthof(zoomlevels) - 1;
679 int new_index, cur_index, sub;
680 Point tile;
681 switch (change) {
682 case ZLC_INITIALIZE:
683 cur_index = - 1; // Definitely different from new_index.
684 new_index = MIN_ZOOM_INDEX;
685 tile.x = tile.y = 0;
686 break;
688 case ZLC_ZOOM_IN:
689 case ZLC_ZOOM_OUT:
690 for (cur_index = MIN_ZOOM_INDEX; cur_index <= MAX_ZOOM_INDEX; cur_index++) {
691 if (this->zoom == zoomlevels[cur_index]) break;
693 assert(cur_index <= MAX_ZOOM_INDEX);
695 tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
696 new_index = Clamp(cur_index + ((change == ZLC_ZOOM_IN) ? -1 : 1), MIN_ZOOM_INDEX, MAX_ZOOM_INDEX);
697 break;
699 default: NOT_REACHED();
702 if (new_index != cur_index) {
703 this->zoom = zoomlevels[new_index];
704 if (cur_index >= 0) {
705 Point new_tile = this->PixelToTile(zoom_pt->x, zoom_pt->y, &sub);
706 this->SetNewScroll(this->scroll_x + (tile.x - new_tile.x) * TILE_SIZE,
707 this->scroll_y + (tile.y - new_tile.y) * TILE_SIZE, sub);
708 } else if (this->map_type == SMT_LINKSTATS) {
709 this->overlay->RebuildCache();
711 this->SetWidgetDisabledState(WID_SM_ZOOM_IN, this->zoom == zoomlevels[MIN_ZOOM_INDEX]);
712 this->SetWidgetDisabledState(WID_SM_ZOOM_OUT, this->zoom == zoomlevels[MAX_ZOOM_INDEX]);
713 this->SetDirty();
718 * Decide which colours to show to the user for a group of tiles.
719 * @param ta Tile area to investigate.
720 * @return Colours to display.
722 inline uint32 SmallMapWindow::GetTileColours(const TileArea &ta) const
724 int importance = 0;
725 TileIndex tile = INVALID_TILE; // Position of the most important tile.
726 TileType et = MP_VOID; // Effective tile type at that position.
728 TILE_AREA_LOOP(ti, ta) {
729 TileType ttype = GetTileType(ti);
731 switch (ttype) {
732 case MP_TUNNELBRIDGE: {
733 TransportType tt = GetTunnelBridgeTransportType(ti);
735 switch (tt) {
736 case TRANSPORT_RAIL: ttype = MP_RAILWAY; break;
737 case TRANSPORT_ROAD: ttype = MP_ROAD; break;
738 default: ttype = MP_WATER; break;
740 break;
743 case MP_INDUSTRY:
744 /* Special handling of industries while in "Industries" smallmap view. */
745 if (this->map_type == SMT_INDUSTRY) {
746 /* If industry is allowed to be seen, use its colour on the map.
747 * This has the highest priority above any value in _tiletype_importance. */
748 IndustryType type = Industry::GetByTile(ti)->type;
749 if (_legend_from_industries[_industry_to_list_pos[type]].show_on_map) {
750 if (type == _smallmap_industry_highlight) {
751 if (_smallmap_industry_highlight_state) return MKCOLOUR_XXXX(PC_WHITE);
752 } else {
753 return GetIndustrySpec(type)->map_colour * 0x01010101;
756 /* Otherwise make it disappear */
757 ttype = IsTileOnWater(ti) ? MP_WATER : MP_CLEAR;
759 break;
761 default:
762 break;
765 if (_tiletype_importance[ttype] > importance) {
766 importance = _tiletype_importance[ttype];
767 tile = ti;
768 et = ttype;
772 switch (this->map_type) {
773 case SMT_CONTOUR:
774 return GetSmallMapContoursPixels(tile, et);
776 case SMT_VEHICLES:
777 return GetSmallMapVehiclesPixels(tile, et);
779 case SMT_INDUSTRY:
780 return GetSmallMapIndustriesPixels(tile, et);
782 case SMT_LINKSTATS:
783 return GetSmallMapLinkStatsPixels(tile, et);
785 case SMT_ROUTES:
786 return GetSmallMapRoutesPixels(tile, et);
788 case SMT_VEGETATION:
789 return GetSmallMapVegetationPixels(tile, et);
791 case SMT_OWNER:
792 return GetSmallMapOwnerPixels(tile, et);
794 default: NOT_REACHED();
799 * Draws one column of tiles of the small map in a certain mode onto the screen buffer, skipping the shifted rows in between.
801 * @param dst Pointer to a part of the screen buffer to write to.
802 * @param xc The X coordinate of the first tile in the column.
803 * @param yc The Y coordinate of the first tile in the column
804 * @param pitch Number of pixels to advance in the screen buffer each time a pixel is written.
805 * @param reps Number of lines to draw
806 * @param start_pos Position of first pixel to draw.
807 * @param end_pos Position of last pixel to draw (exclusive).
808 * @param blitter current blitter
809 * @note If pixel position is below \c 0, skip drawing.
811 void SmallMapWindow::DrawSmallMapColumn(void *dst, uint xc, uint yc, int pitch, int reps, int start_pos, int end_pos, Blitter *blitter) const
813 void *dst_ptr_abs_end = blitter->MoveTo(_screen.dst_ptr, 0, _screen.height);
814 uint min_xy = _settings_game.construction.freeform_edges ? 1 : 0;
816 do {
817 /* Check if the tile (xc,yc) is within the map range */
818 if (xc >= MapMaxX() || yc >= MapMaxY()) continue;
820 /* Check if the dst pointer points to a pixel inside the screen buffer */
821 if (dst < _screen.dst_ptr) continue;
822 if (dst >= dst_ptr_abs_end) continue;
824 /* Construct tilearea covered by (xc, yc, xc + this->zoom, yc + this->zoom) such that it is within min_xy limits. */
825 TileArea ta;
826 if (min_xy == 1 && (xc == 0 || yc == 0)) {
827 if (this->zoom == 1) continue; // The tile area is empty, don't draw anything.
829 ta = TileArea(TileXY(max(min_xy, xc), max(min_xy, yc)), this->zoom - (xc == 0), this->zoom - (yc == 0));
830 } else {
831 ta = TileArea(TileXY(xc, yc), this->zoom, this->zoom);
833 ta.ClampToMap(); // Clamp to map boundaries (may contain MP_VOID tiles!).
835 uint32 val = this->GetTileColours(ta);
836 uint8 *val8 = (uint8 *)&val;
837 int idx = max(0, -start_pos);
838 for (int pos = max(0, start_pos); pos < end_pos; pos++) {
839 blitter->SetPixel(dst, idx, 0, val8[idx]);
840 idx++;
842 /* Switch to next tile in the column */
843 } while (xc += this->zoom, yc += this->zoom, dst = blitter->MoveTo(dst, pitch, 0), --reps != 0);
847 * Adds vehicles to the smallmap.
848 * @param dpi the part of the smallmap to be drawn into
849 * @param blitter current blitter
851 void SmallMapWindow::DrawVehicles(const DrawPixelInfo *dpi, Blitter *blitter) const
853 const Vehicle *v;
854 FOR_ALL_VEHICLES(v) {
855 if (v->type == VEH_EFFECT) continue;
856 if (v->vehstatus & (VS_HIDDEN | VS_UNCLICKABLE)) continue;
858 /* Remap into flat coordinates. */
859 Point pt = this->RemapTile(v->x_pos / (int)TILE_SIZE, v->y_pos / (int)TILE_SIZE);
861 int y = pt.y - dpi->top;
862 if (!IsInsideMM(y, 0, dpi->height)) continue; // y is out of bounds.
864 bool skip = false; // Default is to draw both pixels.
865 int x = pt.x - this->subscroll - 3 - dpi->left; // Offset X coordinate.
866 if (x < 0) {
867 /* if x+1 is 0, that means we're on the very left edge,
868 * and should thus only draw a single pixel */
869 if (++x != 0) continue;
870 skip = true;
871 } else if (x >= dpi->width - 1) {
872 /* Check if we're at the very right edge, and if so draw only a single pixel */
873 if (x != dpi->width - 1) continue;
874 skip = true;
877 /* Calculate pointer to pixel and the colour */
878 byte colour = (this->map_type == SMT_VEHICLES) ? _vehicle_type_colours[v->type] : PC_WHITE;
880 /* And draw either one or two pixels depending on clipping */
881 blitter->SetPixel(dpi->dst_ptr, x, y, colour);
882 if (!skip) blitter->SetPixel(dpi->dst_ptr, x + 1, y, colour);
887 * Adds town names to the smallmap.
888 * @param dpi the part of the smallmap to be drawn into
890 void SmallMapWindow::DrawTowns(const DrawPixelInfo *dpi) const
892 const Town *t;
893 FOR_ALL_TOWNS(t) {
894 /* Remap the town coordinate */
895 Point pt = this->RemapTile(TileX(t->xy), TileY(t->xy));
896 int x = pt.x - this->subscroll - (t->cache.sign.width_small >> 1);
897 int y = pt.y;
899 /* Check if the town sign is within bounds */
900 if (x + t->cache.sign.width_small > dpi->left &&
901 x < dpi->left + dpi->width &&
902 y + FONT_HEIGHT_SMALL > dpi->top &&
903 y < dpi->top + dpi->height) {
904 /* And draw it. */
905 SetDParam(0, t->index);
906 DrawString(x, x + t->cache.sign.width_small, y, STR_SMALLMAP_TOWN);
912 * Adds map indicators to the smallmap.
914 void SmallMapWindow::DrawMapIndicators() const
916 /* Find main viewport. */
917 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
919 Point upper_left_smallmap_coord = TranslateXYToTileCoord(vp, vp->left, vp->top, false);
920 Point lower_right_smallmap_coord = TranslateXYToTileCoord(vp, vp->left + vp->width - 1, vp->top + vp->height - 1, false);
922 Point upper_left = this->RemapTile(upper_left_smallmap_coord.x / (int)TILE_SIZE, upper_left_smallmap_coord.y / (int)TILE_SIZE);
923 upper_left.x -= this->subscroll;
925 Point lower_right = this->RemapTile(lower_right_smallmap_coord.x / (int)TILE_SIZE, lower_right_smallmap_coord.y / (int)TILE_SIZE);
926 lower_right.x -= this->subscroll;
928 SmallMapWindow::DrawVertMapIndicator(upper_left.x, upper_left.y, lower_right.y);
929 SmallMapWindow::DrawVertMapIndicator(lower_right.x, upper_left.y, lower_right.y);
931 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, upper_left.y);
932 SmallMapWindow::DrawHorizMapIndicator(upper_left.x, lower_right.x, lower_right.y);
936 * Draws the small map.
938 * Basically, the small map is draw column of pixels by column of pixels. The pixels
939 * are drawn directly into the screen buffer. The final map is drawn in multiple passes.
940 * The passes are:
941 * <ol><li>The colours of tiles in the different modes.</li>
942 * <li>Town names (optional)</li></ol>
944 * @param dpi pointer to pixel to write onto
946 void SmallMapWindow::DrawSmallMap(DrawPixelInfo *dpi) const
948 Blitter *blitter = BlitterFactory::GetCurrentBlitter();
949 DrawPixelInfo *old_dpi;
951 old_dpi = _cur_dpi;
952 _cur_dpi = dpi;
954 /* Clear it */
955 GfxFillRect(dpi->left, dpi->top, dpi->left + dpi->width - 1, dpi->top + dpi->height - 1, PC_BLACK);
957 /* Which tile is displayed at (dpi->left, dpi->top)? */
958 int dx;
959 Point tile = this->PixelToTile(dpi->left, dpi->top, &dx);
960 int tile_x = this->scroll_x / (int)TILE_SIZE + tile.x;
961 int tile_y = this->scroll_y / (int)TILE_SIZE + tile.y;
963 void *ptr = blitter->MoveTo(dpi->dst_ptr, -dx - 4, 0);
964 int x = - dx - 4;
965 int y = 0;
967 for (;;) {
968 /* Distance from left edge */
969 if (x >= -3) {
970 if (x >= dpi->width) break; // Exit the loop.
972 int end_pos = min(dpi->width, x + 4);
973 int reps = (dpi->height - y + 1) / 2; // Number of lines.
974 if (reps > 0) {
975 this->DrawSmallMapColumn(ptr, tile_x, tile_y, dpi->pitch * 2, reps, x, end_pos, blitter);
979 if (y == 0) {
980 tile_y += this->zoom;
981 y++;
982 ptr = blitter->MoveTo(ptr, 0, 1);
983 } else {
984 tile_x -= this->zoom;
985 y--;
986 ptr = blitter->MoveTo(ptr, 0, -1);
988 ptr = blitter->MoveTo(ptr, 2, 0);
989 x += 2;
992 /* Draw vehicles */
993 if (this->map_type == SMT_CONTOUR || this->map_type == SMT_VEHICLES) this->DrawVehicles(dpi, blitter);
995 /* Draw link stat overlay */
996 if (this->map_type == SMT_LINKSTATS) this->overlay->Draw(dpi);
998 /* Draw town names */
999 if (this->show_towns) this->DrawTowns(dpi);
1001 /* Draw map indicators */
1002 this->DrawMapIndicators();
1004 _cur_dpi = old_dpi;
1008 * Function to set up widgets depending on the information being shown on the smallmap.
1010 void SmallMapWindow::SetupWidgetData()
1012 StringID legend_tooltip;
1013 StringID enable_all_tooltip;
1014 StringID disable_all_tooltip;
1015 int plane;
1016 switch (this->map_type) {
1017 case SMT_INDUSTRY:
1018 legend_tooltip = STR_SMALLMAP_TOOLTIP_INDUSTRY_SELECTION;
1019 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_INDUSTRIES;
1020 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_INDUSTRIES;
1021 plane = 0;
1022 break;
1024 case SMT_OWNER:
1025 legend_tooltip = STR_SMALLMAP_TOOLTIP_COMPANY_SELECTION;
1026 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_COMPANIES;
1027 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_COMPANIES;
1028 plane = 0;
1029 break;
1031 case SMT_LINKSTATS:
1032 legend_tooltip = STR_SMALLMAP_TOOLTIP_CARGO_SELECTION;
1033 enable_all_tooltip = STR_SMALLMAP_TOOLTIP_ENABLE_ALL_CARGOS;
1034 disable_all_tooltip = STR_SMALLMAP_TOOLTIP_DISABLE_ALL_CARGOS;
1035 plane = 0;
1036 break;
1038 default:
1039 legend_tooltip = STR_NULL;
1040 enable_all_tooltip = STR_NULL;
1041 disable_all_tooltip = STR_NULL;
1042 plane = 1;
1043 break;
1046 this->GetWidget<NWidgetCore>(WID_SM_LEGEND)->SetDataTip(STR_NULL, legend_tooltip);
1047 this->GetWidget<NWidgetCore>(WID_SM_ENABLE_ALL)->SetDataTip(STR_SMALLMAP_ENABLE_ALL, enable_all_tooltip);
1048 this->GetWidget<NWidgetCore>(WID_SM_DISABLE_ALL)->SetDataTip(STR_SMALLMAP_DISABLE_ALL, disable_all_tooltip);
1049 this->GetWidget<NWidgetStacked>(WID_SM_SELECT_BUTTONS)->SetDisplayedPlane(plane);
1052 SmallMapWindow::SmallMapWindow(WindowDesc *desc, int window_number) : Window(desc), refresh(FORCE_REFRESH_PERIOD)
1054 _smallmap_industry_highlight = INVALID_INDUSTRYTYPE;
1055 this->overlay = new LinkGraphOverlay(this, WID_SM_MAP, 0, this->GetOverlayCompanyMask(), 1);
1056 this->InitNested(window_number);
1057 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1059 this->RebuildColourIndexIfNecessary();
1061 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1063 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1065 this->SetupWidgetData();
1067 this->SetZoomLevel(ZLC_INITIALIZE, NULL);
1068 this->SmallMapCenterOnCurrentPos();
1069 this->SetOverlayCargoMask();
1073 * Rebuilds the colour indices used for fast access to the smallmap contour colours based on the heightlevel.
1075 void SmallMapWindow::RebuildColourIndexIfNecessary()
1077 /* Rebuild colour indices if necessary. */
1078 if (SmallMapWindow::max_heightlevel == _settings_game.construction.max_heightlevel) return;
1080 for (uint n = 0; n < lengthof(_heightmap_schemes); n++) {
1081 /* The heights go from 0 up to and including maximum. */
1082 int heights = _settings_game.construction.max_heightlevel + 1;
1083 _heightmap_schemes[n].height_colours = ReallocT<uint32>(_heightmap_schemes[n].height_colours, heights);
1085 for (int z = 0; z < heights; z++) {
1086 size_t access_index = (_heightmap_schemes[n].colour_count * z) / heights;
1088 /* Choose colour by mapping the range (0..max heightlevel) on the complete colour table. */
1089 _heightmap_schemes[n].height_colours[z] = _heightmap_schemes[n].height_colours_base[access_index];
1093 SmallMapWindow::max_heightlevel = _settings_game.construction.max_heightlevel;
1094 BuildLandLegend();
1097 /* virtual */ void SmallMapWindow::SetStringParameters(int widget) const
1099 switch (widget) {
1100 case WID_SM_CAPTION:
1101 SetDParam(0, STR_SMALLMAP_TYPE_CONTOURS + this->map_type);
1102 break;
1106 /* virtual */ void SmallMapWindow::OnInit()
1108 uint min_width = 0;
1109 this->min_number_of_columns = INDUSTRY_MIN_NUMBER_OF_COLUMNS;
1110 this->min_number_of_fixed_rows = lengthof(_linkstat_colours_in_legenda);
1111 for (uint i = 0; i < lengthof(_legend_table); i++) {
1112 uint height = 0;
1113 uint num_columns = 1;
1114 for (const LegendAndColour *tbl = _legend_table[i]; !tbl->end; ++tbl) {
1115 StringID str;
1116 if (i == SMT_INDUSTRY) {
1117 SetDParam(0, tbl->legend);
1118 SetDParam(1, IndustryPool::MAX_SIZE);
1119 str = STR_SMALLMAP_INDUSTRY;
1120 } else if (i == SMT_LINKSTATS) {
1121 SetDParam(0, tbl->legend);
1122 str = STR_SMALLMAP_LINKSTATS;
1123 } else if (i == SMT_OWNER) {
1124 if (tbl->company != INVALID_COMPANY) {
1125 if (!Company::IsValidID(tbl->company)) {
1126 /* Rebuild the owner legend. */
1127 BuildOwnerLegend();
1128 this->OnInit();
1129 return;
1131 /* Non-fixed legend entries for the owner view. */
1132 SetDParam(0, tbl->company);
1133 str = STR_SMALLMAP_COMPANY;
1134 } else {
1135 str = tbl->legend;
1137 } else {
1138 if (tbl->col_break) {
1139 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1140 height = 0;
1141 num_columns++;
1143 height++;
1144 str = tbl->legend;
1146 min_width = max(GetStringBoundingBox(str).width, min_width);
1148 this->min_number_of_fixed_rows = max(this->min_number_of_fixed_rows, height);
1149 this->min_number_of_columns = max(this->min_number_of_columns, num_columns);
1152 /* The width of a column is the minimum width of all texts + the size of the blob + some spacing */
1153 this->column_width = min_width + LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT + WD_FRAMERECT_RIGHT;
1156 /* virtual */ void SmallMapWindow::OnPaint()
1158 if (this->map_type == SMT_OWNER) {
1159 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1160 if (tbl->company != INVALID_COMPANY && !Company::IsValidID(tbl->company)) {
1161 /* Rebuild the owner legend. */
1162 BuildOwnerLegend();
1163 this->InvalidateData(1);
1164 break;
1169 this->DrawWidgets();
1172 /* virtual */ void SmallMapWindow::DrawWidget(const Rect &r, int widget) const
1174 switch (widget) {
1175 case WID_SM_MAP: {
1176 DrawPixelInfo new_dpi;
1177 if (!FillDrawPixelInfo(&new_dpi, r.left + 1, r.top + 1, r.right - r.left - 1, r.bottom - r.top - 1)) return;
1178 this->DrawSmallMap(&new_dpi);
1179 break;
1182 case WID_SM_LEGEND: {
1183 uint columns = this->GetNumberColumnsLegend(r.right - r.left + 1);
1184 uint number_of_rows = this->GetNumberRowsLegend(columns);
1185 bool rtl = _current_text_dir == TD_RTL;
1186 uint y_org = r.top + WD_FRAMERECT_TOP;
1187 uint x = rtl ? r.right - this->column_width - WD_FRAMERECT_RIGHT : r.left + WD_FRAMERECT_LEFT;
1188 uint y = y_org;
1189 uint i = 0; // Row counter for industry legend.
1190 uint row_height = FONT_HEIGHT_SMALL;
1192 uint text_left = rtl ? 0 : LEGEND_BLOB_WIDTH + WD_FRAMERECT_LEFT;
1193 uint text_right = this->column_width - 1 - (rtl ? LEGEND_BLOB_WIDTH + WD_FRAMERECT_RIGHT : 0);
1194 uint blob_left = rtl ? this->column_width - 1 - LEGEND_BLOB_WIDTH : 0;
1195 uint blob_right = rtl ? this->column_width - 1 : LEGEND_BLOB_WIDTH;
1197 StringID string = STR_NULL;
1198 switch (this->map_type) {
1199 case SMT_INDUSTRY:
1200 string = STR_SMALLMAP_INDUSTRY;
1201 break;
1202 case SMT_LINKSTATS:
1203 string = STR_SMALLMAP_LINKSTATS;
1204 break;
1205 case SMT_OWNER:
1206 string = STR_SMALLMAP_COMPANY;
1207 break;
1208 default:
1209 break;
1212 for (const LegendAndColour *tbl = _legend_table[this->map_type]; !tbl->end; ++tbl) {
1213 if (tbl->col_break || ((this->map_type == SMT_INDUSTRY || this->map_type == SMT_OWNER || this->map_type == SMT_LINKSTATS) && i++ >= number_of_rows)) {
1214 /* Column break needed, continue at top, COLUMN_WIDTH pixels
1215 * (one "row") to the right. */
1216 x += rtl ? -(int)this->column_width : this->column_width;
1217 y = y_org;
1218 i = 1;
1221 uint8 legend_colour = tbl->colour;
1223 switch (this->map_type) {
1224 case SMT_INDUSTRY:
1225 /* Industry name must be formatted, since it's not in tiny font in the specs.
1226 * So, draw with a parameter and use the STR_SMALLMAP_INDUSTRY string, which is tiny font */
1227 SetDParam(0, tbl->legend);
1228 SetDParam(1, Industry::GetIndustryTypeCount(tbl->type));
1229 if (tbl->show_on_map && tbl->type == _smallmap_industry_highlight) {
1230 legend_colour = _smallmap_industry_highlight_state ? PC_WHITE : PC_BLACK;
1232 /* FALL THROUGH */
1233 case SMT_LINKSTATS:
1234 SetDParam(0, tbl->legend);
1235 /* FALL_THROUGH */
1236 case SMT_OWNER:
1237 if (this->map_type != SMT_OWNER || tbl->company != INVALID_COMPANY) {
1238 if (this->map_type == SMT_OWNER) SetDParam(0, tbl->company);
1239 if (!tbl->show_on_map) {
1240 /* Simply draw the string, not the black border of the legend colour.
1241 * This will enforce the idea of the disabled item */
1242 DrawString(x + text_left, x + text_right, y, string, TC_GREY);
1243 } else {
1244 DrawString(x + text_left, x + text_right, y, string, TC_BLACK);
1245 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK); // Outer border of the legend colour
1247 break;
1249 /* FALL_THROUGH */
1250 default:
1251 if (this->map_type == SMT_CONTOUR) SetDParam(0, tbl->height * TILE_HEIGHT_STEP);
1252 /* Anything that is not an industry or a company is using normal process */
1253 GfxFillRect(x + blob_left, y + 1, x + blob_right, y + row_height - 1, PC_BLACK);
1254 DrawString(x + text_left, x + text_right, y, tbl->legend);
1255 break;
1257 GfxFillRect(x + blob_left + 1, y + 2, x + blob_right - 1, y + row_height - 2, legend_colour); // Legend colour
1259 y += row_height;
1266 * Select a new map type.
1267 * @param map_type New map type.
1269 void SmallMapWindow::SwitchMapType(SmallMapType map_type)
1271 this->RaiseWidget(this->map_type + WID_SM_CONTOUR);
1272 this->map_type = map_type;
1273 this->LowerWidget(this->map_type + WID_SM_CONTOUR);
1275 this->SetupWidgetData();
1277 if (map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1278 this->SetDirty();
1282 * Get the number of rows in the legend from the number of columns. Those
1283 * are at least min_number_of_fixed_rows and possibly more if there are so
1284 * many cargoes, industry types or companies that they won't fit in the
1285 * available space.
1286 * @param columns Number of columns in the legend.
1287 * @return Number of rows needed for everything to fit in.
1289 inline uint SmallMapWindow::GetNumberRowsLegend(uint columns) const
1291 /* Reserve one column for link colours */
1292 uint num_rows_linkstats = CeilDiv(_smallmap_cargo_count, columns - 1);
1293 uint num_rows_others = CeilDiv(max(_smallmap_industry_count, _smallmap_company_count), columns);
1294 return max(this->min_number_of_fixed_rows, max(num_rows_linkstats, num_rows_others));
1298 * Select and toggle a legend item. When CTRL is pressed, disable all other
1299 * items in the group defined by begin_legend_item and end_legend_item and
1300 * keep the clicked one enabled even if it was already enabled before. If
1301 * the other items in the group are all disabled already and CTRL is pressed
1302 * enable them instead.
1303 * @param click_pos the index of the item being selected
1304 * @param legend the legend from which we select
1305 * @param end_legend_item index one past the last item in the group to be inverted
1306 * @param begin_legend_item index of the first item in the group to be inverted
1308 void SmallMapWindow::SelectLegendItem(int click_pos, LegendAndColour *legend, int end_legend_item, int begin_legend_item)
1310 if (_ctrl_pressed) {
1311 /* Disable all, except the clicked one */
1312 bool changes = false;
1313 for (int i = begin_legend_item; i != end_legend_item; i++) {
1314 bool new_state = (i == click_pos);
1315 if (legend[i].show_on_map != new_state) {
1316 changes = true;
1317 legend[i].show_on_map = new_state;
1320 if (!changes) {
1321 /* Nothing changed? Then show all (again). */
1322 for (int i = begin_legend_item; i != end_legend_item; i++) {
1323 legend[i].show_on_map = true;
1326 } else {
1327 legend[click_pos].show_on_map = !legend[click_pos].show_on_map;
1332 * Set the link graph overlay cargo mask from the legend.
1334 void SmallMapWindow::SetOverlayCargoMask()
1336 uint32 cargo_mask = 0;
1337 for (int i = 0; i != _smallmap_cargo_count; ++i) {
1338 if (_legend_linkstats[i].show_on_map) SetBit(cargo_mask, _legend_linkstats[i].type);
1340 this->overlay->SetCargoMask(cargo_mask);
1344 * Determines the mouse position on the legend.
1345 * @param pt Mouse position.
1346 * @return Legend item under the mouse.
1348 int SmallMapWindow::GetPositionOnLegend(Point pt)
1350 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_LEGEND);
1351 uint line = (pt.y - wi->pos_y - WD_FRAMERECT_TOP) / FONT_HEIGHT_SMALL;
1352 uint columns = this->GetNumberColumnsLegend(wi->current_x);
1353 uint number_of_rows = this->GetNumberRowsLegend(columns);
1354 if (line >= number_of_rows) return -1;
1356 bool rtl = _current_text_dir == TD_RTL;
1357 int x = pt.x - wi->pos_x;
1358 if (rtl) x = wi->current_x - x;
1359 uint column = (x - WD_FRAMERECT_LEFT) / this->column_width;
1361 return (column * number_of_rows) + line;
1364 /* virtual */ void SmallMapWindow::OnMouseOver(Point pt, int widget)
1366 IndustryType new_highlight = INVALID_INDUSTRYTYPE;
1367 if (widget == WID_SM_LEGEND && this->map_type == SMT_INDUSTRY) {
1368 int industry_pos = GetPositionOnLegend(pt);
1369 if (industry_pos >= 0 && industry_pos < _smallmap_industry_count) {
1370 new_highlight = _legend_from_industries[industry_pos].type;
1373 if (new_highlight != _smallmap_industry_highlight) {
1374 _smallmap_industry_highlight = new_highlight;
1375 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1376 _smallmap_industry_highlight_state = true;
1377 this->SetDirty();
1381 /* virtual */ void SmallMapWindow::OnClick(Point pt, int widget, int click_count)
1383 /* User clicked something, notify the industry chain window to stop sending newly selected industries. */
1384 InvalidateWindowClassesData(WC_INDUSTRY_CARGOES, NUM_INDUSTRYTYPES);
1386 switch (widget) {
1387 case WID_SM_MAP: { // Map window
1389 * XXX: scrolling with the left mouse button is done by subsequently
1390 * clicking with the left mouse button; clicking once centers the
1391 * large map at the selected point. So by unclicking the left mouse
1392 * button here, it gets reclicked during the next inputloop, which
1393 * would make it look like the mouse is being dragged, while it is
1394 * actually being (virtually) clicked every inputloop.
1396 _left_button_clicked = false;
1398 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1399 Window *w = FindWindowById(WC_MAIN_WINDOW, 0);
1400 int sub;
1401 pt = this->PixelToTile(pt.x - wid->pos_x, pt.y - wid->pos_y, &sub);
1402 ScrollWindowTo(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, -1, w);
1404 this->SetDirty();
1405 break;
1408 case WID_SM_ZOOM_IN:
1409 case WID_SM_ZOOM_OUT: {
1410 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1411 Point pt = { (int)wid->current_x / 2, (int)wid->current_y / 2};
1412 this->SetZoomLevel((widget == WID_SM_ZOOM_IN) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1413 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1414 break;
1417 case WID_SM_CONTOUR: // Show land contours
1418 case WID_SM_VEHICLES: // Show vehicles
1419 case WID_SM_INDUSTRIES: // Show industries
1420 case WID_SM_LINKSTATS: // Show route map
1421 case WID_SM_ROUTES: // Show transport routes
1422 case WID_SM_VEGETATION: // Show vegetation
1423 case WID_SM_OWNERS: // Show land owners
1424 this->SwitchMapType((SmallMapType)(widget - WID_SM_CONTOUR));
1425 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1426 break;
1428 case WID_SM_CENTERMAP: // Center the smallmap again
1429 this->SmallMapCenterOnCurrentPos();
1430 this->HandleButtonClick(WID_SM_CENTERMAP);
1431 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1432 break;
1434 case WID_SM_TOGGLETOWNNAME: // Toggle town names
1435 this->show_towns = !this->show_towns;
1436 this->SetWidgetLoweredState(WID_SM_TOGGLETOWNNAME, this->show_towns);
1438 this->SetDirty();
1439 if (_settings_client.sound.click_beep) SndPlayFx(SND_15_BEEP);
1440 break;
1442 case WID_SM_LEGEND: // Legend
1443 if (this->map_type == SMT_INDUSTRY || this->map_type == SMT_LINKSTATS || this->map_type == SMT_OWNER) {
1444 int click_pos = this->GetPositionOnLegend(pt);
1445 if (click_pos < 0) break;
1447 /* If industry type small map*/
1448 if (this->map_type == SMT_INDUSTRY) {
1449 /* If click on industries label, find right industry type and enable/disable it. */
1450 if (click_pos < _smallmap_industry_count) {
1451 this->SelectLegendItem(click_pos, _legend_from_industries, _smallmap_industry_count);
1453 } else if (this->map_type == SMT_LINKSTATS) {
1454 if (click_pos < _smallmap_cargo_count) {
1455 this->SelectLegendItem(click_pos, _legend_linkstats, _smallmap_cargo_count);
1456 this->SetOverlayCargoMask();
1458 } else if (this->map_type == SMT_OWNER) {
1459 if (click_pos < _smallmap_company_count) {
1460 this->SelectLegendItem(click_pos, _legend_land_owners, _smallmap_company_count, NUM_NO_COMPANY_ENTRIES);
1463 this->SetDirty();
1465 break;
1467 case WID_SM_ENABLE_ALL:
1468 /* FALL THROUGH */
1469 case WID_SM_DISABLE_ALL: {
1470 LegendAndColour *tbl = NULL;
1471 switch (this->map_type) {
1472 case SMT_INDUSTRY:
1473 tbl = _legend_from_industries;
1474 break;
1475 case SMT_OWNER:
1476 tbl = &(_legend_land_owners[NUM_NO_COMPANY_ENTRIES]);
1477 break;
1478 case SMT_LINKSTATS:
1479 tbl = _legend_linkstats;
1480 break;
1481 default:
1482 NOT_REACHED();
1484 for (;!tbl->end && tbl->legend != STR_LINKGRAPH_LEGEND_UNUSED; ++tbl) {
1485 tbl->show_on_map = (widget == WID_SM_ENABLE_ALL);
1487 if (this->map_type == SMT_LINKSTATS) this->SetOverlayCargoMask();
1488 this->SetDirty();
1489 break;
1492 case WID_SM_SHOW_HEIGHT: // Enable/disable showing of heightmap.
1493 _smallmap_show_heightmap = !_smallmap_show_heightmap;
1494 this->SetWidgetLoweredState(WID_SM_SHOW_HEIGHT, _smallmap_show_heightmap);
1495 this->SetDirty();
1496 break;
1501 * Some data on this window has become invalid.
1502 * @param data Information about the changed data.
1503 * - data = 0: Displayed industries at the industry chain window have changed.
1504 * - data = 1: Companies have changed.
1505 * - data = 2: Cheat changing the maximum heightlevel has been used, rebuild our heightlevel-to-colour index
1506 * @param gui_scope Whether the call is done from GUI scope. You may not do everything when not in GUI scope. See #InvalidateWindowData() for details.
1508 /* virtual */ void SmallMapWindow::OnInvalidateData(int data, bool gui_scope)
1510 if (!gui_scope) return;
1512 switch (data) {
1513 case 1:
1514 /* The owner legend has already been rebuilt. */
1515 this->ReInit();
1516 break;
1518 case 0: {
1519 extern std::bitset<NUM_INDUSTRYTYPES> _displayed_industries;
1520 if (this->map_type != SMT_INDUSTRY) this->SwitchMapType(SMT_INDUSTRY);
1522 for (int i = 0; i != _smallmap_industry_count; i++) {
1523 _legend_from_industries[i].show_on_map = _displayed_industries.test(_legend_from_industries[i].type);
1525 break;
1528 case 2:
1529 this->RebuildColourIndexIfNecessary();
1530 break;
1532 default: NOT_REACHED();
1534 this->SetDirty();
1537 /* virtual */ bool SmallMapWindow::OnRightClick(Point pt, int widget)
1539 if (widget != WID_SM_MAP || _scrolling_viewport) return false;
1541 _scrolling_viewport = true;
1542 return true;
1545 /* virtual */ void SmallMapWindow::OnMouseWheel(int wheel)
1547 if (_settings_client.gui.scrollwheel_scrolling == 0) {
1548 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1549 int cursor_x = _cursor.pos.x - this->left - wid->pos_x;
1550 int cursor_y = _cursor.pos.y - this->top - wid->pos_y;
1551 if (IsInsideMM(cursor_x, 0, wid->current_x) && IsInsideMM(cursor_y, 0, wid->current_y)) {
1552 Point pt = {cursor_x, cursor_y};
1553 this->SetZoomLevel((wheel < 0) ? ZLC_ZOOM_IN : ZLC_ZOOM_OUT, &pt);
1558 /* virtual */ void SmallMapWindow::OnTick()
1560 /* Update the window every now and then */
1561 if (--this->refresh != 0) return;
1563 if (this->map_type == SMT_LINKSTATS) {
1564 uint32 company_mask = this->GetOverlayCompanyMask();
1565 if (this->overlay->GetCompanyMask() != company_mask) {
1566 this->overlay->SetCompanyMask(company_mask);
1567 } else {
1568 this->overlay->RebuildCache();
1571 _smallmap_industry_highlight_state = !_smallmap_industry_highlight_state;
1573 this->refresh = _smallmap_industry_highlight != INVALID_INDUSTRYTYPE ? BLINK_PERIOD : FORCE_REFRESH_PERIOD;
1574 this->SetDirty();
1578 * Set new #scroll_x, #scroll_y, and #subscroll values after limiting them such that the center
1579 * of the smallmap always contains a part of the map.
1580 * @param sx Proposed new #scroll_x
1581 * @param sy Proposed new #scroll_y
1582 * @param sub Proposed new #subscroll
1584 void SmallMapWindow::SetNewScroll(int sx, int sy, int sub)
1586 const NWidgetBase *wi = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1587 Point hv = InverseRemapCoords(wi->current_x * ZOOM_LVL_BASE * TILE_SIZE / 2, wi->current_y * ZOOM_LVL_BASE * TILE_SIZE / 2);
1588 hv.x *= this->zoom;
1589 hv.y *= this->zoom;
1591 if (sx < -hv.x) {
1592 sx = -hv.x;
1593 sub = 0;
1595 if (sx > (int)(MapMaxX() * TILE_SIZE) - hv.x) {
1596 sx = MapMaxX() * TILE_SIZE - hv.x;
1597 sub = 0;
1599 if (sy < -hv.y) {
1600 sy = -hv.y;
1601 sub = 0;
1603 if (sy > (int)(MapMaxY() * TILE_SIZE) - hv.y) {
1604 sy = MapMaxY() * TILE_SIZE - hv.y;
1605 sub = 0;
1608 this->scroll_x = sx;
1609 this->scroll_y = sy;
1610 this->subscroll = sub;
1611 if (this->map_type == SMT_LINKSTATS) this->overlay->RebuildCache();
1614 /* virtual */ void SmallMapWindow::OnScroll(Point delta)
1616 _cursor.fix_at = true;
1618 /* While tile is at (delta.x, delta.y)? */
1619 int sub;
1620 Point pt = this->PixelToTile(delta.x, delta.y, &sub);
1621 this->SetNewScroll(this->scroll_x + pt.x * TILE_SIZE, this->scroll_y + pt.y * TILE_SIZE, sub);
1623 this->SetDirty();
1627 * Center the small map on the current center of the viewport.
1629 void SmallMapWindow::SmallMapCenterOnCurrentPos()
1631 const ViewPort *vp = FindWindowById(WC_MAIN_WINDOW, 0)->viewport;
1632 Point viewport_center = TranslateXYToTileCoord(vp, vp->left + vp->width / 2, vp->top + vp->height / 2);
1634 int sub;
1635 const NWidgetBase *wid = this->GetWidget<NWidgetBase>(WID_SM_MAP);
1636 Point sxy = this->ComputeScroll(viewport_center.x / (int)TILE_SIZE, viewport_center.y / (int)TILE_SIZE,
1637 max(0, (int)wid->current_x / 2 - 2), wid->current_y / 2, &sub);
1638 this->SetNewScroll(sxy.x, sxy.y, sub);
1639 this->SetDirty();
1643 * Get the center of the given station as point on the screen in the smallmap window.
1644 * @param st Station to find in the smallmap.
1645 * @return Point with coordinates of the station.
1647 Point SmallMapWindow::GetStationMiddle(const Station *st) const
1649 int x = (st->rect.right + st->rect.left + 1) / 2;
1650 int y = (st->rect.bottom + st->rect.top + 1) / 2;
1651 Point ret = this->RemapTile(x, y);
1653 /* Same magic 3 as in DrawVehicles; that's where I got it from.
1654 * No idea what it is, but without it the result looks bad.
1656 ret.x -= 3 + this->subscroll;
1657 return ret;
1660 SmallMapWindow::SmallMapType SmallMapWindow::map_type = SMT_CONTOUR;
1661 bool SmallMapWindow::show_towns = true;
1662 int SmallMapWindow::max_heightlevel = -1;
1665 * Custom container class for displaying smallmap with a vertically resizing legend panel.
1666 * The legend panel has a smallest height that depends on its width. Standard containers cannot handle this case.
1668 * @note The container assumes it has two children, the first is the display, the second is the bar with legends and selection image buttons.
1669 * Both children should be both horizontally and vertically resizable and horizontally fillable.
1670 * The bar should have a minimal size with a zero-size legends display. Child padding is not supported.
1672 class NWidgetSmallmapDisplay : public NWidgetContainer {
1673 const SmallMapWindow *smallmap_window; ///< Window manager instance.
1674 public:
1675 NWidgetSmallmapDisplay() : NWidgetContainer(NWID_VERTICAL)
1677 this->smallmap_window = NULL;
1680 virtual void SetupSmallestSize(Window *w, bool init_array)
1682 NWidgetBase *display = this->head;
1683 NWidgetBase *bar = display->next;
1685 display->SetupSmallestSize(w, init_array);
1686 bar->SetupSmallestSize(w, init_array);
1688 this->smallmap_window = dynamic_cast<SmallMapWindow *>(w);
1689 assert(this->smallmap_window != NULL);
1690 this->smallest_x = max(display->smallest_x, bar->smallest_x + smallmap_window->GetMinLegendWidth());
1691 this->smallest_y = display->smallest_y + max(bar->smallest_y, smallmap_window->GetLegendHeight(smallmap_window->min_number_of_columns));
1692 this->fill_x = max(display->fill_x, bar->fill_x);
1693 this->fill_y = (display->fill_y == 0 && bar->fill_y == 0) ? 0 : min(display->fill_y, bar->fill_y);
1694 this->resize_x = max(display->resize_x, bar->resize_x);
1695 this->resize_y = min(display->resize_y, bar->resize_y);
1698 virtual void AssignSizePosition(SizingType sizing, uint x, uint y, uint given_width, uint given_height, bool rtl)
1700 this->pos_x = x;
1701 this->pos_y = y;
1702 this->current_x = given_width;
1703 this->current_y = given_height;
1705 NWidgetBase *display = this->head;
1706 NWidgetBase *bar = display->next;
1708 if (sizing == ST_SMALLEST) {
1709 this->smallest_x = given_width;
1710 this->smallest_y = given_height;
1711 /* Make display and bar exactly equal to their minimal size. */
1712 display->AssignSizePosition(ST_SMALLEST, x, y, display->smallest_x, display->smallest_y, rtl);
1713 bar->AssignSizePosition(ST_SMALLEST, x, y + display->smallest_y, bar->smallest_x, bar->smallest_y, rtl);
1716 uint bar_height = max(bar->smallest_y, this->smallmap_window->GetLegendHeight(this->smallmap_window->GetNumberColumnsLegend(given_width - bar->smallest_x)));
1717 uint display_height = given_height - bar_height;
1718 display->AssignSizePosition(ST_RESIZE, x, y, given_width, display_height, rtl);
1719 bar->AssignSizePosition(ST_RESIZE, x, y + display_height, given_width, bar_height, rtl);
1722 virtual NWidgetCore *GetWidgetFromPos(int x, int y)
1724 if (!IsInsideBS(x, this->pos_x, this->current_x) || !IsInsideBS(y, this->pos_y, this->current_y)) return NULL;
1725 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) {
1726 NWidgetCore *widget = child_wid->GetWidgetFromPos(x, y);
1727 if (widget != NULL) return widget;
1729 return NULL;
1732 virtual void Draw(const Window *w)
1734 for (NWidgetBase *child_wid = this->head; child_wid != NULL; child_wid = child_wid->next) child_wid->Draw(w);
1738 /** Widget parts of the smallmap display. */
1739 static const NWidgetPart _nested_smallmap_display[] = {
1740 NWidget(WWT_PANEL, COLOUR_BROWN, WID_SM_MAP_BORDER),
1741 NWidget(WWT_INSET, COLOUR_BROWN, WID_SM_MAP), SetMinimalSize(346, 140), SetResize(1, 1), SetPadding(2, 2, 2, 2), EndContainer(),
1742 EndContainer(),
1745 /** Widget parts of the smallmap legend bar + image buttons. */
1746 static const NWidgetPart _nested_smallmap_bar[] = {
1747 NWidget(WWT_PANEL, COLOUR_BROWN),
1748 NWidget(NWID_HORIZONTAL),
1749 NWidget(WWT_EMPTY, INVALID_COLOUR, WID_SM_LEGEND), SetResize(1, 1),
1750 NWidget(NWID_VERTICAL),
1751 /* Top button row. */
1752 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1753 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_IN),
1754 SetDataTip(SPR_IMG_ZOOMIN, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_IN), SetFill(1, 1),
1755 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_CENTERMAP),
1756 SetDataTip(SPR_IMG_SMALLMAP, STR_SMALLMAP_CENTER), SetFill(1, 1),
1757 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_BLANK),
1758 SetDataTip(SPR_DOT_SMALL, STR_NULL), SetFill(1, 1),
1759 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_CONTOUR),
1760 SetDataTip(SPR_IMG_SHOW_COUNTOURS, STR_SMALLMAP_TOOLTIP_SHOW_LAND_CONTOURS_ON_MAP), SetFill(1, 1),
1761 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEHICLES),
1762 SetDataTip(SPR_IMG_SHOW_VEHICLES, STR_SMALLMAP_TOOLTIP_SHOW_VEHICLES_ON_MAP), SetFill(1, 1),
1763 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_INDUSTRIES),
1764 SetDataTip(SPR_IMG_INDUSTRY, STR_SMALLMAP_TOOLTIP_SHOW_INDUSTRIES_ON_MAP), SetFill(1, 1),
1765 EndContainer(),
1766 /* Bottom button row. */
1767 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1768 NWidget(WWT_PUSHIMGBTN, COLOUR_BROWN, WID_SM_ZOOM_OUT),
1769 SetDataTip(SPR_IMG_ZOOMOUT, STR_TOOLBAR_TOOLTIP_ZOOM_THE_VIEW_OUT), SetFill(1, 1),
1770 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_TOGGLETOWNNAME),
1771 SetDataTip(SPR_IMG_TOWN, STR_SMALLMAP_TOOLTIP_TOGGLE_TOWN_NAMES_ON_OFF), SetFill(1, 1),
1772 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_LINKSTATS),
1773 SetDataTip(SPR_IMG_CARGOFLOW, STR_SMALLMAP_TOOLTIP_SHOW_LINK_STATS_ON_MAP), SetFill(1, 1),
1774 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_ROUTES),
1775 SetDataTip(SPR_IMG_SHOW_ROUTES, STR_SMALLMAP_TOOLTIP_SHOW_TRANSPORT_ROUTES_ON), SetFill(1, 1),
1776 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_VEGETATION),
1777 SetDataTip(SPR_IMG_PLANTTREES, STR_SMALLMAP_TOOLTIP_SHOW_VEGETATION_ON_MAP), SetFill(1, 1),
1778 NWidget(WWT_IMGBTN, COLOUR_BROWN, WID_SM_OWNERS),
1779 SetDataTip(SPR_IMG_COMPANY_GENERAL, STR_SMALLMAP_TOOLTIP_SHOW_LAND_OWNERS_ON_MAP), SetFill(1, 1),
1780 EndContainer(),
1781 NWidget(NWID_SPACER), SetResize(0, 1),
1782 EndContainer(),
1783 EndContainer(),
1784 EndContainer(),
1787 static NWidgetBase *SmallMapDisplay(int *biggest_index)
1789 NWidgetContainer *map_display = new NWidgetSmallmapDisplay;
1791 MakeNWidgets(_nested_smallmap_display, lengthof(_nested_smallmap_display), biggest_index, map_display);
1792 MakeNWidgets(_nested_smallmap_bar, lengthof(_nested_smallmap_bar), biggest_index, map_display);
1793 return map_display;
1797 static const NWidgetPart _nested_smallmap_widgets[] = {
1798 NWidget(NWID_HORIZONTAL),
1799 NWidget(WWT_CLOSEBOX, COLOUR_BROWN),
1800 NWidget(WWT_CAPTION, COLOUR_BROWN, WID_SM_CAPTION), SetDataTip(STR_SMALLMAP_CAPTION, STR_TOOLTIP_WINDOW_TITLE_DRAG_THIS),
1801 NWidget(WWT_SHADEBOX, COLOUR_BROWN),
1802 NWidget(WWT_DEFSIZEBOX, COLOUR_BROWN),
1803 NWidget(WWT_STICKYBOX, COLOUR_BROWN),
1804 EndContainer(),
1805 NWidgetFunction(SmallMapDisplay), // Smallmap display and legend bar + image buttons.
1806 /* Bottom button row and resize box. */
1807 NWidget(NWID_HORIZONTAL),
1808 NWidget(NWID_SELECTION, INVALID_COLOUR, WID_SM_SELECT_BUTTONS),
1809 NWidget(NWID_HORIZONTAL, NC_EQUALSIZE),
1810 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_ENABLE_ALL), SetDataTip(STR_SMALLMAP_ENABLE_ALL, STR_NULL),
1811 NWidget(WWT_PUSHTXTBTN, COLOUR_BROWN, WID_SM_DISABLE_ALL), SetDataTip(STR_SMALLMAP_DISABLE_ALL, STR_NULL),
1812 NWidget(WWT_TEXTBTN, COLOUR_BROWN, WID_SM_SHOW_HEIGHT), SetDataTip(STR_SMALLMAP_SHOW_HEIGHT, STR_SMALLMAP_TOOLTIP_SHOW_HEIGHT),
1813 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1814 EndContainer(),
1815 EndContainer(),
1816 NWidget(WWT_PANEL, COLOUR_BROWN), SetFill(1, 0), SetResize(1, 0),
1817 EndContainer(),
1818 EndContainer(),
1819 NWidget(WWT_RESIZEBOX, COLOUR_BROWN),
1820 EndContainer(),
1823 static WindowDesc _smallmap_desc(
1824 WDP_AUTO, "smallmap", 484, 314,
1825 WC_SMALLMAP, WC_NONE,
1827 _nested_smallmap_widgets, lengthof(_nested_smallmap_widgets)
1831 * Show the smallmap window.
1833 void ShowSmallMap()
1835 AllocateWindowDescFront<SmallMapWindow>(&_smallmap_desc, 0);
1839 * Scrolls the main window to given coordinates.
1840 * @param x x coordinate
1841 * @param y y coordinate
1842 * @param z z coordinate; -1 to scroll to terrain height
1843 * @param instant scroll instantly (meaningful only when smooth_scrolling is active)
1844 * @return did the viewport position change?
1846 bool ScrollMainWindowTo(int x, int y, int z, bool instant)
1848 bool res = ScrollWindowTo(x, y, z, FindWindowById(WC_MAIN_WINDOW, 0), instant);
1850 /* If a user scrolls to a tile (via what way what so ever) and already is on
1851 * that tile (e.g.: pressed twice), move the smallmap to that location,
1852 * so you directly see where you are on the smallmap. */
1854 if (res) return res;
1856 SmallMapWindow *w = dynamic_cast<SmallMapWindow*>(FindWindowById(WC_SMALLMAP, 0));
1857 if (w != NULL) w->SmallMapCenterOnCurrentPos();
1859 return res;